|
// alloc.cpp
// based on:
// LIBCTINY - Matt Pietrek 2001
// MSDN Magazine, January 2001
// 08/12/06 (mv)
// Modified without permission by Domagoj �ari� (dsaritz at gmail . com ).
// 02/05/09
#include <windows.h>
#include <malloc.h>
#include <cassert>
#include "libct.h"
// For 'native' long long arithmetic on 32 bit machines add these to the link:
// llmul.obj lldiv.obj llshl.obj llshr.obj ulldiv.obj
// #pragma comment(lib, "lldiv.obj")
// For runtime checks, alloca and exceptions add these to the link:
// RunTmChk.lib chkstk.obj
HANDLE getLowFragmentationHeap()
{
HANDLE const hLocalProcessHeap( ::GetProcessHeap() );
ULONG useLowFragmentationHeapFlag( 2 );
// The return value/success is not checked because the function always fails
// in debug builds, and if it somehow fails in release there is nothing to
// do about it.
::HeapSetInformation( hLocalProcessHeap, HeapCompatibilityInformation, &useLowFragmentationHeapFlag, sizeof( useLowFragmentationHeapFlag ) );
return hLocalProcessHeap;
}
BEGIN_EXTERN_C
#pragma warning( disable: 4074 ) // initializers put in compiler reserved initialization area
#pragma init_seg( compiler )
static HANDLE const hProcessHeap( getLowFragmentationHeap() );
__declspec(restrict, noalias) void * malloc( size_t const size )
{
return ::HeapAlloc( hProcessHeap, 0, size );
}
__declspec(noalias) void free( void * const p )
{
BOOL const success( ::HeapFree( hProcessHeap, 0, p ) );
assert( success ); success;
}
__declspec(restrict, noalias) void * realloc( void * const p, size_t const size )
{
return p ? ::HeapReAlloc( hProcessHeap, 0, p, size ) : ::HeapAlloc( hProcessHeap, 0, size );
}
__declspec(restrict, noalias) void *calloc( size_t const nitems, size_t const size )
{
return ::HeapAlloc( hProcessHeap, HEAP_ZERO_MEMORY, nitems * size );
}
__declspec(restrict, noalias) void *_recalloc( void * const p, size_t const nitems, size_t const size )
{
return p ? ::HeapReAlloc( hProcessHeap, HEAP_ZERO_MEMORY, p, nitems * size )
: ::HeapAlloc ( hProcessHeap, HEAP_ZERO_MEMORY, nitems * size );
}
void *_calloc_crt(size_t const nitems, size_t const size )
{
return calloc( nitems, size );
}
void *_nh_malloc( size_t const size, int /* nhFlag */ )
{
return ::HeapAlloc( hProcessHeap, 0, size );
}
size_t _msize( void * const p )
{
return ::HeapSize( hProcessHeap, 0, p );
}
END_EXTERN_C
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.