pkix_pl_mem.c (4753B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 /* 5 * pkix_pl_mem.c 6 * 7 * Memory Management Functions 8 * 9 */ 10 11 #include "pkix_pl_mem.h" 12 13 /* 14 * FUNCTION: PKIX_PL_Malloc (see comments in pkix_pl_system.h) 15 */ 16 PKIX_Error * 17 PKIX_PL_Malloc( 18 PKIX_UInt32 size, 19 void **pMemory, 20 void *plContext) 21 { 22 PKIX_PL_NssContext *nssContext = NULL; 23 void *result = NULL; 24 25 PKIX_ENTER(MEM, "PKIX_PL_Malloc"); 26 PKIX_NULLCHECK_ONE(pMemory); 27 28 if (size == 0){ 29 *pMemory = NULL; 30 } else { 31 32 nssContext = (PKIX_PL_NssContext *)plContext; 33 34 if (nssContext != NULL && nssContext->arena != NULL) { 35 PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); 36 *pMemory = PORT_ArenaAlloc(nssContext->arena, size); 37 } else { 38 PKIX_MEM_DEBUG("\tCalling PR_Malloc.\n"); 39 result = (void *) PR_Malloc(size); 40 41 if (result == NULL) { 42 PKIX_MEM_DEBUG("Fatal Error Occurred: " 43 "PR_Malloc failed.\n"); 44 PKIX_ERROR_ALLOC_ERROR(); 45 } else { 46 *pMemory = result; 47 } 48 } 49 } 50 51 cleanup: 52 PKIX_RETURN(MEM); 53 } 54 55 /* 56 * FUNCTION: PKIX_PL_Calloc (see comments in pkix_pl_system.h) 57 */ 58 PKIX_Error * 59 PKIX_PL_Calloc( 60 PKIX_UInt32 nElem, 61 PKIX_UInt32 elSize, 62 void **pMemory, 63 void *plContext) 64 { 65 PKIX_PL_NssContext *nssContext = NULL; 66 void *result = NULL; 67 68 PKIX_ENTER(MEM, "PKIX_PL_Calloc"); 69 PKIX_NULLCHECK_ONE(pMemory); 70 71 if ((nElem == 0) || (elSize == 0)){ 72 *pMemory = NULL; 73 } else { 74 75 nssContext = (PKIX_PL_NssContext *)plContext; 76 77 if (nssContext != NULL && nssContext->arena != NULL) { 78 PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); 79 *pMemory = PORT_ArenaAlloc(nssContext->arena, elSize); 80 } else { 81 PKIX_MEM_DEBUG("\tCalling PR_Calloc.\n"); 82 result = (void *) PR_Calloc(nElem, elSize); 83 84 if (result == NULL) { 85 PKIX_MEM_DEBUG("Fatal Error Occurred: " 86 "PR_Calloc failed.\n"); 87 PKIX_ERROR_ALLOC_ERROR(); 88 } else { 89 *pMemory = result; 90 } 91 } 92 } 93 94 cleanup: 95 96 PKIX_RETURN(MEM); 97 } 98 99 /* 100 * FUNCTION: PKIX_PL_Realloc (see comments in pkix_pl_system.h) 101 */ 102 PKIX_Error * 103 PKIX_PL_Realloc( 104 void *ptr, 105 PKIX_UInt32 size, 106 void **pMemory, 107 void *plContext) 108 { 109 PKIX_PL_NssContext *nssContext = NULL; 110 void *result = NULL; 111 112 PKIX_ENTER(MEM, "PKIX_PL_Realloc"); 113 PKIX_NULLCHECK_ONE(pMemory); 114 115 nssContext = (PKIX_PL_NssContext *)plContext; 116 117 if (nssContext != NULL && nssContext->arena != NULL) { 118 PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); 119 result = PORT_ArenaAlloc(nssContext->arena, size); 120 121 if (result){ 122 PKIX_MEM_DEBUG("\tCalling PORT_Memcpy.\n"); 123 PORT_Memcpy(result, ptr, size); 124 } 125 *pMemory = result; 126 } else { 127 PKIX_MEM_DEBUG("\tCalling PR_Realloc.\n"); 128 result = (void *) PR_Realloc(ptr, size); 129 130 if (result == NULL) { 131 if (size == 0){ 132 *pMemory = NULL; 133 } else { 134 PKIX_MEM_DEBUG 135 ("Fatal Error Occurred: " 136 "PR_Realloc failed.\n"); 137 PKIX_ERROR_ALLOC_ERROR(); 138 } 139 } else { 140 *pMemory = result; 141 } 142 } 143 144 cleanup: 145 146 PKIX_RETURN(MEM); 147 } 148 149 /* 150 * FUNCTION: PKIX_PL_Free (see comments in pkix_pl_system.h) 151 */ 152 PKIX_Error * 153 PKIX_PL_Free( 154 void *ptr, 155 /* ARGSUSED */ void *plContext) 156 { 157 PKIX_PL_NssContext *context = NULL; 158 159 PKIX_ENTER(MEM, "PKIX_PL_Free"); 160 161 context = (PKIX_PL_NssContext *) plContext; 162 if (context == NULL || context->arena == NULL) { 163 PKIX_MEM_DEBUG("\tCalling PR_Free.\n"); 164 (void) PR_Free(ptr); 165 } 166 167 PKIX_RETURN(MEM); 168 }