mpr/malloc.cpp File Reference


Detailed Description

Fast and safe malloc replacemen for embedded use.

Overview:
This memory allocator is fast, thread-safe, SMP scaling. It tracks memory leaks and keeps allocation statistics with minimal overhead. It also extends the standard memory allocation API with "safer" APIs.
Remarks:
This module is thread-safe.


Functions

int  mprCreateMemHeap (char *userBuf, int initialSize, int limit)
void  mprFree (void *ptr)
void *  mprMalloc (uint size)
void *  mprRealloc (void *ptr, uint size)
char *  mprStrdup (const char *str)

Function Documentation

int mprCreateMemHeap char *  userBuf,
int  initialSize,
int  limit
 

Synopsis:
Initialize the memory heap.
Overview:
Initialize the memory heap. The Mbedthis malloc subsystem offers several benefits:
  • It can pre-allocate memory to ensure memory allocations do not fail
  • It can allocate memory out of a static user buffer so that no dynamic memory allocation calls will be made at run-time. Ideal for VxWorks which tends to fragment memory with high dynamic memory loads.
  • It can impose memory allocation limits so that other programs are not compromised.
  • A memory handler is called on memory allocation failures.
Parameters:
userBuf  NULL to dynamically allocate memory from the operating system. Set to a valid buffer of length size and memory will be allocated out of that buffer. Ideal for embedded systems such as VxWorks to ensure memory allocations cannot fail.
initialSize  Define the size of the supplied user buffer, or if userBuf is NULL, it defines the initial size of dynamic memory to allocate.
limit  Specify the maximum amount of dynamic memory to allocate.
Returns:
Returns zero if successful. Otherwise a negative MPR error code.
Stability classification:
Evolving.
Library:
libappWeb
See also:
mprMalloc, mprFree

void mprFree void *  ptr  ) 
 

Synopsis:
Safe replacement for free.
Overview:
mprFree should be used to free memory allocated by mprMalloc, mprRealloc or mprCalloc.
Parameters:
ptr  Memory to free. If NULL, take no action.
Remarks:
mprFree can reduce the overall application code size by allowing the memory block ptr to be NULL.
See also:
mprMalloc, mprCalloc, mprRealloc

void * mprMalloc uint  size  ) 
 

Synopsis:
Safe replacement for malloc.
Overview:
mprMalloc wraps the standard malloc or if BLD_FEATURE_MALLOC is enabled, it will replace malloc with a fast, deterministic embedded memory allocator that is more deterministic with regard to

void * mprRealloc void *  ptr,
uint  size
 

Synopsis:
Safe replacement for realloc
Overview:
mprRealloc should be used to reallocate memory blocks that have been allocated with mprMalloc or mprStrdup.
Parameters:
ptr  Memory to reallocate. If NULL, call malloc.
size  New size of the required memory block.
Returns:
Returns a pointer to the newly allocated memory block.
Remarks:
Do not mix calls to realloc and mprRealloc.

char * mprStrdup const char *  str  ) 
 

Synopsis:
Safe replacement for strdup
Overview:
mprStrdup() should be used as a replacement for strdup wherever possible. It allows the strdup to be copied to be NULL, in which case it will allocate an empty string.
Parameters:
str  Pointer to string to duplicate. If str is NULL, allocate a new string containing only a trailing NULL character.
Returns:
Returns an allocated string including trailing null.
Remarks:
Memory allocated via mprStrdup() must be freed via mprFree().
See also:
mprFree, mprMalloc, mprRealloc, mprCalloc

© Mbedthis Software LLC, 2003-2004. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.