-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobject_pool.h
33 lines (26 loc) · 910 Bytes
/
object_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef _OBJECT_POOL_H_
#define _OBJECT_POOL_H_
#include <stddef.h>
#include <stdint.h>
typedef struct {
// TODO: add other members to speed things up
void *data;
} PoolElement;
typedef int (*Ctor)(void *(*malloc)(size_t), void (*free)(void *), void *dst, void *args);
typedef int (*Dtor)(void *(*malloc)(size_t), void (*free)(void *), void *dst);
typedef struct {
void *(*malloc)(size_t size);
void (*free)(void *mem);
Ctor ctor;
Dtor dtor;
uint32_t size;
uint32_t element_size;
PoolElement *objects;
} ObjectPool;
ObjectPool op_allocate(uint32_t size, uint32_t element_size,
void *(*)(size_t), void (*)(void *), Ctor ctor, Dtor dtor);
void op_release(ObjectPool *pool);
// although this function return index, you can access it through pool->objects[id]
int32_t op_register(ObjectPool *pool, void *args);
void op_unregister(ObjectPool *pool, int32_t id);
#endif