andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: prmem.h andre@0: ** Description: API to NSPR memory management functions andre@0: ** andre@0: */ andre@0: #ifndef prmem_h___ andre@0: #define prmem_h___ andre@0: andre@0: #include "prtypes.h" andre@0: #include andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* andre@0: ** Thread safe memory allocation. andre@0: ** andre@0: ** NOTE: pr wraps up malloc, free, calloc, realloc so they are already andre@0: ** thread safe (and are not declared here - look in stdlib.h). andre@0: */ andre@0: andre@0: /* andre@0: ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures andre@0: ** as their libc equivalent malloc, calloc, realloc, and free, and have andre@0: ** the same semantics. (Note that the argument type size_t is replaced andre@0: ** by PRUint32.) Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc andre@0: ** must be freed by PR_Free. andre@0: */ andre@0: andre@0: NSPR_API(void *) PR_Malloc(PRUint32 size); andre@0: andre@0: NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize); andre@0: andre@0: NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size); andre@0: andre@0: NSPR_API(void) PR_Free(void *ptr); andre@0: andre@0: /* andre@0: ** The following are some convenience macros defined in terms of andre@0: ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free. andre@0: */ andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_MALLOC() andre@0: ** DESCRIPTION: andre@0: ** PR_NEW() allocates an untyped item of size _size from the heap. andre@0: ** INPUTS: _size: size in bytes of item to be allocated andre@0: ** OUTPUTS: untyped pointer to the node allocated andre@0: ** RETURN: pointer to node or error returned from malloc(). andre@0: ***********************************************************************/ andre@0: #define PR_MALLOC(_bytes) (PR_Malloc((_bytes))) andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_NEW() andre@0: ** DESCRIPTION: andre@0: ** PR_NEW() allocates an item of type _struct from the heap. andre@0: ** INPUTS: _struct: a data type andre@0: ** OUTPUTS: pointer to _struct andre@0: ** RETURN: pointer to _struct or error returns from malloc(). andre@0: ***********************************************************************/ andre@0: #define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct))) andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_REALLOC() andre@0: ** DESCRIPTION: andre@0: ** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size andre@0: ** untyped item. andre@0: ** INPUTS: _ptr: pointer to node to reallocate andre@0: ** _size: size of node to allocate andre@0: ** OUTPUTS: pointer to node allocated andre@0: ** RETURN: pointer to node allocated andre@0: ***********************************************************************/ andre@0: #define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size))) andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_CALLOC() andre@0: ** DESCRIPTION: andre@0: ** PR_CALLOC() allocates a _size bytes untyped item from the heap andre@0: ** and sets the allocated memory to all 0x00. andre@0: ** INPUTS: _size: size of node to allocate andre@0: ** OUTPUTS: pointer to node allocated andre@0: ** RETURN: pointer to node allocated andre@0: ***********************************************************************/ andre@0: #define PR_CALLOC(_size) (PR_Calloc(1, (_size))) andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_NEWZAP() andre@0: ** DESCRIPTION: andre@0: ** PR_NEWZAP() allocates an item of type _struct from the heap andre@0: ** and sets the allocated memory to all 0x00. andre@0: ** INPUTS: _struct: a data type andre@0: ** OUTPUTS: pointer to _struct andre@0: ** RETURN: pointer to _struct andre@0: ***********************************************************************/ andre@0: #define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct))) andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_DELETE() andre@0: ** DESCRIPTION: andre@0: ** PR_DELETE() unallocates an object previosly allocated via PR_NEW() andre@0: ** or PR_NEWZAP() to the heap. andre@0: ** INPUTS: pointer to previously allocated object andre@0: ** OUTPUTS: the referenced object is returned to the heap andre@0: ** RETURN: void andre@0: ***********************************************************************/ andre@0: #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; } andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_FREEIF() andre@0: ** DESCRIPTION: andre@0: ** PR_FREEIF() conditionally unallocates an object previously allocated andre@0: ** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is andre@0: ** equal to zero (0), the object is not released. andre@0: ** INPUTS: pointer to previously allocated object andre@0: ** OUTPUTS: the referenced object is conditionally returned to the heap andre@0: ** RETURN: void andre@0: ***********************************************************************/ andre@0: #define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr) andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* prmem_h___ */