offline pcap reading working
[netsniff-ng.git] / src / tlsf.h
blobb254cf6eb57766ab2894c6f4256a8226a9c90980
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL.
6 */
8 /*
9 * Two Levels Segregate Fit memory allocator (TLSF), Version 2.4.6
10 * Written by Miguel Masmano Tello <mimastel@doctor.upv.es>
11 * Thanks to Ismael Ripoll for his suggestions and reviews
12 * Copyright (C) 2008, 2007, 2006, 2005, 2004
13 * This code is released using a dual license strategy: GPL/LGPL
14 * You can choose the licence that better fits your requirements.
15 * Released under the terms of the GNU General Public License Version 2.0
16 * Released under the terms of the GNU Lesser General Public License Version 2.1
19 #ifndef TLSF_H
20 #define TLSF_H
22 #include <sys/types.h>
23 #include <string.h>
25 #include "die.h"
26 #include "compiler.h"
27 #include "strlcpy.h"
29 extern size_t init_memory_pool(size_t, void *);
30 extern size_t get_used_size(void *);
31 extern size_t get_max_size(void *);
32 extern void destroy_memory_pool(void *);
33 extern size_t add_new_area(void *, size_t, void *);
34 extern void *malloc_ex(size_t, void *);
35 extern void free_ex(void *, void *);
36 extern void *realloc_ex(void *, size_t, void *);
37 extern void *calloc_ex(size_t, size_t, void *);
39 extern void *tlsf_malloc(size_t size);
40 extern void tlsf_free(void *ptr);
41 extern void *tlsf_realloc(void *ptr, size_t size);
42 extern void *tlsf_calloc(size_t nelem, size_t elem_size);
44 static inline void *xtlsf_malloc(size_t size)
46 void *ptr;
47 if (unlikely(size == 0))
48 panic("xtlsf_malloc: zero size!\n");
49 ptr = tlsf_malloc(size);
50 if (unlikely(!ptr))
51 panic("xtlsf_malloc: out of mem!\n");
52 return ptr;
55 static inline void xtlsf_free(void *ptr)
57 if (unlikely(!ptr))
58 panic("xtlsf_free: freeing NULL ptr!\n");
59 tlsf_free(ptr);
62 static inline char *xtlsf_strdup(const char *str)
64 size_t len;
65 char *cp;
66 len = strlen(str) + 1;
67 cp = xtlsf_malloc(len);
68 strlcpy(cp, str, len);
69 return cp;
72 #endif /* TLSF_H */