File: aug_alloc.c

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (656 lines) | stat: -rw-r--r-- 14,385 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
 * $Id: aug_alloc.c,v 1.3 2006/07/10 14:26:32 bogdan_iancu Exp $
 *
 * POSTGRES module, portions of this code were templated using
 * the mysql module, thus it's similarity.
 *
 * Copyright (C) 2003 August.Net Services, LLC
 *
 * This file is part of openser, a free SIP server.
 *
 * openser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * openser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ---
 *
 * History
 * -------
 * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
 *
 */
/*
** ________________________________________________________________________
**
**
**                      $RCSfile: aug_alloc.c,v $
**                     $Revision: 1.3 $
**
**             Last change $Date: 2006/07/10 14:26:32 $
**           Last change $Author: bogdan_iancu $
**                        $State: Exp $
**                       $Locker:  $
**
**               Original author: Andrew Fullford
**
**           Copyright (C) August Associates  1995
**
** ________________________________________________________________________
*/

#include "aug_std.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef double MemAlign;
typedef augUInt32 MemMagic;
typedef union MemHead MemHead;
typedef struct MemOpt MemOpt;
typedef struct MemDestructor MemDestructor;	/* not yet implemented */

/*
**  One of these MemHead structs is allocated at the head of
**  each alloc, plus an extra magic number at the end area.
**  This gives an allocation overhead of:
**
**	malloc_overhead + sizeof MemHead + sizeof MemMagic
**
**  "Notes" entry for the man page: the allocation overhead is way
**  too high.  (On a 32bit machine and assuming a malloc overhead
**  of 8 bytes, the total will be 8 + 32 + 4 = 44 bytes).
*/
struct MemHeadStruct
{
	MemHead *parent, *sibling, *child;
	MemOpt *options;
	char *end;
	char *file;
	augUInt32 line;
	MemMagic magic;
};

/*
**  Attempt to guarantee alignment.
*/
union MemHead
{
	struct MemHeadStruct m;
	MemAlign align[1];
};

/*
**  MemOpt holds optional features.  The only current example
**  is the memory destructor state.
*/
struct MemOpt
{
	MemMagic magic;
	MemDestructor *destructor_list;
};

/*
**  These magic numbers are used to validate headers, force memory
**  to a known state, etc.
*/
#define MEM_MAGIC_BOUND	0xC0EDBABE
#define MEM_MAGIC_FILL	0xDEADC0DE

static int mem_bad(MemHead *mem, char *where, char *file, int line)
{
	aug_abort(file, line, "Corrupted memory in %s", where);
	return 0;
}

/*
**  Calculate the MemHead address given an aug_alloc() pointer.
*/
#define MEM_CROWN(alloc) ((MemHead *)(((char *)alloc) - sizeof (MemHead)))
#define MEM_DECAPITATE(mem) ((void *)(((char *)mem) + sizeof (MemHead)))

static MemMagic mem_magic = MEM_MAGIC_BOUND;
#define MEM_TAIL(p) (memcmp((p)->m.end,(char*)&mem_magic,sizeof mem_magic)==0)
#define MEM_CHECK(p,w) \
		do {\
			if ((p) && ((p)->m.magic != MEM_MAGIC_BOUND || !MEM_TAIL(p))) \
				mem_bad((p),(w),file,line);\
		}while(0);

/*  Initialize stats structure with estimated overhead */
static augAllocStats mem_stats = {sizeof (MemHead) + sizeof (MemMagic) + 8, 0};

static augNoMemFunc *mem_nomem_func = 0;
static void mem_nomem(size_t size, char *func, char *file, int line)
{
	static augBool active = augFALSE;
	char *module;

	if(!func)
		func = "unknown function";

	if(active)
		fprintf(stderr, "\r\n\nPANIC: nomem bounce\r\n\n");
	else
	{
		active = augTRUE;
		if(mem_nomem_func)
			(*mem_nomem_func)(size, func, file, line);
	}

	fprintf(stderr, "\r\n\n");

	module = aug_module();
	if(module && *module)
		fprintf(stderr, "FATAL in %s: ", module);
	else
		fprintf(stderr, "FATAL: ");

	fprintf(stderr, "%s failure allocating %lu bytes ",
		func, (long unsigned int)size);

	if(file && *file)
		fprintf(stderr, "from +%d %s \r\n", line, file);
	else
		fprintf(stderr, "(unknown location) \r\n");

	fprintf(stderr, "              Current allocations: %10lu \r\n",
		(mem_stats.alloc_ops - mem_stats.free_ops));
	fprintf(stderr, "                Total allocations: %10lu \r\n",
		mem_stats.alloc_ops);
	fprintf(stderr, "              Total reallocations: %10lu \r\n",
		mem_stats.realloc_ops);
	fprintf(stderr, "                      Total frees: %10lu \r\n",
		mem_stats.free_ops);
	fprintf(stderr, "Estimated total heap use (KBytes): %10lu \r\n",
		(mem_stats.current_bytes_allocated +
		(mem_stats.alloc_ops - mem_stats.free_ops) *
		mem_stats.estimated_overhead_per_alloc + 512)/1024);
	fprintf(stderr, "\n");
		
	aug_exit(augEXIT_NOMEM);
}

