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