]>
Commit | Line | Data |
---|---|---|
18038256 | 1 | /* GNU Thales - IRC to Relational Database Gateway |
2ace9480 | 2 | * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net> |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | ||
19 | #include "thales.h" | |
20 | #include "memory.h" | |
21 | #include "log.h" | |
22 | ||
23 | /*************************************************************************/ | |
24 | /*************************************************************************/ | |
25 | ||
26 | /* smalloc, scalloc, srealloc, sstrdup: | |
27 | * Versions of the memory allocation functions which will cause the | |
28 | * program to terminate with an "Out of memory" error if the memory | |
29 | * cannot be allocated. (Hence, the return value from these functions | |
30 | * is never NULL.) | |
31 | */ | |
32 | ||
33 | void *smalloc(long size) | |
34 | { | |
35 | void *buf; | |
36 | ||
37 | if (!size) | |
38 | { | |
18038256 | 39 | mylog("smalloc: Illegal attempt to allocate 0 bytes"); |
2ace9480 | 40 | size = 1; |
41 | } | |
42 | buf = malloc(size); | |
43 | if (!buf) | |
44 | #if !defined(USE_THREADS) || !defined(LINUX20) | |
45 | raise(SIGUSR1); | |
46 | #else | |
47 | abort(); | |
48 | #endif | |
49 | return buf; | |
50 | } | |
51 | ||
52 | void *scalloc(long elsize, long els) | |
53 | { | |
54 | void *buf; | |
55 | ||
56 | if (!elsize || !els) | |
57 | { | |
18038256 | 58 | mylog("scalloc: Illegal attempt to allocate 0 bytes"); |
2ace9480 | 59 | elsize = els = 1; |
60 | } | |
61 | buf = calloc(elsize, els); | |
62 | if (!buf) | |
63 | #if !defined(USE_THREADS) || !defined(LINUX20) | |
64 | raise(SIGUSR1); | |
65 | #else | |
66 | abort(); | |
67 | #endif | |
68 | return buf; | |
69 | } | |
70 | ||
71 | void *srealloc(void *oldptr, long newsize) | |
72 | { | |
73 | void *buf; | |
74 | ||
75 | if (!newsize) | |
76 | { | |
18038256 | 77 | mylog("srealloc: Illegal attempt to allocate 0 bytes"); |
2ace9480 | 78 | newsize = 1; |
79 | } | |
80 | buf = realloc(oldptr, newsize); | |
81 | if (!buf) | |
82 | #if !defined(USE_THREADS) || !defined(LINUX20) | |
83 | raise(SIGUSR1); | |
84 | #else | |
85 | abort(); | |
86 | #endif | |
87 | return buf; | |
88 | } | |
89 | ||
90 | char *sstrdup(const char *s) | |
91 | { | |
92 | char *t = strdup(s); | |
93 | if (!t) | |
94 | #if !defined(USE_THREADS) || !defined(LINUX20) | |
95 | raise(SIGUSR1); | |
96 | #else | |
97 | abort(); | |
98 | #endif | |
99 | return t; | |
100 | } | |
101 | ||
102 | /*************************************************************************/ | |
103 | /*************************************************************************/ | |
104 | ||
105 | /* In the future: malloc() replacements that tell us if we're leaking and | |
106 | * maybe do sanity checks too... */ | |
107 | ||
108 | /*************************************************************************/ |