]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/prmem.h
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / libsecurity_asn1 / lib / prmem.h
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/
7 *
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.
12 *
13 * The Original Code is the Netscape Portable Runtime (NSPR).
14 *
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
18 * Rights Reserved.
19 *
20 * Contributor(s):
21 *
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
32 * GPL.
33 */
34
35 /*
36 ** File: prmem.h
37 ** Description: API to NSPR 2.0 memory management functions
38 **
39 */
40 #ifndef prmem_h___
41 #define prmem_h___
42
43 #include <security_asn1/prtypes.h>
44 #include <stddef.h>
45 #include <stdlib.h>
46
47 PR_BEGIN_EXTERN_C
48
49 /*
50 ** Thread safe memory allocation.
51 **
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).
54 */
55
56 /*
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.
62 */
63
64 NSPR_API(void *) PR_Malloc(PRSize size);
65
66 NSPR_API(void *) PR_Calloc(PRSize nelem, PRSize elsize);
67
68 NSPR_API(void *) PR_Realloc(void *ptr, PRSize size);
69
70 NSPR_API(void) PR_Free(void *ptr);
71
72 /*
73 ** The following are some convenience macros defined in terms of
74 ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
75 */
76
77 /***********************************************************************
78 ** FUNCTION: PR_MALLOC()
79 ** DESCRIPTION:
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)))
86
87 /***********************************************************************
88 ** FUNCTION: PR_NEW()
89 ** DESCRIPTION:
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)))
96
97 /***********************************************************************
98 ** FUNCTION: PR_REALLOC()
99 ** DESCRIPTION:
100 ** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
101 ** untyped item.
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)))
108
109 /***********************************************************************
110 ** FUNCTION: PR_CALLOC()
111 ** DESCRIPTION:
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)))
119
120 /***********************************************************************
121 ** FUNCTION: PR_NEWZAP()
122 ** DESCRIPTION:
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)))
130
131 /***********************************************************************
132 ** FUNCTION: PR_DELETE()
133 ** DESCRIPTION:
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
138 ** RETURN: void
139 ***********************************************************************/
140 #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
141
142 /***********************************************************************
143 ** FUNCTION: PR_FREEIF()
144 ** DESCRIPTION:
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
150 ** RETURN: void
151 ***********************************************************************/
152 #define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr)
153
154 PR_END_EXTERN_C
155
156 #endif /* prmem_h___ */