static void *mem_alloc(size_t size, void *parent, char *file, int line)
{
	MemHead *mem, *par;
	DABNAME("mem_alloc");

	if(parent)
	{
		par = MEM_CROWN(parent);
		MEM_CHECK(par, "parent");
		MEM_CHECK(par->m.child, "sibling");
		MEM_CHECK(par->m.sibling, "uncle");
	}
	else
		par = 0;

	mem_stats.current_bytes_allocated += size;
	mem_stats.alloc_ops++;

	/*  Adjust for overhead  */
	size += sizeof (MemHead);

	mem = malloc(size + sizeof (MemMagic));
	if(!mem)
		mem_nomem(size, "aug_alloc", file, line);

	if(DABLEVEL(DAB_STD))
	{
		unsigned long *p;
		p = (unsigned long *)mem;
		while((char *)p <= (char *)mem + size)
			*p++ = MEM_MAGIC_FILL;
	}

	mem->m.magic = MEM_MAGIC_BOUND;
	mem->m.file = file;
	mem->m.line = line;
	mem->m.end = (char *)mem + size;
	mem->m.options = 0;
	mem->m.child = 0;
	mem->m.parent = par;

	if(par)
	{
		if( (mem->m.sibling = par->m.child) )
			mem->m.sibling->m.parent = mem;
		par->m.child = mem;
	}
	else
		mem->m.sibling = 0;

	memcpy(mem->m.end, (char *)&mem_magic, sizeof mem_magic);

	return MEM_DECAPITATE(mem);
}

static void mem_free(MemHead *mem)
{
	size_t size;
	DABNAME("mem_free");

	while(mem)
	{
		MemHead *next = mem->m.sibling;
		if(mem->m.child)
			mem_free(mem->m.child);
		size = (char *)mem->m.end - (char *)mem;
		size -= sizeof (MemHead) + sizeof (MemMagic);
		mem_stats.current_bytes_allocated -= size;
		mem_stats.free_ops++;
		if(DABLEVEL(DAB_STD))
		{
			unsigned long *p = (unsigned long *)(mem+1);

			while((char *)p <= mem->m.end)
				*p++ = MEM_MAGIC_FILL;
			p = (unsigned long *)mem;
			while(p < (unsigned long *)(mem+1))
				*p++ = MEM_MAGIC_FILL;
		}
		free(mem);
		mem = next;
	}
}

static augBool mem_find(MemHead *mem, MemHead *p)
{
	while(mem)
	{
		MemHead *next;

		if(mem == p)
			return augTRUE;
		next = mem->m.sibling;
		if(mem->m.child)
			if(mem_find(mem->m.child, p))
				return augTRUE;
		mem = next;
	}
	return augFALSE;
}

augExport augNoMemFunc *aug_set_nomem_func(augNoMemFunc *new_func)
{
	augNoMemFunc *old = mem_nomem_func;
	DABNAME("aug_set_nomem_func");

	mem_nomem_func = new_func;

#if augDAB_DEBUG == augTRUE
	DABTRACE("New nomem func %08lx, previous %08lx",
		(unsigned long)mem_nomem_func, (unsigned long)old);
#endif
	return old;
}

augExport augAllocStats *aug_alloc_stats(void)
{
	return &mem_stats;
}

augExport void *aug_alloc_loc(size_t size, void *parent, char *file, int line)
{
	void *alloc;
	DABNAME("aug_alloc");

#if augDAB_DEBUG == augTRUE
	DAB("size %lu, parent %08lx [+%d %s]",
		(unsigned long)size, (unsigned long)parent, line, file);
#endif

	alloc = mem_alloc(size, parent, file, line);

#if augDAB_DEBUG == augTRUE
	DABL(80)("size %lu with header, caller mem at %08lx",
		MEM_CROWN(alloc)->m.end - (char *)MEM_CROWN(alloc),
		(unsigned long)alloc);
#endif

	return alloc;
}

