最近一直在DM365平台上进行音视频录制的开发工作, 由于系统为wince6.0,所以采用了directshow实现。视频编码模块封装成单独的filter, 由于硬编码器的输入缓冲区要求物理地址连续(TI提供了一个叫CMEM的模块),这就需要重新实现directshow的内存管理机制。
一、实现自己的内存分配器
1. 内存管理的几个相关接口及类有:
1).IMemAllocator
2).CBaseAllocator
3).CMemAllocator
2. 实际的内存分配相关函数是在CMemAllocator中实现,涉及3个函数:
1).HRESULT CMemAllocator::Alloc(void), 此函数进行内存分配,代码如下:
2).void CMemAllocator::Free(void),此致函数体为空,有一个注释如下
// in our case, we keep the memory until we are deleted, so
// we do nothing here. The memory is deleted in the destructor by
// calling ReallyFree()
3).void CMemAllocator::ReallyFree(void),这才是真正的内存释放函
3. 要实现自己的MemAllocator,只需要对CMemAllocator中的,Alloc、RellyFree函数中内存分配与释放进行修改。由于RellyFree不是虚函数,不能继承CmemAllocator。我们可以从CBaseAllocator继承一个类,其实现参考CMemAllocator。
二、自定义内存分配器的调用
1.directshow中内存分配器的调用流程
2. 从上述调用过程可以发现,我们只需要重新实现CBaseInputPin::GetAllocator即可,我的实现如下: