Something went wrong. Try again.
forked from cz-nic/bird
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469/* * BIRD Resource Manager * * (c) 1998--2000 Martin Mares <mj@ucw.cz> * (c) 2021 Maria Matejka <mq@jmq.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"
/** * 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. */
struct pool { resource r; list inside; const char *name;};
char invalid_nonzero_pointer[0];
static void pool_dump(struct dump_request *, resource *);static void pool_free(resource *);static resource *pool_lookup(resource *, unsigned long);static struct resmem pool_memsize(resource *P);
static const struct resclass pool_class = { .name = "Pool", .size = sizeof(pool), .free = pool_free, .dump = pool_dump, .lookup = pool_lookup, .memsize = pool_memsize,};
pool root_pool;
/** * rp_new - create a resource pool * @p: parent pool * @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, const char *name){ pool *z = ralloc(p, &pool_class); z->name = name; init_list(&z->inside); return z;}
pool *rp_newf(pool *p, const char *fmt, ...){ pool *z = rp_new(p, NULL);
va_list args; va_start(args, fmt); z->name = mb_vsprintf(p, fmt, args); va_end(args);
return z;}
static voidpool_free(resource *P){ pool *p = (pool *) P; resource *r, *rr;
r = HEAD(p->inside); while (rr = (resource *) r->n.next) { r->class->free(r); xfree(r); r = rr; }}
static voidpool_dump(struct dump_request *dreq, resource *P){ pool *p = (pool *) P; resource *r;
RDUMP("%s\n", p->name); dreq->indent += 3; WALK_LIST(r, p->inside) rdump(dreq, r); dreq->indent -= 3;}
static struct resmempool_memsize(resource *P){ pool *p = (pool *) P; resource *r; struct resmem sum = { .effective = 0, .overhead = sizeof(pool) + ALLOC_OVERHEAD, };
WALK_LIST(r, p->inside) { struct resmem add = rmemsize(r); sum.effective += add.effective; sum.overhead += add.overhead; }
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. */voidrfree(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. */voidrdump(struct dump_request *dreq, void *res){ char x[16]; resource *r = res;
bsprintf(x, "%%%ds%%p ", dreq->indent); RDUMP(x, "", r); if (r) { RDUMP("%s ", r->class->name); r->class->dump(dreq, r); } else RDUMP("NULL\n");}
void page_dump(struct dump_request *req);
void resource_dump(struct dump_request *req){ rdump(req, &root_pool); page_dump(req);}
struct resmemrmemsize(void *res){ resource *r = res; if (!r) return (struct resmem) {}; if (!r->class->memsize) return (struct resmem) { .effective = r->class->size - sizeof(resource), .overhead = ALLOC_OVERHEAD + sizeof(resource), }; 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, const struct resclass *c){ resource *r = xmalloc(c->size); bzero(r, c->size);
r->class = c; if (p) add_tail(&p->inside, &r->n); return r;}
#if 0/** * 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. */voidrlookup(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");}#endif
/** * 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. */voidresource_init(void){ resource_sys_init();
root_pool.r.class = &pool_class; root_pool.name = "Root"; init_list(&root_pool.inside); tmp_init(&root_pool);}
/** * 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; max_align_t data_align[0]; byte data[0];};
static void mbl_free(resource *r UNUSED){}
static void mbl_debug(struct dump_request *dreq, resource *r){ struct mblock *m = (struct mblock *) r;
RDUMP("(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 struct resmemmbl_memsize(resource *r){ struct mblock *m = (struct mblock *) r; return (struct resmem) { .effective = m->size, .overhead = ALLOC_OVERHEAD + sizeof(struct mblock), };}
static const struct resclass mb_class = { .name = "Memory", .size = 0, .free = mbl_free, .dump = mbl_debug, .lookup = mbl_lookup, .memsize = 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[0], (byte *) m);
b = xrealloc(b, sizeof(struct mblock) + size); update_node(&b->r.n); b->size = size; return b->data;}
/** * mb_free - free a memory block * @m: memory block * * mb_free() frees all memory associated with the block @m. */voidmb_free(void *m){ if (!m) return;
struct mblock *b = SKIP_BACK(struct mblock, data[0], (byte *) m); rfree(b);}
#define STEP_UP(x) ((x) + (x)/2 + 4)
voidbuffer_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;}