augExport void *aug_realloc_loc(size_t size, void *prev, char *file, int line)
{
	void *alloc;
	size_t prev_size;
	MemHead *mem, *par, *kid, *sib, *new;
	DABNAME("aug_realloc");

	if(!prev)
		aug_abort(file, line, "Attempt to realloc a NULL pointer");

	mem = MEM_CROWN(prev);
	MEM_CHECK(mem, "previous alloc");

	par = mem->m.parent;	MEM_CHECK(par, "realloc parent");
	kid = mem->m.child;	MEM_CHECK(kid, "realloc child");
	sib = mem->m.sibling;	MEM_CHECK(sib, "realloc sibling");

	prev_size = (mem->m.end - (char *)mem) - sizeof (MemHead);

#if augDAB_DEBUG == augTRUE
	DAB("prior size %lu, new %lu [+%d %s]",
				(unsigned long)prev_size, (unsigned long)size,
				line, file);
	DABL(80)("prior mem %08lx", (unsigned long)mem);
#endif

	mem_stats.current_bytes_allocated += size - prev_size;
	mem_stats.realloc_ops++;

	size += sizeof (MemHead);

	new = realloc(mem, size + sizeof (MemMagic));
	if(!new)
		mem_nomem(size, "aug_realloc", file, line);
	new->m.end = (char *)new + size;

	memcpy(new->m.end, (char *)&mem_magic, sizeof mem_magic);

	if(par)
	{
		if(par->m.sibling == mem)
			par->m.sibling = new;
		else
			par->m.child = new;
	}
	if(kid)
		kid->m.parent = new;
	if(sib)
		sib->m.parent = new;

	alloc = MEM_DECAPITATE(new);

#if augDAB_DEBUG == augTRUE
	DABL(80)("size %lu with header, caller mem at %08lx",
			new->m.end - (char *)new, (unsigned long)alloc);
#endif

	return alloc;
}

augExport void aug_free_loc(void *alloc, char *file, int line)
{
	MemHead *mem, *par;
	DABNAME("aug_free");

	if(!alloc)
		aug_abort(file, line, "Attempt to free a NULL pointer");

#if augDAB_DEBUG == augTRUE
	DAB("Freeing %08lx [+%d %s]", (unsigned long)alloc, line, file);
#endif

	mem = MEM_CROWN(alloc);
	MEM_CHECK(mem, "alloc to free");

	par = mem->m.parent;
	MEM_CHECK(par, "parent in free");

	if(par)
	{
		if(par->m.sibling == mem)
			par->m.sibling = mem->m.sibling;
		else
			par->m.child = mem->m.sibling;
	}

	if(mem->m.sibling)
	{
		mem->m.sibling->m.parent = par;
		mem->m.sibling = 0;
	}

	mem_free(mem);
}

augExport void aug_foster_loc(void *alloc, void *parent, char *file, int line)
{
	MemHead *mem, *fpar, *ppar, *sib;
	DABNAME("aug_foster");

#if augDAB_DEBUG == augTRUE
	DAB("Foster %08lx to %08lx [+%d %s]",
						alloc, parent, line, file);
#endif

	if(!alloc)
		aug_abort(file, line, "Attempt to foster a NULL pointer");

	mem = MEM_CROWN(alloc);
	MEM_CHECK(mem, "alloc to foster");

	if(parent)
	{
		fpar = MEM_CROWN(parent);
		MEM_CHECK(fpar, "foster parent");
	}
	else
		fpar = 0;

	ppar = mem->m.parent; MEM_CHECK(ppar, "prior parent");
	sib = mem->m.sibling; MEM_CHECK(ppar, "sibling in foster");

	if(fpar == ppar)
	{
#if augDAB_DEBUG == augTRUE
		DABTRACE("No change in parent (%08lx)", (unsigned long)fpar);
#endif
		return;
	}

	if(mem == fpar)
		aug_abort(file, line, "Attempt to adopt self");

	/*
	**  Check for incest - isnew parent actually our child?
	*/
	if(mem_find(mem->m.child, fpar))
		aug_abort(file, line, "Attempt to adopt a parent");

	/*
	**  Leave home.
	*/
	if(!ppar)
	{
#if augDAB_DEBUG == augTRUE
		DABBULK("Leaving orphanage");
#endif
		if(mem->m.sibling)
			mem->m.sibling->m.parent = 0;
	}
	else if(ppar->m.sibling == mem)
	{
#if augDAB_DEBUG == augTRUE
		DABBULK("Older child");
#endif
		ppar->m.sibling = mem->m.sibling;
		if(ppar->m.sibling)
			ppar->m.sibling->m.parent = ppar;
	}
	else
	{
#if augDAB_DEBUG == augTRUE
		DABBULK("Youngest child");
#endif
		ppar->m.child = mem->m.sibling;
		if(ppar->m.child)
			ppar->m.child->m.parent = ppar;
	}

	/*
	**  Find new home.
	*/
	mem->m.parent = fpar;
	if(fpar)
	{
		mem->m.sibling = fpar->m.child;
		fpar->m.child = mem;
		if(mem->m.sibling)
			mem->m.sibling->m.parent = mem;
	}
	else
		mem->m.sibling = 0;
}

