slab: bypass slab logic
authorMarko Kreen <markokr@gmail.com>
Tue, 2 Mar 2010 12:16:54 +0000 (12:16 +0000)
committerMarko Kreen <markokr@gmail.com>
Wed, 3 Mar 2010 07:50:22 +0000 (09:50 +0200)
usual/slab.c

index 3beffa95b2ef9a95c4d3905da30791781efcb62e..faf85dd2fdf10dd88cdd06e9dbfa980f77cd1bbc 100644 (file)
@@ -33,6 +33,8 @@
 
 #include <usual/statlist.h>
 
+#ifndef USUAL_FAKE_SLAB
+
 /*
  * Store for pre-initialized objects of one type.
  */
@@ -46,6 +48,7 @@ struct Slab {
        slab_init_fn  init_func;
 };
 
+
 /*
  * Header for each slab.
  */
@@ -219,3 +222,65 @@ void slab_stats(slab_stat_fn cb_func, void *cb_arg)
        }
 }
 
+#else
+
+struct Slab {
+       int size;
+       struct StatList obj_list;
+       slab_init_fn init_func;
+};
+
+
+struct Slab *slab_create(const char *name, unsigned obj_size, unsigned align,
+                            slab_init_fn init_func)
+{
+       struct Slab *s = malloc(sizeof(*s));
+       if (s) {
+               s->size = obj_size;
+               s->init_func = init_func;
+               statlist_init(&s->obj_list, "obj_list");
+       }
+       return s;
+}
+
+void slab_destroy(struct Slab *slab)
+{
+       struct List *el, *tmp;
+       statlist_for_each_safe(el, &slab->obj_list, tmp) {
+               statlist_remove(&slab->obj_list, el);
+               free(el);
+       }
+       free(slab);
+}
+
+void *slab_alloc(struct Slab *slab)
+{
+       struct List *o;
+       void *res;
+       o = calloc(1, sizeof(struct List) + slab->size);
+       if (!o)
+               return NULL;
+       list_init(o);
+       statlist_append(&slab->obj_list, o);
+       res = (void *)(o + 1);
+       if (slab->init_func)
+               slab->init_func(res);
+       return res;
+}
+
+void slab_free(struct Slab *slab, void *obj)
+{
+       if (obj) {
+               struct List *el = obj;
+               statlist_remove(&slab->obj_list, el - 1);
+               free(el - 1);
+       }
+}
+
+int slab_total_count(const struct Slab *slab) { return 0; }
+int slab_free_count(const struct Slab *slab) { return 0; }
+int slab_active_count(const struct Slab *slab) { return 0; }
+void slab_stats(slab_stat_fn cb_func, void *cb_arg) {}
+
+
+#endif