1998-05-03 16:43:39 +00:00
|
|
|
/*
|
|
|
|
* BIRD Resource Manager
|
|
|
|
*
|
2000-06-05 11:41:41 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
2021-11-26 23:21:12 +00:00
|
|
|
* (c) 2021 Maria Matejka <mq@jmq.cz>
|
1998-05-03 16:43:39 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-09-17 15:52:36 +00:00
|
|
|
#include <stdint.h>
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
2000-03-31 23:30:21 +00:00
|
|
|
#include "lib/string.h"
|
2021-11-12 21:58:40 +00:00
|
|
|
#include "lib/rcu.h"
|
1998-05-03 16:43:39 +00:00
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-28 09:42:47 +00:00
|
|
|
static void pool_dump(resource *, unsigned);
|
1998-05-24 14:46:20 +00:00
|
|
|
static void pool_free(resource *);
|
2000-05-08 22:33:38 +00:00
|
|
|
static resource *pool_lookup(resource *, unsigned long);
|
2021-11-26 23:21:12 +00:00
|
|
|
static struct resmem pool_memsize(resource *P);
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
static struct resclass pool_class = {
|
|
|
|
"Pool",
|
|
|
|
sizeof(pool),
|
|
|
|
pool_free,
|
2000-05-08 22:33:38 +00:00
|
|
|
pool_dump,
|
2010-06-02 20:20:40 +00:00
|
|
|
pool_lookup,
|
|
|
|
pool_memsize
|
1998-05-03 16:43:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pool root_pool;
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
static void
|
|
|
|
rp_init(pool *z, struct domain_generic *dom, const char *name)
|
|
|
|
{
|
|
|
|
ASSERT_DIE(DG_IS_LOCKED(dom));
|
|
|
|
|
2023-04-24 14:10:59 +00:00
|
|
|
if (name && !domain_name(dom))
|
|
|
|
domain_setup(dom, name, z);
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
z->name = name;
|
|
|
|
z->domain = dom;
|
|
|
|
z->inside = (TLIST_LIST(resource)) {};
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
pool *
|
2023-04-21 13:26:06 +00:00
|
|
|
rp_new(pool *p, struct domain_generic *dom, const char *name)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
pool *z = ralloc(p, &pool_class);
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
if (dg_order(p->domain) > dg_order(dom))
|
2023-11-09 16:42:22 +00:00
|
|
|
bug("Requested reverse order pool creation: %s (%s, order %d) can't be a parent of %s (%s, order %d)",
|
|
|
|
p->name, domain_name(p->domain), dg_order(p->domain),
|
|
|
|
name, domain_name(dom), dg_order(dom));
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
if ((dg_order(p->domain) == dg_order(dom)) && (p->domain != dom))
|
2023-11-09 16:42:22 +00:00
|
|
|
bug("Requested incomparable order pool creation: %s (%s, order %d) can't be a parent of %s (%s, order %d)",
|
|
|
|
p->name, domain_name(p->domain), dg_order(p->domain),
|
|
|
|
name, domain_name(dom), dg_order(dom));
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
rp_init(z, dom, name);
|
1998-05-03 16:43:39 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2022-03-15 10:21:46 +00:00
|
|
|
pool *
|
2023-04-21 13:26:06 +00:00
|
|
|
rp_vnewf(pool *p, struct domain_generic *dom, const char *fmt, va_list args)
|
2022-03-15 10:21:46 +00:00
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
pool *z = rp_new(p, dom, NULL);
|
2023-04-24 14:10:59 +00:00
|
|
|
z->name = mb_vsprintf(z, fmt, args);
|
|
|
|
if (!domain_name(dom))
|
|
|
|
domain_setup(dom, z->name, z);
|
2023-04-22 19:20:19 +00:00
|
|
|
return z;
|
|
|
|
}
|
2022-03-15 10:21:46 +00:00
|
|
|
|
2023-04-22 19:20:19 +00:00
|
|
|
pool *
|
2023-04-21 13:26:06 +00:00
|
|
|
rp_newf(pool *p, struct domain_generic *dom, const char *fmt, ...)
|
2023-04-22 19:20:19 +00:00
|
|
|
{
|
2022-03-15 10:21:46 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2023-04-21 13:26:06 +00:00
|
|
|
pool *z = rp_vnewf(p, dom, fmt, args);
|
2022-03-15 10:21:46 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
#define POOL_LOCK \
|
|
|
|
struct domain_generic *dom = p->domain; \
|
|
|
|
int locking = !DG_IS_LOCKED(dom); \
|
|
|
|
if (locking) \
|
|
|
|
DG_LOCK(dom); \
|
|
|
|
|
|
|
|
#define POOL_UNLOCK if (locking) DG_UNLOCK(dom);\
|
|
|
|
|
|
|
|
void rp_free(pool *p)
|
|
|
|
{
|
|
|
|
ASSERT_DIE(DG_IS_LOCKED(p->domain));
|
|
|
|
rfree(p);
|
|
|
|
}
|
2022-03-15 10:21:46 +00:00
|
|
|
|
1998-05-24 14:46:20 +00:00
|
|
|
static void
|
1998-05-03 16:43:39 +00:00
|
|
|
pool_free(resource *P)
|
|
|
|
{
|
|
|
|
pool *p = (pool *) P;
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
POOL_LOCK;
|
|
|
|
WALK_TLIST_DELSAFE(resource, r, &p->inside)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
r->class->free(r);
|
|
|
|
xfree(r);
|
|
|
|
}
|
2023-04-21 13:26:06 +00:00
|
|
|
POOL_UNLOCK;
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
|
1998-05-24 14:46:20 +00:00
|
|
|
static void
|
2023-02-28 09:42:47 +00:00
|
|
|
pool_dump(resource *P, unsigned indent)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
pool *p = (pool *) P;
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
POOL_LOCK;
|
1998-05-03 16:43:39 +00:00
|
|
|
|
1999-03-29 20:14:33 +00:00
|
|
|
debug("%s\n", p->name);
|
2023-04-21 13:26:06 +00:00
|
|
|
WALK_TLIST_DELSAFE(resource, r, &p->inside)
|
2023-02-28 09:42:47 +00:00
|
|
|
rdump(r, indent + 3);
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
POOL_UNLOCK;
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 23:21:12 +00:00
|
|
|
static struct resmem
|
2010-06-02 20:20:40 +00:00
|
|
|
pool_memsize(resource *P)
|
|
|
|
{
|
|
|
|
pool *p = (pool *) P;
|
2021-11-26 23:21:12 +00:00
|
|
|
struct resmem sum = {
|
|
|
|
.effective = 0,
|
|
|
|
.overhead = sizeof(pool) + ALLOC_OVERHEAD,
|
|
|
|
};
|
2010-06-02 20:20:40 +00:00
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
POOL_LOCK;
|
|
|
|
|
|
|
|
WALK_TLIST(resource, r, &p->inside)
|
2021-11-26 23:21:12 +00:00
|
|
|
{
|
|
|
|
struct resmem add = rmemsize(r);
|
|
|
|
sum.effective += add.effective;
|
|
|
|
sum.overhead += add.overhead;
|
|
|
|
}
|
2010-06-02 20:20:40 +00:00
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
POOL_UNLOCK;
|
|
|
|
|
2010-06-02 20:20:40 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2000-05-08 22:33:38 +00:00
|
|
|
static resource *
|
|
|
|
pool_lookup(resource *P, unsigned long a)
|
|
|
|
{
|
|
|
|
pool *p = (pool *) P;
|
2023-04-21 13:26:06 +00:00
|
|
|
resource *q = NULL;
|
|
|
|
|
|
|
|
POOL_LOCK;
|
2000-05-08 22:33:38 +00:00
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
WALK_TLIST(resource, r, &p->inside)
|
2000-05-08 22:33:38 +00:00
|
|
|
if (r->class->lookup && (q = r->class->lookup(r, a)))
|
2023-04-21 13:26:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
POOL_UNLOCK;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2004-05-31 18:47:19 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2023-04-21 13:26:06 +00:00
|
|
|
pool *orig = resource_parent(r);
|
2004-05-31 18:47:19 +00:00
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(orig->domain));
|
|
|
|
ASSERT_DIE(DG_IS_LOCKED(p->domain));
|
|
|
|
|
|
|
|
resource_rem_node(&orig->inside, r);
|
|
|
|
resource_add_tail(&p->inside, r);
|
2004-05-31 18:47:19 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void
|
|
|
|
rfree(void *res)
|
|
|
|
{
|
|
|
|
resource *r = res;
|
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
if (!r)
|
|
|
|
return;
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
pool *orig = resource_parent(r);
|
|
|
|
ASSERT_DIE(DG_IS_LOCKED(orig->domain));
|
|
|
|
resource_rem_node(&orig->inside, r);
|
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
r->class->free(r);
|
2016-03-22 12:35:40 +00:00
|
|
|
r->class = NULL;
|
2014-02-06 16:46:01 +00:00
|
|
|
xfree(r);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void
|
2023-02-28 09:42:47 +00:00
|
|
|
rdump(void *res, unsigned indent)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
char x[16];
|
|
|
|
resource *r = res;
|
|
|
|
|
2008-12-25 10:55:27 +00:00
|
|
|
bsprintf(x, "%%%ds%%p ", indent);
|
|
|
|
debug(x, "", r);
|
1998-05-03 16:43:39 +00:00
|
|
|
if (r)
|
|
|
|
{
|
1999-03-29 20:14:33 +00:00
|
|
|
debug("%s ", r->class->name);
|
2023-02-28 09:42:47 +00:00
|
|
|
r->class->dump(r, indent);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
debug("NULL\n");
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:21:12 +00:00
|
|
|
struct resmem
|
2010-06-02 20:20:40 +00:00
|
|
|
rmemsize(void *res)
|
|
|
|
{
|
|
|
|
resource *r = res;
|
|
|
|
if (!r)
|
2021-11-26 23:21:12 +00:00
|
|
|
return (struct resmem) {};
|
2010-06-02 20:20:40 +00:00
|
|
|
if (!r->class->memsize)
|
2021-11-26 23:21:12 +00:00
|
|
|
return (struct resmem) {
|
|
|
|
.effective = r->class->size - sizeof(resource),
|
|
|
|
.overhead = ALLOC_OVERHEAD + sizeof(resource),
|
|
|
|
};
|
2010-06-02 20:20:40 +00:00
|
|
|
return r->class->memsize(r);
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2009-09-04 09:24:08 +00:00
|
|
|
* Allocated memory is zeroed. Size of the resource structure is taken
|
|
|
|
* from the @size field of the &resclass.
|
2000-06-05 11:41:41 +00:00
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void *
|
|
|
|
ralloc(pool *p, struct resclass *c)
|
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(p->domain));
|
|
|
|
|
1998-05-03 16:43:39 +00:00
|
|
|
resource *r = xmalloc(c->size);
|
2009-09-04 09:24:08 +00:00
|
|
|
bzero(r, c->size);
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
r->class = c;
|
2023-04-21 13:26:06 +00:00
|
|
|
resource_add_tail(&p->inside, r);
|
|
|
|
|
1998-05-03 16:43:39 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-05-08 22:33:38 +00:00
|
|
|
void
|
|
|
|
rlookup(unsigned long a)
|
|
|
|
{
|
|
|
|
resource *r;
|
|
|
|
|
|
|
|
debug("Looking up %08lx\n", a);
|
|
|
|
if (r = pool_lookup(&root_pool.r, a))
|
2023-02-28 09:42:47 +00:00
|
|
|
rdump(r, 3);
|
2000-05-08 22:33:38 +00:00
|
|
|
else
|
|
|
|
debug("Not found.\n");
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void
|
|
|
|
resource_init(void)
|
|
|
|
{
|
2021-11-12 21:58:40 +00:00
|
|
|
rcu_init();
|
2022-09-09 18:57:59 +00:00
|
|
|
resource_sys_init();
|
2022-03-09 08:10:44 +00:00
|
|
|
|
2023-11-01 17:23:43 +00:00
|
|
|
root_pool.r.class = &pool_class;
|
2023-04-21 13:26:06 +00:00
|
|
|
rp_init(&root_pool, the_bird_domain.the_bird, "Root");
|
2024-05-08 16:55:01 +00:00
|
|
|
tmp_init(&root_pool);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 16:55:01 +00:00
|
|
|
_Thread_local linpool *tmp_linpool;
|
2022-03-19 15:23:42 +00:00
|
|
|
|
|
|
|
void
|
2024-05-08 16:55:01 +00:00
|
|
|
tmp_init(pool *p)
|
2022-03-19 15:23:42 +00:00
|
|
|
{
|
2024-05-08 16:55:01 +00:00
|
|
|
ASSERT_DIE(!tmp_linpool);
|
|
|
|
tmp_linpool = lp_new_default(p);
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tmp_flush(void)
|
|
|
|
{
|
|
|
|
lp_flush(tmp_linpool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
1998-05-03 16:43:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct mblock {
|
|
|
|
resource r;
|
|
|
|
unsigned size;
|
2024-12-11 16:51:46 +00:00
|
|
|
union {
|
|
|
|
uintptr_t bigint;
|
|
|
|
_Atomic u64 atom;
|
|
|
|
} _align[0];
|
1998-05-03 16:43:39 +00:00
|
|
|
byte data[0];
|
|
|
|
};
|
|
|
|
|
2004-06-05 09:27:02 +00:00
|
|
|
static void mbl_free(resource *r UNUSED)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-28 09:42:47 +00:00
|
|
|
static void mbl_debug(resource *r, unsigned indent UNUSED)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
|
|
|
struct mblock *m = (struct mblock *) r;
|
|
|
|
|
|
|
|
debug("(size=%d)\n", m->size);
|
|
|
|
}
|
|
|
|
|
2000-05-08 22:33:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:21:12 +00:00
|
|
|
static struct resmem
|
2010-06-02 20:20:40 +00:00
|
|
|
mbl_memsize(resource *r)
|
|
|
|
{
|
|
|
|
struct mblock *m = (struct mblock *) r;
|
2021-11-26 23:21:12 +00:00
|
|
|
return (struct resmem) {
|
|
|
|
.effective = m->size,
|
|
|
|
.overhead = ALLOC_OVERHEAD + sizeof(struct mblock),
|
|
|
|
};
|
2010-06-02 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
1998-05-24 14:46:20 +00:00
|
|
|
static struct resclass mb_class = {
|
1998-05-03 16:43:39 +00:00
|
|
|
"Memory",
|
|
|
|
0,
|
|
|
|
mbl_free,
|
|
|
|
mbl_debug,
|
2010-06-02 20:20:40 +00:00
|
|
|
mbl_lookup,
|
|
|
|
mbl_memsize
|
1998-05-03 16:43:39 +00:00
|
|
|
};
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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().
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void *
|
|
|
|
mb_alloc(pool *p, unsigned size)
|
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(p->domain));
|
|
|
|
|
1998-05-03 16:43:39 +00:00
|
|
|
struct mblock *b = xmalloc(sizeof(struct mblock) + size);
|
|
|
|
|
|
|
|
b->r.class = &mb_class;
|
2023-04-21 13:26:06 +00:00
|
|
|
b->r.n = (struct resource_node) {};
|
|
|
|
resource_add_tail(&p->inside, &b->r);
|
1998-05-03 16:43:39 +00:00
|
|
|
b->size = size;
|
|
|
|
return b->data;
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2012-04-22 12:03:07 +00:00
|
|
|
* Please note that mb_allocz() returns a pointer to the memory
|
2000-06-05 11:41:41 +00:00
|
|
|
* chunk, not to the resource, hence you have to free it using
|
|
|
|
* mb_free(), not rfree().
|
|
|
|
*/
|
1999-03-04 11:36:26 +00:00
|
|
|
void *
|
|
|
|
mb_allocz(pool *p, unsigned size)
|
|
|
|
{
|
|
|
|
void *x = mb_alloc(p, size);
|
|
|
|
bzero(x, size);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:45:08 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2013-09-10 10:09:36 +00:00
|
|
|
* newly allocated memory will be uninitialized. Contrary to realloc()
|
|
|
|
* behavior, @m must be non-NULL, because the resource pool is inherited
|
|
|
|
* from it.
|
2009-06-10 21:45:08 +00:00
|
|
|
*
|
|
|
|
* Like mb_alloc(), mb_realloc() also returns a pointer to the memory
|
2013-09-10 10:09:36 +00:00
|
|
|
* chunk, not to the resource, hence you have to free it using
|
2009-06-10 21:45:08 +00:00
|
|
|
* mb_free(), not rfree().
|
|
|
|
*/
|
|
|
|
void *
|
2013-09-10 10:09:36 +00:00
|
|
|
mb_realloc(void *m, unsigned size)
|
2009-06-10 21:45:08 +00:00
|
|
|
{
|
2024-04-26 10:14:33 +00:00
|
|
|
SKIP_BACK_DECLARE(struct mblock, b, data, m);
|
2023-04-21 13:26:06 +00:00
|
|
|
struct pool *p = resource_parent(&b->r);
|
|
|
|
|
|
|
|
ASSERT_DIE(DG_IS_LOCKED(p->domain));
|
2009-06-10 21:45:08 +00:00
|
|
|
|
2016-03-22 12:35:40 +00:00
|
|
|
b = xrealloc(b, sizeof(struct mblock) + size);
|
2009-06-10 21:45:08 +00:00
|
|
|
b->size = size;
|
2023-04-21 13:26:06 +00:00
|
|
|
|
|
|
|
resource_update_node(&p->inside, &b->r);
|
2009-06-10 21:45:08 +00:00
|
|
|
return b->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* mb_free - free a memory block
|
|
|
|
* @m: memory block
|
|
|
|
*
|
|
|
|
* mb_free() frees all memory associated with the block @m.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void
|
|
|
|
mb_free(void *m)
|
|
|
|
{
|
2014-02-06 16:46:01 +00:00
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
2024-04-26 10:14:33 +00:00
|
|
|
SKIP_BACK_DECLARE(struct mblock, b, data, m);
|
2023-04-20 19:08:38 +00:00
|
|
|
rfree(&b->r);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
2009-06-10 21:45:08 +00:00
|
|
|
|
2013-09-10 10:09:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2013-09-16 21:57:40 +00:00
|
|
|
*buf = mb_realloc(*buf, nsize * item_size);
|
2013-09-10 10:09:36 +00:00
|
|
|
*size = nsize;
|
|
|
|
}
|