augExport char *aug_strdup_loc(char *str, void *parent, char *file, int line)
{
	char *new;
	size_t size;
	DABNAME("aug_strdup");

	if(!str)
		aug_abort(file, line, "Attempt to duplicate a NULL string");

	size = strlen(str)+1;
#if augDAB_DEBUG == augTRUE
	DAB("string length %lu [+%d %s]", (unsigned long)size, line, file);
#endif

	new = mem_alloc(size, parent, file, line);

#if augDAB_DEBUG == augTRUE
	DABL(80)("size %lu with header, caller mem at %08lx",
		MEM_CROWN(new)->m.end - (char *)MEM_CROWN(new),
		(unsigned long)new);
#endif

	strcpy(new, str);
	return new;
}

augExport char **aug_vecdup_loc(char **vec, void *parent, char *file, int line)
{
	char **nv, **v, *c;
	size_t size;
	int vsize;
	DABNAME("aug_vecdup");

	if(!vec)
		aug_abort(file, line, "Attempt to duplicate a NULL vector");

	size = 0;
	for(v = vec; *v; v++)
		size += strlen(*v) + 1;
	vsize = v - vec;
#if augDAB_DEBUG == augTRUE
	DABL(80)("%d elements, total string size %d", vsize, size);
#endif

	vsize++;

	nv = (char **)mem_alloc(vsize * sizeof *v + size, parent, file, line);
	c = (char *)(nv + vsize);
	for(v = nv; *vec; v++, vec++)
	{
		strcpy(c, *vec);
		*v = c;
		c += strlen(c) + 1;
	}
	*v = 0;

	return nv;
}

#ifdef TEST
static void nomem(size_t size, char *func, char *file, int line)
{
	fprintf(stderr, "\nNOMEM on %lu bytes via %s, called from %s line %d\n",
		(unsigned long)size, func, file, line);
	/*
	**  Normally would exit from here, but might as well test the
	**  default trap.
	*/
	return;
}

main(int argc, char **argv)
{
	int i;
	void *par;
	char *mem, *m, **v;

	aug_setmodule(argv[0]);

	printf("<MemHead size %lu> should equal <struct MemHead %lu>\n",
		sizeof (MemHead), sizeof (struct MemHead));

	par = aug_alloc(20, 0);
	for(i = 0; i < 20; i++)
	{
		if(i == 10)
			mem = aug_strdup("Hello, world\n", par);
		else
			(void)aug_alloc(3000, par);
	}

	mem = aug_realloc(10000, mem);

	for(i = 0; i < 20; i++)
	{
		if(i == 10)
			m = aug_strdup("Hello, world\n", mem);
		else
			(void)aug_alloc(3000, par);
	}

	v = aug_vecdup(argv, par);

	printf("Program args:");
	while(*v)
	{
		printf(" %s", *v++);
		fflush(stdout);
	}
	printf("\n");

	aug_foster(m, par);

	if(argc > 1)
	{
		printf("Checking anti-incest test ... this should abort\n");
		aug_foster(par, mem);
	}

	for(i = 0; i < 20; i++)
	{
		if(i == 10)
			m = aug_strdup("Hello, world\n", mem);
		else
			(void)aug_alloc(3000, mem);
	}

	mem = aug_realloc(10000, mem);

	aug_foster(m, par);

	aug_free(mem);

	printf("If you can read this, the test completed ok\n");
	printf("Now testing NOMEM func - this should abort after a while ... ");
	fflush(stdout);

	aug_set_nomem_func(nomem);

	while(m = aug_alloc(128*1024, par))
		continue;

	/* Should never get to here */

	aug_free(par);

	aug_exit(augEXIT_YES);
}
#endif