void * __cdecl _expand_base (void * pBlock, size_t newsize) { PHEADER pHeader; void * pvReturn; /* validate size */ if ( newsize > _HEAP_MAXREQ ) return NULL; _mlock(_HEAP_LOCK); // if allocation block lies within the small-block heap, // try to resize it there if ((pHeader = __sbh_find_block(pBlock)) != NULL) { pvReturn = NULL; if (newsize <= __sbh_threshold && __sbh_resize_block(pHeader, pBlock, newsize)) pvReturn = pBlock; _munlock(_HEAP_LOCK); return pvReturn; } _munlock(_HEAP_LOCK); // force nonzero size and round up to next paragraph if (newsize == 0) newsize = 1; newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); return (HeapReAlloc(_crtheap, HEAP_REALLOC_IN_PLACE_ONLY, pBlock, newsize)); }
void * __cdecl _expand_base (void * pBlock, size_t newsize) { void * pvReturn; /* validation section */ _VALIDATE_RETURN(pBlock != NULL, EINVAL, NULL); if (newsize > _HEAP_MAXREQ) { errno = ENOMEM; return NULL; } #ifndef _WIN64 if ( __active_heap == __V6_HEAP ) { PHEADER pHeader; _mlock( _HEAP_LOCK ); __try { // if allocation block lies within the small-block heap, // try to resize it there if ((pHeader = __sbh_find_block(pBlock)) != NULL) { pvReturn = NULL; if ( (newsize <= __sbh_threshold) && __sbh_resize_block(pHeader, pBlock, (int)newsize) ) pvReturn = pBlock; } } __finally { _munlock( _HEAP_LOCK ); } if ( pHeader == NULL ) { // force nonzero size and round up to next paragraph if (newsize == 0) newsize = 1; newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); pvReturn = HeapReAlloc(_crtheap, HEAP_REALLOC_IN_PLACE_ONLY, pBlock, newsize); if (pvReturn == NULL) { errno = _get_errno_from_oserr(GetLastError()); } } }
inline void* ObjectMemory::reallocChunk(void* pChunk, MWORD newChunkSize) { #if defined(_DEBUG) if (__sbh_find_block(pChunk) != NULL) // Hang on a minute, this block is from the SBH ASSERT(FALSE); else { for (int i=0;i<NumPools;i++) ASSERT(!m_pools[i].isMyChunk(reinterpret_cast<MWORD*>(pChunk))); } #endif #ifdef PRIVATE_HEAP return ::HeapReAlloc(m_hHeap, 0, pChunk, newChunkSize); #else return realloc(pChunk, newChunkSize); #endif }
void __cdecl _free_base (void * pBlock) { PHEADER pHeader; if (pBlock == NULL) return; _mlock(_HEAP_LOCK); if ((pHeader = __sbh_find_block(pBlock)) != NULL) { __sbh_free_block(pHeader, pBlock); _munlock(_HEAP_LOCK); } else { _munlock(_HEAP_LOCK); HeapFree(_crtheap, 0, pBlock); } }
void * __cdecl _realloc_base (void * pBlock, size_t newsize) { void * pvReturn; size_t origSize = newsize; // if ptr is NULL, call malloc if (pBlock == NULL) return(_malloc_base(newsize)); // if ptr is nonNULL and size is zero, call free and return NULL if (newsize == 0) { _free_base(pBlock); return(NULL); } #ifndef _WIN64 if ( __active_heap == __V6_HEAP ) { PHEADER pHeader; size_t oldsize; for (;;) { pvReturn = NULL; if (newsize <= _HEAP_MAXREQ) { _mlock( _HEAP_LOCK ); __try { // test if current block is in the small-block heap if ((pHeader = __sbh_find_block(pBlock)) != NULL) { // if the new size is not over __sbh_threshold, attempt // to reallocate within the small-block heap if (newsize <= __sbh_threshold) { if (__sbh_resize_block(pHeader, pBlock, (int)newsize)) pvReturn = pBlock; else if ((pvReturn = __sbh_alloc_block((int)newsize)) != NULL) { oldsize = ((PENTRY)((char *)pBlock - sizeof(int)))->sizeFront - 1; memcpy(pvReturn, pBlock, __min(oldsize, newsize)); // headers may have moved, get pHeader again pHeader = __sbh_find_block(pBlock); __sbh_free_block(pHeader, pBlock); } } // If the reallocation has not been (successfully) // performed in the small-block heap, try to allocate // a new block with HeapAlloc. if (pvReturn == NULL) { if (newsize == 0) newsize = 1; newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); if ((pvReturn = HeapAlloc(_crtheap, 0, newsize)) != NULL) { oldsize = ((PENTRY)((char *)pBlock - sizeof(int)))->sizeFront - 1; memcpy(pvReturn, pBlock, __min(oldsize, newsize)); __sbh_free_block(pHeader, pBlock); } } } } __finally { _munlock( _HEAP_LOCK ); } // the current block is NOT in the small block heap iff pHeader // is NULL if ( pHeader == NULL ) { if (newsize == 0) newsize = 1; newsize = (newsize + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1); pvReturn = HeapReAlloc(_crtheap, 0, pBlock, newsize); } } else /* newsize > _HEAP_MAXREQ */ {