uLib  User mode C/C++ extended API library for Win32 programmers.
Dynamic Memory Allocation

Module Description

The mem_Nnn functions provide solid heap management.
#include <uLib/MemFunc.h>

The mem_Alloc() and mem_Realloc() functions go to considerable length to succeed,
including coalescing the heap to satisfy failed allocations, and mem_Free()
wraps deallocation in SEH (compiler permitting) to avoid termination-by-mistake.

Groups

 Page Buffers
 Page aligned virtual memory.
 
 Physical Memory (AWE)
 Allocation of physical memory.
 

Classes

class  SharedMem
 Interprocess shared memory. More...
 

Functions

void * mem_Alloc (size_t Bytes)
 
void * mem_Realloc (void *pBlk, size_t Bytes)
 
void * mem_Free (void *pBlk)
 
bool mem_Compact ()
 

Variables

HANDLE app_Heap
 

Function Documentation

◆ mem_Alloc()

void* mem_Alloc ( size_t  Bytes)

Allocate a block from the app_Heap.

Parameters
BytesSize to allocate. If zero, a NULL pointer is returned.
Returns
The re/allocated block, or NULL.

Note: The mem_Alloc() and mem_Realloc() functions both try to coalesce the heap if an allocation fails,
and if a retry fails, validate the whole heap before they give up.

Definition at line 33 of file MemFunc.cpp.

◆ mem_Realloc()

void* mem_Realloc ( void *  pBlk,
size_t  Bytes 
)

Reallocate an existing memory block.
Note: Guaranteed zero,null contract, forever.
(C23 'victims' will appreciate the age proven 'realloc' compliance.)

Parameters
pBlkBlock ptr. If NULL, mem_Alloc is invoked.
BytesSize to allocate. If zero, and pBlk is not null, invoke mem_Free.
Returns
The re/allocated block, or NULL.
See also
See mem_Alloc(). mem_Free().

Definition at line 80 of file MemFunc.cpp.

◆ mem_Free()

void* mem_Free ( void *  pBlk)

Free a heap block and return NULL on success, or pBlk on error.
The deallocation is wrapped in SEH (compiler permitting) to avoid termination-by-mistake from invalid block pointers.
Note: mem_Free is NULL-safe.

Definition at line 124 of file MemFunc.cpp.

◆ mem_Compact()

bool mem_Compact ( )

mem_Compact() coalesces free blocks and decommits unused pages.
This is done using HeapCompact(), which is fairly expensive, so don't overuse.

Definition at line 150 of file MemFunc.cpp.

Variable Documentation

◆ app_Heap

HANDLE app_Heap

The heap used by the mem_Nnn functions.
The app_Heap is globally initialized with GetProcessHeap during startup.
However, nothing prevents you from changing it with HeapCreate if you prefer a customized heap.
Just be sure to do it before using any mem_Nnn functions.

Definition at line 12 of file MemFunc.cpp.