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