2017-12-20 19:53:09 +01:00
|
|
|
/*
|
2019-08-03 19:43:48 +02: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.
|
|
|
|
* ---
|
2024-05-16 23:30:41 +02:00
|
|
|
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
|
2017-12-20 19:53:09 +01:00
|
|
|
*/
|
2017-12-13 20:05:12 +01:00
|
|
|
|
2021-08-12 23:09:01 +02:00
|
|
|
#pragma once
|
2017-12-20 19:57:29 +01:00
|
|
|
#include "taisei.h"
|
2017-12-13 20:05:12 +01:00
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
#include "arena.h"
|
2017-12-13 20:05:12 +01:00
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
typedef struct MemPool MemPool;
|
2017-12-13 20:05:12 +01:00
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
typedef struct MemPoolObjectHeader {
|
|
|
|
alignas(alignof(max_align_t)) struct MemPoolObjectHeader *next;
|
|
|
|
} MemPoolObjectHeader;
|
2023-04-03 03:49:39 +02:00
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
struct MemPool {
|
2023-04-03 03:49:39 +02:00
|
|
|
MemArena *arena;
|
2024-06-05 18:35:08 +02:00
|
|
|
MemPoolObjectHeader *free_objects;
|
2018-01-12 19:26:07 +01:00
|
|
|
const char *tag;
|
2023-04-03 03:49:39 +02:00
|
|
|
size_t obj_size;
|
|
|
|
size_t obj_align;
|
|
|
|
int num_allocated;
|
|
|
|
int num_used;
|
2017-12-13 20:05:12 +01:00
|
|
|
};
|
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
void mempool_init(
|
|
|
|
MemPool *pool,
|
2023-04-03 03:49:39 +02:00
|
|
|
const char *tag,
|
|
|
|
MemArena *arena,
|
|
|
|
size_t obj_size,
|
|
|
|
size_t obj_align
|
|
|
|
) attr_nonnull(1, 2, 3);
|
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
void *mempool_acquire(MemPool *pool)
|
2023-04-03 03:49:39 +02:00
|
|
|
attr_returns_allocated attr_hot attr_nonnull(1);
|
|
|
|
|
2024-06-05 18:35:08 +02:00
|
|
|
void mempool_release(MemPool *pool, void *object)
|
2023-04-03 03:49:39 +02:00
|
|
|
attr_hot attr_nonnull(1, 2);
|