1998-05-04 00:43:39 +08:00
|
|
|
/*
|
|
|
|
* BIRD Resource Manager -- Memory Pools
|
|
|
|
*
|
2000-06-05 19:41:41 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-04 00:43:39 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 19:41:41 +08: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-13 02:51:36 +08:00
|
|
|
* the whole collection at once (or in stack order).
|
2000-06-05 19:41:41 +08: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-04 00:43:39 +08:00
|
|
|
#include <stdlib.h>
|
2009-09-17 23:52:36 +08:00
|
|
|
#include <stdint.h>
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1998-05-04 00:43:39 +08:00
|
|
|
|
1998-12-06 19:59:18 +08:00
|
|
|
struct lp_chunk {
|
|
|
|
struct lp_chunk *next;
|
2015-05-19 14:53:34 +08:00
|
|
|
uint size;
|
2009-07-07 01:07:01 +08:00
|
|
|
uintptr_t data_align[0];
|
1998-05-04 00:43:39 +08:00
|
|
|
byte data[0];
|
|
|
|
};
|
|
|
|
|
2017-05-16 20:31:16 +08:00
|
|
|
const int lp_chunk_size = sizeof(struct lp_chunk);
|
|
|
|
|
1998-12-06 19:59:18 +08:00
|
|
|
struct linpool {
|
1998-05-04 00:43:39 +08:00
|
|
|
resource r;
|
|
|
|
byte *ptr, *end;
|
2017-12-13 02:51:36 +08:00
|
|
|
struct lp_chunk *first, *current; /* Normal (reusable) chunks */
|
1999-03-30 03:35:47 +08:00
|
|
|
struct lp_chunk *first_large; /* Large chunks */
|
2015-05-19 14:53:34 +08:00
|
|
|
uint chunk_size, threshold, total, total_large;
|
1998-05-04 00:43:39 +08:00
|
|
|
};
|
|
|
|
|
2000-05-09 06:33:38 +08:00
|
|
|
static void lp_free(resource *);
|
|
|
|
static void lp_dump(resource *);
|
|
|
|
static resource *lp_lookup(resource *, unsigned long);
|
2010-06-03 04:20:40 +08:00
|
|
|
static size_t lp_memsize(resource *r);
|
1998-05-04 00:43:39 +08:00
|
|
|
|
1998-12-06 19:59:18 +08:00
|
|
|
static struct resclass lp_class = {
|
|
|
|
"LinPool",
|
|
|
|
sizeof(struct linpool),
|
|
|
|
lp_free,
|
2000-05-09 06:33:38 +08:00
|
|
|
lp_dump,
|
2010-06-03 04:20:40 +08:00
|
|
|
lp_lookup,
|
|
|
|
lp_memsize
|
1998-05-04 00:43:39 +08:00
|
|
|
};
|
|
|
|
|
2000-06-05 19:41:41 +08:00
|
|
|
/**
|
|
|
|
* lp_new - create a new linear memory pool
|
|
|
|
* @p: pool
|
|
|
|
* @blk: block size
|
|
|
|
*
|
|
|
|
* lp_new() creates a new linear memory pool resource inside the pool @p.
|
|
|
|
* The linear pool consists of a list of memory chunks of size at least
|
|
|
|
* @blk.
|
|
|
|
*/
|
1998-12-06 19:59:18 +08:00
|
|
|
linpool
|
2015-05-19 14:53:34 +08:00
|
|
|
*lp_new(pool *p, uint blk)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
1998-12-06 19:59:18 +08:00
|
|
|
linpool *m = ralloc(p, &lp_class);
|
1998-05-04 00:43:39 +08:00
|
|
|
m->chunk_size = blk;
|
|
|
|
m->threshold = 3*blk/4;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08: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-04 00:43:39 +08:00
|
|
|
void *
|
2015-05-19 14:53:34 +08:00
|
|
|
lp_alloc(linpool *m, uint size)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
2004-06-01 18:28:25 +08:00
|
|
|
byte *a = (byte *) BIRD_ALIGN((unsigned long) m->ptr, CPU_STRUCT_ALIGN);
|
1998-05-04 00:43:39 +08:00
|
|
|
byte *e = a + size;
|
|
|
|
|
|
|
|
if (e <= m->end)
|
|
|
|
{
|
|
|
|
m->ptr = e;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-12-06 19:59:18 +08:00
|
|
|
struct lp_chunk *c;
|
1998-05-04 00:43:39 +08:00
|
|
|
if (size >= m->threshold)
|
|
|
|
{
|
1999-03-30 03:35:47 +08:00
|
|
|
/* Too large => allocate large chunk */
|
1998-12-06 19:59:18 +08:00
|
|
|
c = xmalloc(sizeof(struct lp_chunk) + size);
|
1999-03-30 03:35:47 +08:00
|
|
|
m->total_large += size;
|
|
|
|
c->next = m->first_large;
|
2001-01-17 16:32:28 +08:00
|
|
|
m->first_large = c;
|
2000-05-09 06:33:38 +08:00
|
|
|
c->size = size;
|
1998-05-04 00:43:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-13 02:51:36 +08:00
|
|
|
if (m->current && m->current->next)
|
1999-10-29 18:08:27 +08:00
|
|
|
{
|
|
|
|
/* Still have free chunks from previous incarnation (before lp_flush()) */
|
2017-12-13 02:51:36 +08:00
|
|
|
c = m->current->next;
|
1999-10-29 18:08:27 +08:00
|
|
|
}
|
1999-03-30 03:35:47 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Need to allocate a new chunk */
|
|
|
|
c = xmalloc(sizeof(struct lp_chunk) + m->chunk_size);
|
|
|
|
m->total += m->chunk_size;
|
|
|
|
c->next = NULL;
|
2000-05-09 06:33:38 +08:00
|
|
|
c->size = m->chunk_size;
|
2017-12-13 02:51:36 +08:00
|
|
|
|
|
|
|
if (m->current)
|
|
|
|
m->current->next = c;
|
|
|
|
else
|
|
|
|
m->first = c;
|
1999-03-30 03:35:47 +08:00
|
|
|
}
|
2017-12-13 02:51:36 +08:00
|
|
|
m->current = c;
|
1998-05-04 00:43:39 +08:00
|
|
|
m->ptr = c->data + size;
|
|
|
|
m->end = c->data + m->chunk_size;
|
|
|
|
}
|
|
|
|
return c->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08: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-04 00:43:39 +08:00
|
|
|
void *
|
2015-05-19 14:53:34 +08:00
|
|
|
lp_allocu(linpool *m, uint size)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
|
|
|
byte *a = m->ptr;
|
|
|
|
byte *e = a + size;
|
|
|
|
|
|
|
|
if (e <= m->end)
|
|
|
|
{
|
|
|
|
m->ptr = e;
|
|
|
|
return a;
|
|
|
|
}
|
1998-12-06 19:59:18 +08:00
|
|
|
return lp_alloc(m, size);
|
1998-05-04 00:43:39 +08:00
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08: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-04 00:43:39 +08:00
|
|
|
void *
|
2015-05-19 14:53:34 +08:00
|
|
|
lp_allocz(linpool *m, uint size)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
1998-12-06 19:59:18 +08:00
|
|
|
void *z = lp_alloc(m, size);
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
bzero(z, size);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08: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-30 03:35:47 +08:00
|
|
|
void
|
|
|
|
lp_flush(linpool *m)
|
|
|
|
{
|
|
|
|
struct lp_chunk *c;
|
|
|
|
|
2017-12-13 02:51:36 +08:00
|
|
|
/* Move ptr to the first chunk and free all large chunks */
|
|
|
|
m->current = c = m->first;
|
|
|
|
m->ptr = c ? c->data : NULL;
|
|
|
|
m->end = c ? c->data + m->chunk_size : NULL;
|
|
|
|
|
1999-03-30 03:35:47 +08:00
|
|
|
while (c = m->first_large)
|
|
|
|
{
|
|
|
|
m->first_large = c->next;
|
|
|
|
xfree(c);
|
|
|
|
}
|
|
|
|
m->total_large = 0;
|
|
|
|
}
|
|
|
|
|
2017-12-13 02:51:36 +08: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).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
lp_save(linpool *m, lp_state *p)
|
|
|
|
{
|
|
|
|
p->current = m->current;
|
|
|
|
p->large = m->first_large;
|
|
|
|
p->ptr = m->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/* Move ptr to the saved pos and free all newer large chunks */
|
|
|
|
m->current = c = p->current;
|
|
|
|
m->ptr = p->ptr;
|
|
|
|
m->end = c ? c->data + m->chunk_size : NULL;
|
|
|
|
|
|
|
|
while ((c = m->first_large) && (c != p->large))
|
|
|
|
{
|
|
|
|
m->first_large = c->next;
|
|
|
|
xfree(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-09 06:33:38 +08:00
|
|
|
static void
|
1998-12-06 19:59:18 +08:00
|
|
|
lp_free(resource *r)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
1998-12-06 19:59:18 +08:00
|
|
|
linpool *m = (linpool *) r;
|
|
|
|
struct lp_chunk *c, *d;
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
for(d=m->first; d; d = c)
|
|
|
|
{
|
|
|
|
c = d->next;
|
|
|
|
xfree(d);
|
|
|
|
}
|
1999-10-02 18:55:19 +08:00
|
|
|
for(d=m->first_large; d; d = c)
|
|
|
|
{
|
|
|
|
c = d->next;
|
|
|
|
xfree(d);
|
|
|
|
}
|
1998-05-04 00:43:39 +08:00
|
|
|
}
|
|
|
|
|
2000-05-09 06:33:38 +08:00
|
|
|
static void
|
1998-12-06 19:59:18 +08:00
|
|
|
lp_dump(resource *r)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
1998-12-06 19:59:18 +08:00
|
|
|
linpool *m = (linpool *) r;
|
|
|
|
struct lp_chunk *c;
|
1999-03-30 04:14:33 +08:00
|
|
|
int cnt, cntl;
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
for(cnt=0, c=m->first; c; c=c->next, cnt++)
|
|
|
|
;
|
1999-03-30 04:14:33 +08:00
|
|
|
for(cntl=0, c=m->first_large; c; c=c->next, cntl++)
|
|
|
|
;
|
|
|
|
debug("(chunk=%d threshold=%d count=%d+%d total=%d+%d)\n",
|
1998-05-04 00:43:39 +08:00
|
|
|
m->chunk_size,
|
|
|
|
m->threshold,
|
|
|
|
cnt,
|
1999-03-30 04:14:33 +08:00
|
|
|
cntl,
|
|
|
|
m->total,
|
|
|
|
m->total_large);
|
1998-05-04 00:43:39 +08:00
|
|
|
}
|
2000-05-09 06:33:38 +08:00
|
|
|
|
2010-06-03 04:20:40 +08:00
|
|
|
static size_t
|
|
|
|
lp_memsize(resource *r)
|
|
|
|
{
|
|
|
|
linpool *m = (linpool *) r;
|
|
|
|
struct lp_chunk *c;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
for(c=m->first; c; c=c->next)
|
|
|
|
cnt++;
|
|
|
|
for(c=m->first_large; c; c=c->next)
|
|
|
|
cnt++;
|
|
|
|
|
|
|
|
return ALLOC_OVERHEAD + sizeof(struct linpool) +
|
2014-04-14 18:28:30 +08:00
|
|
|
cnt * (ALLOC_OVERHEAD + sizeof(struct lp_chunk)) +
|
2010-06-03 04:20:40 +08:00
|
|
|
m->total + m->total_large;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-09 06:33:38 +08: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)
|
|
|
|
if ((unsigned long) c->data <= a && (unsigned long) c->data + c->size > a)
|
|
|
|
return r;
|
|
|
|
for(c=m->first_large; c; c=c->next)
|
|
|
|
if ((unsigned long) c->data <= a && (unsigned long) c->data + c->size > a)
|
|
|
|
return r;
|
|
|
|
return NULL;
|
|
|
|
}
|