- updated Makefiles ( gcc-3.0 specific options, arch. specific defines section,
[sip-router] / mem / q_malloc.h
1 /* $Id$
2  *
3  * simple & fast malloc library
4  */
5
6 #if !defined(q_malloc_h) && !defined(VQ_MALLOC) && !defined(F_MALLOC)
7 #define q_malloc_h
8
9
10
11 /* defs*/
12
13 #define ROUNDTO         16 /* size we round to, must be = 2^n  and also
14                                                  sizeof(qm_frag)+sizeof(qm_frag_end)
15                                                  must be mutliple of ROUNDTO!
16                                            */
17 #define MIN_FRAG_SIZE   ROUNDTO
18
19
20
21 #define QM_MALLOC_OPTIMIZE_FACTOR 10 /*used below */
22 #define QM_MALLOC_OPTIMIZE  (1<<QM_MALLOC_OPTIMIZE_FACTOR)
23                                                                 /* size to optimize for,
24                                                                         (most allocs < this size),
25                                                                         must be 2^k */
26
27 #define QM_HASH_SIZE (QM_MALLOC_OPTIMIZE/ROUNDTO + \
28                 (32-QM_MALLOC_OPTIMIZE_FACTOR)+1)
29
30 /* hash structure:
31  * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO  - small buckets, size increases with
32  *                            ROUNDTO from bucket to bucket
33  * +1 .... end -  size = 2^k, big buckets */
34
35 struct qm_frag{
36         unsigned int size;
37         union{
38                 struct qm_frag* nxt_free;
39                 int is_free;
40         }u;
41 #ifdef DBG_QM_MALLOC
42         char* file;
43         char* func;
44         unsigned int line;
45         unsigned int check;
46 #endif
47 };
48
49 struct qm_frag_end{
50 #ifdef DBG_QM_MALLOC
51         unsigned int check1;
52         unsigned int check2;
53         unsigned int reserved1;
54         unsigned int reserved2;
55 #endif
56         unsigned int size;
57         struct qm_frag* prev_free;
58 };
59
60
61
62 struct qm_frag_full{
63         struct qm_frag head;
64         struct qm_frag_end tail;
65 };
66
67
68 struct qm_block{
69         unsigned int size; /* total size */
70         unsigned int used; /* alloc'ed size*/
71         unsigned int real_used; /* used+malloc overhead*/
72         unsigned int max_real_used;
73         
74         struct qm_frag* first_frag;
75         struct qm_frag_end* last_frag_end;
76         
77         struct qm_frag_full free_hash[QM_HASH_SIZE];
78         /*struct qm_frag_end free_lst_end;*/
79 };
80
81
82
83 struct qm_block* qm_malloc_init(char* address, unsigned int size);
84
85 #ifdef DBG_QM_MALLOC
86 void* qm_malloc(struct qm_block*, unsigned int size, char* file, char* func, 
87                                         unsigned int line);
88 #else
89 void* qm_malloc(struct qm_block*, unsigned int size);
90 #endif
91
92 #ifdef DBG_QM_MALLOC
93 void  qm_free(struct qm_block*, void* p, char* file, char* func, 
94                                 unsigned int line);
95 #else
96 void  qm_free(struct qm_block*, void* p);
97 #endif
98
99 void  qm_status(struct qm_block*);
100
101
102 #endif