2
* Copyright 2010 Marek Olšák <maraeo@gmail.com>
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* on the rights to use, copy, modify, merge, publish, distribute, sub
8
* license, and/or sell copies of the Software, and to permit persons to whom
9
* the Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
#include "util/u_mempool.h"
25
#include "util/u_math.h"
26
#include "util/u_memory.h"
27
#include "util/u_simple_list.h"
31
#define UTIL_MEMPOOL_MAGIC 0xcafe4321
33
/* The block is either allocated memory or free space. */
34
struct util_mempool_block {
36
/* The first next free block. */
37
struct util_mempool_block *next_free;
41
/* Memory after the last member is dedicated to the block itself.
42
* The allocated size is always larger than this structure. */
45
static struct util_mempool_block *
46
util_mempool_get_block(struct util_mempool *pool,
47
struct util_mempool_page *page, unsigned index)
49
return (struct util_mempool_block*)
50
((uint8_t*)page + sizeof(struct util_mempool_page) +
51
(pool->block_size * index));
54
static void util_mempool_add_new_page(struct util_mempool *pool)
56
struct util_mempool_page *page;
57
struct util_mempool_block *block;
60
page = MALLOC(pool->page_size);
61
insert_at_tail(&pool->list, page);
63
/* Mark all blocks as free. */
64
for (i = 0; i < pool->num_blocks-1; i++) {
65
block = util_mempool_get_block(pool, page, i);
66
block->next_free = util_mempool_get_block(pool, page, i+1);
67
block->magic = UTIL_MEMPOOL_MAGIC;
70
block = util_mempool_get_block(pool, page, pool->num_blocks-1);
71
block->next_free = pool->first_free;
72
block->magic = UTIL_MEMPOOL_MAGIC;
73
pool->first_free = util_mempool_get_block(pool, page, 0);
77
fprintf(stderr, "New page! Num of pages: %i\n", pool->num_pages);
81
static void *util_mempool_malloc_st(struct util_mempool *pool)
83
struct util_mempool_block *block;
85
if (!pool->first_free)
86
util_mempool_add_new_page(pool);
88
block = pool->first_free;
89
assert(block->magic == UTIL_MEMPOOL_MAGIC);
90
pool->first_free = block->next_free;
92
return (uint8_t*)block + sizeof(struct util_mempool_block);
95
static void util_mempool_free_st(struct util_mempool *pool, void *ptr)
97
struct util_mempool_block *block =
98
(struct util_mempool_block*)
99
((uint8_t*)ptr - sizeof(struct util_mempool_block));
101
assert(block->magic == UTIL_MEMPOOL_MAGIC);
102
block->next_free = pool->first_free;
103
pool->first_free = block;
106
static void *util_mempool_malloc_mt(struct util_mempool *pool)
110
pipe_mutex_lock(pool->mutex);
111
mem = util_mempool_malloc_st(pool);
112
pipe_mutex_unlock(pool->mutex);
116
static void util_mempool_free_mt(struct util_mempool *pool, void *ptr)
118
pipe_mutex_lock(pool->mutex);
119
util_mempool_free_st(pool, ptr);
120
pipe_mutex_unlock(pool->mutex);
123
void util_mempool_set_thread_safety(struct util_mempool *pool,
124
enum util_mempool_threading threading)
126
pool->threading = threading;
129
pool->malloc = util_mempool_malloc_mt;
130
pool->free = util_mempool_free_mt;
132
pool->malloc = util_mempool_malloc_st;
133
pool->free = util_mempool_free_st;
137
void util_mempool_create(struct util_mempool *pool,
140
enum util_mempool_threading threading)
142
item_size = align(item_size, sizeof(intptr_t));
145
pool->num_blocks = num_blocks;
146
pool->block_size = sizeof(struct util_mempool_block) + item_size;
147
pool->block_size = align(pool->block_size, sizeof(intptr_t));
148
pool->page_size = sizeof(struct util_mempool_page) +
149
num_blocks * pool->block_size;
150
pool->first_free = NULL;
152
make_empty_list(&pool->list);
154
pipe_mutex_init(pool->mutex);
156
util_mempool_set_thread_safety(pool, threading);
159
void util_mempool_destroy(struct util_mempool *pool)
161
struct util_mempool_page *page, *temp;
163
foreach_s(page, temp, &pool->list) {
164
remove_from_list(page);
168
pipe_mutex_destroy(pool->mutex);