Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | ** File: prmem.h |
michael@0 | 8 | ** Description: API to NSPR memory management functions |
michael@0 | 9 | ** |
michael@0 | 10 | */ |
michael@0 | 11 | #ifndef prmem_h___ |
michael@0 | 12 | #define prmem_h___ |
michael@0 | 13 | |
michael@0 | 14 | #include "prtypes.h" |
michael@0 | 15 | #include <stdlib.h> |
michael@0 | 16 | |
michael@0 | 17 | PR_BEGIN_EXTERN_C |
michael@0 | 18 | |
michael@0 | 19 | /* |
michael@0 | 20 | ** Thread safe memory allocation. |
michael@0 | 21 | ** |
michael@0 | 22 | ** NOTE: pr wraps up malloc, free, calloc, realloc so they are already |
michael@0 | 23 | ** thread safe (and are not declared here - look in stdlib.h). |
michael@0 | 24 | */ |
michael@0 | 25 | |
michael@0 | 26 | /* |
michael@0 | 27 | ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures |
michael@0 | 28 | ** as their libc equivalent malloc, calloc, realloc, and free, and have |
michael@0 | 29 | ** the same semantics. (Note that the argument type size_t is replaced |
michael@0 | 30 | ** by PRUint32.) Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc |
michael@0 | 31 | ** must be freed by PR_Free. |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | NSPR_API(void *) PR_Malloc(PRUint32 size); |
michael@0 | 35 | |
michael@0 | 36 | NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize); |
michael@0 | 37 | |
michael@0 | 38 | NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size); |
michael@0 | 39 | |
michael@0 | 40 | NSPR_API(void) PR_Free(void *ptr); |
michael@0 | 41 | |
michael@0 | 42 | /* |
michael@0 | 43 | ** The following are some convenience macros defined in terms of |
michael@0 | 44 | ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free. |
michael@0 | 45 | */ |
michael@0 | 46 | |
michael@0 | 47 | /*********************************************************************** |
michael@0 | 48 | ** FUNCTION: PR_MALLOC() |
michael@0 | 49 | ** DESCRIPTION: |
michael@0 | 50 | ** PR_NEW() allocates an untyped item of size _size from the heap. |
michael@0 | 51 | ** INPUTS: _size: size in bytes of item to be allocated |
michael@0 | 52 | ** OUTPUTS: untyped pointer to the node allocated |
michael@0 | 53 | ** RETURN: pointer to node or error returned from malloc(). |
michael@0 | 54 | ***********************************************************************/ |
michael@0 | 55 | #define PR_MALLOC(_bytes) (PR_Malloc((_bytes))) |
michael@0 | 56 | |
michael@0 | 57 | /*********************************************************************** |
michael@0 | 58 | ** FUNCTION: PR_NEW() |
michael@0 | 59 | ** DESCRIPTION: |
michael@0 | 60 | ** PR_NEW() allocates an item of type _struct from the heap. |
michael@0 | 61 | ** INPUTS: _struct: a data type |
michael@0 | 62 | ** OUTPUTS: pointer to _struct |
michael@0 | 63 | ** RETURN: pointer to _struct or error returns from malloc(). |
michael@0 | 64 | ***********************************************************************/ |
michael@0 | 65 | #define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct))) |
michael@0 | 66 | |
michael@0 | 67 | /*********************************************************************** |
michael@0 | 68 | ** FUNCTION: PR_REALLOC() |
michael@0 | 69 | ** DESCRIPTION: |
michael@0 | 70 | ** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size |
michael@0 | 71 | ** untyped item. |
michael@0 | 72 | ** INPUTS: _ptr: pointer to node to reallocate |
michael@0 | 73 | ** _size: size of node to allocate |
michael@0 | 74 | ** OUTPUTS: pointer to node allocated |
michael@0 | 75 | ** RETURN: pointer to node allocated |
michael@0 | 76 | ***********************************************************************/ |
michael@0 | 77 | #define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size))) |
michael@0 | 78 | |
michael@0 | 79 | /*********************************************************************** |
michael@0 | 80 | ** FUNCTION: PR_CALLOC() |
michael@0 | 81 | ** DESCRIPTION: |
michael@0 | 82 | ** PR_CALLOC() allocates a _size bytes untyped item from the heap |
michael@0 | 83 | ** and sets the allocated memory to all 0x00. |
michael@0 | 84 | ** INPUTS: _size: size of node to allocate |
michael@0 | 85 | ** OUTPUTS: pointer to node allocated |
michael@0 | 86 | ** RETURN: pointer to node allocated |
michael@0 | 87 | ***********************************************************************/ |
michael@0 | 88 | #define PR_CALLOC(_size) (PR_Calloc(1, (_size))) |
michael@0 | 89 | |
michael@0 | 90 | /*********************************************************************** |
michael@0 | 91 | ** FUNCTION: PR_NEWZAP() |
michael@0 | 92 | ** DESCRIPTION: |
michael@0 | 93 | ** PR_NEWZAP() allocates an item of type _struct from the heap |
michael@0 | 94 | ** and sets the allocated memory to all 0x00. |
michael@0 | 95 | ** INPUTS: _struct: a data type |
michael@0 | 96 | ** OUTPUTS: pointer to _struct |
michael@0 | 97 | ** RETURN: pointer to _struct |
michael@0 | 98 | ***********************************************************************/ |
michael@0 | 99 | #define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct))) |
michael@0 | 100 | |
michael@0 | 101 | /*********************************************************************** |
michael@0 | 102 | ** FUNCTION: PR_DELETE() |
michael@0 | 103 | ** DESCRIPTION: |
michael@0 | 104 | ** PR_DELETE() unallocates an object previosly allocated via PR_NEW() |
michael@0 | 105 | ** or PR_NEWZAP() to the heap. |
michael@0 | 106 | ** INPUTS: pointer to previously allocated object |
michael@0 | 107 | ** OUTPUTS: the referenced object is returned to the heap |
michael@0 | 108 | ** RETURN: void |
michael@0 | 109 | ***********************************************************************/ |
michael@0 | 110 | #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; } |
michael@0 | 111 | |
michael@0 | 112 | /*********************************************************************** |
michael@0 | 113 | ** FUNCTION: PR_FREEIF() |
michael@0 | 114 | ** DESCRIPTION: |
michael@0 | 115 | ** PR_FREEIF() conditionally unallocates an object previously allocated |
michael@0 | 116 | ** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is |
michael@0 | 117 | ** equal to zero (0), the object is not released. |
michael@0 | 118 | ** INPUTS: pointer to previously allocated object |
michael@0 | 119 | ** OUTPUTS: the referenced object is conditionally returned to the heap |
michael@0 | 120 | ** RETURN: void |
michael@0 | 121 | ***********************************************************************/ |
michael@0 | 122 | #define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr) |
michael@0 | 123 | |
michael@0 | 124 | PR_END_EXTERN_C |
michael@0 | 125 | |
michael@0 | 126 | #endif /* prmem_h___ */ |