aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/allocator.c
blob: ca63f695623e7c65459d30858351208f8500ef7e (plain)
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
/**
 * @file allocator.c
 * 
 * @brief Memory allocation with LEAK_DETECTION support
 * 
 * Thread-save implementation 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.
 */

#include <stddef.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
 
#include "allocator.h"

#ifdef LEAK_DETECTIVE

typedef union memory_hdr_u memory_hdr_t;

union memory_hdr_u {
    struct {
	const char *filename;
	size_t line;
	size_t size_of_memory;
	memory_hdr_t *older, *newer;
    } info;    /* info */
    unsigned long junk;	/* force maximal alignment */
};

/**
 * global list of allocations
 * 
 * thread-save through mutex
 */
static memory_hdr_t *allocations = NULL;

/**
 * Mutex to ensure, all functions are thread-save
 */
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * described in header
 */ 
void * allocate(size_t bytes, char * file,int line)
{
    memory_hdr_t *allocated_memory = malloc(sizeof(memory_hdr_t) + bytes);

    if (allocated_memory == NULL)
    {
		return allocated_memory;
    }
    
    pthread_mutex_lock( &mutex);
    
    allocated_memory->info.line = line;
    allocated_memory->info.filename = file;
    allocated_memory->info.size_of_memory = bytes;
    allocated_memory->info.older = allocations;
    if (allocations != NULL)
    {
		allocations->info.newer = allocated_memory;
    }
    allocations = allocated_memory;
    allocated_memory->info.newer = NULL;

	/* fill memory with zero's */
    memset(allocated_memory+1, '\0', bytes);
    pthread_mutex_unlock( &mutex);
    /* real memory starts after header */
    return (allocated_memory+1);
}

/*
 * described in header
 */ 
void free_pointer(void * pointer)
{
    memory_hdr_t *allocated_memory;

    if (pointer == NULL)
    {
	    	return;	
    }
	pthread_mutex_lock( &mutex);
    allocated_memory = ((memory_hdr_t *)pointer) - 1;

    if (allocated_memory->info.older != NULL)
    {
		assert(allocated_memory->info.older->info.newer == allocated_memory);
		allocated_memory->info.older->info.newer = allocated_memory->info.newer;
    }
    if (allocated_memory->info.newer == NULL)
    {
		assert(allocated_memory == allocations);
		allocations = allocated_memory->info.older;
    }
    else
    {
		assert(allocated_memory->info.newer->info.older == allocated_memory);
		allocated_memory->info.newer->info.older = allocated_memory->info.older;
    }
    pthread_mutex_unlock( &mutex);
    free(allocated_memory);
}

/*
 * described in header
 */ 
void * reallocate(void * old, size_t bytes, char * file,int line)
{
    memory_hdr_t *allocated_memory;

    if (old == NULL)
    {
	    	return NULL;
    }
	pthread_mutex_lock( &mutex);
    allocated_memory = ((memory_hdr_t *)old) - 1;
    
	void *new_space = allocate(bytes,file,line);
	if (new_space == NULL)
	{
		free_pointer(old);
	    pthread_mutex_unlock( &mutex);
		return NULL;
	}
	
	memcpy(new_space,old,allocated_memory->info.size_of_memory);
    pthread_mutex_unlock( &mutex);
	
	return new_space;
}


/*
 * described in header
 */ 
void report_memory_leaks(void)
{
    memory_hdr_t *p = allocations,
    				 *pprev = NULL;
    unsigned long n = 0;
	pthread_mutex_lock( &mutex);

    while (p != NULL)
    {
	assert(pprev == p->info.newer);
	pprev = p;
	p = p->info.older;
	n++;
	if (p == NULL || pprev->info.filename != p->info.filename)
	{
	    if (n != 1)
		fprintf(stderr,"leak: %lu * File %s, Line %d\n", n, pprev->info.filename,pprev->info.line);
	    else
		fprintf(stderr,"leak: File %s, Line %d\n", pprev->info.filename,pprev->info.line);
	    n = 0;
	}
    }
    pthread_mutex_unlock( &mutex);
}

#endif