taisei/src/objectpool.h

50 lines
1.5 KiB
C
Raw Normal View History

2017-12-20 19:53:09 +01:00
/*
* This software is licensed under the terms of the MIT License.
2017-12-20 19:53:09 +01:00
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
2019-07-03 20:00:56 +02:00
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
2017-12-20 19:53:09 +01:00
*/
2017-12-13 20:05:12 +01:00
#pragma once
#include "taisei.h"
2017-12-13 20:05:12 +01:00
#include "list.h"
#ifdef DEBUG
#define OBJPOOL_DEBUG
2017-12-13 20:05:12 +01:00
#endif
#ifdef OBJPOOL_DEBUG
2019-04-12 10:36:40 +02:00
#define OBJPOOL_TRACK_STATS
#define IF_OBJPOOL_DEBUG(code) code
2017-12-13 20:05:12 +01:00
#else
#define IF_OBJPOOL_DEBUG(code)
2017-12-13 20:05:12 +01:00
#endif
typedef struct ObjectPool ObjectPool;
typedef struct ObjectPoolStats ObjectPoolStats;
struct ObjectPoolStats {
const char *tag;
size_t capacity;
size_t usage;
size_t peak_usage;
2017-12-13 20:05:12 +01:00
};
2019-04-12 10:36:40 +02:00
#define OBJPOOL_ALLOC(typename,max_objects) objpool_alloc(sizeof(typename), max_objects, #typename)
#define OBJPOOL_ACQUIRE(pool, type) CASTPTR_ASSUME_ALIGNED(objpool_acquire(pool), type)
2017-12-13 20:05:12 +01:00
ObjectPool *objpool_alloc(size_t obj_size, size_t max_objects, const char *tag) attr_returns_allocated attr_nonnull(3);
2019-04-12 10:36:40 +02:00
void objpool_free(ObjectPool *pool) attr_nonnull(1);
void *objpool_acquire(ObjectPool *pool) attr_returns_allocated attr_hot attr_nonnull(1);
2019-04-12 10:36:40 +02:00
void objpool_release(ObjectPool *pool, void *object) attr_hot attr_nonnull(1, 2);
void objpool_get_stats(ObjectPool *pool, ObjectPoolStats *stats) attr_nonnull(1, 2);
size_t objpool_object_size(ObjectPool *pool) attr_nonnull(1);
2019-04-12 10:36:40 +02:00
#ifdef OBJPOOL_DEBUG
void objpool_memtest(ObjectPool *pool, void *object) attr_nonnull(1, 2);
#else
2019-04-12 10:36:40 +02:00
#define objpool_memtest(pool, object) ((void)(pool), (void)(object))
#endif