1 |
/** |
2 |
* Copyright © 2005 Atheme Development Group |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Memory functions. |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id: memory.C,v 1.4 2007-07-25 00:03:21 pippijn Exp $"; |
9 |
|
10 |
#include "atheme.h" |
11 |
|
12 |
std::vector<void *> gc::mortals; |
13 |
|
14 |
/* does malloc()'s job and dies if malloc() fails */ |
15 |
void * |
16 |
smalloc (size_t size) |
17 |
{ |
18 |
void *buf = calloc (size, 1); |
19 |
|
20 |
if (!buf) |
21 |
raise (SIGUSR1); |
22 |
return buf; |
23 |
} |
24 |
|
25 |
/* does calloc()'s job and dies if calloc() fails */ |
26 |
void * |
27 |
scalloc (size_t elsize, size_t els) |
28 |
{ |
29 |
void *buf = calloc (elsize, els); |
30 |
|
31 |
if (!buf) |
32 |
raise (SIGUSR1); |
33 |
return buf; |
34 |
} |
35 |
|
36 |
/* does realloc()'s job and dies if realloc() fails */ |
37 |
void * |
38 |
srealloc (void *oldptr, size_t newsize) |
39 |
{ |
40 |
void *buf = realloc (oldptr, newsize); |
41 |
|
42 |
if (!buf) |
43 |
raise (SIGUSR1); |
44 |
return buf; |
45 |
} |
46 |
|
47 |
/* does strdup()'s job, only with the above memory functions */ |
48 |
char * |
49 |
sstrdup (char const *s) |
50 |
{ |
51 |
char *t; |
52 |
|
53 |
if (s == NULL) |
54 |
return NULL; |
55 |
|
56 |
t = static_cast < char *>(smalloc (strlen (s) + 1)); |
57 |
|
58 |
strcpy (t, s); |
59 |
return t; |
60 |
} |
61 |
|
62 |
/* does strndup()'s job, only with the above memory functions */ |
63 |
char * |
64 |
sstrndup (char const *s, int len) |
65 |
{ |
66 |
char *t; |
67 |
|
68 |
if (s == NULL) |
69 |
return NULL; |
70 |
|
71 |
t = static_cast < char *>(smalloc (len + 1)); |
72 |
|
73 |
strlcpy (t, s, len); |
74 |
return t; |
75 |
} |