File: alloc.c

package info (click to toggle)
openswan 1%3A2.4.6%2Bdfsg.2-1.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 25,000 kB
  • ctags: 16,877
  • sloc: ansic: 121,112; sh: 19,782; xml: 9,699; asm: 4,422; perl: 4,087; makefile: 3,367; tcl: 713; exp: 657; yacc: 396; pascal: 328; lex: 289; sed: 265; awk: 124; lisp: 3
file content (208 lines) | stat: -rw-r--r-- 4,513 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
/* misc. universal things
 * Copyright (C) 1998-2001  D. Hugh Redelmeier.
 *
 * This program 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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.
 *
 * RCSID $Id: alloc.c,v 1.2 2004/10/16 23:42:13 mcr Exp $
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>

#include <openswan.h>

#include "constants.h"
#include "oswlog.h"

#define LEAK_DETECTIVE
#include "oswalloc.h"

const chunk_t empty_chunk = { NULL, 0 };

exit_log_func_t exit_log_func;

void set_exit_log_func(exit_log_func_t func)
{
    exit_log_func = func;
}

bool
all_zero(const unsigned char *m, size_t len)
{
    size_t i;

    for (i = 0; i != len; i++)
	if (m[i] != '\0')
	    return FALSE;
    return TRUE;
}

/* memory allocation
 *
 * LEAK_DETECTIVE puts a wrapper around each allocation and maintains
 * a list of live ones.  If a dead one is freed, an assertion MIGHT fail.
 * If the live list is currupted, that will often be detected.
 * In the end, report_leaks() is called, and the names of remaining
 * live allocations are printed.  At the moment, it is hoped, not that
 * the list is empty, but that there will be no surprises.
 *
 * Accepted Leaks:
 * - "struct iface" and "device name" (for "discovered" net interfaces)
 * - "struct event in event_schedule()" (events not associated with states)
 * - "Pluto lock name" (one only, needed until end -- why bother?)
 */

/* this magic number is 3671129837 decimal (623837458 complemented) */
#define LEAK_MAGIC 0xDAD0FEEDul

union mhdr {
    struct {
	const char *name;
	union mhdr *older, *newer;
	unsigned long magic;
    } i;    /* info */
    unsigned long junk;	/* force maximal alignment */
};

static union mhdr *allocs = NULL;

void *alloc_bytes1(size_t size, const char *name, int leak_detective)
{
    union mhdr *p;

    if(leak_detective) {
	p = malloc(sizeof(union mhdr) + size);
    } else {
	p = malloc(size);
    }

    if (p == NULL) {
	if(exit_log_func) {
	    (*exit_log_func)("unable to malloc %lu bytes for %s"
			     , (unsigned long) size, name);
	}
    }

    if(leak_detective) {
	p->i.name = name;
	p->i.older = allocs;
	if (allocs != NULL)
	    allocs->i.newer = p;
	allocs = p;
	p->i.newer = NULL;
	p->i.magic = LEAK_MAGIC;
	return p+1;
    } else {
	return p;
    }
	
}

void
leak_pfree(void *ptr, int leak)
{
    union mhdr *p;

    if(leak) {
	passert(ptr != NULL);
	p = ((union mhdr *)ptr) - 1;
	passert(p->i.magic == LEAK_MAGIC);
	if (p->i.older != NULL)
	    {
		passert(p->i.older->i.newer == p);
		p->i.older->i.newer = p->i.newer;
	    }
	if (p->i.newer == NULL)
	    {
		passert(p == allocs);
		allocs = p->i.older;
	    }
	else
	    {
		passert(p->i.newer->i.older == p);
		p->i.newer->i.older = p->i.older;
	    }
	p->i.magic = ~LEAK_MAGIC;
	free(p);
    } else {
	free(ptr);
    }
}

#ifdef LEAK_DETECTIVE
void
report_leaks(void)
{
    union mhdr
	*p = allocs,
	*pprev = NULL;
    unsigned long n = 0;

    while (p != NULL)
    {
	passert(p->i.magic == LEAK_MAGIC);
	passert(pprev == p->i.newer);
	pprev = p;
	p = p->i.older;
	n++;
	if (p == NULL || pprev->i.name != p->i.name)
	{
	    if (n != 1)
		openswan_log("leak: %lu * %s", n, pprev->i.name);
	    else
		openswan_log("leak: %s", pprev->i.name);
	    n = 0;
	}
    }
}

#endif /* !LEAK_DETECTIVE */

void *alloc_bytes2(size_t size, const char *name, int leak_detective)
{
    void *p = alloc_bytes1(size, name, leak_detective);

    if (p == NULL) {
	if(exit_log_func) {
	    (*exit_log_func)("unable to malloc %lu bytes for %s"
			     , (unsigned long) size, name);
	}
    }
    memset(p, '\0', size);
    return p;
}

void *clone_bytes2(const void *orig, size_t size, const char *name, int leak_detective)
{
    void *p = alloc_bytes1(size, name, leak_detective);

    if (p == NULL) {
	if(exit_log_func) {
	    (*exit_log_func)("unable to malloc %lu bytes for %s"
			     , (unsigned long) size, name);
	}
    }
    memcpy(p, orig, size);
    return p;
}

/*
 * Local Variables:
 * c-basic-offset:4
 * c-style: pluto
 * End:
 */