aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils/allocator.c
blob: a56369f95a0e2cc8cd430ff7ebd65999644339a5 (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
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
/**
 * @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_t memory_hdr_t;

/**
 * Header of each allocated memory area
 * 
 * Used to detect memory leaks
 */
union memory_hdr_t {
    struct {
    	/**
    	 * Filename withing memory was allocated
    	 */
	const char *filename;
	/**
	 * Line number in given file
	 */
	size_t line;
	/**
	 * Allocated memory size. Needed for reallocation
	 */
	size_t size_of_memory;
	/**
	 * Link to the previous and next memory area
	 */
	memory_hdr_t *older, *newer;
    } info;    /* info */
    /**
     * force maximal alignment ?
     */
    unsigned long junk;	
};

/**
 * @brief Private allocator_t object.
 * 
 * Contains private variables of allocator_t object.
 */
typedef struct private_allocator_t private_allocator_t;

struct private_allocator_t
{
	/**
	 * Public part of an allocator_t object.
	 */
	allocator_t public;
	
	/**
	 * Global list of allocations
	 * 
	 * Thread-save through mutex
	 */
	memory_hdr_t *allocations;

	/**
	 * Mutex used to make sure, all functions are thread-save
	 */
	pthread_mutex_t mutex;
	
	/**
	 * Allocates memory with LEAK_DETECTION and 
	 * returns an empty data area filled with zeros.
	 *
	 * @param this 		private_allocator_t object
	 * @param bytes 		number of bytes to allocate
	 * @param file 		filename from which the memory is allocated
	 * @param line 		line number in specific file
	 * @param use_mutex If FALSE no mutex is used for allocation
	 * @return 		
	 * 				- pointer to allocated memory area if successful
	 * 				- NULL otherwise
	 */ 
	void * (*allocate_special) (private_allocator_t *this,size_t bytes, char * file,int line, bool use_mutex);
};

/**
 * Implements private_allocator_t's function allocate_special. 
 * See #private_allocator_s.allocate_special for description.
 */
static void *allocate_special(private_allocator_t *this,size_t bytes, char * file,int line, bool use_mutex)
{
    memory_hdr_t *allocated_memory = malloc(sizeof(memory_hdr_t) + bytes);;
  
	if (allocated_memory == NULL)
    {
		return allocated_memory;
    }

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

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

/**
 * Implements allocator_t's function allocate. 
 * See #allocator_s.allocate for description.
 */
static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line)
{
	private_allocator_t *this = (private_allocator_t *) allocator;
	return (this->allocate_special(this,bytes, file,line,TRUE));
}

/**
 * Implements allocator_t's function allocate_as_chunk. 
 * See #allocator_s.allocate_as_chunk for description.
 */
static chunk_t allocate_as_chunk(allocator_t *allocator,size_t bytes, char * file,int line)
{
	private_allocator_t *this = (private_allocator_t *) allocator;
	chunk_t new_chunk;
	new_chunk.ptr = this->allocate_special(this,bytes, file,line,TRUE);
	new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes;
	return new_chunk;
}

/*
 * Implements allocator_t's free_pointer function. 
 * See #allocator_s.free_pointer for description.
 */
static void free_pointer(allocator_t *allocator, void * pointer)
{
	private_allocator_t *this = (private_allocator_t *) allocator;
    memory_hdr_t *allocated_memory;

    if (pointer == NULL)
    {
	    	return;	
    }
	pthread_mutex_lock( &(this->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 == this->allocations);
		this->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(&(this->mutex));
    free(allocated_memory);
}

/*
 * Implements allocator_t's reallocate function. 
 * See #allocator_s.reallocate for description.
 */
static void * reallocate(allocator_t *allocator, void * old, size_t bytes, char * file,int line)
{
	private_allocator_t *this = (private_allocator_t *) allocator;
    memory_hdr_t *allocated_memory;

    if (old == NULL)
    {
	    	return NULL;
    }

	pthread_mutex_lock( &(this->mutex));
    allocated_memory = ((memory_hdr_t *)old) - 1;
    
	void *new_space = this->allocate_special(this,bytes,file,line,FALSE);

	if (new_space == NULL)
	{
	    pthread_mutex_unlock(&(this->mutex));
		this->public.free_pointer(&(this->public),old);
		return NULL;
	}
	
	
	/* the smaller size is copied to avoid overflows */
	memcpy(new_space,old,(allocated_memory->info.size_of_memory < bytes) ? allocated_memory->info.size_of_memory : bytes);
    pthread_mutex_unlock(&(this->mutex));
    this->public.free_pointer(&(this->public),old);
	
	return new_space;
}

/*
 * Implements allocator_t's clone_bytes function. 
 * See #allocator_s.clone_bytes for description.
 */
static void * clone_bytes(allocator_t *allocator,void * to_clone, size_t bytes, char * file, int line)
{
	private_allocator_t *this = (private_allocator_t *) allocator;

    if (to_clone == NULL)
    {
	    	return NULL;
    }

    
	void *new_space = this->allocate_special(this,bytes,file,line,TRUE);

	if (new_space == NULL)
	{
		return NULL;
	}
	
	memcpy(new_space,to_clone,bytes);
	
	return new_space;
}

/*
 * Implements allocator_t's report_memory_leaks allocate. 
 * See #allocator_s.report_memory_leaks for description.
 */
static void allocator_report_memory_leaks(allocator_t *allocator)
{
	private_allocator_t *this = (private_allocator_t *) allocator;
    memory_hdr_t *p = this->allocations;
    memory_hdr_t *pprev = NULL;
    unsigned long n = 0;

	pthread_mutex_lock(&(this->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: \"%s, Line %d\"\n", pprev->info.filename,pprev->info.line);
	    n = 0;
	}
    }
    pthread_mutex_unlock( &(this->mutex));
}

/** 
 * Only initiation of allocator object.
 * 
 * All allocation macros use this object.
 */
static private_allocator_t allocator = {
	public: {allocate: allocate,
	  		 allocate_as_chunk: allocate_as_chunk,
			 free_pointer: free_pointer,
			 reallocate: reallocate,
			 clone_bytes : clone_bytes,
 			 report_memory_leaks: allocator_report_memory_leaks},
	allocations: NULL,
	allocate_special : allocate_special,
	mutex: PTHREAD_MUTEX_INITIALIZER
};

allocator_t *global_allocator = &(allocator.public);
#else /* !LEAK_DETECTION */


chunk_t allocator_alloc_as_chunk(size_t bytes)
{
	chunk_t new_chunk;
	new_chunk.ptr = malloc(bytes); 
	new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes; 
	return new_chunk; 

}

void * allocator_realloc(void * old, size_t newsize)
{
	void *data = realloc(old,newsize);
	return data;
} 

void * allocator_clone_bytes(void * pointer, size_t size)
{
	
	void *data;
	data = malloc(size);
	
	if (data == NULL){return NULL;}
	memmove(data,pointer,size);
	
	return (data);
}


void allocator_free_chunk(chunk_t chunk)
{
	free(chunk.ptr);		
}


#endif /* LEAK_DETECTION */