X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcmemory.c;h=d2a0dba459f9c6bf563dd53e0b95da7e2cc2b734;hp=93fd8751e8334a593817bb3079004da40d829e07;hb=0f0340b9fbce9704cc7171f8f0104ce9103d2de6;hpb=d60003d3019371d4ce834a6cbfbf41c257f5a5f7 diff --git a/lib/silcutil/silcmemory.c b/lib/silcutil/silcmemory.c index 93fd8751..d2a0dba4 100644 --- a/lib/silcutil/silcmemory.c +++ b/lib/silcutil/silcmemory.c @@ -1,10 +1,10 @@ /* - silcmemory.c + silcmemory.c Author: Pekka Riikonen - Copyright (C) 1999 - 2002 Pekka Riikonen + Copyright (C) 1999 - 2005 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,27 +27,39 @@ void *silc_malloc(size_t size) { void *addr; - assert(size >= 0 && size <= SILC_MAX_ALLOC); + if (size <= 0 || size >= SILC_MAX_ALLOC) { + SILC_LOG_ERROR(("Invalid memory allocation")); + return NULL; + } addr = malloc(size); - assert(addr != NULL); + if (!addr) + SILC_LOG_ERROR(("System out of memory")); return addr; } void *silc_calloc(size_t items, size_t size) { void *addr; - assert(size * items >= 0 && size * items <= SILC_MAX_ALLOC); + if (size * items <= 0 || size * items >= SILC_MAX_ALLOC) { + SILC_LOG_ERROR(("Invalid memory allocation")); + return NULL; + } addr = calloc(items, size); - assert(addr != NULL); + if (!addr) + SILC_LOG_ERROR(("System out of memory")); return addr; } void *silc_realloc(void *ptr, size_t size) { void *addr; - assert(size >= 0 && size <= SILC_MAX_ALLOC); + if (size <= 0 || size >= SILC_MAX_ALLOC) { + SILC_LOG_ERROR(("Invalid memory allocation")); + return NULL; + } addr = realloc(ptr, size); - assert(addr != NULL); + if (!addr) + SILC_LOG_ERROR(("System out of memory")); return addr; } @@ -60,10 +72,84 @@ void *silc_memdup(const void *ptr, size_t size) { unsigned char *addr; addr = silc_malloc(size + 1); - assert(addr != NULL); + if (!addr) { + SILC_LOG_ERROR(("System out of memory")); + return NULL; + } memcpy((void *)addr, ptr, size); addr[size] = '\0'; return (void *)addr; } #endif /* !SILC_STACKTRACE */ + +/* SilcStack aware routines */ + +void *silc_smalloc(SilcStack stack, SilcUInt32 size) +{ + return stack ? silc_stack_malloc(stack, size, TRUE) : silc_malloc(size); +} + +void *silc_smalloc_ua(SilcStack stack, SilcUInt32 size) +{ + return stack ? silc_stack_malloc(stack, size, FALSE) : silc_malloc(size); +} + +void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size) +{ + unsigned char *addr; + + if (!stack) + return silc_calloc(items, size); + + addr = silc_stack_malloc(stack, items * size, TRUE); + if (!addr) + return NULL; + memset(addr, 0, items * size); + return (void *)addr; +} + +void *silc_srealloc(SilcStack stack, SilcUInt32 old_size, + void *ptr, SilcUInt32 size) +{ + return stack ? silc_stack_realloc(stack, old_size, ptr, size, TRUE) : + silc_realloc(ptr, size); +} + +void *silc_srealloc_ua(SilcStack stack, SilcUInt32 old_size, + void *ptr, SilcUInt32 size) +{ + return stack ? silc_stack_realloc(stack, old_size, ptr, size, FALSE) : + silc_realloc(ptr, size); +} + +void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size) +{ + unsigned char *addr; + + if (!stack) + return silc_memdup(ptr, size); + + addr = silc_stack_malloc(stack, size + 1, TRUE); + if (!addr) + return NULL; + memcpy((void *)addr, ptr, size); + addr[size] = '\0'; + return (void *)addr; +} + +char *silc_sstrdup(SilcStack stack, const char *str) +{ + SilcInt32 size = strlen(str); + char *addr; + + if (!stack) + return silc_memdup(str, size); + + addr = silc_stack_malloc(stack, size + 1, FALSE); + if (!addr) + return NULL; + memcpy((void *)addr, str, size); + addr[size] = '\0'; + return addr; +}