168
|
1 #include <stdio.h>
|
|
2 #include "allocator.h"
|
|
3 #include <com.h>
|
|
4 #define E_NOTIMPL 0x80004001
|
|
5 class AllocatorKeeper
|
|
6 {
|
|
7 public:
|
|
8 AllocatorKeeper()
|
|
9 {
|
|
10 RegisterComClass(&CLSID_MemoryAllocator, MemAllocator::CreateAllocator);
|
|
11 }
|
|
12 };
|
|
13 static AllocatorKeeper keeper;
|
|
14 GUID MemAllocator::interfaces[]=
|
|
15 {
|
|
16 IID_IUnknown,
|
|
17 IID_IMemAllocator,
|
|
18 };
|
|
19 IMPLEMENT_IUNKNOWN(MemAllocator)
|
|
20
|
|
21 MemAllocator::MemAllocator()
|
|
22 :refcount(1)
|
|
23 {
|
|
24 Debug printf("MemAllocator::MemAllocator() called\n");
|
|
25 vt=new IMemAllocator_vt;
|
|
26 vt->QueryInterface = QueryInterface;
|
|
27 vt->AddRef = AddRef;
|
|
28 vt->Release = Release;
|
|
29 vt->SetProperties = SetProperties;
|
|
30 vt->GetProperties = GetProperties;
|
|
31 vt->Commit = Commit;
|
|
32 vt->Decommit = Decommit;
|
|
33 vt->GetBuffer = GetBuffer;
|
|
34 vt->ReleaseBuffer = ReleaseBuffer;
|
|
35
|
|
36 props.cBuffers=1;
|
|
37 props.cbBuffer=65536;/* :/ */
|
|
38 props.cbAlign=props.cbPrefix=0;
|
|
39 }
|
|
40
|
|
41 long MemAllocator::CreateAllocator(GUID* clsid, GUID* iid, void** ppv)
|
|
42 {
|
|
43 if(!ppv)return -1;
|
|
44 *ppv=0;
|
|
45 if(memcmp(clsid, &CLSID_MemoryAllocator, sizeof(GUID)))
|
|
46 return -1;
|
|
47
|
|
48 IMemAllocator* p=new MemAllocator;
|
|
49 int result=p->vt->QueryInterface((IUnknown*)p, iid, ppv);
|
|
50 p->vt->Release((IUnknown*)p);
|
|
51 return result;
|
|
52 }
|
|
53
|
|
54
|
|
55 /*
|
|
56 long cBuffers;
|
|
57 long cbBuffer;
|
|
58 long cbAlign;
|
|
59 long cbPrefix;
|
|
60 */
|
|
61 HRESULT STDCALL MemAllocator::SetProperties (
|
|
62 IMemAllocator * This,
|
|
63 /* [in] */ ALLOCATOR_PROPERTIES *pRequest,
|
|
64 /* [out] */ ALLOCATOR_PROPERTIES *pActual)
|
|
65 {
|
|
66 Debug printf("MemAllocator::SetProperties() called\n");
|
|
67 if(!pRequest)return 0x80004003;
|
|
68 if(!pActual)return 0x80004003;
|
|
69 if(pRequest->cBuffers<=0)return -1;
|
|
70 if(pRequest->cbBuffer<=0)return -1;
|
|
71 MemAllocator* me=(MemAllocator*)This;
|
|
72 if(me->used_list.size() || me->free_list.size())return -1;
|
|
73 me->props=*pRequest;
|
|
74 *pActual=*pRequest;
|
|
75 return 0;
|
|
76 }
|
|
77
|
|
78 HRESULT STDCALL MemAllocator::GetProperties (
|
|
79 IMemAllocator * This,
|
|
80 /* [out] */ ALLOCATOR_PROPERTIES *pProps)
|
|
81 {
|
|
82 Debug printf("MemAllocator::GetProperties() called\n");
|
|
83 if(!pProps)return -1;
|
|
84 if(((MemAllocator*)This)->props.cbBuffer<0)return -1;
|
|
85 *pProps=((MemAllocator*)This)->props;
|
|
86 return 0;
|
|
87 }
|
|
88
|
|
89 HRESULT STDCALL MemAllocator::Commit (
|
|
90 IMemAllocator * This)
|
|
91 {
|
|
92 Debug printf("MemAllocator::Commit() called\n");
|
|
93 MemAllocator* me=(MemAllocator*)This;
|
|
94 if(((MemAllocator*)This)->props.cbBuffer<0)return -1;
|
|
95 if(me->used_list.size() || me->free_list.size())return -1;
|
|
96 for(int i=0; i<me->props.cBuffers; i++)
|
|
97 me->free_list.push_back(new CMediaSample(me, me->props.cbBuffer));
|
|
98 return 0;
|
|
99 }
|
|
100
|
|
101 HRESULT STDCALL MemAllocator::Decommit (
|
|
102 IMemAllocator * This)
|
|
103 {
|
|
104 Debug printf("MemAllocator::Decommit() called\n");
|
|
105 MemAllocator* me=(MemAllocator*)This;
|
|
106 list<CMediaSample*>::iterator it;
|
|
107 for(it=me->free_list.begin(); it!=me->free_list.end(); it++)
|
|
108 delete *it;
|
|
109 for(it=me->used_list.begin(); it!=me->used_list.end(); it++)
|
|
110 delete *it;
|
|
111 me->free_list.clear();
|
|
112 me->used_list.clear();
|
|
113 return 0;
|
|
114 }
|
|
115
|
|
116 HRESULT STDCALL MemAllocator::GetBuffer (
|
|
117 IMemAllocator * This,
|
|
118 /* [out] */ IMediaSample **ppBuffer,
|
|
119 /* [in] */ REFERENCE_TIME *pStartTime,
|
|
120 /* [in] */ REFERENCE_TIME *pEndTime,
|
|
121 /* [in] */ DWORD dwFlags)
|
|
122 {
|
|
123 Debug printf("%x: MemAllocator::GetBuffer() called\n", This);
|
|
124 MemAllocator* me=(MemAllocator*)This;
|
|
125 if(me->free_list.size()==0)
|
|
126 {
|
|
127 Debug printf("No samples available\n");
|
|
128 return -1;//should block here if no samples are available
|
|
129 }
|
|
130 list<CMediaSample*>::iterator it=me->free_list.begin();
|
|
131 me->used_list.push_back(*it);
|
|
132 *ppBuffer=*it;
|
|
133 (*ppBuffer)->vt->AddRef((IUnknown*)*ppBuffer);
|
|
134 me->free_list.remove(*it);
|
|
135 return 0;
|
|
136 }
|
|
137
|
|
138 HRESULT STDCALL MemAllocator::ReleaseBuffer (
|
|
139 IMemAllocator * This,
|
|
140 /* [in] */ IMediaSample *pBuffer)
|
|
141 {
|
|
142 Debug printf("%x: MemAllocator::ReleaseBuffer() called\n", This);
|
|
143 MemAllocator* me=(MemAllocator*)This;
|
|
144 list<CMediaSample*>::iterator it;
|
|
145 for(it=me->used_list.begin(); it!=me->used_list.end(); it++)
|
|
146 if(*it==pBuffer)
|
|
147 {
|
|
148 me->used_list.erase(it);
|
|
149 me->free_list.push_back((CMediaSample*)pBuffer);
|
|
150 return 0;
|
|
151 }
|
|
152 Debug printf("Releasing unknown buffer\n");
|
|
153 return -1;
|
|
154 }
|