int __cdecl _heapadd ( void * block, size_t size ) { int retval; /* * Validate user's input. Note that _GRANULARITY must be a power * of 2 for the tests below to be valid! */ if ( (size == 0) || ((unsigned)block & (_GRANULARITY - 1)) || (size & (_GRANULARITY - 1)) ) return(-1); /* * Add the block to the heap. */ _mlock(_HEAP_LOCK); retval = _heap_addblock(block, size); _munlock(_HEAP_LOCK); return(retval); }
int __cdecl _heap_grow_region ( REG1 unsigned index, size_t size ) { size_t left; REG2 size_t growsize; void * base; struct _heap_region_ *pHeapRegions; struct _heap_region_ *pHRTmp; /* * Init some variables * left = space left in region * base = base of next section of region to validate */ pHeapRegions = (struct _heap_region_ *)(*hHeapRegions); pHRTmp = pHeapRegions + index; left = pHRTmp->_totalsize - pHRTmp->_currsize; base = (char *) (pHRTmp->_regbase) + pHRTmp->_currsize; /* * Make sure we can satisfy request */ growsize = _ROUND2(size, _GRANULARITY); if (left < growsize) { size_t sizeTmp; sizeTmp = growsize-left+1+ pHRTmp->_totalsize; sizeTmp = _ROUND2(sizeTmp, _GRANULARITY); SetPtrSize(pHRTmp->_regbase, sizeTmp); pHeapRegions = (struct _heap_region_ *)(*hHeapRegions); pHRTmp = pHeapRegions + index; if (*pMemErr != 0) { goto error; } pHRTmp->_totalsize = sizeTmp; left = sizeTmp - pHRTmp->_currsize; base = (char *) (pHRTmp->_regbase) + pHRTmp->_currsize; } /* * Update the region data base */ pHRTmp->_currsize += growsize; #ifdef DEBUG /* * The current size should never be greater than the total size */ if ((pHeapRegions + index)->_currsize > (pHeapRegions + index)->_totalsize) _heap_abort(); #endif /* * Add the memory to the heap */ if (_heap_addblock(base, growsize) != 0) _heap_abort(); /* * Good return */ /* done: unreferenced label to be removed */ return(0); /* * Error return */ error: return(-1); }
int __cdecl _heap_grow_region ( REG1 unsigned index, size_t size ) { size_t left; REG2 size_t growsize; void * base; unsigned dosretval; /* * Init some variables * left = space left in region * base = base of next section of region to validate */ left = _heap_regions[index]._totalsize - _heap_regions[index]._currsize; base = (char *) _heap_regions[index]._regbase + _heap_regions[index]._currsize; /* * Make sure we can satisfy request */ if (left < size) goto error; /* * Round size up to next _heap_growsize boundary. * (Must round _heap_growsize itself to page boundary, in * case user set it himself). */ growsize = _ROUND2(_heap_growsize, _PAGESIZE_); growsize = _ROUND(size, growsize); if (left < growsize) growsize = left; /* * Validate the new portion of the region */ if (!VirtualAlloc(base, growsize, MEM_COMMIT, PAGE_READWRITE)) dosretval = GetLastError(); else dosretval = 0; if (dosretval) /* * Error committing pages. If out of memory, return * error, else abort. */ if (dosretval == ERROR_NOT_ENOUGH_MEMORY) goto error; else _heap_abort(); /* * Update the region data base */ _heap_regions[index]._currsize += growsize; #ifdef DEBUG /* * The current size should never be greater than the total size */ if (_heap_regions[index]._currsize > _heap_regions[index]._totalsize) _heap_abort(); #endif /* * Add the memory to the heap */ if (_heap_addblock(base, growsize) != 0) _heap_abort(); /* * Good return */ /* done: unreferenced label to be removed */ return(0); /* * Error return */ error: return(-1); }