_WCRTLINK void _nheapgrow( void ) { #if defined( __WINDOWS_286__ ) || !defined( _M_I86 ) _nfree( _nmalloc( 1 ) ); /* get something into the heap */ #else unsigned max_paras; unsigned curr_paras; unsigned diff_paras; unsigned expand; _AccessNHeap(); /* calculate # pages which always has extra slack space (ie. 0x10) */ curr_paras = (( _curbrk + 0x10 ) & ~0x0f ) >> 4; if( curr_paras == 0 ) { /* we're already at 64k */ _ReleaseNHeap(); return; } #if defined(__QNX__) if( qnx_segment_realloc( _DGroup(), 65536L ) == -1 ) { _ReleaseNHeap(); return; } max_paras = PARAS_IN_64K; #elif defined(__OS2__) if( DosReallocSeg( 0, _DGroup() ) ) { _ReleaseNHeap(); return; } max_paras = PARAS_IN_64K; #else if( _RWD_osmode != DOS_MODE ) { /* 23-apr-91 */ max_paras = PARAS_IN_64K; } else { max_paras = TinyMaxSet( _RWD_psp ); /* subtract off code size */ max_paras -= _DGroup() - _RWD_psp; if( max_paras > PARAS_IN_64K ) { max_paras = PARAS_IN_64K; } } #endif if( max_paras <= curr_paras ) { /* '<' -> something is wrong, '==' -> can't change size */ _ReleaseNHeap(); return; } diff_paras = max_paras - curr_paras; expand = (( diff_paras + 1 ) << 4 ) - ( _curbrk & 0x0f ); expand += __LastFree(); /* compensate for _expand's adjustment */ _ReleaseNHeap(); _nfree( _nmalloc( expand - ( sizeof( size_t ) + sizeof(frl) ) ) ); #endif }
static void *_allocate( unsigned amount ) { void *p; #if defined(__386__) p = malloc( amount ); #else p = _nmalloc( amount ); #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__) if( (void near *) p == NULL ) p = malloc( amount ); #endif #endif if( p == NULL ) _Not_Enough_Memory(); return( p ); }
void f5( void ) { void *p; void __near *q; q = _nmalloc( 20 ); p = malloc( 10 ); free( p ); _nfree( q ); }
_WCRTLINK void *_os2hmalloc( size_t amount ) { int prior; void _WCNEAR *ptr; _AccessNHeap(); prior = _use_os2_high_mem( 1 ); ptr = _nmalloc( amount ); _use_os2_high_mem(prior); _ReleaseNHeap(); return( ptr ); }
_WCRTLINK void _WCNEAR *_nrealloc( void _WCI86NEAR *stg, size_t req_size ) { void _WCNEAR *p; size_t old_size; if( stg == NULL ) { return( _nmalloc( req_size ) ); } if( req_size == 0 ) { _nfree( stg ); return( (void _WCNEAR *) NULL ); } old_size = _nmsize( stg ); p = _nexpand( stg, req_size ); /* try to expand it in place */ if( p == NULL ) { /* if couldn't be expanded in place */ #if defined(__DOS_EXT__) if( _IsRational() ) { frlptr flp, newflp; flp = (frlptr) ((PTR)stg - TAG_SIZE); newflp = __ReAllocDPMIBlock( flp, req_size + TAG_SIZE ); if( newflp ) { return( (void _WCNEAR *)((PTR)newflp + TAG_SIZE) ); } } #endif p = _nmalloc( req_size ); /* - allocate a new block */ if( p != NULL ) { /* - if we got one */ memcpy( p, stg, old_size ); /* copy it */ _nfree( stg ); /* and free old one */ } else { _nexpand( stg, old_size ); /* reset back to old size */ } } return( p ); }
_WCRTLINK void _WCNEAR *_ncalloc( size_t n, size_t el_size ) { void _WCI86NEAR *p; unsigned long chk_size; chk_size = (unsigned long) n * el_size; el_size = chk_size; #if defined( _M_I86 ) if( el_size != chk_size ) { return( (void _WCNEAR *)NULL ); } #endif p = _nmalloc( el_size ); if( p != (void _WCNEAR *)NULL ) { memset( p, 0, el_size ); } return( p ); }
_WCRTLINK void *malloc( size_t amount ) { return( _nmalloc( amount ) ); }
_WCRTLINK void _WCFAR *_fmalloc( size_t amt ) { unsigned size; unsigned offset; unsigned short seg; unsigned short prev_seg; struct heapblk _WCFAR *p; if( amt == 0 || amt > - (sizeof(struct heapblk) + TAG_SIZE*2) ) { return( (void _WCFAR *)NULL ); } // Try to determine which segment to begin allocating from. // first, round up the amount size = (amt + TAG_SIZE + ROUND_SIZE) & ~ROUND_SIZE; if( size < FRL_SIZE ) { size = FRL_SIZE; } _AccessFHeap(); for(;;) { if( size > __LargestSizeB4Rover ) { seg = __fheapRover; } else { __LargestSizeB4Rover = 0; // force value to be updated seg = __fheap; } for(;;) { if( seg == 0 ) { seg = __AllocSeg( amt ); if( seg == 0 ) break; if( __fheap == 0 ) { __fheap = seg; } else { p->nextseg = seg; p = MK_FP( seg, 0 ); p->prevseg = prev_seg; } } for(;;) { __fheapRover = seg; offset = __MemAllocator( amt, seg, 0 ); if( offset != 0 ) goto release_heap; if( __GrowSeg( seg, amt ) == 0 ) break; } prev_seg = seg; p = MK_FP( seg, 0 ); if( p->largest_blk > __LargestSizeB4Rover ) { __LargestSizeB4Rover = p->largest_blk; } seg = p->nextseg; } if( __fmemneed( amt ) == 0 ) break; } if( seg == 0 ) { offset = (unsigned)_nmalloc( amt ); if( offset != 0 ) seg = _DGroup(); } release_heap: _ReleaseFHeap(); return( MK_FP( seg, offset ) ); }
_WCRTLINK void _nheapgrow( void ) { _nfree( _nmalloc( 1 ) ); /* get something into the heap */ }