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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef included_mem_mheap_h
#define included_mem_mheap_h
/* Bootstrap include so that #include <vppinfra/mem.h> can include e.g.
<vppinfra/mheap.h> which depends on <vppinfra/vec.h>. */
#include <vppinfra/vec_bootstrap.h>
#include <vppinfra/error_bootstrap.h>
#include <vppinfra/os.h>
#include <vppinfra/vector.h>
/* Each element in heap is immediately followed by this struct. */
typedef struct
{
/* Number of mheap_size_t words of user data in previous object.
Used to find mheap_elt_t for previous object. */
#if CLIB_VEC64 > 0
u64 prev_n_user_data:63;
/* Used to mark end/start of of doubly-linked list of mheap_elt_t's. */
#define MHEAP_N_USER_DATA_INVALID (0x7fffffffffffffffULL)
#define MHEAP_GROUNDED (~0ULL)
/* Set if previous object is free. */
u64 prev_is_free:1;
/* Number of mheap_size_t words of user data that follow this object. */
u64 n_user_data:63;
/* Set if this object is on free list (and therefore following free_elt
is valid). */
u64 is_free:1;
#else
u32 prev_n_user_data:31;
/* Used to mark end/start of of doubly-linked list of mheap_elt_t's. */
#define MHEAP_N_USER_DATA_INVALID (0x7fffffff)
#define MHEAP_GROUNDED (~0)
/* Set if previous object is free. */
u32 prev_is_free:1;
/* Number of mheap_size_t words of user data that follow this object. */
u32 n_user_data:31;
/* Set if this object is on free list (and therefore following free_elt
is valid). */
u32 is_free:1;
#endif
union
{
#if CLIB_VEC64 > 0
/* For allocated objects: user data follows.
User data is allocated in units of typeof (user_data[0]). */
u64 user_data[0];
/* For free objects, offsets of next and previous free objects of this size;
~0 means end of doubly-linked list.
This is stored in user data (guaranteed to be at least 8 bytes)
but only for *free* objects. */
struct
{
u64 next_uoffset, prev_uoffset;
} free_elt;
#else
/* For allocated objects: user data follows.
User data is allocated in units of typeof (user_data[0]). */
u32 user_data[0];
/* For free objects, offsets of next and previous free objects of this size;
~0 means end of doubly-linked list.
This is stored in user data (guaranteed to be at least 8 bytes)
but only for *free* objects. */
struct
{
u32 next_uoffset, prev_uoffset;
} free_elt;
#endif
};
} mheap_elt_t;
/* Number of bytes of "overhead": e.g. not user data. */
#define MHEAP_ELT_OVERHEAD_BYTES (sizeof (mheap_elt_t) - STRUCT_OFFSET_OF (mheap_elt_t, user_data))
/* User objects must be large enough to hold 2 x u32 free offsets in free elt. */
#define MHEAP_MIN_USER_DATA_BYTES MHEAP_ELT_OVERHEAD_BYTES
/* Number of byte in user data "words". */
#define MHEAP_USER_DATA_WORD_BYTES STRUCT_SIZE_OF (mheap_elt_t, user_data[0])
typedef struct
{
/* Address of callers: outer first, inner last. */
uword callers[12];
/* Count of allocations with this traceback. */
#if CLIB_VEC64 > 0
u64 n_allocations;
#else
u32 n_allocations;
#endif
/* Count of bytes allocated with this traceback. */
u32 n_bytes;
/* Offset of this item */
uword offset;
} mheap_trace_t;
typedef struct
{
mheap_trace_t *traces;
/* Indices of free traces. */
u32 *trace_free_list;
/* Hash table mapping callers to trace index. */
uword *trace_by_callers;
/* Hash table mapping mheap offset to trace index. */
uword *trace_index_by_offset;
} mheap_trace_main_t;
/* Without vector instructions don't bother with small object cache. */
#ifdef CLIB_HAVE_VEC128
#define MHEAP_HAVE_SMALL_OBJECT_CACHE 1
#else
#define MHEAP_HAVE_SMALL_OBJECT_CACHE 0
#endif
/* Small object bin i is for objects with
user_size > sizeof (mheap_elt_t) + sizeof (mheap_elt_t) * (i - 1)
user_size <= sizeof (mheap_elt_t) + sizeof (mheap_size_t) * i. */
#if MHEAP_HAVE_SMALL_OBJECT_CACHE > 0
#define MHEAP_LOG2_N_SMALL_OBJECT_BINS 8
#define MHEAP_N_SMALL_OBJECT_BINS (1 << MHEAP_LOG2_N_SMALL_OBJECT_BINS)
#else
#define MHEAP_LOG2_N_SMALL_OBJECT_BINS 0
#define MHEAP_N_SMALL_OBJECT_BINS 0
#endif
#define MHEAP_N_BINS \
(MHEAP_N_SMALL_OBJECT_BINS \
+ (STRUCT_BITS_OF (mheap_elt_t, user_data[0]) - MHEAP_LOG2_N_SMALL_OBJECT_BINS))
typedef struct
{
struct
{
u64 n_search_attempts;
u64 n_objects_searched;
u64 n_objects_found;
} free_list;
u64 n_vector_expands;
u64 n_small_object_cache_hits;
u64 n_small_object_cache_attempts;
u64 n_gets, n_puts;
u64 n_clocks_get, n_clocks_put;
} mheap_stats_t;
/* For objects with align == 4 and align_offset == 0 (e.g. vector strings). */
typedef struct
{
union
{
#ifdef CLIB_HAVE_VEC128
u8x16 as_u8x16[BITS (uword) / 16];
#endif
/* Store bin + 1; zero means unused. */
u8 as_u8[BITS (uword)];
} bins;
uword offsets[BITS (uword)];
u32 replacement_index;
} mheap_small_object_cache_t;
/* Vec header for heaps. */
typedef struct
{
/* User offsets for head of doubly-linked list of free objects of this size. */
#if CLIB_VEC64 > 0
u64 first_free_elt_uoffset_by_bin[MHEAP_N_BINS];
#else
u32 first_free_elt_uoffset_by_bin[MHEAP_N_BINS];
#endif
/* Bitmap of non-empty free list bins. */
uword non_empty_free_elt_heads[(MHEAP_N_BINS + BITS (uword) - 1) /
BITS (uword)];
mheap_small_object_cache_t small_object_cache;
u32 flags;
#define MHEAP_FLAG_TRACE (1 << 0)
#define MHEAP_FLAG_DISABLE_VM (1 << 1)
#define MHEAP_FLAG_THREAD_SAFE (1 << 2)
#define MHEAP_FLAG_SMALL_OBJECT_CACHE (1 << 3)
#define MHEAP_FLAG_VALIDATE (1 << 4)
/* Lock use when MHEAP_FLAG_THREAD_SAFE is set. */
volatile u32 lock;
volatile u32 owner_cpu;
int recursion_count;
/* Number of allocated objects. */
u64 n_elts;
/* Maximum size (in bytes) this heap is allowed to grow to.
Set to ~0 to grow heap (via vec_resize) arbitrarily. */
u64 max_size;
uword vm_alloc_offset_from_header;
uword vm_alloc_size;
/* Each successful mheap_validate call increments this serial number.
Used to debug heap corruption problems. GDB breakpoints can be
made conditional on validate_serial. */
u64 validate_serial;
mheap_trace_main_t trace_main;
mheap_stats_t stats;
} mheap_t;
always_inline mheap_t *
mheap_header (u8 * v)
{
return vec_aligned_header (v, sizeof (mheap_t), 16);
}
always_inline u8 *
mheap_vector (mheap_t * h)
{
return vec_aligned_header_end (h, sizeof (mheap_t), 16);
}
always_inline uword
mheap_elt_uoffset (void *v, mheap_elt_t * e)
{
return (uword) e->user_data - (uword) v;
}
always_inline mheap_elt_t *
mheap_user_pointer_to_elt (void *v)
{
return v - STRUCT_OFFSET_OF (mheap_elt_t, user_data);
}
/* For debugging we keep track of offsets for valid objects.
We make sure user is not trying to free object with invalid offset. */
always_inline uword
mheap_offset_is_valid (void *v, uword uo)
{
return uo >= MHEAP_ELT_OVERHEAD_BYTES && uo <= vec_len (v);
}
always_inline mheap_elt_t *
mheap_elt_at_uoffset (void *v, uword uo)
{
ASSERT (mheap_offset_is_valid (v, uo));
return (mheap_elt_t *) (v + uo - STRUCT_OFFSET_OF (mheap_elt_t, user_data));
}
always_inline void *
mheap_elt_data (void *v, mheap_elt_t * e)
{
return v + mheap_elt_uoffset (v, e);
}
always_inline uword
mheap_elt_data_bytes (mheap_elt_t * e)
{
return e->n_user_data * sizeof (e->user_data[0]);
}
always_inline uword
mheap_data_bytes (void *v, uword uo)
{
mheap_elt_t *e = mheap_elt_at_uoffset (v, uo);
return mheap_elt_data_bytes (e);
}
#define mheap_len(v,d) (mheap_data_bytes((v),(void *) (d) - (void *) (v)) / sizeof ((d)[0]))
always_inline mheap_elt_t *
mheap_next_elt (mheap_elt_t * e)
{
ASSERT (e->n_user_data < MHEAP_N_USER_DATA_INVALID);
return (mheap_elt_t *) (e->user_data + e->n_user_data);
}
always_inline mheap_elt_t *
mheap_prev_elt (mheap_elt_t * e)
{
ASSERT (e->prev_n_user_data < MHEAP_N_USER_DATA_INVALID);
return ((void *) e
- e->prev_n_user_data * sizeof (e->user_data[0])
- MHEAP_ELT_OVERHEAD_BYTES);
}
/* Exported operations. */
always_inline uword
mheap_elts (void *v)
{
return v ? mheap_header (v)->n_elts : 0;
}
always_inline uword
mheap_max_size (void *v)
{
return v ? mheap_header (v)->max_size : ~0;
}
/* Free previously allocated offset. */
void mheap_put (void *v, uword offset);
/* Allocate object from mheap. */
void *mheap_get_aligned (void *v, uword size, uword align, uword align_offset,
uword * offset_return);
#endif /* included_mem_mheap_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|