1 /* ==================================================================== 2 * The Kannel Software License, Version 1.0 3 * 4 * Copyright (c) 2001-2014 Kannel Group 5 * Copyright (c) 1998-2001 WapIT Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Kannel Group (http://www.kannel.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Kannel" and "Kannel Group" must not be used to 28 * endorse or promote products derived from this software without 29 * prior written permission. For written permission, please 30 * contact org@kannel.org. 31 * 32 * 5. Products derived from this software may not be called "Kannel", 33 * nor may "Kannel" appear in their name, without prior written 34 * permission of the Kannel Group. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 41 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 42 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 45 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 46 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Kannel Group. For more information on 51 * the Kannel Group, please see <http://www.kannel.org/>. 52 * 53 * Portions of this software are based upon software originally written at 54 * WapIT Ltd., Helsinki, Finland for the Kannel project. 55 */ 56 57 /* 58 * 59 * wsalloc.h 60 * 61 * Author: Markku Rossi <mtr@iki.fi> 62 * 63 * Copyright (c) 1999-2000 WAPIT OY LTD. 64 * All rights reserved. 65 * 66 * Memory allocation routines. 67 * 68 */ 69 70 #ifndef WSALLOC_H 71 #define WSALLOC_H 72 73 #if WS_DEBUG 74 #define WS_MEM_DEBUG 1 75 #endif /* WS_DEBUG */ 76 77 #if !WS_MEM_DEBUG 78 79 /********************* Prototypes for global functions ******************/ 80 81 /* Allocate `size' bytes of memory. The function returns NULL if the 82 * allocation fails. */ 83 void *ws_malloc(size_t size); 84 85 /* Allocate `num' items of size `size'. The returned memory block is 86 * initialied with zero. The function returns NULL if the allocation 87 * fails .*/ 88 void *ws_calloc(size_t num, size_t size); 89 90 /* Reallocate the memory block `ptr' to size `size'. The old data is 91 * preserved in the new memory block. The function returns NULL if 92 * the allocation fails. It is permissible to call the function with 93 * NULL as the `ptr' argument of 0 as the `size' argument. In these 94 * cases, the function acts the "Right Way". If the `ptr' is NULL, 95 * the function allocates a fresh block of size `size'. If the `size' 96 * is NULL, the memory block `ptr' is freed. */ 97 void *ws_realloc(void *ptr, size_t size); 98 99 /* Take a copy of the memory buffer `ptr' which has `size' bytes of 100 * data. The function returns NULL if the allocation fails. The 101 * returned buffer is null-terminated. */ 102 void *ws_memdup(const void *ptr, size_t size); 103 104 /* Take a copy of the C-string `str'. The function returns NULL if 105 * the allocation fails. */ 106 void *ws_strdup(const char *str); 107 108 /* Free the memory block `ptr' that was previously allocated with one 109 * of the ws_{m,c,re}alloc() functions. It is allowed to call the 110 * function with NULL as the `ptr' argument. */ 111 void ws_free(void *ptr); 112 113 #else /* WS_MEM_DEBUG */ 114 115 /********************* Memory debugging routines ************************/ 116 117 /* These macros and functions are used in debugging memory usage of 118 * the compiler and to find out memory leaks. When these functions 119 * are used, each dynamically allocated block is recorded in a list of 120 * active blocks, and allocated blocks are tagged with information 121 * about their allocation location. When the block is freed, it is 122 * removed from the list and its contents is marked freed. Typically 123 * these functions detect memory leaks and freeing same memory block 124 * multiple times. 125 * 126 * These functions can also be used to test error recovery code of 127 * memory allocation failures. The function ws_clear_leaks() clears 128 * the current information about used blocks and it sets the limit of 129 * successful memory allocations. When more than the limit number of 130 * memory allocations have been performed, all memory allocations 131 * fail. When the tested function has returned, you can see if you 132 * cleanup code did not free all blocks by using the functions 133 * ws_hash_leaks() and ws_dump_blocks(). 134 * 135 * These functions are not thread safe. They use shared static list 136 * to record the active blocks and they do not use any sorts of 137 * locking. 138 */ 139 140 /* Macros to tag the allocation source file location to the allocated 141 memory block. */ 142 143 #define ws_malloc(_s) ws_malloc_i((_s), __FILE__, __LINE__) 144 #define ws_calloc(_n, _s) ws_calloc_i((_n), (_s), __FILE__, __LINE__) 145 #define ws_realloc(_p, _s) ws_realloc_i((_p), (_s), __FILE__, __LINE__) 146 #define ws_memdup(_p, _s) ws_memdup_i((_p), (_s), __FILE__, __LINE__) 147 #define ws_strdup(_s) ws_strdup_i((_s), __FILE__, __LINE__) 148 #define ws_free(_p) ws_free_i((_p)) 149 150 /* The allocation and freeing functions. */ 151 152 void *ws_malloc_i(size_t size, const char *file, int line); 153 void *ws_calloc_i(size_t num, size_t size, const char *file, int line); 154 void *ws_realloc_i(void *ptr, size_t size, const char *file, int line); 155 void *ws_memdup_i(const void *ptr, size_t size, const char *file, int line); 156 void *ws_strdup_i(const char *str, const char *file, int line); 157 void ws_free_i(void *ptr); 158 159 /* A predicate to check if the system currently has any allocated 160 * blocks. The function returns 1 if it has any blocks and 0 161 * otherwise. */ 162 int ws_has_leaks(void); 163 164 /* Dumps all currently allocated blocks, including their allocation 165 * location, to standard error (stderr). The function also prints 166 * statistics about maximum memory usage. */ 167 void ws_dump_blocks(void); 168 169 /* Clear all statistics and the list containing the currently 170 * allocated leaks. The argument `num_successful_allocs' sets the 171 * limit how many memory allocations (assuming that the system has 172 * enought memory) are successful. If more than 173 * `num_successful_allocs' are performed, the allocations routines 174 * will fail and return the value NULL. */ 175 void ws_clear_leaks(unsigned int num_successful_allocs); 176 177 #endif /* WS_MEM_DEBUG */ 178 179 #endif /* not WSALLOC_H */ 180