]>
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 (or in stack order).
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
; /* 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
);
73 m
->threshold
= 3*blk
/4;
78 * lp_alloc - allocate memory from a &linpool
79 * @m: linear memory pool
80 * @size: amount of memory
82 * lp_alloc() allocates @size bytes of memory from a &linpool @m
83 * and it returns a pointer to the allocated memory.
85 * It works by trying to find free space in the last memory chunk
86 * associated with the &linpool and creating a new chunk of the standard
87 * size (as specified during lp_new()) if the free space is too small
88 * to satisfy the allocation. If @size is too large to fit in a standard
89 * size chunk, an "overflow" chunk is created for it instead.
92 lp_alloc(linpool
*m
, uint size
)
94 byte
*a
= (byte
*) BIRD_ALIGN((unsigned long) m
->ptr
, CPU_STRUCT_ALIGN
);
105 if (size
>= m
->threshold
)
107 /* Too large => allocate large chunk */
108 c
= xmalloc(sizeof(struct lp_chunk
) + size
);
109 m
->total_large
+= size
;
110 c
->next
= m
->first_large
;
116 if (m
->current
&& m
->current
->next
)
118 /* Still have free chunks from previous incarnation (before lp_flush()) */
119 c
= m
->current
->next
;
123 /* Need to allocate a new chunk */
124 c
= xmalloc(sizeof(struct lp_chunk
) + m
->chunk_size
);
125 m
->total
+= m
->chunk_size
;
127 c
->size
= m
->chunk_size
;
130 m
->current
->next
= c
;
135 m
->ptr
= c
->data
+ size
;
136 m
->end
= c
->data
+ m
->chunk_size
;
143 * lp_allocu - allocate unaligned memory from a &linpool
144 * @m: linear memory pool
145 * @size: amount of memory
147 * lp_allocu() allocates @size bytes of memory from a &linpool @m
148 * and it returns a pointer to the allocated memory. It doesn't
149 * attempt to align the memory block, giving a very efficient way
150 * how to allocate strings without any space overhead.
153 lp_allocu(linpool
*m
, uint size
)
163 return lp_alloc(m
, size
);
167 * lp_allocz - allocate cleared memory from a &linpool
168 * @m: linear memory pool
169 * @size: amount of memory
171 * This function is identical to lp_alloc() except that it
172 * clears the allocated memory block.
175 lp_allocz(linpool
*m
, uint size
)
177 void *z
= lp_alloc(m
, size
);
184 * lp_flush - flush a linear memory pool
185 * @m: linear memory pool
187 * This function frees the whole contents of the given &linpool @m,
188 * but leaves the pool itself.
195 /* Move ptr to the first chunk and free all large chunks */
196 m
->current
= c
= m
->first
;
197 m
->ptr
= c
? c
->data
: NULL
;
198 m
->end
= c
? c
->data
+ m
->chunk_size
: NULL
;
200 while (c
= m
->first_large
)
202 m
->first_large
= c
->next
;
209 * lp_save - save the state of a linear memory pool
210 * @m: linear memory pool
213 * This function saves the state of a linear memory pool. Saved state can be
214 * used later to restore the pool (to free memory allocated since).
217 lp_save(linpool
*m
, lp_state
*p
)
219 p
->current
= m
->current
;
220 p
->large
= m
->first_large
;
225 * lp_restore - restore the state of a linear memory pool
226 * @m: linear memory pool
229 * This function restores the state of a linear memory pool, freeing all memory
230 * allocated since the state was saved. Note that the function cannot un-free
231 * the memory, therefore the function also invalidates other states that were
232 * saved between (on the same pool).
235 lp_restore(linpool
*m
, lp_state
*p
)
239 /* Move ptr to the saved pos and free all newer large chunks */
240 m
->current
= c
= p
->current
;
242 m
->end
= c
? c
->data
+ m
->chunk_size
: NULL
;
244 while ((c
= m
->first_large
) && (c
!= p
->large
))
246 m
->first_large
= c
->next
;
247 m
->total_large
-= c
->size
;
255 linpool
*m
= (linpool
*) r
;
256 struct lp_chunk
*c
, *d
;
258 for(d
=m
->first
; d
; d
= c
)
263 for(d
=m
->first_large
; d
; d
= c
)
273 linpool
*m
= (linpool
*) r
;
277 for(cnt
=0, c
=m
->first
; c
; c
=c
->next
, cnt
++)
279 for(cntl
=0, c
=m
->first_large
; c
; c
=c
->next
, cntl
++)
281 debug("(chunk=%d threshold=%d count=%d+%d total=%d+%d)\n",
291 lp_memsize(resource
*r
)
293 linpool
*m
= (linpool
*) r
;
297 for(c
=m
->first
; c
; c
=c
->next
)
299 for(c
=m
->first_large
; c
; c
=c
->next
)
302 return ALLOC_OVERHEAD
+ sizeof(struct linpool
) +
303 cnt
* (ALLOC_OVERHEAD
+ sizeof(struct lp_chunk
)) +
304 m
->total
+ m
->total_large
;
309 lp_lookup(resource
*r
, unsigned long a
)
311 linpool
*m
= (linpool
*) r
;
314 for(c
=m
->first
; c
; c
=c
->next
)
315 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)
317 for(c
=m
->first_large
; c
; c
=c
->next
)
318 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)