2015-04-23 21:32:58 -05:00
// 4 december 2014
# include "uipriv_windows.h"
// wrappers for allocator of choice
// panics on memory exhausted, undefined on heap corruption or other unreliably-detected malady (see http://stackoverflow.com/questions/28761680/is-there-a-windows-api-memory-allocator-deallocator-i-can-use-that-will-just-giv)
// new memory is set to zero
// passing NULL to tableRealloc() acts like tableAlloc()
// passing NULL to tableFree() is a no-op
2015-05-02 11:27:53 -05:00
static HANDLE heap ;
2015-05-02 11:29:55 -05:00
int initAlloc ( void )
2015-05-02 11:27:53 -05:00
{
heap = HeapCreate ( 0 , 0 , 0 ) ;
return heap ! = NULL ;
}
2015-05-03 18:52:24 -05:00
void * uiAlloc ( size_t size )
2015-04-23 21:32:58 -05:00
{
void * out ;
2015-05-02 11:27:53 -05:00
out = HeapAlloc ( heap , HEAP_ZERO_MEMORY , size ) ;
2015-04-23 21:32:58 -05:00
if ( out = = NULL ) {
2015-05-03 18:52:24 -05:00
fprintf ( stderr , " memory exhausted in uiAlloc() \n " ) ;
2015-04-23 21:32:58 -05:00
abort ( ) ;
}
return out ;
}
2015-05-03 18:52:24 -05:00
void * uiRealloc ( void * p , size_t size )
2015-04-23 21:32:58 -05:00
{
void * out ;
if ( p = = NULL )
2015-05-03 18:52:24 -05:00
return uiAlloc ( size ) ;
2015-05-02 11:27:53 -05:00
out = HeapReAlloc ( heap , HEAP_ZERO_MEMORY , p , size ) ;
2015-04-23 21:32:58 -05:00
if ( out = = NULL ) {
2015-05-03 18:52:24 -05:00
fprintf ( stderr , " memory exhausted in uiRealloc() \n " ) ;
2015-04-23 21:32:58 -05:00
abort ( ) ;
}
return out ;
}
void uiFree ( void * p )
{
if ( p = = NULL )
return ;
2015-05-02 11:27:53 -05:00
if ( HeapFree ( heap , 0 , p ) = = 0 )
logLastError ( " error freeing memory in uiFree() " ) ;
2015-04-23 21:32:58 -05:00
}