1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* ====================================================================
* The Kannel Software License, Version 1.0
*
* Copyright (c) 2001-2018 Kannel Group
* Copyright (c) 1998-2001 WapIT Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Kannel Group (http://www.kannel.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Kannel" and "Kannel Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact org@kannel.org.
*
* 5. Products derived from this software may not be called "Kannel",
* nor may "Kannel" appear in their name, without prior written
* permission of the Kannel Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Kannel Group. For more information on
* the Kannel Group, please see <http://www.kannel.org/>.
*
* Portions of this software are based upon software originally written at
* WapIT Ltd., Helsinki, Finland for the Kannel project.
*/
/*
*
* wsalloc.c
*
* Author: Markku Rossi <mtr@iki.fi>
*
* Copyright (c) 1999-2000 WAPIT OY LTD.
* All rights reserved.
*
* Memory allocation routines. These are simple stub functions to fix
* some brain damages, found from some system's default allocators.
*
*/
#include "wsint.h"
#if !WS_MEM_DEBUG
/********************* Global functions *********************************/
void *ws_malloc(size_t size)
{
return malloc(size);
}
void *ws_calloc(size_t num, size_t size)
{
return calloc(num, size);
}
void *ws_realloc(void *ptr, size_t size)
{
if (size == 0) {
if (ptr)
free(ptr);
return NULL;
}
if (ptr == NULL)
return malloc(size);
return realloc(ptr, size);
}
void *ws_memdup(const void *ptr, size_t size)
{
unsigned char *data = ws_malloc(size + 1);
if (data == NULL)
return NULL;
memcpy(data, ptr, size);
data[size] = '\0';
return data;
}
void *ws_strdup(const char *str)
{
size_t len;
void *s;
if (str == NULL)
return NULL;
len = strlen(str);
s = ws_malloc(len + 1);
if (s == NULL)
return NULL;
memcpy(s, str, len + 1);
return s;
}
void ws_free(void *ptr)
{
if (ptr)
free(ptr);
}
#else /* WS_MEM_DEBUG */
/********************* Memory debugging routines ************************/
#define SIZE(_size) (sizeof(WsMemBlockHdr) + (_size))
#define MAGIC 0xfe01fa77
struct WsMemBlockHdrRec
{
unsigned long magic;
struct WsMemBlockHdrRec *next;
struct WsMemBlockHdrRec *prev;
size_t size;
const char *file;
int line;
};
typedef struct WsMemBlockHdrRec WsMemBlockHdr;
/* A linked list of currently allocated blocks. */
WsMemBlockHdr *blocks = NULL;
/* How many blocks are currently allocated. */
unsigned int num_blocks = 0;
/* The maximum amount of blocks used. */
unsigned int max_num_blocks = 0;
/* How many (user) bytes of memory the currently allocated blocks
use. */
size_t balance = 0;
/* The maximum amount of memory used. */
size_t max_balance = 0;
/* The alloc sequence number. */
unsigned int alloc_number = 0;
/* How many allocations are successful. */
unsigned int num_successful_allocs = -1;
static void add_block(WsMemBlockHdr *b, size_t size, const char *file, int line)
{
b->magic = MAGIC;
b->next = blocks;
b->prev = NULL;
if (blocks)
blocks->prev = b;
blocks = b;
b->size = size;
b->file = file;
b->line = line;
num_blocks++;
balance += size;
if (balance > max_balance)
max_balance = balance;
if (num_blocks > max_num_blocks)
max_num_blocks = num_blocks;
}
static void remove_block(WsMemBlockHdr *b)
{
if (b->magic != MAGIC)
ws_fatal("remove_block(): invalid magic\n");
if (b->next)
b->next->prev = b->prev;
if (b->prev)
b->prev->next = b->next;
else
blocks = b->next;
balance -= b->size;
num_blocks--;
memset(b, 0xfe, SIZE(b->size));
}
void *ws_malloc_i(size_t size, const char *file, int line)
{
WsMemBlockHdr *b;
if (alloc_number++ >= num_successful_allocs)
return NULL;
b = malloc(SIZE(size));
if (b == NULL)
return NULL;
add_block(b, size, file, line);
return b + 1;
}
void *ws_calloc_i(size_t num, size_t size, const char *file, int line)
{
void *p = ws_malloc_i(num * size, file, line);
if (p)
memset(p, 0, num * size);
return p;
}
void *ws_realloc_i(void *ptr, size_t size, const char *file, int line)
{
WsMemBlockHdr *b = ((WsMemBlockHdr *) ptr) - 1;
void *n;
if (ptr == NULL)
return ws_malloc_i(size, file, line);
if (b->size >= size)
/* We can use the old block. */
return ptr;
/* Allocate a bigger block. */
n = ws_malloc_i(size, file, line);
if (n == NULL)
return NULL;
memcpy(n, ptr, b->size);
/* Free old block. */
remove_block(b);
free(b);
return n;
}
void *ws_memdup_i(const void *ptr, size_t size, const char *file, int line)
{
void *p = ws_malloc_i(size + 1, file, line);
if (p) {
unsigned char *cp = (unsigned char *) p;
memcpy(p, ptr, size);
cp[size] = '\0';
}
return p;
}
void *ws_strdup_i(const char *str, const char *file, int line)
{
return ws_memdup_i(str, strlen(str), file, line);
}
void ws_free_i(void *ptr)
{
WsMemBlockHdr *b = ((WsMemBlockHdr *) ptr) - 1;
if (ptr == NULL)
return;
remove_block(b);
free(b);
}
int ws_has_leaks(void)
{
return num_blocks || balance;
}
void ws_dump_blocks(void)
{
WsMemBlockHdr *b;
fprintf(stderr, "ws: maximum memory usage: %u blocks, %ld bytes\n",
max_num_blocks, (long) max_balance);
fprintf(stderr, "ws: number of allocs: %u\n", alloc_number);
if (num_blocks || balance) {
fprintf(stderr, "ws: memory leaks: %u blocks, %ld bytes:\n",
num_blocks, (long) balance);
for (b = blocks; b; b = b->next)
fprintf(stderr, "%s:%d: %ld\n", b->file, b->line, (long) b->size);
}
}
void ws_clear_leaks(unsigned int num_successful_allocs_)
{
alloc_number = 0;
num_successful_allocs = num_successful_allocs_;
blocks = NULL;
}
#endif /* WS_MEM_DEBUG */
|