Anope IRC Services  Version 1.8
memory.c
Go to the documentation of this file.
1 /* Memory management routines.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 
14 #include "services.h"
15 
16 /* smalloc, scalloc, srealloc, sstrdup:
17  * Versions of the memory allocation functions which will cause the
18  * program to terminate with an "Out of memory" error if the memory
19  * cannot be allocated. (Hence, the return value from these functions
20  * is never NULL.)
21  */
22 
23 /*************************************************************************/
24 
30 void *smalloc(long size)
31 {
32  void *buf;
33 
34  if (!size) {
35  size = 1;
36  }
37  buf = malloc(size);
38  if (!buf)
39 #ifndef _WIN32
40  raise(SIGUSR1);
41 #else
42  abort();
43 #endif
44  return buf;
45 }
46 
47 /*************************************************************************/
48 
55 void *scalloc(long elsize, long els)
56 {
57  void *buf;
58 
59  if (!elsize || !els) {
60  elsize = els = 1;
61  }
62  buf = calloc(elsize, els);
63  if (!buf)
64 #ifndef _WIN32
65  raise(SIGUSR1);
66 #else
67  abort();
68 #endif
69  return buf;
70 }
71 
72 /*************************************************************************/
73 
80 void *srealloc(void *oldptr, long newsize)
81 {
82  void *buf;
83 
84  if (!newsize) {
85  newsize = 1;
86  }
87  buf = realloc(oldptr, newsize);
88  if (!buf)
89 #ifndef _WIN32
90  raise(SIGUSR1);
91 #else
92  abort();
93 #endif
94  return buf;
95 }
96 
97 /*************************************************************************/
98 
105 char *sstrdup(const char *src)
106 {
107  char *ret = NULL;
108  if (src) {
109 #ifdef __STRICT_ANSI__
110  if ((ret = (char *) malloc(strlen(src) + 1))) {;
111  strcpy(ret, src);
112  }
113 #else
114  ret = strdup(src);
115 #endif
116  if (!ret)
117 #ifndef _WIN32
118  raise(SIGUSR1);
119 #else
120  abort();
121 #endif
122  } else {
123  alog("sstrdup() called with NULL-arg");
124  if (debug)
125  do_backtrace(0);
126  }
127 
128  return ret;
129 }
130 
131 /*************************************************************************/
132 /*************************************************************************/
133 
134 /* In the future: malloc() replacements that tell us if we're leaking and
135  * maybe do sanity checks too... */
136 
137 /*************************************************************************/
void * scalloc(long elsize, long els)
Definition: memory.c:55
E char * strdup(const char *s)
Definition: compat.c:94
void * smalloc(long size)
Definition: memory.c:30
char * sstrdup(const char *src)
Definition: memory.c:105
E void do_backtrace(int show_segheader)
Definition: main.c:729
E void alog(const char *fmt,...) FORMAT(printf
E int debug
Definition: extern.h:775
void * srealloc(void *oldptr, long newsize)
Definition: memory.c:80