1 /* 2 * Primitive slab allocator. 3 * 4 * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /** 20 * @file 21 * 22 * Slab allocator for same-size objects. 23 * 24 * Basic behaviour: 25 * - On each alloc initializer is called. 26 * - if init func is not given, memset() is done 27 * - init func gets either zeroed obj or old obj from _free(). 28 * 'struct List' on obj start is non-zero. 29 * 30 * ATM custom 'align' larger than malloc() alignment does not work. 31 */ 32 #ifndef _USUAL_SLAB_H_ 33 #define _USUAL_SLAB_H_ 34 35 #include <usual/cxalloc.h> 36 37 /** Reference to main */ 38 struct Slab; 39 40 /** Signature for object init function */ 41 typedef void (*slab_init_fn)(void *obj); 42 43 /** Create new slab context for specific size */ 44 struct Slab *slab_create(const char *name, unsigned obj_size, unsigned align, 45 slab_init_fn init_func, 46 CxMem *cx); 47 48 /** Free whole context */ 49 void slab_destroy(struct Slab *slab); 50 51 /** Allocate single object from slab cache */ 52 void *slab_alloc(struct Slab *slab) _MALLOC _MUSTCHECK; 53 54 /** Release single object back */ 55 void slab_free(struct Slab *slab, void *obj); 56 57 /** Return sum of free and used objects */ 58 int slab_total_count(const struct Slab *slab); 59 60 /** Return number of free objects in cache */ 61 int slab_free_count(const struct Slab *slab); 62 63 /** Return number of used objects */ 64 int slab_active_count(const struct Slab *slab); 65 66 /** Signature for stat info callback */ 67 typedef void (*slab_stat_fn)(void *arg, const char *slab_name, 68 unsigned size, unsigned free, 69 unsigned total); 70 71 /** Run stat info callback on all slabs */ 72 void slab_stats(slab_stat_fn cb_func, void *cb_arg); 73 74 #endif 75