File: memory.c

package info (click to toggle)
xymon 4.3.30-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,288 kB
  • sloc: ansic: 69,112; sh: 3,595; makefile: 857; javascript: 452; perl: 48
file content (316 lines) | stat: -rw-r--r-- 6,565 bytes parent folder | download | duplicates (2)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* This is a library module, part of libxymon.                                */
/* It contains memory management routines.                                    */
/*                                                                            */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk>                 */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid[] = "$Id: memory.c 8069 2019-07-23 15:29:06Z jccleaver $";

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

#define LIB_MEMORY_C_COMPILE 1

#include "libxymon.h"

#ifdef MEMORY_DEBUG
static xmemory_t *mhead = NULL;
static xmemory_t *dmem;
static void *allocend, *copyend;
#endif

#ifdef MEMORY_DEBUG
void add_to_memlist(void *ptr, size_t memsize)
{
	xmemory_t *newitem;

	newitem = (xmemory_t *)malloc(sizeof(xmemory_t));
	newitem->sdata = ptr;
	newitem->ssize = memsize;
	newitem->next = mhead;
	mhead = newitem;
}

static void dump_memitems(void)
{
	xmemory_t *mwalk;

	for (mwalk = mhead; (mwalk); mwalk = mwalk->next) {
		errprintf("%8x  : %5d\n", mwalk->sdata, mwalk->ssize);
	}
}

static xmemory_t *find_in_memlist(void *ptr)
{
	xmemory_t *mwalk = mhead;
	int found = 0;

	while (mwalk) {
		found = (((void *)ptr >= (void *)mwalk->sdata) && ((void *)ptr < (void *)(mwalk->sdata + mwalk->ssize)));
		if (found) return mwalk;

		mwalk = mwalk->next;
	}

	return NULL;
}


void remove_from_memlist(void *ptr)
{
	xmemory_t *mwalk, *mitem;
	
	if (ptr == NULL) {
		errprintf("remove_from_memlist called with NULL pointer\n");
		dump_memitems();
		abort();
	}

	mitem= find_in_memlist(ptr);
	if (mitem == NULL) {
		errprintf("remove_from_memlist called with bogus pointer\n");
		abort();
	}

	if (mitem == mhead) {
		mhead = mhead->next;
		free(mitem);
	}
	else {
		for (mwalk = mhead; (mwalk->next != mitem); mwalk = mwalk->next) ;
		mwalk->next = mitem->next;
		free(mitem);
	}
}
#endif

const char *xfreenullstr = "xfree: Trying to free a NULL pointer\n";

void *xcalloc(size_t nmemb, size_t size)
{
	void *result;

	result = calloc(nmemb, size);
	if (result == NULL) {
		errprintf("xcalloc: Out of memory!\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	add_to_memlist(result, nmemb*size);
#endif
	return result;
}


void *xmalloc(size_t size)
{
	void *result;

	result = malloc(size);
	if (result == NULL) {
		errprintf("xmalloc: Out of memory!\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	add_to_memlist(result, size);
#endif
	return result;
}


void *xrealloc(void *ptr, size_t size)
{
	void *result;

	if (ptr == NULL) {
		errprintf("xrealloc: Cannot realloc NULL pointer\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	dmem = find_in_memlist(ptr);
	if (dmem == NULL) {
		errprintf("xrealloc: Called with bogus pointer\n");
		abort();
	}
#endif

	result = realloc(ptr, size);
	if (result == NULL) {
		errprintf("xrealloc: Out of memory!\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	dmem->sdata = result;
	dmem->ssize = size;
#endif

	return result;
}

char *xstrdup(const char *s)
{
	char *result;

	if (s == NULL) {
		errprintf("xstrdup: Cannot dup NULL string\n");
		abort();
	}

	result = strdup(s);
	if (result == NULL) {
		errprintf("xstrdup: Out of memory\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	add_to_memlist(result, strlen(result)+1);
#endif

	return result;
}

char *xstrncat(char *dest, const char *src, size_t maxlen)
{
	if (src == NULL) {
		errprintf("xstrncat: NULL destination\n");
		abort();
	}

	if (dest == NULL) {
		errprintf("xstrncat: NULL destination\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	dmem = find_in_memlist(dest);
	if (dmem == NULL) {
		errprintf("xstrncat: Bogus destination\n");
		abort();
	}

	allocend = dmem->sdata + dmem->ssize - 1;
	if (strlen(src) <= maxlen)
		copyend = dest + strlen(dest) + strlen(src);
	else
		copyend = dest + strlen(dest) + maxlen;

	if ((void *)copyend > (void *)allocend) {
		errprintf("xstrncat: Potential overwrite of %d bytes\n", (copyend - allocend));
		abort();
	}

	if (strlen(dest) + strlen(src) >= maxlen) {
		errprintf("xstrncat: destination is not NULL terminated - dst '%s', src '%s', max %d\n", dest, src, maxlen);
	}
#endif

	strncat(dest, src, maxlen);
	return dest;
}

char *xstrncpy(char *dest, const char *src, size_t maxlen)
{
	if (src == NULL) {
		errprintf("xstrncpy: NULL destination\n");
		abort();
	}

	if (dest == NULL) {
		errprintf("xstrncpy: NULL destination\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	dmem = find_in_memlist(dest);
	if (dmem == NULL) {
		errprintf("xstrncpy: Bogus destination\n");
		abort();
	}

	allocend = dmem->sdata + dmem->ssize - 1;
	if (strlen(src) <= maxlen)
		copyend = dest + strlen(src);
	else
		copyend = dest + maxlen;
	if ((void *)copyend > (void *)allocend) {
		errprintf("xstrncpy: Potential overwrite of %d bytes\n", (copyend - allocend));
		abort();
	}

	if (strlen(src) >= maxlen) {
		errprintf("xstrncpy: destination is not NULL terminated - src '%s', max %d\n", dest, src, maxlen);
	}
#endif

	strncpy(dest, src, maxlen);
	return dest;
}

int xsprintf(char *dest, const char *fmt, ...)
{
	va_list args;
	size_t printedbytes;
#ifdef MEMORY_DEBUG
	size_t availablebytes;
#endif

	if (dest == NULL) {
		errprintf("xsprintf: NULL destination\n");
		abort();
	}

#ifdef MEMORY_DEBUG
	dmem = find_in_memlist(dest);
	if (dmem == NULL) {
		errprintf("xsprintf: Bogus destination\n");
		abort();
	}

	availablebytes = (dmem->sdata + dmem->ssize - dest);
	va_start(args, fmt);
	printedbytes = vsnprintf(dest, availablebytes, fmt, args);
	va_end(args);

	if (printedbytes >= availablebytes) {
		errprintf("xsprintf: Output was truncated\n");
		abort();
	}
#else
	va_start(args, fmt);
	printedbytes = vsprintf(dest, fmt, args);
	va_end(args);
#endif

	return printedbytes;
}


char *xresultbuf(int maxsz)
{
	static char rrbuf[10000];
	static char *rrbufnext = rrbuf;
	char *result;

	if ((rrbufnext + maxsz) >= (rrbuf + sizeof(rrbuf))) 
		result = rrbufnext = rrbuf;
	else {
		result = rrbufnext;
		rrbufnext += maxsz;
	}

	return result;
}