//------------------------------------------------------------------------------ // Destructor // CCustomAllocator::~CCustomAllocator(void) { Decommit(); ReallyFree(); if (m_hSemFree) { EXECUTE_ASSERT(CloseHandle(m_hSemFree)); } }
//------------------------------------------------------------------------------ // Alloc // Base definition of allocation which checks we are ok to go ahead and do // the full allocation. We return S_FALSE if the requirements are the same HRESULT CCustomAllocator::Alloc(void) { CAutoLock lck(this); /* Check he has called SetProperties */ /**************************************************/ // override the call to the base class to check our // internal free list HRESULT hr = NOERROR; /* Error if he hasn't set the size yet */ if (m_lCount <= 0 || m_lSize <= 0 || m_lAlignment <= 0) { hr = VFW_E_SIZENOTSET; } /* should never get here while buffers outstanding */ ASSERT(m_lFree.GetCount() == m_lAllocated); /* If the requirements haven't changed then don't reallocate */ if (m_bChanged == FALSE) { hr = S_FALSE; } /**************************************************/ if (FAILED(hr)) { return hr; } // If the requirements haven't changed then don't reallocate if (hr == S_FALSE) { ASSERT(m_pBuffer); return NOERROR; } ASSERT(hr == S_OK); // Free the old resources if (m_pBuffer) ReallyFree(); // Compute the aligned size LONG lAlignedSize = m_lSize + m_lPrefix; if (m_lAlignment > 1) { LONG lRemainder = lAlignedSize % m_lAlignment; if (lRemainder != 0) lAlignedSize += (m_lAlignment - lRemainder); } // Create the contiguous memory block for the samples // making sure it's properly aligned ASSERT(lAlignedSize % m_lAlignment == 0); m_pBuffer = (PBYTE)VirtualAlloc(NULL, m_lCount * lAlignedSize, MEM_COMMIT, PAGE_READWRITE); if (m_pBuffer == NULL) { return E_OUTOFMEMORY; } LPBYTE pNext = m_pBuffer; CMediaSample* pSample; ASSERT(m_lAllocated == 0); // Create the new samples - we have allocated m_lSize bytes for each sample // plus m_lPrefix bytes per sample as a prefix. We set the pointer to // the memory after the prefix - so that GetPointer() will return a pointer // to m_lSize bytes. for (; m_lAllocated < m_lCount; m_lAllocated++, pNext += lAlignedSize) { pSample = new CMediaSample(NAME("Default memory media sample"), this, &hr, NULL, 0); ASSERT(SUCCEEDED(hr)); if (pSample == NULL) { return E_OUTOFMEMORY; } // This CANNOT fail m_lFree.AddTail(pSample); m_lBuffers.AddTail(pNext + m_lPrefix); } m_bChanged = FALSE; return hr; }
HRESULT CPacketAllocator::Alloc(void) { CAutoLock lck(this); /* Check he has called SetProperties */ HRESULT hr = CBaseAllocator::Alloc(); if (FAILED(hr)) { return hr; } /* If the requirements haven't changed then don't reallocate */ if (hr == S_FALSE) { return NOERROR; } ASSERT(hr == S_OK); // we use this fact in the loop below /* Free the old resources */ if (m_bAllocated) { ReallyFree(); } /* Make sure we've got reasonable values */ if ( m_lSize < 0 || m_lPrefix < 0 || m_lCount < 0 ) { return E_OUTOFMEMORY; } /* Compute the aligned size */ LONG lAlignedSize = m_lSize + m_lPrefix; /* Check overflow */ if (lAlignedSize < m_lSize) { return E_OUTOFMEMORY; } if (m_lAlignment > 1) { LONG lRemainder = lAlignedSize % m_lAlignment; if (lRemainder != 0) { LONG lNewSize = lAlignedSize + m_lAlignment - lRemainder; if (lNewSize < lAlignedSize) { return E_OUTOFMEMORY; } lAlignedSize = lNewSize; } } /* Create the contiguous memory block for the samples making sure it's properly aligned (64K should be enough!) */ ASSERT(lAlignedSize % m_lAlignment == 0); LONGLONG lToAllocate = m_lCount * (LONGLONG)lAlignedSize; /* Check overflow */ if (lToAllocate > MAXLONG) { return E_OUTOFMEMORY; } m_bAllocated = TRUE; CMediaPacketSample *pSample = nullptr; ASSERT(m_lAllocated == 0); // Create the new samples - we have allocated m_lSize bytes for each sample // plus m_lPrefix bytes per sample as a prefix. We set the pointer to // the memory after the prefix - so that GetPointer() will return a pointer // to m_lSize bytes. for (; m_lAllocated < m_lCount; m_lAllocated++) { pSample = new CMediaPacketSample(NAME("LAV Package media sample"), this, &hr); ASSERT(SUCCEEDED(hr)); if (pSample == nullptr) { return E_OUTOFMEMORY; } // This CANNOT fail m_lFree.Add(pSample); } m_bChanged = FALSE; return NOERROR; }
HRESULT CSampleSenderAllocator::Alloc( ) { // look at the base class code to see where this came from! CAutoLock lck(this); /* Check he has called SetProperties */ HRESULT hr = CBaseAllocator::Alloc(); if (FAILED(hr)) { return hr; } /* If the requirements haven't changed then don't reallocate */ if (hr == S_FALSE) { ASSERT(m_pBuffer); return NOERROR; } ASSERT(hr == S_OK); // we use this fact in the loop below /* Free the old resources */ if (m_pBuffer) { ReallyFree(); } /* Compute the aligned size */ LONG lAlignedSize = m_lSize + m_lPrefix; if (m_lAlignment > 1) { LONG lRemainder = lAlignedSize % m_lAlignment; if (lRemainder != 0) { lAlignedSize += (m_lAlignment - lRemainder); } } /* Create the contiguous memory block for the samples making sure it's properly aligned (64K should be enough!) */ ASSERT(lAlignedSize % m_lAlignment == 0); // don't create the buffer - use what was passed to us // m_pBuffer = m_pPin->m_pBuffer; if (m_pBuffer == NULL) { return E_OUTOFMEMORY; } LPBYTE pNext = m_pBuffer; CMediaSample *pSample; ASSERT(m_lAllocated == 0); // Create the new samples - we have allocated m_lSize bytes for each sample // plus m_lPrefix bytes per sample as a prefix. We set the pointer to // the memory after the prefix - so that GetPointer() will return a pointer // to m_lSize bytes. for (; m_lAllocated < m_lCount; m_lAllocated++, pNext += lAlignedSize) { pSample = new CMediaSample( NAME("Sample Grabber memory media sample"), this, &hr, pNext + m_lPrefix, // GetPointer() value m_lSize); // not including prefix ASSERT(SUCCEEDED(hr)); if (pSample == NULL) { return E_OUTOFMEMORY; } // This CANNOT fail m_lFree.Add(pSample); } m_bChanged = FALSE; return NOERROR; }
CPacketAllocator::~CPacketAllocator(void) { Decommit(); ReallyFree(); }