andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* andre@0: * pkix_pl_mem.c andre@0: * andre@0: * Memory Management Functions andre@0: * andre@0: */ andre@0: andre@0: #include "pkix_pl_mem.h" andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Malloc (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Malloc( andre@0: PKIX_UInt32 size, andre@0: void **pMemory, andre@0: void *plContext) andre@0: { andre@0: PKIX_PL_NssContext *nssContext = NULL; andre@0: void *result = NULL; andre@0: andre@0: PKIX_ENTER(MEM, "PKIX_PL_Malloc"); andre@0: PKIX_NULLCHECK_ONE(pMemory); andre@0: andre@0: if (size == 0){ andre@0: *pMemory = NULL; andre@0: } else { andre@0: andre@0: nssContext = (PKIX_PL_NssContext *)plContext; andre@0: andre@0: if (nssContext != NULL && nssContext->arena != NULL) { andre@0: PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); andre@0: *pMemory = PORT_ArenaAlloc(nssContext->arena, size); andre@0: } else { andre@0: PKIX_MEM_DEBUG("\tCalling PR_Malloc.\n"); andre@0: result = (void *) PR_Malloc(size); andre@0: andre@0: if (result == NULL) { andre@0: PKIX_MEM_DEBUG("Fatal Error Occurred: " andre@0: "PR_Malloc failed.\n"); andre@0: PKIX_ERROR_ALLOC_ERROR(); andre@0: } else { andre@0: *pMemory = result; andre@0: } andre@0: } andre@0: } andre@0: andre@0: cleanup: andre@0: PKIX_RETURN(MEM); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Calloc (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Calloc( andre@0: PKIX_UInt32 nElem, andre@0: PKIX_UInt32 elSize, andre@0: void **pMemory, andre@0: void *plContext) andre@0: { andre@0: PKIX_PL_NssContext *nssContext = NULL; andre@0: void *result = NULL; andre@0: andre@0: PKIX_ENTER(MEM, "PKIX_PL_Calloc"); andre@0: PKIX_NULLCHECK_ONE(pMemory); andre@0: andre@0: if ((nElem == 0) || (elSize == 0)){ andre@0: *pMemory = NULL; andre@0: } else { andre@0: andre@0: nssContext = (PKIX_PL_NssContext *)plContext; andre@0: andre@0: if (nssContext != NULL && nssContext->arena != NULL) { andre@0: PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); andre@0: *pMemory = PORT_ArenaAlloc(nssContext->arena, elSize); andre@0: } else { andre@0: PKIX_MEM_DEBUG("\tCalling PR_Calloc.\n"); andre@0: result = (void *) PR_Calloc(nElem, elSize); andre@0: andre@0: if (result == NULL) { andre@0: PKIX_MEM_DEBUG("Fatal Error Occurred: " andre@0: "PR_Calloc failed.\n"); andre@0: PKIX_ERROR_ALLOC_ERROR(); andre@0: } else { andre@0: *pMemory = result; andre@0: } andre@0: } andre@0: } andre@0: andre@0: cleanup: andre@0: andre@0: PKIX_RETURN(MEM); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Realloc (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Realloc( andre@0: void *ptr, andre@0: PKIX_UInt32 size, andre@0: void **pMemory, andre@0: void *plContext) andre@0: { andre@0: PKIX_PL_NssContext *nssContext = NULL; andre@0: void *result = NULL; andre@0: andre@0: PKIX_ENTER(MEM, "PKIX_PL_Realloc"); andre@0: PKIX_NULLCHECK_ONE(pMemory); andre@0: andre@0: nssContext = (PKIX_PL_NssContext *)plContext; andre@0: andre@0: if (nssContext != NULL && nssContext->arena != NULL) { andre@0: PKIX_MEM_DEBUG("\tCalling PORT_ArenaAlloc.\n"); andre@0: result = PORT_ArenaAlloc(nssContext->arena, size); andre@0: andre@0: if (result){ andre@0: PKIX_MEM_DEBUG("\tCalling PORT_Memcpy.\n"); andre@0: PORT_Memcpy(result, ptr, size); andre@0: } andre@0: *pMemory = result; andre@0: } else { andre@0: PKIX_MEM_DEBUG("\tCalling PR_Realloc.\n"); andre@0: result = (void *) PR_Realloc(ptr, size); andre@0: andre@0: if (result == NULL) { andre@0: if (size == 0){ andre@0: *pMemory = NULL; andre@0: } else { andre@0: PKIX_MEM_DEBUG andre@0: ("Fatal Error Occurred: " andre@0: "PR_Realloc failed.\n"); andre@0: PKIX_ERROR_ALLOC_ERROR(); andre@0: } andre@0: } else { andre@0: *pMemory = result; andre@0: } andre@0: } andre@0: andre@0: cleanup: andre@0: andre@0: PKIX_RETURN(MEM); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Free (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Free( andre@0: void *ptr, andre@0: /* ARGSUSED */ void *plContext) andre@0: { andre@0: PKIX_PL_NssContext *context = NULL; andre@0: andre@0: PKIX_ENTER(MEM, "PKIX_PL_Free"); andre@0: andre@0: context = (PKIX_PL_NssContext *) plContext; andre@0: if (context == NULL || context->arena == NULL) { andre@0: PKIX_MEM_DEBUG("\tCalling PR_Free.\n"); andre@0: (void) PR_Free(ptr); andre@0: } andre@0: andre@0: PKIX_RETURN(MEM); andre@0: }