static uint32_t GetFreeIndex( MemPool* pPool, uint32_t levelDeep ) { if ( levelDeep < CEIL_DIV( pPool->numOfUnits, sizeof( uint32_t ) * CHAR_BIT ) ) { uint32_t bitField = *( pPool->pFreeBits + levelDeep ); uint32_t freeLocation = __builtin_ctz( bitField ); if ( freeLocation < 32 ) { return ( levelDeep * ( CHAR_BIT * sizeof( uint32_t ) ) + freeLocation ); } else { return GetFreeIndex( pPool, levelDeep + 1 ); } } else return levelDeep * ( CHAR_BIT * sizeof( uint32_t ) ); }
void* PoolAlloc( MemPool* pPool ) { void* retval = 0; if ( pPool ) { if ( KMutexLock( &pPool->mutex, WAIT_FOREVER ) ) { uint32_t freeIndex = GetFreeIndex( pPool, 0 ); if ( freeIndex < pPool->numOfUnits ) { uint32_t sizeofUnit = pPool->backingBufferSize / pPool->numOfUnits; retval = ( ( uint8_t* )pPool->pBackingStore + ( sizeofUnit * freeIndex ) ); LOG( "%s(): Retval: %p ( index: %d )", __FUNCTION__, retval, freeIndex ); } KMutexUnlock( &pPool->mutex ); } else { LOG( "%s(): Couldn't lock mutex", __FUNCTION__ ); assert( 0 ); } } return retval; }
////////////////////////////////////////////////////////////////////////////// // append // Appends a new decoded frame buffer to the "end" of the linked list ////////////////////////////////////////////////////////////////////////////// void H264DecoderFrameList::append(H264DecoderFrame *pFrame) { // Error check if (!pFrame) { // Sent in a NULL frame return; } pFrame->m_index = GetFreeIndex(); // Has a list been constructed - is their a head? if (!m_pHead) { // Must be the first frame appended // Set the head to the current m_pHead = pFrame; m_pHead->setPrevious(0); } if (m_pTail) { // Set the old tail as the previous for the current pFrame->setPrevious(m_pTail); // Set the old tail's future to the current m_pTail->setFuture(pFrame); } else { // Must be the first frame appended // Set the tail to the current m_pTail = pFrame; } // The current is now the new tail m_pTail = pFrame; m_pTail->setFuture(0); // }
// // CreateInstance // // Create a new footprint instance // Instance & CreateInstance(MapObj *obj, Placement &place) { ASSERT(initialized); ASSERT(obj); ASSERT(!obj->GetFootInstance()); // Get a free index U32 index = GetFreeIndex(); ASSERT(!instances[index]); // Create new instance Instance *i = new Instance(obj, index, place); // Add to array instances[index] = i; // Increase instance count instanceCount++; // Return a reference return (*i); }
//+------------------------------------------------------------------------ // // Member: GetDisp // // Synopsis: Get a dispatch ptr from the NON-RESERVED part of the cache. // N.B. non-reserved can never be identity collecitons, // //------------------------------------------------------------------------- HRESULT CCollectionCache::GetDisp( long lIndex, LPCTSTR Name, CollCacheType CacheType, IDispatch** ppdisp, BOOL fCaseSensitive/*=FALSE*/, RECT* pRect/*=NULL*/, BOOL fAlwaysCollection/*=FALSE*/) { long lSize = _aryItems.Size(); long l; HRESULT hr = S_OK; CacheItem* pce; CRect rectCellRange(CRect::CRECT_EMPTY); // named arrays are always built into an AryCacheItem CElementAryCacheItem aryItem; Assert(CacheType==CacheType_Tag || CacheType==CacheType_Named || CacheType==CacheType_CellRange || CacheType==CacheType_Urn); typedef int (*COMPAREFN)(LPCTSTR, LPCTSTR); COMPAREFN CompareFn = fCaseSensitive ? FormsStringCmp : FormsStringICmp; *ppdisp = NULL; pce = &_aryItems[_lReservedSize]; // Return this named collection if it already exists. for(l=_lReservedSize; l<lSize; ++l,++pce) { if(pce->Type==CacheType && lIndex==pce->sIndex && pce->fIsCaseSensitive==(unsigned)fCaseSensitive && !CompareFn(Name, (BSTR)pce->cstrName)) { pce->pdisp->AddRef(); *ppdisp = pce->pdisp; goto Cleanup; } } // Build the list if(CacheType != CacheType_CellRange) { hr=BuildNamedArray(lIndex, Name, CacheType==CacheType_Tag, &aryItem, 0, fCaseSensitive, CacheType==CacheType_Urn); } else { if(!pRect) { // Mark the rect as empty rectCellRange.right = -1; } else { // Use the passed in rect rectCellRange = *pRect; } hr = BuildCellRangeArray(lIndex, Name, &rectCellRange, &aryItem); } if(hr) { goto Cleanup; } // Return based on what the list of named elements looks like. if(!aryItem.Length() && !((CacheType==CacheType_Tag) || (CacheType==CacheType_Urn) || fAlwaysCollection)) { hr = DISP_E_MEMBERNOTFOUND; goto Error; } // The tags method ALWAYS should return a collection (else case) else if(aryItem.Length()==1 && !((CacheType==CacheType_Tag) || (CacheType==CacheType_Urn) || fAlwaysCollection)) { CElement* pElem = aryItem.GetAt(0); hr = pElem->QueryInterface(IID_IDispatch, (void**)ppdisp); // Keep the ppdisp around we'll return that and just release the array. goto Cleanup2; } else { CElementAryCacheItem* pAryItem; hr = GetFreeIndex(&l); // always returns Idx from non-reserved part of cache if(hr) { goto Error; } hr = CreateCollectionHelper(ppdisp, l); if(hr) { goto Error; } pce = &_aryItems[l]; pce->Init(); hr = pce->cstrName.Set(Name); if(hr) { goto Error; } Assert(!pce->_pCacheItem); pce->_pCacheItem = new CElementAryCacheItem(); if(!pce->_pCacheItem) { hr = E_OUTOFMEMORY; goto Cleanup; } // Copy the array. // For perf reasons assume that the destination collection is a // ary cache - which it is for now, by design pAryItem = DYNCAST(CElementAryCacheItem, pce->_pCacheItem); pAryItem->CopyAry(&aryItem); // this just copies the ptrarray _pv across pce->pdisp = *ppdisp; pce->sIndex = lIndex; // Remember the index we depend on. pce->Type = CacheType; pce->fIsCaseSensitive = fCaseSensitive; // Save the range for the cell range type cache so we do not need to parse // the name later if(CacheType == CacheType_CellRange) { pce->rectCellRange = rectCellRange; } // The collection this named collection was built from is now // used to rebuild (ensure) this collection. so we need to // put a reference on it so that it will not go away and its // location re-assigned by another call to GetFreeIndex. // The matching Release() will be done in the dtor // although it is not necessary to addref the reserved collections // it is done anyhow, simply for consistency. This addref // only needs to be done for non-reserved collections if(lIndex >= _lReservedSize) { _aryItems[lIndex].pdisp->AddRef(); } } Cleanup: RRETURN(hr); Error: ClearInterface(ppdisp); Cleanup2: goto Cleanup; }
// Find pElement in the lIndex base Collection HRESULT CCollectionCache::CreateChildrenCollection( long lCollectionIndex, CElement* pElement, IDispatch** ppDisp, BOOL fAllChildren, BOOL fDOMCollection) { CacheItem* pce; long lSize = _aryItems.Size(), l; HRESULT hr = S_OK; CollCacheType Type = fAllChildren ? CacheType_AllChildren : (fDOMCollection)?CacheType_DOMChildNodes:CacheType_Children; Assert(ppDisp); *ppDisp = NULL; hr = EnsureAry(lCollectionIndex); if(hr) { goto Cleanup; } // Try and locate an exiting collection pce = &_aryItems[_lReservedSize]; // Return this named collection if it already exists. for(l=_lReservedSize; l<lSize; ++l,++pce) { if(pce->Type==Type && pElement==pce->pElementBase) { pce->pdisp->AddRef(); *ppDisp = pce->pdisp; goto Cleanup; } } // Didn't find it, create a new collection hr = GetFreeIndex(&l); // always returns Idx from non-reserved part of cache if(hr) { goto Cleanup; } hr = CreateCollectionHelper(ppDisp, l); if(hr) { goto Cleanup; } pce = &_aryItems[l]; pce->Init(); Assert(!pce->_pCacheItem); pce->_pCacheItem = new CElementAryCacheItem(); if(!pce->_pCacheItem) { hr = E_OUTOFMEMORY; goto Cleanup; } hr = BuildChildArray(lCollectionIndex, pElement, pce->_pCacheItem, fAllChildren); if(hr) { goto Cleanup; } pce->pElementBase = pElement; pce->pdisp = *ppDisp; pce->sIndex = lCollectionIndex; // Remember the index we depend on. pce->Type = Type; Cleanup: RRETURN(hr); }