/home/mob/svn/appWeb/1.2.3/mpr/malloc.cpp File Reference


Detailed Description

Fast and safe malloc replacemen for embedded use.

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.

This module is thread-safe.

#include "mpr.h"


Functions

int mprCreateMemHeap (char *userBuf, int bufsize, int limit)
void mprSetMemHandler (MprMemProc cback)
void mprRequestMemStats (bool on)
void * mprMalloc (uint size)
void mprFree (void *ptr)
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 should be used as a replacement for malloc wherever possible. It uses a fast, embedded memory allocator that is more deterministic with regard to fragmentation.
Parameters:
size Size of the memory block to allocate.
Returns:
Returns a pointer to the allocated block. This routine will never return NULL if the block cannot be allocated. Rather the memory exhaustion handler specified by mprSetMemHandler will be called to allow global recovery.
See also:
mprFree, mprRealloc, mprCalloc, mprSetMemHandler

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.

void mprRequestMemStats bool  on  ) 
 

Synopsis:
Output a memory statistics report to stdout on program exit.
Parameters:
on TRUE if memory statistics are required

void mprSetMemHandler MprMemProc  cback  ) 
 

Define a memory callback to be called if all available memory is exhausted.

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.