forked from casoalb/K9
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.c
More file actions
263 lines (219 loc) · 6.16 KB
/
memory.c
File metadata and controls
263 lines (219 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Memory management routines.
**
** Based on ircdservices ver. by Andy Church
** Chatnet modifications (c) 2001-2002
**
** E-mail: routing@lists.chatnet.org
** Authors:
**
** Vampire (vampire@alias.chatnet.org)
** Thread (thread@alias.chatnet.org)
** MRX (tom@rooted.net)
**
** *** DO NOT DISTRIBUTE ***
**/
#define NO_MEMREDEF
#include "services.h"
/*************************************************************************/
/*************************************************************************/
/*
* To enable memory leak checks add the _MEMCHCKS define to the
* config.h or the command line when compiling.
* -- Kelmar (2001-01-13)
* Note that this is not very useful at present because Services does not
* clean up after itself on exit, so there will always be large amounts of
* memory "leaked". --AC 2001/2/15
*/
/*
* WARNING: The following define will bloat your log files VERY quickly!
* Use at your own risk!
* -- Kelmar (2001-01-16)
*/
/* #define SHOWALLOCS */
#ifdef SHOWALLOCS
int showallocs = 1; /* Actually log allocations? */
#endif
/*************************************************************************/
#ifdef MEMCHECKS
typedef struct _smemblock {
int32 size; /* Size of this block */
int32 sig; /* Signature word: 0x5AFEC0DE */
void *data; /* Start of the user's data */
} MemBlock;
#define SIGNATURE 0x5AFEC0DE
#define FREED_SIGNATURE 0xDEADBEEF /* Used for freed memory */
#endif /* MEMCHECKS */
/*************************************************************************/
/*************************************************************************/
#ifdef MEMCHECKS
static uint runtime = 0;
static uint allocated = 0;
static void show_leaks(void)
{
if ((allocated - runtime) > 0) {
log("SAFEMEM: There were %d bytes leaked on exit!",
(allocated - runtime));
allocated = 0;
} else {
log("SAFEMEM: No memory leaks detected.");
}
}
void init_memory(void)
{
runtime = allocated;
fprintf(stderr, "init_memory(): runtime = %d\n", runtime);
atexit(show_leaks);
}
#endif /* MEMCHECKS */
/*************************************************************************/
/* smalloc, scalloc, srealloc, (sfree), sstrdup:
* Versions of the memory allocation functions which will cause the
* program to terminate with an "Out of memory" error if the memory
* cannot be allocated. (Hence, the return value from these functions
* is never NULL.) sfree() is used only with MEMCHECKS enabled.
*/
/*************************************************************************/
void *smalloc(long size)
{
#ifdef MEMCHECKS
MemBlock *mb;
#else
void *buf;
#endif
if (size == 0) {
log("smalloc: Illegal attempt to allocate 0 bytes");
size = 1;
}
#ifdef MEMCHECKS
mb = malloc(size + sizeof(MemBlock));
if (mb == NULL)
raise(SIGUSR1);
mb->size = size;
mb->sig = SIGNATURE;
mb->data = (void *)((char *)(mb) + sizeof(MemBlock));
allocated += size;
# ifdef SHOWALLOCS
if (showallocs)
log("smalloc(): Allocated %ld bytes at %p", size, mb->data);
# endif
return mb->data;
#else /* !MEMCHECKS */
buf = malloc(size);
if (buf == NULL)
raise(SIGUSR1);
return buf;
#endif /* MEMCHECKS */
}
/*************************************************************************/
void *scalloc(long elsize, long els)
{
#ifdef MEMCHECKS
MemBlock *mb;
#else
void *buf;
#endif
if (elsize == 0)
{
log("scalloc: Illegal attempt to allocate 0 bytes");
wallops(s_ChanServ, "Warning: Internal memory allocation error. Please notify #k9dev");
elsize = els = 1;
}
#ifdef MEMCHECKS
mb = malloc(elsize * els + sizeof(MemBlock));
if (mb == NULL)
raise(SIGUSR1);
mb->size = elsize * els;
mb->sig = SIGNATURE;
mb->data = (void *)((char *)(mb) + sizeof(MemBlock));
memset(mb->data, 0, elsize * els);
allocated += mb->size;
# ifdef SHOWALLOCS
if (showallocs)
log("scalloc(): Allocated %ld bytes at %p", els*elsize, mb->data);
# endif
return mb->data;
#else /* !MEMCHECKS */
buf = calloc(els, elsize);
if (buf == NULL)
raise(SIGUSR1);
return buf;
#endif /* MEMCHECKS */
}
/*************************************************************************/
void *srealloc(void *oldptr, long newsize)
{
#ifdef MEMCHECKS
MemBlock *newb, *oldb;
long oldsize;
# ifdef SHOWALLOCS
void *olddata;
# endif
#else
void *buf;
#endif /* MEMCHECKS */
if (newsize == 0) {
#ifdef MEMCHECKS
sfree(oldptr);
#else
free(oldptr);
#endif
return NULL;
}
#ifdef MEMCHECKS
if (oldptr == NULL)
return smalloc(newsize);
oldb = (MemBlock *)((char *)(oldptr) - sizeof(MemBlock));
if (oldb->sig != SIGNATURE)
fatal("Attempt to realloc() block on an invalid pointer!");
# ifdef SHOWALLOCS
olddata = oldb->data;
# endif
oldsize = oldb->size;
newb = realloc(oldb, newsize + sizeof(MemBlock));
if (newb == NULL)
raise(SIGUSR1);
newb->size = newsize;
newb->sig = SIGNATURE;
newb->data = (void *)((char *)(newb) + sizeof(MemBlock));
/* Adjust our tracker acordingly */
allocated += (newsize - oldsize);
# ifdef SHOWALLOCS
if (showallocs)
log("srealloc(): Adjusted %ld bytes (%p) to %ld bytes (%p)",
oldsize, olddata, newsize, newb->data);
# endif
return newb->data;
#else /* !MEMCHECKS */
buf = realloc(oldptr, newsize);
if (buf == NULL)
raise(SIGUSR1);
return buf;
#endif /* MEMCHECKS */
}
/*************************************************************************/
#ifdef MEMCHECKS
void sfree(void *ptr)
{
MemBlock *mb;
if (ptr == NULL)
fatal("Attempt to sfree() a NULL pointer!");
mb = (MemBlock *)((char *)(ptr) - sizeof(MemBlock));
if (mb->sig != SIGNATURE)
fatal("Attempt to sfree() an invalid pointer! (%p)", ptr);
allocated -= mb->size;
# ifdef SHOWALLOCS
if (showallocs)
log("sfree(): Released %d bytes at %p", mb->size, mb->data);
# endif
mb->sig = FREED_SIGNATURE;
free(mb);
}
#endif /* MEMCHECKS */
/*************************************************************************/
char *sstrdup(const char *s)
{
char *t = smalloc(strlen(s) + 1);
strcpy(t, s);
return t;
}
/*************************************************************************/