]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/mempool.c
2 * BIRD Resource Manager -- Memory Pools
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
10 * DOC: Linear memory pools
12 * Linear memory pools are collections of memory blocks which
13 * support very fast allocation of new blocks, but are able to free only
14 * the whole collection at once.
16 * Example: Each configuration is described by a complex system of structures,
17 * linked lists and function trees which are all allocated from a single linear
18 * pool, thus they can be freed at once when the configuration is no longer used.
24 #include "nest/bird.h"
25 #include "lib/resource.h"
26 #include "lib/string.h"
29 struct lp_chunk
*next
;
31 uintptr_t data_align
[0];
35 const int lp_chunk_size
= sizeof(struct lp_chunk
);
40 struct lp_chunk
*first
, *current
, **plast
; /* Normal (reusable) chunks */
41 struct lp_chunk
*first_large
; /* Large chunks */
42 uint chunk_size
, threshold
, total
, total_large
;
45 static void lp_free(resource
*);
46 static void lp_dump(resource
*);
47 static resource
*lp_lookup(resource
*, unsigned long);
48 static size_t lp_memsize(resource
*r
);
50 static struct resclass lp_class
= {
52 sizeof(struct linpool
),
60 * lp_new - create a new linear memory pool
64 * lp_new() creates a new linear memory pool resource inside the pool @p.
65 * The linear pool consists of a list of memory chunks of size at least
69 *lp_new(pool
*p
, uint blk
)
71 linpool
*m
= ralloc(p
, &lp_class
);
74 m
->threshold
= 3*blk
/4;
79 * lp_alloc - allocate memory from a &linpool
80 * @m: linear memory pool
81 * @size: amount of memory
83 * lp_alloc() allocates @size bytes of memory from a &linpool @m
84 * and it returns a pointer to the allocated memory.
86 * It works by trying to find free space in the last memory chunk
87 * associated with the &linpool and creating a new chunk of the standard
88 * size (as specified during lp_new()) if the free space is too small
89 * to satisfy the allocation. If @size is too large to fit in a standard
90 * size chunk, an "overflow" chunk is created for it instead.
93 lp_alloc(linpool
*m
, uint size
)
95 byte
*a
= (byte
*) BIRD_ALIGN((unsigned long) m
->ptr
, CPU_STRUCT_ALIGN
);
106 if (size
>= m
->threshold
)
108 /* Too large => allocate large chunk */
109 c
= xmalloc(sizeof(struct lp_chunk
) + size
);
110 m
->total_large
+= size
;
111 c
->next
= m
->first_large
;
119 /* Still have free chunks from previous incarnation (before lp_flush()) */
121 m
->current
= c
->next
;
125 /* Need to allocate a new chunk */
126 c
= xmalloc(sizeof(struct lp_chunk
) + m
->chunk_size
);
127 m
->total
+= m
->chunk_size
;
131 c
->size
= m
->chunk_size
;
133 m
->ptr
= c
->data
+ size
;
134 m
->end
= c
->data
+ m
->chunk_size
;
141 * lp_allocu - allocate unaligned memory from a &linpool
142 * @m: linear memory pool
143 * @size: amount of memory
145 * lp_allocu() allocates @size bytes of memory from a &linpool @m
146 * and it returns a pointer to the allocated memory. It doesn't
147 * attempt to align the memory block, giving a very efficient way
148 * how to allocate strings without any space overhead.
151 lp_allocu(linpool
*m
, uint size
)
161 return lp_alloc(m
, size
);
165 * lp_allocz - allocate cleared memory from a &linpool
166 * @m: linear memory pool
167 * @size: amount of memory
169 * This function is identical to lp_alloc() except that it
170 * clears the allocated memory block.
173 lp_allocz(linpool
*m
, uint size
)
175 void *z
= lp_alloc(m
, size
);
182 * lp_flush - flush a linear memory pool
183 * @m: linear memory pool
185 * This function frees the whole contents of the given &linpool @m,
186 * but leaves the pool itself.
193 /* Relink all normal chunks to free list and free all large chunks */
194 m
->ptr
= m
->end
= NULL
;
195 m
->current
= m
->first
;
196 while (c
= m
->first_large
)
198 m
->first_large
= c
->next
;
207 linpool
*m
= (linpool
*) r
;
208 struct lp_chunk
*c
, *d
;
210 for(d
=m
->first
; d
; d
= c
)
215 for(d
=m
->first_large
; d
; d
= c
)
225 linpool
*m
= (linpool
*) r
;
229 for(cnt
=0, c
=m
->first
; c
; c
=c
->next
, cnt
++)
231 for(cntl
=0, c
=m
->first_large
; c
; c
=c
->next
, cntl
++)
233 debug("(chunk=%d threshold=%d count=%d+%d total=%d+%d)\n",
243 lp_memsize(resource
*r
)
245 linpool
*m
= (linpool
*) r
;
249 for(c
=m
->first
; c
; c
=c
->next
)
251 for(c
=m
->first_large
; c
; c
=c
->next
)
254 return ALLOC_OVERHEAD
+ sizeof(struct linpool
) +
255 cnt
* (ALLOC_OVERHEAD
+ sizeof(struct lp_chunk
)) +
256 m
->total
+ m
->total_large
;
261 lp_lookup(resource
*r
, unsigned long a
)
263 linpool
*m
= (linpool
*) r
;
266 for(c
=m
->first
; c
; c
=c
->next
)
267 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)
269 for(c
=m
->first_large
; c
; c
=c
->next
)
270 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)