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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
/*
* BIRD Resource Manager
*
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "nest/bird.h"
#include "lib/resource.h"
#include "lib/string.h"
#include "lib/rcu.h"
#include "lib/io-loop.h"
/**
* DOC: Resource pools
*
* Resource pools (&pool) are just containers holding a list of
* other resources. Freeing a pool causes all the listed resources
* to be freed as well. Each existing &resource is linked to some pool
* except for a root pool which isn't linked anywhere, so all the
* resources form a tree structure with internal nodes corresponding
* to pools and leaves being the other resources.
*
* Example: Almost all modules of BIRD have their private pool which
* is freed upon shutdown of the module.
*/
static void pool_dump(resource *);
static void pool_free(resource *);
static resource *pool_lookup(resource *, unsigned long);
static size_t pool_memsize(resource *P);
static struct resclass pool_class = {
"Pool",
sizeof(pool),
pool_free,
pool_dump,
pool_lookup,
pool_memsize
};
pool root_pool;
static int indent;
/**
* rp_new - create a resource pool
* @p: parent pool
* @l: loop to assign
* @name: pool name (to be included in debugging dumps)
*
* rp_new() creates a new resource pool inside the specified
* parent pool.
*/
pool *
rp_new(pool *p, struct birdloop *loop, const char *name)
{
ASSERT_DIE(birdloop_inside(p->loop));
ASSERT_DIE(birdloop_inside(loop));
pool *z = ralloc(p, &pool_class);
z->loop = loop;
z->name = name;
init_list(&z->inside);
return z;
}
_Thread_local static pool *pool_parent = NULL;
static void
pool_free(resource *P)
{
ASSERT_DIE(pool_parent);
pool *p = (pool *) P;
ASSERT_DIE(birdloop_inside(p->loop));
pool *parent = pool_parent;
pool_parent = p;
resource *r, *rr;
r = HEAD(p->inside);
while (rr = (resource *) r->n.next)
{
r->class->free(r);
xfree(r);
r = rr;
}
pool_parent = parent;
}
void
rp_free(pool *p, pool *parent)
{
ASSERT_DIE(pool_parent == NULL);
pool_parent = parent;
rfree(p);
ASSERT_DIE(pool_parent == parent);
pool_parent = NULL;
}
static void
pool_dump_locked(pool *p)
{
resource *r;
debug("%s\n", p->name);
indent += 3;
WALK_LIST(r, p->inside)
rdump(r);
indent -= 3;
}
static void
pool_dump(resource *P)
{
pool *p = (pool *) P;
if (p->loop != pool_parent->loop)
birdloop_enter(p->loop);
pool *parent = pool_parent;
pool_parent = p;
pool_dump_locked(p);
pool_parent = parent;
if (p->loop != pool_parent->loop)
birdloop_leave(p->loop);
}
void
rp_dump(pool *p)
{
int inside = birdloop_inside(p->loop);
if (!inside)
birdloop_enter(p->loop);
ASSERT_DIE(pool_parent == NULL);
pool_parent = p;
pool_dump_locked(p);
ASSERT_DIE(pool_parent == p);
pool_parent = NULL;
if (!inside)
birdloop_leave(p->loop);
}
static size_t
pool_memsize_locked(pool *p)
{
resource *r;
size_t sum = sizeof(pool) + ALLOC_OVERHEAD;
WALK_LIST(r, p->inside)
sum += rmemsize(r);
return sum;
}
static size_t
pool_memsize(resource *P)
{
pool *p = (pool *) P;
pool *parent = pool_parent;
pool_parent = p;
if (p->loop != parent->loop)
birdloop_enter(p->loop);
size_t sum = pool_memsize_locked(p);
if (p->loop != parent->loop)
birdloop_leave(p->loop);
pool_parent = parent;
return sum;
}
size_t
rp_memsize(pool *p)
{
int inside = birdloop_inside(p->loop);
if (!inside)
birdloop_enter(p->loop);
ASSERT_DIE(pool_parent == NULL);
pool_parent = p;
size_t sum = pool_memsize_locked(p);
ASSERT_DIE(pool_parent == p);
pool_parent = NULL;
if (!inside)
birdloop_leave(p->loop);
return sum;
}
static resource *
pool_lookup(resource *P, unsigned long a)
{
pool *p = (pool *) P;
resource *r, *q;
WALK_LIST(r, p->inside)
if (r->class->lookup && (q = r->class->lookup(r, a)))
return q;
return NULL;
}
/**
* rmove - move a resource
* @res: resource
* @p: pool to move the resource to
*
* rmove() moves a resource from one pool to another.
*/
void rmove(void *res, pool *p)
{
resource *r = res;
if (r)
{
if (r->n.next)
rem_node(&r->n);
add_tail(&p->inside, &r->n);
}
}
/**
* rfree - free a resource
* @res: resource
*
* rfree() frees the given resource and all information associated
* with it. In case it's a resource pool, it also frees all the objects
* living inside the pool.
*
* It works by calling a class-specific freeing function.
*/
void
rfree(void *res)
{
resource *r = res;
if (!r)
return;
if (r->n.next)
rem_node(&r->n);
r->class->free(r);
r->class = NULL;
xfree(r);
}
/**
* rdump - dump a resource
* @res: resource
*
* This function prints out all available information about the given
* resource to the debugging output.
*
* It works by calling a class-specific dump function.
*/
void
rdump(void *res)
{
char x[16];
resource *r = res;
bsprintf(x, "%%%ds%%p ", indent);
debug(x, "", r);
if (r)
{
debug("%s ", r->class->name);
r->class->dump(r);
}
else
debug("NULL\n");
}
size_t
rmemsize(void *res)
{
resource *r = res;
if (!r)
return 0;
if (!r->class->memsize)
return r->class->size + ALLOC_OVERHEAD;
return r->class->memsize(r);
}
/**
* ralloc - create a resource
* @p: pool to create the resource in
* @c: class of the new resource
*
* This function is called by the resource classes to create a new
* resource of the specified class and link it to the given pool.
* Allocated memory is zeroed. Size of the resource structure is taken
* from the @size field of the &resclass.
*/
void *
ralloc(pool *p, struct resclass *c)
{
ASSERT_DIE(p);
ASSERT_DIE(birdloop_inside(p->loop));
resource *r = xmalloc(c->size);
bzero(r, c->size);
r->class = c;
add_tail(&p->inside, &r->n);
return r;
}
/**
* rlookup - look up a memory location
* @a: memory address
*
* This function examines all existing resources to see whether
* the address @a is inside any resource. It's used for debugging
* purposes only.
*
* It works by calling a class-specific lookup function for each
* resource.
*/
void
rlookup(unsigned long a)
{
resource *r;
debug("Looking up %08lx\n", a);
if (r = pool_lookup(&root_pool.r, a))
rdump(r);
else
debug("Not found.\n");
}
/**
* resource_init - initialize the resource manager
*
* This function is called during BIRD startup. It initializes
* all data structures of the resource manager and creates the
* root pool.
*/
void
resource_init(void)
{
rcu_init();
root_pool.r.class = &pool_class;
root_pool.name = "Root";
init_list(&root_pool.inside);
}
/**
* DOC: Memory blocks
*
* Memory blocks are pieces of contiguous allocated memory.
* They are a bit non-standard since they are represented not by a pointer
* to &resource, but by a void pointer to the start of data of the
* memory block. All memory block functions know how to locate the header
* given the data pointer.
*
* Example: All "unique" data structures such as hash tables are allocated
* as memory blocks.
*/
struct mblock {
resource r;
unsigned size;
uintptr_t data_align[0];
byte data[0];
};
static void mbl_free(resource *r UNUSED)
{
}
static void mbl_debug(resource *r)
{
struct mblock *m = (struct mblock *) r;
debug("(size=%d)\n", m->size);
}
static resource *
mbl_lookup(resource *r, unsigned long a)
{
struct mblock *m = (struct mblock *) r;
if ((unsigned long) m->data <= a && (unsigned long) m->data + m->size > a)
return r;
return NULL;
}
static size_t
mbl_memsize(resource *r)
{
struct mblock *m = (struct mblock *) r;
return ALLOC_OVERHEAD + sizeof(struct mblock) + m->size;
}
static struct resclass mb_class = {
"Memory",
0,
mbl_free,
mbl_debug,
mbl_lookup,
mbl_memsize
};
/**
* mb_alloc - allocate a memory block
* @p: pool
* @size: size of the block
*
* mb_alloc() allocates memory of a given size and creates
* a memory block resource representing this memory chunk
* in the pool @p.
*
* Please note that mb_alloc() returns a pointer to the memory
* chunk, not to the resource, hence you have to free it using
* mb_free(), not rfree().
*/
void *
mb_alloc(pool *p, unsigned size)
{
struct mblock *b = xmalloc(sizeof(struct mblock) + size);
b->r.class = &mb_class;
b->r.n = (node) {};
add_tail(&p->inside, &b->r.n);
b->size = size;
return b->data;
}
/**
* mb_allocz - allocate and clear a memory block
* @p: pool
* @size: size of the block
*
* mb_allocz() allocates memory of a given size, initializes it to
* zeroes and creates a memory block resource representing this memory
* chunk in the pool @p.
*
* Please note that mb_allocz() returns a pointer to the memory
* chunk, not to the resource, hence you have to free it using
* mb_free(), not rfree().
*/
void *
mb_allocz(pool *p, unsigned size)
{
void *x = mb_alloc(p, size);
bzero(x, size);
return x;
}
/**
* mb_realloc - reallocate a memory block
* @m: memory block
* @size: new size of the block
*
* mb_realloc() changes the size of the memory block @m to a given size.
* The contents will be unchanged to the minimum of the old and new sizes;
* newly allocated memory will be uninitialized. Contrary to realloc()
* behavior, @m must be non-NULL, because the resource pool is inherited
* from it.
*
* Like mb_alloc(), mb_realloc() also returns a pointer to the memory
* chunk, not to the resource, hence you have to free it using
* mb_free(), not rfree().
*/
void *
mb_realloc(void *m, unsigned size)
{
struct mblock *b = SKIP_BACK(struct mblock, data, m);
b = xrealloc(b, sizeof(struct mblock) + size);
update_node(&b->r.n);
b->size = size;
return b->data;
}
/**
* mb_move - move a memory block
* @m: memory block
* @p: target pool
*
* mb_move() moves the given memory block to another pool in the same way
* as rmove() moves a plain resource.
*/
void
mb_move(void *m, pool *p)
{
struct mblock *b = SKIP_BACK(struct mblock, data, m);
rmove(b, p);
}
/**
* mb_free - free a memory block
* @m: memory block
*
* mb_free() frees all memory associated with the block @m.
*/
void
mb_free(void *m)
{
if (!m)
return;
struct mblock *b = SKIP_BACK(struct mblock, data, m);
rfree(b);
}
#define STEP_UP(x) ((x) + (x)/2 + 4)
void
buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_size)
{
unsigned nsize = MIN(*size, need);
while (nsize < need)
nsize = STEP_UP(nsize);
*buf = mb_realloc(*buf, nsize * item_size);
*size = nsize;
}
|