5#ifdef TMEMORY_USE_DLMALLOC
10# define SIZE_T_SIZE ( sizeof( TSIZE ) )
11# define SIZE_T_BITSIZE ( sizeof( TSIZE ) << 3 )
15# define SIZE_T_ZERO ( (TSIZE)0 )
16# define SIZE_T_ONE ( (TSIZE)1 )
17# define SIZE_T_TWO ( (TSIZE)2 )
18# define SIZE_T_FOUR ( (TSIZE)4 )
19# define TWO_SIZE_T_SIZES ( SIZE_T_SIZE << 1 )
20# define FOUR_SIZE_T_SIZES ( SIZE_T_SIZE << 2 )
21# define SIX_SIZE_T_SIZES ( FOUR_SIZE_T_SIZES + TWO_SIZE_T_SIZES )
22# define HALF_MAX_SIZE_T ( MAX_SIZE_T / 2U )
25# define CHUNK_ALIGN_MASK ( MALLOC_ALIGNMENT - SIZE_T_ONE )
28# define is_aligned( A ) ( ( (TSIZE)( ( A ) ) & ( CHUNK_ALIGN_MASK ) ) == 0 )
31# define align_offset( A ) \
32 ( ( ( (TSIZE)(A)&CHUNK_ALIGN_MASK ) == 0 ) ? 0 : \
33 ( ( MALLOC_ALIGNMENT - ( (TSIZE)(A)&CHUNK_ALIGN_MASK ) ) & CHUNK_ALIGN_MASK ) )
39 struct malloc_chunk* fd;
40 struct malloc_chunk* bk;
43typedef struct malloc_chunk mchunk;
44typedef struct malloc_chunk* mchunkptr;
45typedef struct malloc_chunk* sbinptr;
46typedef unsigned int bindex_t;
47typedef unsigned int binmap_t;
48typedef unsigned int flag_t;
52# define MCHUNK_SIZE ( sizeof( mchunk ) )
55# define CHUNK_OVERHEAD ( TWO_SIZE_T_SIZES )
57# define CHUNK_OVERHEAD ( SIZE_T_SIZE )
61# define MMAP_CHUNK_OVERHEAD ( TWO_SIZE_T_SIZES )
63# define MMAP_FOOT_PAD ( FOUR_SIZE_T_SIZES )
66# define MIN_CHUNK_SIZE \
67 ( ( MCHUNK_SIZE + CHUNK_ALIGN_MASK ) & ~CHUNK_ALIGN_MASK )
70# define chunk2mem( p ) ( (void*)( (char*)( p ) + TWO_SIZE_T_SIZES ) )
71# define mem2chunk( mem ) ( (mchunkptr)( (char*)(mem)-TWO_SIZE_T_SIZES ) )
73# define align_as_chunk( A ) ( mchunkptr )( ( A ) + align_offset( chunk2mem( A ) ) )
85# define PINUSE_BIT ( SIZE_T_ONE )
86# define CINUSE_BIT ( SIZE_T_TWO )
87# define FLAG4_BIT ( SIZE_T_FOUR )
88# define INUSE_BITS ( PINUSE_BIT | CINUSE_BIT )
89# define FLAG_BITS ( PINUSE_BIT | CINUSE_BIT | FLAG4_BIT )
91# define chunksize( p ) ( ( p )->head & ~( FLAG_BITS ) )
95void TMemoryDL::Shutdown()
97 dlheapdestroy( g_pMemoryDL->m_GlobalHeap );
98 g_pMemoryDL->m_GlobalHeap =
TNULL;
100 if ( g_pMemoryDL->m_Context.s_Heap )
102 HeapFree( g_pMemoryDL->m_Context.s_Sysheap, NULL, g_pMemoryDL->m_Context.s_Heap );
105 if ( TMemoryDL::GetFlags() & Flags_Standard )
108 g_pMemoryDL->m_Context.s_cbMalloc = []( TMemoryDL* pMemModule,
TSIZE size ) ->
void* {
111 g_pMemoryDL->m_Context.s_cbCalloc = []( TMemoryDL* pMemModule,
TSIZE nitems,
TSIZE size ) ->
void* {
114 g_pMemoryDL->m_Context.s_cbRealloc = []( TMemoryDL* pMemModule,
void* ptr,
TSIZE size ) ->
void* {
117 g_pMemoryDL->m_Context.s_cbMemalign = []( TMemoryDL* pMemModule,
TSIZE alignment,
TSIZE size ) ->
void* {
120 g_pMemoryDL->m_Context.s_cbFree = []( TMemoryDL* pMemModule,
void* ptr ) ->
void {
122 g_pMemoryDL->m_Context.s_cbIdk = []( TMemoryDL* pMemModule,
void* ptr,
TSIZE size ) ->
void {
126 TUtil::MemSet( &g_pMemoryDL->m_Context, 0,
sizeof( m_Context ) );
129TMemoryDL::Error TMemoryDL::Init()
132 TASSERT( m_Context.s_Sysheap == NULL,
"TMemoryDL is already initialized" );
134 m_Context.m_pMemModule =
this;
135 m_Context.s_Sysheap = GetProcessHeap();
138 if ( m_Context.s_Sysheap == NULL )
144 if ( m_Flags & Flags_NativeMethods )
146 m_Context.s_cbMalloc = TMemoryDLContext::MallocNative;
147 m_Context.s_cbCalloc = TMemoryDLContext::CallocNative;
148 m_Context.s_cbRealloc = TMemoryDLContext::ReallocNative;
149 m_Context.s_cbMemalign = TMemoryDLContext::MemalignNative;
150 m_Context.s_cbFree = TMemoryDLContext::FreeNative;
151 m_Context.s_cbIdk = TMemoryDLContext::IdkNative;
157 m_Context.s_Heap = HeapAlloc( m_Context.s_Sysheap, HEAP_ZERO_MEMORY, m_GlobalSize );
160 if ( m_Context.s_Heap == NULL )
165 m_Context.s_cbMalloc = []( TMemoryDL* pMemModule,
TSIZE size ) ->
void* {
166 return pMemModule->dlheapmalloc( pMemModule->GetHeap(), size );
169 m_Context.s_cbCalloc = []( TMemoryDL* pMemModule,
TSIZE nitems,
TSIZE size ) ->
void* {
170 return pMemModule->dlheapcalloc( pMemModule->GetHeap(), nitems, size );
173 m_Context.s_cbRealloc = []( TMemoryDL* pMemModule,
void* ptr,
TSIZE size ) ->
void* {
174 return pMemModule->dlheaprealloc( pMemModule->GetHeap(), ptr, size );
177 m_Context.s_cbMemalign = []( TMemoryDL* pMemModule,
TSIZE alignment,
TSIZE size ) ->
void* {
178 return pMemModule->dlheapmemalign( pMemModule->GetHeap(), alignment, size );
181 m_Context.s_cbFree = []( TMemoryDL* pMemModule,
void* ptr ) ->
void {
182 pMemModule->dlheapfree( pMemModule->GetHeap(), ptr );
185 m_Context.s_cbIdk = []( TMemoryDL* pMemModule,
void* ptr,
TSIZE size ) ->
void {
189 m_GlobalHeap = CreateHeapInPlace( m_Context.s_Heap, m_GlobalSize, TMemoryHeapFlags_UseMutex,
"global" );
#define TOSHI_NAMESPACE_START
#define TOSHI_NAMESPACE_END
static void MemSet(void *ptr, TINT value, TSIZE size)