Share via


CBaseAllocator Class (Compact 2013)

3/26/2014

CCritSec Class

     IMemAllocator Interface

          CBaseAllocator Class

This is an abstract base class that implements the basic mechanisms for an allocator with a fixed number of fixed-size buffers.

The number of buffers and their size can be changed using the CBaseAllocator::SetProperties member function when an input pin and an output pin negotiate the allocator between them.

The class provides the basic functionality for a memory allocator by implementing the IMemAllocator interface.

It provides support for managing a list of CMediaSample objects (or objects derived from this class), including support for the IMemAllocator::Commit and IMemAllocator::Decommit methods, and blocking the IMemAllocator::GetBuffer method.

Any class derived from this class (such as CMemAllocator) must create CMediaSample objects, which this base class does not.

A signaling mechanism employing a semaphore is used so that if there are no samples, a thread can wait until there are samples or until the allocator is decommitted. The m_lFree and m_hSem member variables are used to implement this simple signaling mechanism as follows.

When a thread calls CBaseAllocator::GetBuffer and there are no samples available, m_lWaiting is incremented and the thread calls the Win32 WaitForSingleObject function on the semaphore indicated by m_hSem.

When a sample is freed (by the IUnknown::Release method returning the reference count to zero) or CBaseAllocator::Decommit is called and m_lWaiting is nonzero, the Win32 ReleaseSemaphore function is called on m_hSem with a release count of m_lWaiting, and m_lWaiting is reset to zero.

All member functions in this class that return HRESULT and accept a pointer as a parameter return E_POINTER when passed a null pointer.

Protected Data Members

Member

Description

m_bChanged

TRUE if the buffers have changed; otherwise, FALSE.

m_bCommitted

If TRUE, the allocator is in a state in which all IMemAllocator::GetBuffer methods fail.

The IMemAllocator::SetProperties method is the only member function permitted to operate in this state.

m_bDecommitInProgress

If TRUE, the decommit process completes upon the return of all media samples.

Until the decommit process has completed, any calls to IMemAllocator::GetBuffer return E_OUTOFMEMORY.

m_hSem

Semaphore for signaling.

m_lAlignment

Agreed alignment of the buffer.

m_lAllocated

Number of buffers actually allocated.

m_lCount

Established number of buffers to provide.

m_lFree

List of CMediaSample objects that are currently free (free list).

m_lPrefix

Agreed prefix of the buffer (precedes value returned by IMediaSample::GetPointer).

m_lSize

Size of each buffer.

m_lWaiting

Count of threads waiting for samples.

Member Functions

Member function

Description

CBaseAllocator

Constructs a CBaseAllocator object.

NotifySample

Notifies a waiting thread that a sample is available on the free list.

SetWaiting

Increments the m_lWaiting data member to indicate that a thread is waiting for a sample.

Overrideable Member Functions

Member function

Description

Alloc

Allocates memory, instantiates CMediaSample objects, and adds them to the m_lAllocated and m_lFree data members.

Free

Decommits memory when the last buffer is freed.

Implemented IMemAllocator Methods

Method

Description

Commit

Allocates memory by calling the CBaseAllocator::Alloc member function, which you must override.

Decommit

Releases any resources and enters the inactive state.

Blocking calls to IMemAllocator::GetBuffer should return with an error value, and all further calls to GetBuffer fail when in the inactive state.

GetBuffer

Retrieves a container for a sample.

GetProperties

Determines the size, number, and alignment of blocks.

ReleaseBuffer

Releases the CMediaSample object.

SetProperties

Specifies a desired number of blocks, size of the blocks, and block alignment figure. This method returns the actual values for the same.

Implemented INonDelegatingUnknown Methods

Method

Description

NonDelegatingQueryInterface

Passes out pointers to any interfaces added to the derived filter class.

Requirements

Header

dshow.h,
Streams.h

Library

ole32.lib,
Ole32auth.lib,
Strmbase.lib,
Strmiids.lib,
uuid.lib

See Also

Reference

DirectShow Classes