| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|---|
| 2 |
/* ***** BEGIN LICENSE BLOCK ***** |
|---|
| 3 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|---|
| 4 |
* |
|---|
| 5 |
* The contents of this file are subject to the Mozilla Public License Version |
|---|
| 6 |
* 1.1 (the "License"); you may not use this file except in compliance with |
|---|
| 7 |
* the License. You may obtain a copy of the License at |
|---|
| 8 |
* http://www.mozilla.org/MPL/ |
|---|
| 9 |
* |
|---|
| 10 |
* Software distributed under the License is distributed on an "AS IS" basis, |
|---|
| 11 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|---|
| 12 |
* for the specific language governing rights and limitations under the |
|---|
| 13 |
* License. |
|---|
| 14 |
* |
|---|
| 15 |
* The Original Code is the Netscape Portable Runtime (NSPR). |
|---|
| 16 |
* |
|---|
| 17 |
* The Initial Developer of the Original Code is |
|---|
| 18 |
* Netscape Communications Corporation. |
|---|
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1998-2000 |
|---|
| 20 |
* the Initial Developer. All Rights Reserved. |
|---|
| 21 |
* |
|---|
| 22 |
* Contributor(s): |
|---|
| 23 |
* |
|---|
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
|---|
| 25 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
|---|
| 26 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|---|
| 27 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
|---|
| 28 |
* of those above. If you wish to allow use of your version of this file only |
|---|
| 29 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
|---|
| 30 |
* use your version of this file under the terms of the MPL, indicate your |
|---|
| 31 |
* decision by deleting the provisions above and replace them with the notice |
|---|
| 32 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
|---|
| 33 |
* the provisions above, a recipient may use your version of this file under |
|---|
| 34 |
* the terms of any one of the MPL, the GPL or the LGPL. |
|---|
| 35 |
* |
|---|
| 36 |
* ***** END LICENSE BLOCK ***** */ |
|---|
| 37 |
|
|---|
| 38 |
/* |
|---|
| 39 |
** File: prmem.h |
|---|
| 40 |
** Description: API to NSPR memory management functions |
|---|
| 41 |
** |
|---|
| 42 |
*/ |
|---|
| 43 |
#ifndef prmem_h___ |
|---|
| 44 |
#define prmem_h___ |
|---|
| 45 |
|
|---|
| 46 |
#include "prtypes.h" |
|---|
| 47 |
#include <stdlib.h> |
|---|
| 48 |
|
|---|
| 49 |
PR_BEGIN_EXTERN_C |
|---|
| 50 |
|
|---|
| 51 |
/* |
|---|
| 52 |
** Thread safe memory allocation. |
|---|
| 53 |
** |
|---|
| 54 |
** NOTE: pr wraps up malloc, free, calloc, realloc so they are already |
|---|
| 55 |
** thread safe (and are not declared here - look in stdlib.h). |
|---|
| 56 |
*/ |
|---|
| 57 |
|
|---|
| 58 |
/* |
|---|
| 59 |
** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures |
|---|
| 60 |
** as their libc equivalent malloc, calloc, realloc, and free, and have |
|---|
| 61 |
** the same semantics. (Note that the argument type size_t is replaced |
|---|
| 62 |
** by PRUint32.) Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc |
|---|
| 63 |
** must be freed by PR_Free. |
|---|
| 64 |
*/ |
|---|
| 65 |
|
|---|
| 66 |
NSPR_API(void *) PR_Malloc(PRUint32 size); |
|---|
| 67 |
|
|---|
| 68 |
NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize); |
|---|
| 69 |
|
|---|
| 70 |
NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size); |
|---|
| 71 |
|
|---|
| 72 |
NSPR_API(void) PR_Free(void *ptr); |
|---|
| 73 |
|
|---|
| 74 |
/* |
|---|
| 75 |
** The following are some convenience macros defined in terms of |
|---|
| 76 |
** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free. |
|---|
| 77 |
*/ |
|---|
| 78 |
|
|---|
| 79 |
/*********************************************************************** |
|---|
| 80 |
** FUNCTION: PR_MALLOC() |
|---|
| 81 |
** DESCRIPTION: |
|---|
| 82 |
** PR_NEW() allocates an untyped item of size _size from the heap. |
|---|
| 83 |
** INPUTS: _size: size in bytes of item to be allocated |
|---|
| 84 |
** OUTPUTS: untyped pointer to the node allocated |
|---|
| 85 |
** RETURN: pointer to node or error returned from malloc(). |
|---|
| 86 |
***********************************************************************/ |
|---|
| 87 |
#define PR_MALLOC(_bytes) (PR_Malloc((_bytes))) |
|---|
| 88 |
|
|---|
| 89 |
/*********************************************************************** |
|---|
| 90 |
** FUNCTION: PR_NEW() |
|---|
| 91 |
** DESCRIPTION: |
|---|
| 92 |
** PR_NEW() allocates an item of type _struct from the heap. |
|---|
| 93 |
** INPUTS: _struct: a data type |
|---|
| 94 |
** OUTPUTS: pointer to _struct |
|---|
| 95 |
** RETURN: pointer to _struct or error returns from malloc(). |
|---|
| 96 |
***********************************************************************/ |
|---|
| 97 |
#define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct))) |
|---|
| 98 |
|
|---|
| 99 |
/*********************************************************************** |
|---|
| 100 |
** FUNCTION: PR_REALLOC() |
|---|
| 101 |
** DESCRIPTION: |
|---|
| 102 |
** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size |
|---|
| 103 |
** untyped item. |
|---|
| 104 |
** INPUTS: _ptr: pointer to node to reallocate |
|---|
| 105 |
** _size: size of node to allocate |
|---|
| 106 |
** OUTPUTS: pointer to node allocated |
|---|
| 107 |
** RETURN: pointer to node allocated |
|---|
| 108 |
***********************************************************************/ |
|---|
| 109 |
#define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size))) |
|---|
| 110 |
|
|---|
| 111 |
/*********************************************************************** |
|---|
| 112 |
** FUNCTION: PR_CALLOC() |
|---|
| 113 |
** DESCRIPTION: |
|---|
| 114 |
** PR_CALLOC() allocates a _size bytes untyped item from the heap |
|---|
| 115 |
** and sets the allocated memory to all 0x00. |
|---|
| 116 |
** INPUTS: _size: size of node to allocate |
|---|
| 117 |
** OUTPUTS: pointer to node allocated |
|---|
| 118 |
** RETURN: pointer to node allocated |
|---|
| 119 |
***********************************************************************/ |
|---|
| 120 |
#define PR_CALLOC(_size) (PR_Calloc(1, (_size))) |
|---|
| 121 |
|
|---|
| 122 |
/*********************************************************************** |
|---|
| 123 |
** FUNCTION: PR_NEWZAP() |
|---|
| 124 |
** DESCRIPTION: |
|---|
| 125 |
** PR_NEWZAP() allocates an item of type _struct from the heap |
|---|
| 126 |
** and sets the allocated memory to all 0x00. |
|---|
| 127 |
** INPUTS: _struct: a data type |
|---|
| 128 |
** OUTPUTS: pointer to _struct |
|---|
| 129 |
** RETURN: pointer to _struct |
|---|
| 130 |
***********************************************************************/ |
|---|
| 131 |
#define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct))) |
|---|
| 132 |
|
|---|
| 133 |
/*********************************************************************** |
|---|
| 134 |
** FUNCTION: PR_DELETE() |
|---|
| 135 |
** DESCRIPTION: |
|---|
| 136 |
** PR_DELETE() unallocates an object previosly allocated via PR_NEW() |
|---|
| 137 |
** or PR_NEWZAP() to the heap. |
|---|
| 138 |
** INPUTS: pointer to previously allocated object |
|---|
| 139 |
** OUTPUTS: the referenced object is returned to the heap |
|---|
| 140 |
** RETURN: void |
|---|
| 141 |
***********************************************************************/ |
|---|
| 142 |
#define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; } |
|---|
| 143 |
|
|---|
| 144 |
/*********************************************************************** |
|---|
| 145 |
** FUNCTION: PR_FREEIF() |
|---|
| 146 |
** DESCRIPTION: |
|---|
| 147 |
** PR_FREEIF() conditionally unallocates an object previously allocated |
|---|
| 148 |
** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is |
|---|
| 149 |
** equal to zero (0), the object is not released. |
|---|
| 150 |
** INPUTS: pointer to previously allocated object |
|---|
| 151 |
** OUTPUTS: the referenced object is conditionally returned to the heap |
|---|
| 152 |
** RETURN: void |
|---|
| 153 |
***********************************************************************/ |
|---|
| 154 |
#define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr) |
|---|
| 155 |
|
|---|
| 156 |
PR_END_EXTERN_C |
|---|
| 157 |
|
|---|
| 158 |
#endif /* prmem_h___ */ |
|---|