Lines Matching defs:alloc

2  * util/alloc.c - memory allocation service. 
43 #include "util/alloc.h"
64 * @param alloc: the structure to fill up.
67 prealloc_setup(struct alloc_cache* alloc)
78 alloc_set_special_next(p, alloc->quar);
79 alloc->quar = p;
80 alloc->num_quar++;
86 prealloc_blocks(struct alloc_cache* alloc, size_t num)
96 r->next = (char*)alloc->reg_list;
97 alloc->reg_list = r;
98 alloc->num_reg_blocks ++;
103 alloc_init(struct alloc_cache* alloc, struct alloc_cache* super,
106 memset(alloc, 0, sizeof(*alloc));
107 alloc->super = super;
108 alloc->thread_num = thread_num;
109 alloc->next_id = (uint64_t)thread_num; /* in steps, so that type */
110 alloc->next_id <<= THRNUM_SHIFT; /* of *_id is used. */
111 alloc->last_id = 1; /* so no 64bit constants, */
112 alloc->last_id <<= THRNUM_SHIFT; /* or implicit 'int' ops. */
113 alloc->last_id -= 1; /* for compiler portability. */
114 alloc->last_id |= alloc->next_id;
115 alloc->next_id += 1; /* because id=0 is special. */
116 alloc->max_reg_blocks = 100;
117 alloc->num_reg_blocks = 0;
118 alloc->reg_list = NULL;
119 alloc->cleanup = NULL;
120 alloc->cleanup_arg = NULL;
121 if(alloc->super)
122 prealloc_blocks(alloc, alloc->max_reg_blocks);
123 if(!alloc->super) {
124 lock_quick_init(&alloc->lock);
125 lock_protect(&alloc->lock, alloc, sizeof(*alloc));
131 alloc_clear_special_list(struct alloc_cache* alloc)
135 p = alloc->quar;
146 alloc_clear_special(struct alloc_cache* alloc)
148 if(!alloc->super) {
149 lock_quick_lock(&alloc->lock);
151 alloc_clear_special_list(alloc);
152 alloc->quar = 0;
153 alloc->num_quar = 0;
154 if(!alloc->super) {
155 lock_quick_unlock(&alloc->lock);
160 alloc_clear(struct alloc_cache* alloc)
164 if(!alloc)
166 if(!alloc->super) {
167 lock_quick_destroy(&alloc->lock);
169 if(alloc->super && alloc->quar) {
171 p = alloc->quar;
174 lock_quick_lock(&alloc->super->lock);
175 alloc_set_special_next(p, alloc->super->quar);
176 alloc->super->quar = alloc->quar;
177 alloc->super->num_quar += alloc->num_quar;
178 lock_quick_unlock(&alloc->super->lock);
180 alloc_clear_special_list(alloc);
182 alloc->quar = 0;
183 alloc->num_quar = 0;
184 r = alloc->reg_list;
190 alloc->reg_list = NULL;
191 alloc->num_reg_blocks = 0;
195 alloc_get_id(struct alloc_cache* alloc)
197 uint64_t id = alloc->next_id++;
198 if(id == alloc->last_id) {
199 log_warn("rrset alloc: out of 64bit ids. Clearing cache.");
200 fptr_ok(fptr_whitelist_alloc_cleanup(alloc->cleanup));
201 (*alloc->cleanup)(alloc->cleanup_arg);
204 alloc->next_id = (uint64_t)alloc->thread_num;
205 alloc->next_id <<= THRNUM_SHIFT; /* in steps for comp. */
206 alloc->next_id += 1; /* portability. */
208 id = alloc->next_id++;
214 alloc_special_obtain(struct alloc_cache* alloc)
217 log_assert(alloc);
219 if(alloc->quar) {
220 p = alloc->quar;
221 alloc->quar = alloc_special_next(p);
222 alloc->num_quar--;
223 p->id = alloc_get_id(alloc);
227 if(alloc->super) {
230 lock_quick_lock(&alloc->super->lock);
231 if((p = alloc->super->quar)) {
232 alloc->super->quar = alloc_special_next(p);
233 alloc->super->num_quar--;
235 lock_quick_unlock(&alloc->super->lock);
237 p->id = alloc_get_id(alloc);
242 prealloc_setup(alloc);
248 p->id = alloc_get_id(alloc);
254 pushintosuper(struct alloc_cache* alloc, alloc_special_type* mem)
257 alloc_special_type *p = alloc->quar;
259 log_assert(alloc && alloc->super &&
260 alloc->num_quar >= ALLOC_SPECIAL_MAX);
262 alloc_set_special_next(mem, alloc->quar);
266 alloc->quar = alloc_special_next(p);
267 alloc->num_quar -= ALLOC_SPECIAL_MAX/2;
270 lock_quick_lock(&alloc->super->lock);
271 alloc_set_special_next(p, alloc->super->quar);
272 alloc->super->quar = mem;
273 alloc->super->num_quar += ALLOC_SPECIAL_MAX/2 + 1;
274 lock_quick_unlock(&alloc->super->lock);
275 /* so 1 lock per mem+alloc/2 deletes */
279 alloc_special_release(struct alloc_cache* alloc, alloc_special_type* mem)
281 log_assert(alloc);
284 if(!alloc->super) {
285 lock_quick_lock(&alloc->lock); /* superalloc needs locking */
289 if(alloc->super && alloc->num_quar >= ALLOC_SPECIAL_MAX) {
291 pushintosuper(alloc, mem);
295 alloc_set_special_next(mem, alloc->quar);
296 alloc->quar = mem;
297 alloc->num_quar++;
298 if(!alloc->super) {
299 lock_quick_unlock(&alloc->lock);
304 alloc_stats(struct alloc_cache* alloc)
306 log_info("%salloc: %d in cache, %d blocks.", alloc->super?"":"sup",
307 (int)alloc->num_quar, (int)alloc->num_reg_blocks);
310 size_t alloc_get_mem(struct alloc_cache* alloc)
313 size_t s = sizeof(*alloc);
314 if(!alloc->super) {
315 lock_quick_lock(&alloc->lock); /* superalloc needs locking */
317 s += sizeof(alloc_special_type) * alloc->num_quar;
318 for(p = alloc->quar; p; p = alloc_special_next(p)) {
321 s += alloc->num_reg_blocks * ALLOC_REG_SIZE;
322 if(!alloc->super) {
323 lock_quick_unlock(&alloc->lock);
329 alloc_reg_obtain(struct alloc_cache* alloc)
331 if(alloc->num_reg_blocks > 0) {
332 struct regional* r = alloc->reg_list;
333 alloc->reg_list = (struct regional*)r->next;
335 alloc->num_reg_blocks--;
342 alloc_reg_release(struct alloc_cache* alloc, struct regional* r)
344 if(alloc->num_reg_blocks >= alloc->max_reg_blocks) {
351 r->next = (char*)alloc->reg_list;
352 alloc->reg_list = r;
353 alloc->num_reg_blocks++;
357 alloc_set_id_cleanup(struct alloc_cache* alloc, void (*cleanup)(void*),
360 alloc->cleanup = cleanup;
361 alloc->cleanup_arg = arg;
482 /** log to file where alloc was done */
490 /** log to file where alloc was done */
513 /** log to file where alloc was done */
522 /** log to file where alloc was done */
597 fatal_exit("alloc assertion failed");
602 log_err("alloc size is %d", (int)orig);
606 fatal_exit("alloc assertion failed");
633 fatal_exit("alloc assertion failed");
638 log_err("alloc size is %d", (int)orig);
642 fatal_exit("alloc assertion failed");
644 /* new alloc and copy over */