Next: , Previous: , Up: Top   [Contents][Index]


7 Memory handling

There are basic memory handling wrappers around the standard C library memory handling routines. The key difference is, that the ooc memory handling throws exceptions in case of a system failure, and performs some tasks that are necessary for multi-threaded or reentrant programming.

7.1 Memory allocation

You can allocate memory with the following routines:

void * ooc_malloc( size_t size );

The same to the standard malloc, except that throws an exception on failure, thus never returns NULL.


void * ooc_calloc( size_t num, size_t size );

The same to the standard calloc, except that throws an exception on failure, thus never returns NULL.


void * ooc_realloc( void *ptr, size_t size );

The same to the standard realloc, except that throws an exception on failure, thus never returns NULL.


void * ooc_memdup( const void * ptr, size_t size );

Duplicates a memory block using the standard memcpy, and throws an exception on failure, thus never returns NULL.

7.2 Freeing the allocated memory

Deallocate the memory with one of the following methods:

void ooc_free( void * mem );

Frees a memory block allocated with one of the above allocation codes. Never failes, mem can be NULL;


void ooc_free_and_null( void ** mem_ptr );

Frees a memory block via a memory pointer and nulls the pointer simultaneously. It is very important for thread-safe or reentrant codes. It is also very important for freeing memory blocks with circular references. mem_ptr can point to a NULL pointer.


Always use this, if your class has a memory pointer as a class member, for example in a class destructor!

7.3 Thread safety

Important! ooc is thread safe only if the underlying standard C library (malloc, calloc, realloc, setjmp/longjmp) is thread-safe too!


The thread safety of ooc does not mean that your code will be thread safe automatically! You must take care about the proper thread-safe implementation of your classes!


The ooc_init_class(), ooc_finalize_class() and ooc_finalize_all() functions are not thread safe! It is advisable calling ooc_init_class() from the main thread before the fork and ooc_finalize_all() after the join.


Next: , Previous: , Up: Top   [Contents][Index]