1998-05-03 16:43:39 +00:00
|
|
|
/*
|
|
|
|
* BIRD Resource Manager -- Memory Pools
|
|
|
|
*
|
2000-06-05 11:41:41 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-03 16:43:39 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* DOC: Linear memory pools
|
|
|
|
*
|
|
|
|
* Linear memory pools are collections of memory blocks which
|
|
|
|
* support very fast allocation of new blocks, but are able to free only
|
2017-12-12 18:51:36 +00:00
|
|
|
* the whole collection at once (or in stack order).
|
2000-06-05 11:41:41 +00:00
|
|
|
*
|
|
|
|
* Example: Each configuration is described by a complex system of structures,
|
|
|
|
* linked lists and function trees which are all allocated from a single linear
|
|
|
|
* pool, thus they can be freed at once when the configuration is no longer used.
|
|
|
|
*/
|
|
|
|
|
1998-05-03 16:43:39 +00:00
|
|
|
#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"
|
1998-05-03 16:43:39 +00:00
|
|
|
|
1998-12-06 11:59:18 +00:00
|
|
|
struct lp_chunk {
|
|
|
|
struct lp_chunk *next;
|
2023-04-28 10:32:41 +00:00
|
|
|
struct linpool *lp;
|
2009-07-06 17:07:01 +00:00
|
|
|
uintptr_t data_align[0];
|
2024-12-11 16:51:46 +00:00
|
|
|
_Atomic u64 data_align_atomic[0];
|
1998-05-03 16:43:39 +00:00
|
|
|
byte data[0];
|
|
|
|
};
|
|
|
|
|
2022-04-04 20:34:14 +00:00
|
|
|
#define LP_DATA_SIZE (page_size - OFFSETOF(struct lp_chunk, data))
|
2017-05-16 12:31:16 +00:00
|
|
|
|
1998-12-06 11:59:18 +00:00
|
|
|
struct linpool {
|
1998-05-03 16:43:39 +00:00
|
|
|
resource r;
|
|
|
|
byte *ptr, *end;
|
2017-12-12 18:51:36 +00:00
|
|
|
struct lp_chunk *first, *current; /* Normal (reusable) chunks */
|
1999-03-29 19:35:47 +00:00
|
|
|
struct lp_chunk *first_large; /* Large chunks */
|
2023-05-01 13:10:53 +00:00
|
|
|
struct lp_state *initial; /* Initial state to restore to */
|
2022-04-04 20:34:14 +00:00
|
|
|
uint total, total_large;
|
1998-05-03 16:43:39 +00:00
|
|
|
};
|
|
|
|
|
2024-06-25 12:02:15 +00:00
|
|
|
static void *lp_alloc_slow(struct linpool *, uint);
|
2000-05-08 22:33:38 +00:00
|
|
|
static void lp_free(resource *);
|
2024-11-14 19:43:35 +00:00
|
|
|
static void lp_dump(struct dump_request *, resource *);
|
2000-05-08 22:33:38 +00:00
|
|
|
static resource *lp_lookup(resource *, unsigned long);
|
2021-11-26 23:21:12 +00:00
|
|
|
static struct resmem lp_memsize(resource *r);
|
1998-05-03 16:43:39 +00:00
|
|
|
|
1998-12-06 11:59:18 +00:00
|
|
|
static struct resclass lp_class = {
|
|
|
|
"LinPool",
|
|
|
|
sizeof(struct linpool),
|
|
|
|
lp_free,
|
2000-05-08 22:33:38 +00:00
|
|
|
lp_dump,
|
2010-06-02 20:20:40 +00:00
|
|
|
lp_lookup,
|
|
|
|
lp_memsize
|
1998-05-03 16:43:39 +00:00
|
|
|
};
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* lp_new - create a new linear memory pool
|
|
|
|
* @p: pool
|
|
|
|
*
|
|
|
|
* lp_new() creates a new linear memory pool resource inside the pool @p.
|
2022-04-04 20:34:14 +00:00
|
|
|
* The linear pool consists of a list of memory chunks of page size.
|
2000-06-05 11:41:41 +00:00
|
|
|
*/
|
1998-12-06 11:59:18 +00:00
|
|
|
linpool
|
2022-04-04 20:34:14 +00:00
|
|
|
*lp_new(pool *p)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
2023-05-01 13:10:53 +00:00
|
|
|
linpool *m = ralloc(p, &lp_class);
|
|
|
|
m->initial = lp_save(m);
|
|
|
|
return m;
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* lp_alloc - allocate memory from a &linpool
|
|
|
|
* @m: linear memory pool
|
|
|
|
* @size: amount of memory
|
|
|
|
*
|
|
|
|
* lp_alloc() allocates @size bytes of memory from a &linpool @m
|
|
|
|
* and it returns a pointer to the allocated memory.
|
|
|
|
*
|
|
|
|
* It works by trying to find free space in the last memory chunk
|
|
|
|
* associated with the &linpool and creating a new chunk of the standard
|
|
|
|
* size (as specified during lp_new()) if the free space is too small
|
|
|
|
* to satisfy the allocation. If @size is too large to fit in a standard
|
|
|
|
* size chunk, an "overflow" chunk is created for it instead.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void *
|
2015-05-19 06:53:34 +00:00
|
|
|
lp_alloc(linpool *m, uint size)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
2023-04-28 21:48:03 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(resource_parent(&m->r)->domain));
|
2004-06-01 10:28:25 +00:00
|
|
|
byte *a = (byte *) BIRD_ALIGN((unsigned long) m->ptr, CPU_STRUCT_ALIGN);
|
1998-05-03 16:43:39 +00:00
|
|
|
byte *e = a + size;
|
|
|
|
|
|
|
|
if (e <= m->end)
|
|
|
|
{
|
|
|
|
m->ptr = e;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
else
|
2024-06-25 12:02:15 +00:00
|
|
|
return lp_alloc_slow(m, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
lp_alloc_slow(linpool *m, uint size)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
1998-12-06 11:59:18 +00:00
|
|
|
struct lp_chunk *c;
|
2022-04-04 20:34:14 +00:00
|
|
|
if (size > LP_DATA_SIZE)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
1999-03-29 19:35:47 +00:00
|
|
|
/* Too large => allocate large chunk */
|
1998-12-06 11:59:18 +00:00
|
|
|
c = xmalloc(sizeof(struct lp_chunk) + size);
|
2023-04-28 10:32:41 +00:00
|
|
|
c->lp = m;
|
1999-03-29 19:35:47 +00:00
|
|
|
c->next = m->first_large;
|
2023-04-28 10:32:41 +00:00
|
|
|
|
|
|
|
m->total_large += size;
|
2001-01-17 08:32:28 +00:00
|
|
|
m->first_large = c;
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-28 10:32:41 +00:00
|
|
|
if (m->current)
|
|
|
|
ASSERT_DIE(!m->current->next);
|
|
|
|
|
|
|
|
/* Need to allocate a new chunk */
|
|
|
|
c = alloc_page();
|
|
|
|
|
|
|
|
m->total += LP_DATA_SIZE;
|
|
|
|
c->next = NULL;
|
|
|
|
c->lp = m;
|
|
|
|
|
|
|
|
if (m->current)
|
|
|
|
m->current->next = c;
|
1999-03-29 19:35:47 +00:00
|
|
|
else
|
2023-04-28 10:32:41 +00:00
|
|
|
m->first = c;
|
|
|
|
|
2017-12-12 18:51:36 +00:00
|
|
|
m->current = c;
|
1998-05-03 16:43:39 +00:00
|
|
|
m->ptr = c->data + size;
|
2022-04-04 20:34:14 +00:00
|
|
|
m->end = c->data + LP_DATA_SIZE;
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
return c->data;
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* lp_allocu - allocate unaligned memory from a &linpool
|
|
|
|
* @m: linear memory pool
|
|
|
|
* @size: amount of memory
|
|
|
|
*
|
|
|
|
* lp_allocu() allocates @size bytes of memory from a &linpool @m
|
|
|
|
* and it returns a pointer to the allocated memory. It doesn't
|
|
|
|
* attempt to align the memory block, giving a very efficient way
|
|
|
|
* how to allocate strings without any space overhead.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void *
|
2015-05-19 06:53:34 +00:00
|
|
|
lp_allocu(linpool *m, uint size)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
2023-04-28 21:48:03 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(resource_parent(&m->r)->domain));
|
1998-05-03 16:43:39 +00:00
|
|
|
byte *a = m->ptr;
|
|
|
|
byte *e = a + size;
|
|
|
|
|
|
|
|
if (e <= m->end)
|
|
|
|
{
|
|
|
|
m->ptr = e;
|
|
|
|
return a;
|
|
|
|
}
|
2024-06-25 12:02:15 +00:00
|
|
|
return lp_alloc_slow(m, size);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* lp_allocz - allocate cleared memory from a &linpool
|
|
|
|
* @m: linear memory pool
|
|
|
|
* @size: amount of memory
|
|
|
|
*
|
|
|
|
* This function is identical to lp_alloc() except that it
|
|
|
|
* clears the allocated memory block.
|
|
|
|
*/
|
1998-05-03 16:43:39 +00:00
|
|
|
void *
|
2015-05-19 06:53:34 +00:00
|
|
|
lp_allocz(linpool *m, uint size)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
1998-12-06 11:59:18 +00:00
|
|
|
void *z = lp_alloc(m, size);
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
bzero(z, size);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2000-06-05 11:41:41 +00:00
|
|
|
/**
|
|
|
|
* lp_flush - flush a linear memory pool
|
|
|
|
* @m: linear memory pool
|
|
|
|
*
|
|
|
|
* This function frees the whole contents of the given &linpool @m,
|
|
|
|
* but leaves the pool itself.
|
|
|
|
*/
|
1999-03-29 19:35:47 +00:00
|
|
|
void
|
|
|
|
lp_flush(linpool *m)
|
|
|
|
{
|
2023-05-01 13:10:53 +00:00
|
|
|
lp_restore(m, m->initial);
|
|
|
|
m->initial = lp_save(m);
|
1999-03-29 19:35:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 18:51:36 +00:00
|
|
|
/**
|
|
|
|
* lp_save - save the state of a linear memory pool
|
|
|
|
* @m: linear memory pool
|
|
|
|
* @p: state buffer
|
|
|
|
*
|
|
|
|
* This function saves the state of a linear memory pool. Saved state can be
|
|
|
|
* used later to restore the pool (to free memory allocated since).
|
|
|
|
*/
|
2023-05-01 13:10:53 +00:00
|
|
|
struct lp_state *
|
|
|
|
lp_save(linpool *m)
|
2017-12-12 18:51:36 +00:00
|
|
|
{
|
2023-04-28 21:48:03 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(resource_parent(&m->r)->domain));
|
2023-05-01 13:10:53 +00:00
|
|
|
struct lp_state *p = lp_alloc(m, sizeof(struct lp_state));
|
|
|
|
ASSERT_DIE(m->current);
|
|
|
|
*p = (struct lp_state) {
|
2024-05-08 16:55:01 +00:00
|
|
|
.p = m,
|
2023-05-01 13:10:53 +00:00
|
|
|
.current = m->current,
|
|
|
|
.large = m->first_large,
|
|
|
|
.total_large = m->total_large,
|
|
|
|
};
|
|
|
|
|
|
|
|
return p;
|
2017-12-12 18:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lp_restore - restore the state of a linear memory pool
|
|
|
|
* @m: linear memory pool
|
|
|
|
* @p: saved state
|
|
|
|
*
|
|
|
|
* This function restores the state of a linear memory pool, freeing all memory
|
|
|
|
* allocated since the state was saved. Note that the function cannot un-free
|
|
|
|
* the memory, therefore the function also invalidates other states that were
|
|
|
|
* saved between (on the same pool).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lp_restore(linpool *m, lp_state *p)
|
|
|
|
{
|
|
|
|
struct lp_chunk *c;
|
2023-04-28 21:48:03 +00:00
|
|
|
ASSERT_DIE(DG_IS_LOCKED(resource_parent(&m->r)->domain));
|
2017-12-12 18:51:36 +00:00
|
|
|
|
|
|
|
/* Move ptr to the saved pos and free all newer large chunks */
|
2023-05-01 13:10:53 +00:00
|
|
|
ASSERT_DIE(p->current);
|
2024-05-08 16:55:01 +00:00
|
|
|
ASSERT_DIE(p->p == m);
|
2023-05-01 13:10:53 +00:00
|
|
|
m->current = c = p->current;
|
|
|
|
m->ptr = (byte *) p;
|
|
|
|
m->end = c->data + LP_DATA_SIZE;
|
2022-04-04 20:34:14 +00:00
|
|
|
m->total_large = p->total_large;
|
2017-12-12 18:51:36 +00:00
|
|
|
|
|
|
|
while ((c = m->first_large) && (c != p->large))
|
|
|
|
{
|
|
|
|
m->first_large = c->next;
|
|
|
|
xfree(c);
|
|
|
|
}
|
2023-04-28 10:32:41 +00:00
|
|
|
|
2023-05-01 13:10:53 +00:00
|
|
|
while (c = m->current->next)
|
2023-04-28 10:32:41 +00:00
|
|
|
{
|
|
|
|
m->current->next = c->next;
|
|
|
|
free_page(c);
|
|
|
|
}
|
2017-12-12 18:51:36 +00:00
|
|
|
}
|
|
|
|
|
2000-05-08 22:33:38 +00:00
|
|
|
static void
|
1998-12-06 11:59:18 +00:00
|
|
|
lp_free(resource *r)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
1998-12-06 11:59:18 +00:00
|
|
|
linpool *m = (linpool *) r;
|
|
|
|
struct lp_chunk *c, *d;
|
1998-05-03 16:43:39 +00:00
|
|
|
|
|
|
|
for(d=m->first; d; d = c)
|
|
|
|
{
|
|
|
|
c = d->next;
|
2022-04-04 20:34:14 +00:00
|
|
|
free_page(d);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
1999-10-02 10:55:19 +00:00
|
|
|
for(d=m->first_large; d; d = c)
|
|
|
|
{
|
|
|
|
c = d->next;
|
|
|
|
xfree(d);
|
|
|
|
}
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
|
|
|
|
2000-05-08 22:33:38 +00:00
|
|
|
static void
|
2024-11-14 19:43:35 +00:00
|
|
|
lp_dump(struct dump_request *dreq, resource *r)
|
1998-05-03 16:43:39 +00:00
|
|
|
{
|
1998-12-06 11:59:18 +00:00
|
|
|
linpool *m = (linpool *) r;
|
2024-11-15 17:35:30 +00:00
|
|
|
|
|
|
|
int chunks = 0, large = 0;
|
|
|
|
|
|
|
|
RDUMP("\n%*schunks:\n", dreq->indent+3, "");
|
|
|
|
for (struct lp_chunk *c = m->first; c; c = c->next)
|
|
|
|
{
|
|
|
|
RDUMP("%*s%p\n", dreq->indent+6, "", c);
|
|
|
|
chunks++;
|
|
|
|
}
|
|
|
|
RDUMP("%*scount=%d total=%d\n", dreq->indent+3, "", chunks, m->total);
|
|
|
|
|
|
|
|
RDUMP("%*slarge:\n", dreq->indent+3, "");
|
|
|
|
for (struct lp_chunk *c = m->first_large; c; c = c->next)
|
|
|
|
{
|
|
|
|
RDUMP("%*s%p\n", dreq->indent+6, "", c);
|
|
|
|
large++;
|
|
|
|
}
|
|
|
|
RDUMP("%*scount=%d total=%d\n", dreq->indent+3, "", large, m->total_large);
|
1998-05-03 16:43:39 +00:00
|
|
|
}
|
2000-05-08 22:33:38 +00:00
|
|
|
|
2021-11-26 23:21:12 +00:00
|
|
|
static struct resmem
|
2010-06-02 20:20:40 +00:00
|
|
|
lp_memsize(resource *r)
|
|
|
|
{
|
|
|
|
linpool *m = (linpool *) r;
|
2022-03-09 09:30:33 +00:00
|
|
|
struct resmem sz = {
|
|
|
|
.overhead = sizeof(struct linpool) + ALLOC_OVERHEAD,
|
2022-04-04 20:34:14 +00:00
|
|
|
.effective = m->total_large,
|
2022-03-09 09:30:33 +00:00
|
|
|
};
|
2010-06-02 20:20:40 +00:00
|
|
|
|
2022-03-09 09:30:33 +00:00
|
|
|
for (struct lp_chunk *c = m->first_large; c; c = c->next)
|
2022-04-04 20:34:14 +00:00
|
|
|
sz.overhead += sizeof(struct lp_chunk) + ALLOC_OVERHEAD;
|
2010-06-02 20:20:40 +00:00
|
|
|
|
2022-03-09 09:30:33 +00:00
|
|
|
uint regular = 0;
|
|
|
|
for (struct lp_chunk *c = m->first; c; c = c->next)
|
|
|
|
regular++;
|
|
|
|
|
2022-04-04 20:34:14 +00:00
|
|
|
sz.effective += LP_DATA_SIZE * regular;
|
|
|
|
sz.overhead += (sizeof(struct lp_chunk) + ALLOC_OVERHEAD) * regular;
|
2022-03-09 09:30:33 +00:00
|
|
|
|
|
|
|
return sz;
|
2010-06-02 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-08 22:33:38 +00:00
|
|
|
static resource *
|
|
|
|
lp_lookup(resource *r, unsigned long a)
|
|
|
|
{
|
|
|
|
linpool *m = (linpool *) r;
|
|
|
|
struct lp_chunk *c;
|
|
|
|
|
|
|
|
for(c=m->first; c; c=c->next)
|
2022-04-04 20:34:14 +00:00
|
|
|
if ((unsigned long) c->data <= a && (unsigned long) c->data + LP_DATA_SIZE > a)
|
2000-05-08 22:33:38 +00:00
|
|
|
return r;
|
|
|
|
return NULL;
|
|
|
|
}
|