Merged from silc_1_0_branch.
[silc.git] / lib / silcutil / silcmemory.c
1 /*
2
3   silcmemory.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22
23 #ifndef SILC_STACKTRACE
24
25 #define SILC_MAX_ALLOC (1024 * 1024L * 1024L)
26
27 void *silc_malloc(size_t size)
28 {
29   void *addr;
30   assert(size >= 0 && size <= SILC_MAX_ALLOC);
31   addr = malloc(size);
32   assert(addr != NULL);
33   return addr;
34 }
35
36 void *silc_calloc(size_t items, size_t size)
37 {
38   void *addr;
39   assert(size * items >= 0 && size * items <= SILC_MAX_ALLOC);
40   addr = calloc(items, size);
41   assert(addr != NULL);
42   return addr;
43 }
44
45 void *silc_realloc(void *ptr, size_t size)
46 {
47   void *addr;
48   assert(size >= 0 && size <= SILC_MAX_ALLOC);
49   addr = realloc(ptr, size);
50   assert(addr != NULL);
51   return addr;
52 }
53
54 void silc_free(void *ptr)
55 {
56   free(ptr);
57 }
58
59 void *silc_memdup(const void *ptr, size_t size)
60 {
61   unsigned char *addr;
62   addr = silc_malloc(size + 1);
63   assert(addr != NULL);
64   memcpy((void *)addr, ptr, size);
65   addr[size] = '\0';
66   return (void *)addr;
67 }
68
69 #endif /* !SILC_STACKTRACE */