5 #ifndef _CTHREAD_OBJCACHE_H_
6 #define _CTHREAD_OBJCACHE_H_
12 #include "cthread_int.h"
13 #include "cthread_queue.h"
21 struct cthread_objcache {
22 struct cthread_queue *q;
25 char name[CTHREAD_NAME_SIZE];
40 static inline struct cthread_objcache *
41 _cthread_objcache_create(
const char *name,
size_t obj_size,
int prealloc_size)
43 struct cthread_objcache *c = calloc(1,
sizeof(
struct cthread_objcache));
48 c->q = _cthread_queue_create(
"cache queue");
53 c->obj_size = obj_size;
54 c->prealloc_size = prealloc_size;
58 strlcpy(c->name, name,
sizeof(c->name));
72 _cthread_objcache_destroy(
struct cthread_objcache *c)
76 if (_cthread_queue_destroy(c->q) == 0) {
92 _cthread_objcache_alloc(
struct cthread_objcache *c)
96 struct cthread_queue *q = c->q;
97 size_t obj_size = c->obj_size;
98 int prealloc_size = c->prealloc_size;
100 data = _cthread_queue_remove(q);
103 for (i = 0; i < prealloc_size; i++) {
104 data = calloc(1, obj_size);
108 _cthread_queue_insert_mp(q, data);
110 data = _cthread_queue_remove(q);
122 _cthread_objcache_free(
struct cthread_objcache *c,
void *obj)
124 _cthread_queue_insert_mp(c->q, obj);
#define CNE_DECLARE_PER_THREAD(type, name)