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