]>
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];
38 struct lp_chunk
*first
, *current
, **plast
; /* Normal (reusable) chunks */
39 struct lp_chunk
*first_large
; /* Large chunks */
40 unsigned chunk_size
, threshold
, total
, total_large
;
43 static void lp_free(resource
*);
44 static void lp_dump(resource
*);
45 static resource
*lp_lookup(resource
*, unsigned long);
47 static struct resclass lp_class
= {
49 sizeof(struct linpool
),
56 * lp_new - create a new linear memory pool
60 * lp_new() creates a new linear memory pool resource inside the pool @p.
61 * The linear pool consists of a list of memory chunks of size at least
65 *lp_new(pool
*p
, unsigned blk
)
67 linpool
*m
= ralloc(p
, &lp_class
);
70 m
->threshold
= 3*blk
/4;
75 * lp_alloc - allocate memory from a &linpool
76 * @m: linear memory pool
77 * @size: amount of memory
79 * lp_alloc() allocates @size bytes of memory from a &linpool @m
80 * and it returns a pointer to the allocated memory.
82 * It works by trying to find free space in the last memory chunk
83 * associated with the &linpool and creating a new chunk of the standard
84 * size (as specified during lp_new()) if the free space is too small
85 * to satisfy the allocation. If @size is too large to fit in a standard
86 * size chunk, an "overflow" chunk is created for it instead.
89 lp_alloc(linpool
*m
, unsigned size
)
91 byte
*a
= (byte
*) BIRD_ALIGN((unsigned long) m
->ptr
, CPU_STRUCT_ALIGN
);
102 if (size
>= m
->threshold
)
104 /* Too large => allocate large chunk */
105 c
= xmalloc(sizeof(struct lp_chunk
) + size
);
106 m
->total_large
+= size
;
107 c
->next
= m
->first_large
;
115 /* Still have free chunks from previous incarnation (before lp_flush()) */
117 m
->current
= c
->next
;
121 /* Need to allocate a new chunk */
122 c
= xmalloc(sizeof(struct lp_chunk
) + m
->chunk_size
);
123 m
->total
+= m
->chunk_size
;
127 c
->size
= m
->chunk_size
;
129 m
->ptr
= c
->data
+ size
;
130 m
->end
= c
->data
+ m
->chunk_size
;
137 * lp_allocu - allocate unaligned memory from a &linpool
138 * @m: linear memory pool
139 * @size: amount of memory
141 * lp_allocu() allocates @size bytes of memory from a &linpool @m
142 * and it returns a pointer to the allocated memory. It doesn't
143 * attempt to align the memory block, giving a very efficient way
144 * how to allocate strings without any space overhead.
147 lp_allocu(linpool
*m
, unsigned size
)
157 return lp_alloc(m
, size
);
161 * lp_allocz - allocate cleared memory from a &linpool
162 * @m: linear memory pool
163 * @size: amount of memory
165 * This function is identical to lp_alloc() except that it
166 * clears the allocated memory block.
169 lp_allocz(linpool
*m
, unsigned size
)
171 void *z
= lp_alloc(m
, size
);
178 * lp_flush - flush a linear memory pool
179 * @m: linear memory pool
181 * This function frees the whole contents of the given &linpool @m,
182 * but leaves the pool itself.
189 /* Relink all normal chunks to free list and free all large chunks */
190 m
->ptr
= m
->end
= NULL
;
191 m
->current
= m
->first
;
192 while (c
= m
->first_large
)
194 m
->first_large
= c
->next
;
203 linpool
*m
= (linpool
*) r
;
204 struct lp_chunk
*c
, *d
;
206 for(d
=m
->first
; d
; d
= c
)
211 for(d
=m
->first_large
; d
; d
= c
)
221 linpool
*m
= (linpool
*) r
;
225 for(cnt
=0, c
=m
->first
; c
; c
=c
->next
, cnt
++)
227 for(cntl
=0, c
=m
->first_large
; c
; c
=c
->next
, cntl
++)
229 debug("(chunk=%d threshold=%d count=%d+%d total=%d+%d)\n",
239 lp_lookup(resource
*r
, unsigned long a
)
241 linpool
*m
= (linpool
*) r
;
244 for(c
=m
->first
; c
; c
=c
->next
)
245 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)
247 for(c
=m
->first_large
; c
; c
=c
->next
)
248 if ((unsigned long) c
->data
<= a
&& (unsigned long) c
->data
+ c
->size
> a
)