X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcmemory.h;h=64c13b5b5243a7658d3aa454ad81bea68a576fa6;hb=abc4156464a66d9961da10b9882df8c32cdc6164;hp=233f38943618080124475725052af96dad449dbe;hpb=e2c551b9693b6d42e5997b9df416a17fb94c1ccb;p=runtime.git diff --git a/lib/silcutil/silcmemory.h b/lib/silcutil/silcmemory.h index 233f3894..64c13b5b 100644 --- a/lib/silcutil/silcmemory.h +++ b/lib/silcutil/silcmemory.h @@ -1,10 +1,10 @@ /* - silcmemory.h + silcmemory.h Author: Pekka Riikonen - Copyright (C) 1999 - 2002 Pekka Riikonen + Copyright (C) 1999 - 2008 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 @@ -17,16 +17,11 @@ */ -/****h* silcutil/SILC Memory Interface +/****h* silcutil/Memory Interface * * DESCRIPTION * - * Basic utility functions for allocating memory. All SILC routines, and - * applications use these functions when they need to allocate, manipulate - * and free memory. - * - * Currently all allocation routines assert() that the memory was allocated - * successfully. Hence, if memory allocation fails it is fatal error. + * Routines for allocating and freeing memory. * ***/ @@ -35,9 +30,9 @@ /* Prototypes */ -#ifndef SILC_STACKTRACE +#ifndef SILC_MEMTRACE -/****f* silcutil/SilcMemoryAPI/silc_malloc +/****f* silcutil/silc_malloc * * SYNOPSIS * @@ -46,12 +41,13 @@ * DESCRIPTION * * Allocates memory of `size' bytes and returns pointer to the allocated - * memory area. Free the memory by calling silc_free. + * memory area. Free the memory by calling silc_free. Returns NULL on + * error and sets silc_errno. * ***/ void *silc_malloc(size_t size); -/****f* silcutil/SilcMemoryAPI/silc_calloc +/****f* silcutil/silc_calloc * * SYNOPSIS * @@ -61,12 +57,13 @@ void *silc_malloc(size_t size); * * Allocates memory of for an array of `items' elements of `size' bytes * and returns pointer to the allocated memory area. The memory area is - * also zeroed. Free the memory by calling silc_free. + * also zeroed. Free the memory by calling silc_free. Returns NULL on + * error and sets silc_errno. * ***/ void *silc_calloc(size_t items, size_t size); -/****f* silcutil/SilcMemoryAPI/silc_realloc +/****f* silcutil/silc_realloc * * SYNOPSIS * @@ -88,7 +85,7 @@ void *silc_calloc(size_t items, size_t size); ***/ void *silc_realloc(void *ptr, size_t size); -/****f* silcutil/SilcMemoryAPI/silc_free +/****f* silcutil/silc_free * * SYNOPSIS * @@ -102,7 +99,7 @@ void *silc_realloc(void *ptr, size_t size); ***/ void silc_free(void *ptr); -/****f* silcutil/SilcMemoryAPI/silc_memdup +/****f* silcutil/silc_memdup * * SYNOPSIS * @@ -119,8 +116,162 @@ void silc_free(void *ptr); ***/ void *silc_memdup(const void *ptr, size_t size); +/****f* silcutil/silc_strdup + * + * SYNOPSIS + * + * char *silc_strdup(const char *str); + * + * DESCRIPTION + * + * Duplicates the string indicated by `str' and returns the duplicated + * string. Returns NULL on error and sets silc_errno. + * + ***/ +char *silc_strdup(const char *str); + #else -#include "stacktrace.h" -#endif /* SILC_STACKTRACE */ +#include "memtrace.h" +#endif /* SILC_MEMTRACE */ + + +/* Following functions that use SilcStack as memory source. */ + +/****f* silcutil/silc_smalloc + * + * SYNOPSIS + * + * void *silc_smalloc(SilcStack stack, SilcUInt32 size); + * + * DESCRIPTION + * + * Allocate memory block of size of `size' from the stack indicated by + * `stack' and return pointer to it. Returns NULL on error. This + * function allocates aligned memory so it can be used to allocate + * memory for structures, for example. + * + * NOTES + * + * Be careful with this function: do not free the returned pointer + * explicitly and do not save the returned pointer to a permanent + * location. + * + * If `stack' is NULL this function calls silc_malloc. + * + ***/ +void *silc_smalloc(SilcStack stack, SilcUInt32 size); + +/****f* silcutil/silc_scalloc + * + * SYNOPSIS + * + * void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size); + * + * DESCRIPTION + * + * Allocate memory block of size of `size' from the stack indicated by + * `stack', zero the memory area and return pointer to it. This + * function allocates aligned memory. Returns NULL on error. + * + * NOTES + * + * Be careful with this function: do not free the returned pointer + * explicitly and do not save the returned pointer to a permanent + * location. + * + * If `stack' is NULL this function calls silc_calloc. + * + ***/ +void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size); + +/****f* silcutil/silc_srealloc + * + * SYNOPSIS + * + * void *silc_srealloc(SilcStack stack, SilcUInt32 old_size, + * void *ptr, SilcUInt32 size); + * + * DESCRIPTION + * + * Change the size of the memory block indicated by `ptr' to the new + * size of `size' bytes. The contents of `ptr' will not be changed. + * If `ptr' is NULL the call is equivalent to silc_smalloc. If `size' + * is zero (0) error will occur. Returns NULL on error and the old + * pointer remain intact. This may return different pointer from `ptr' + * + * NOTES + * + * If the reallocation from `stack' fails, this function will allocate + * new block of size of `size' bytes from `stack' and copy the data from + * `ptr' to the new memory block. + * + * If `stack' is NULL this function calls silc_realloc. + * + ***/ +void *silc_srealloc(SilcStack stack, SilcUInt32 old_size, + void *ptr, SilcUInt32 size); + +/****f* silcutil/silc_smemdup + * + * SYNOPSIS + * + * void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size); + * + * DESCRIPTION + * + * Duplicates the memory area indicated by `ptr' which is the size of + * `size' bytes. Returns pointer to the duplicated memory area. This + * NULL terminates the dupped memory area by allocating `size' + 1 + * bytes, so this function can be used to duplicate strings that does not + * have NULL termination. This function allocates aligned memory so + * it can be used to duplicate also structures. Returns NULL on error. + * + * NOTES + * + * Be careful with this function: do not free the returned pointer + * explicitly and do not save the returned pointer to a permanent + * location. + * + * If `stack' is NULL this function calls silc_memdup. + * + ***/ +void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size); + +/****f* silcutil/silc_sfree + * + * SYNOPSIS + * + * void silc_scalloc(SilcStack stack, void *ptr); + * + * DESCRIPTION + * + * This function can be used to free the `ptr' if `stack' is NULL. This + * function does nothing if `stack' is non-NULL. When `stack' is NULL + * this function calls silc_free. + * + ***/ +void silc_sfree(SilcStack stack, void *ptr); + +/****f* silcutil/silc_sstrdup + * + * SYNOPSIS + * + * char *silc_sstrdup(SilcStack stack, const char *str); + * + * DESCRIPTION + * + * Duplicates the string indicated by `str' and returns the duplicated + * string. Returns NULL on error. + * + * NOTES + * + * Be careful with this function: do not free the returned pointer + * explicitly and do not save the returned pointer to a permanent + * location. + * + * If `stack' is NULL this function calls silc_strdup. + * + ***/ +char *silc_sstrdup(SilcStack stack, const char *str); #endif /* SILCMEMORY_H */