forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_malloc.h
178 lines (151 loc) · 4.91 KB
/
sf_malloc.h
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
/* $Id$
*
* shared memory, multi-process safe, pool based version of f_malloc
*
* Copyright (C) 2007 iptelorg GmbH
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* History:
* --------
* 2003-05-21 on sparc64 roundto 8 even in debugging mode (so malloc'ed
* long longs will be 64 bit aligned) (andrei)
* 2004-07-19 support for 64 bit (2^64 mem. block) and more info
* for the future de-fragmentation support (andrei)
* 2004-11-10 support for > 4Gb mem., switched to long (andrei)
* 2007-06-09 forked from the f_malloc code (andrei)
*/
#if !defined(sf_malloc_h)
#define sf_malloc_h
#include "meminfo.h"
#include "../lock_ops.h"
#include "../atomic_ops.h"
#include "../compiler_opt.h"
/* defs*/
#ifdef GEN_LOCK_T_UNLIMITED
#define SFM_LOCK_PER_BUCKET
#else
#define SFM_ONE_LOCK
#endif
#ifdef DBG_SF_MALLOC
#if defined(__CPU_sparc64) || defined(__CPU_sparc)
/* tricky, on sun in 32 bits mode long long must be 64 bits aligned
* but long can be 32 bits aligned => malloc should return long long
* aligned memory */
#define SF_ROUNDTO sizeof(long long)
#else
#define SF_ROUNDTO sizeof(void*) /* size we round to, must be = 2^n, and
sizeof(sfm_frag) must be multiple of SF_ROUNDTO !*/
#endif
#else /* DBG_SF_MALLOC */
#define SF_ROUNDTO 8UL
#endif
#define SF_MIN_FRAG_SIZE SF_ROUNDTO
#define SFM_POOLS_NO 4U /* the more the better, but higher initial
mem. consumption */
#define SF_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
#define SF_MALLOC_OPTIMIZE (1UL<<SF_MALLOC_OPTIMIZE_FACTOR)
/* size to optimize for,
(most allocs <= this size),
must be 2^k */
#define SF_HASH_POOL_SIZE (SF_MALLOC_OPTIMIZE/SF_ROUNDTO + 1)
#define SF_POOL_MAX_SIZE SF_MALLOC_OPTIMIZE
#define SF_HASH_SIZE (SF_MALLOC_OPTIMIZE/SF_ROUNDTO + \
(sizeof(long)*8-SF_MALLOC_OPTIMIZE_FACTOR)+1)
/* hash structure:
* 0 .... SF_MALLOC_OPTIMIZE/SF_ROUNDTO - small buckets, size increases with
* SF_ROUNDTO from bucket to bucket
* +1 .... end - size = 2^k, big buckets */
struct sfm_frag{
union{
struct sfm_frag* nxt_free;
long reserved;
}u;
unsigned long size;
unsigned long id; /* TODO better optimize the size */
/* pad to SF_ROUNDTO multiple */
char _pad[((3*sizeof(long)+SF_ROUNDTO-1)&~(SF_ROUNDTO-1))-3*sizeof(long)];
#ifdef DBG_SF_MALLOC
const char* file;
const char* func;
unsigned long line;
unsigned long check;
#endif
};
struct sfm_frag_lnk{
struct sfm_frag* first;
#ifdef SFM_LOCK_PER_BUCKET
gen_lock_t lock;
#endif
unsigned long no;
};
struct sfm_pool_head{
struct sfm_frag* first;
#ifdef SFM_LOCK_PER_BUCKET
gen_lock_t lock;
#endif
unsigned long no;
unsigned long misses;
};
struct sfm_pool{
#ifdef SFM_ONE_LOCK
gen_lock_t lock;
#endif
unsigned long missed;
unsigned long hits; /* debugging only TODO: remove */
unsigned long bitmap;
struct sfm_pool_head pool_hash[SF_HASH_POOL_SIZE];
};
struct sfm_block{
#ifdef SFM_ONE_LOCK
gen_lock_t lock;
#endif
atomic_t crt_id; /* current pool */
int type;
unsigned long size; /* total size */
/* stats are kept now per bucket */
struct sfm_frag* first_frag;
struct sfm_frag* last_frag;
unsigned long bitmap; /* only up to SF_MALLOC_OPTIMIZE */
struct sfm_frag_lnk free_hash[SF_HASH_SIZE];
struct sfm_pool pool[SFM_POOLS_NO];
int is_init;
gen_lock_t get_and_split;
char _pad[256];
};
struct sfm_block* sfm_malloc_init(char* address, unsigned long size, int type);
void sfm_malloc_destroy(struct sfm_block* qm);
int sfm_pool_reset();
#ifdef DBG_SF_MALLOC
void* sfm_malloc(struct sfm_block*, unsigned long size,
const char* file, const char* func, unsigned int line);
#else
void* sfm_malloc(struct sfm_block*, unsigned long size);
#endif
#ifdef DBG_SF_MALLOC
void sfm_free(struct sfm_block*, void* p, const char* file, const char* func,
unsigned int line);
#else
void sfm_free(struct sfm_block*, void* p);
#endif
#ifdef DBG_SF_MALLOC
void* sfm_realloc(struct sfm_block*, void* p, unsigned long size,
const char* file, const char* func, unsigned int line);
#else
void* sfm_realloc(struct sfm_block*, void* p, unsigned long size);
#endif
void sfm_status(struct sfm_block*);
void sfm_info(struct sfm_block*, struct mem_info*);
unsigned long sfm_available(struct sfm_block*);
#endif