blob: c8f1642eb54cfc7863182286be03d03599dd4dae [file] [log] [blame]
Jens Axboed24c33a2008-03-01 18:27:36 +01001/*
2 * simple memory allocator, backed by mmap() so that it hands out memory
3 * that can be shared across processes and threads
4 */
5#include <sys/mman.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <assert.h>
9#include <string.h>
10#include <unistd.h>
Bruce Crane43606c2012-02-20 09:34:24 +010011#include <inttypes.h>
Jens Axboed24c33a2008-03-01 18:27:36 +010012#include <sys/types.h>
13#include <limits.h>
Greg Edwards3a8600b2010-06-25 09:24:19 -060014#include <fcntl.h>
Jens Axboed24c33a2008-03-01 18:27:36 +010015
Jens Axboe6548f472008-06-13 08:38:31 +020016#include "mutex.h"
Jens Axboeb3268b92008-06-02 09:59:32 +020017#include "arch/arch.h"
Greg Edwards3a8600b2010-06-25 09:24:19 -060018#include "os/os.h"
Jens Axboe10aa1362014-04-01 21:10:36 -060019#include "smalloc.h"
Jens Axboed24c33a2008-03-01 18:27:36 +010020
Jens Axboe55f64912008-05-26 09:37:21 +020021#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010022
Jens Axboeec996e92008-05-27 11:39:39 +020023#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
24#define SMALLOC_BPI (sizeof(unsigned int) * 8)
25#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
26
Jens Axboe5e012982009-07-31 11:23:19 +020027#define INITIAL_SIZE 8192*1024 /* new pool size */
Jens Axboe68857682008-07-31 19:50:54 +020028#define MAX_POOLS 128 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010029
Jens Axboe55f64912008-05-26 09:37:21 +020030#define SMALLOC_PRE_RED 0xdeadbeefU
31#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020032
Jens Axboe2b386d22008-03-26 10:32:57 +010033unsigned int smalloc_pool_size = INITIAL_SIZE;
Jens Axboe10aa1362014-04-01 21:10:36 -060034static const int int_mask = sizeof(int) - 1;
Jens Axboe2b386d22008-03-26 10:32:57 +010035
Jens Axboed24c33a2008-03-01 18:27:36 +010036struct pool {
Jens Axboe6548f472008-06-13 08:38:31 +020037 struct fio_mutex *lock; /* protects this pool */
Jens Axboed24c33a2008-03-01 18:27:36 +010038 void *map; /* map of blocks */
Jens Axboeec996e92008-05-27 11:39:39 +020039 unsigned int *bitmap; /* blocks free/busy map */
Jens Axboea3ebe7e2012-11-02 16:47:03 +010040 size_t free_blocks; /* free blocks */
41 size_t nr_blocks; /* total blocks */
42 size_t next_non_full;
43 size_t mmap_size;
Jens Axboeec996e92008-05-27 11:39:39 +020044};
45
46struct block_hdr {
Jens Axboea3ebe7e2012-11-02 16:47:03 +010047 size_t size;
Jens Axboeec996e92008-05-27 11:39:39 +020048#ifdef SMALLOC_REDZONE
49 unsigned int prered;
50#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010051};
52
53static struct pool mp[MAX_POOLS];
54static unsigned int nr_pools;
55static unsigned int last_pool;
Jens Axboed7df1d12013-03-20 19:57:01 -060056static struct fio_rwlock *lock;
Jens Axboed24c33a2008-03-01 18:27:36 +010057
Jens Axboed24c33a2008-03-01 18:27:36 +010058static inline void pool_lock(struct pool *pool)
59{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010060 fio_mutex_down(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010061}
62
63static inline void pool_unlock(struct pool *pool)
64{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010065 fio_mutex_up(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010066}
67
Jens Axboe65864cf2008-03-03 10:36:36 +010068static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010069{
Jens Axboed7df1d12013-03-20 19:57:01 -060070 fio_rwlock_read(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010071}
72
Jens Axboe65864cf2008-03-03 10:36:36 +010073static inline void global_read_unlock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010074{
Jens Axboed7df1d12013-03-20 19:57:01 -060075 fio_rwlock_unlock(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010076}
77
78static inline void global_write_lock(void)
79{
Jens Axboed7df1d12013-03-20 19:57:01 -060080 fio_rwlock_write(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010081}
82
83static inline void global_write_unlock(void)
84{
Jens Axboed7df1d12013-03-20 19:57:01 -060085 fio_rwlock_unlock(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010086}
87
Jens Axboed24c33a2008-03-01 18:27:36 +010088static inline int ptr_valid(struct pool *pool, void *ptr)
89{
Jens Axboedcb69092008-05-27 20:35:18 +020090 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
Jens Axboeec996e92008-05-27 11:39:39 +020091
92 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010093}
94
Jens Axboea3ebe7e2012-11-02 16:47:03 +010095static inline size_t size_to_blocks(size_t size)
Jens Axboe808e9ea2008-05-27 14:12:45 +020096{
97 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
98}
99
Jens Axboedcb69092008-05-27 20:35:18 +0200100static int blocks_iter(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100101 unsigned int idx, size_t nr_blocks,
Jens Axboeec996e92008-05-27 11:39:39 +0200102 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100103{
Jens Axboedcb69092008-05-27 20:35:18 +0200104
Jens Axboeec996e92008-05-27 11:39:39 +0200105 while (nr_blocks) {
106 unsigned int this_blocks, mask;
Jens Axboedcb69092008-05-27 20:35:18 +0200107 unsigned int *map;
108
109 if (pool_idx >= pool->nr_blocks)
110 return 0;
111
112 map = &pool->bitmap[pool_idx];
Jens Axboed24c33a2008-03-01 18:27:36 +0100113
Jens Axboeec996e92008-05-27 11:39:39 +0200114 this_blocks = nr_blocks;
115 if (this_blocks + idx > SMALLOC_BPI) {
116 this_blocks = SMALLOC_BPI - idx;
117 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100118 }
Jens Axboeec996e92008-05-27 11:39:39 +0200119
120 if (this_blocks == SMALLOC_BPI)
121 mask = -1U;
122 else
123 mask = ((1U << this_blocks) - 1) << idx;
124
125 if (!func(map, mask))
126 return 0;
127
128 nr_blocks -= this_blocks;
129 idx = 0;
Jens Axboedcb69092008-05-27 20:35:18 +0200130 pool_idx++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100131 }
132
Jens Axboeec996e92008-05-27 11:39:39 +0200133 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200134}
135
136static int mask_cmp(unsigned int *map, unsigned int mask)
137{
138 return !(*map & mask);
139}
140
141static int mask_clear(unsigned int *map, unsigned int mask)
142{
Jens Axboedcb69092008-05-27 20:35:18 +0200143 assert((*map & mask) == mask);
Jens Axboeec996e92008-05-27 11:39:39 +0200144 *map &= ~mask;
145 return 1;
146}
147
148static int mask_set(unsigned int *map, unsigned int mask)
149{
Jens Axboedcb69092008-05-27 20:35:18 +0200150 assert(!(*map & mask));
Jens Axboeec996e92008-05-27 11:39:39 +0200151 *map |= mask;
152 return 1;
153}
154
Jens Axboedcb69092008-05-27 20:35:18 +0200155static int blocks_free(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100156 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200157{
Jens Axboedcb69092008-05-27 20:35:18 +0200158 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
Jens Axboeec996e92008-05-27 11:39:39 +0200159}
160
Jens Axboedcb69092008-05-27 20:35:18 +0200161static void set_blocks(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100162 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200163{
Jens Axboedcb69092008-05-27 20:35:18 +0200164 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
Jens Axboeec996e92008-05-27 11:39:39 +0200165}
166
Jens Axboedcb69092008-05-27 20:35:18 +0200167static void clear_blocks(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100168 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200169{
Jens Axboedcb69092008-05-27 20:35:18 +0200170 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
Jens Axboeec996e92008-05-27 11:39:39 +0200171}
172
Jens Axboeec996e92008-05-27 11:39:39 +0200173static int find_next_zero(int word, int start)
174{
175 assert(word != -1U);
Jiri Horky271067a2011-08-15 15:18:03 +0200176 word >>= start;
177 return ffz(word) + start;
Jens Axboed24c33a2008-03-01 18:27:36 +0100178}
179
Jens Axboeadf57092008-04-16 19:43:17 +0200180static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100181{
Jens Axboe8d5844e2011-06-29 09:50:08 +0200182 int bitmap_blocks;
Jens Axboeb8a65822008-12-04 14:33:41 +0100183 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200184
Jens Axboe55f64912008-05-26 09:37:21 +0200185#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200186 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200187#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200188 alloc_size += sizeof(struct block_hdr);
189 if (alloc_size < INITIAL_SIZE)
190 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200191
Jens Axboeec996e92008-05-27 11:39:39 +0200192 /* round up to nearest full number of blocks */
193 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
194 bitmap_blocks = alloc_size / SMALLOC_BPL;
195 alloc_size += bitmap_blocks * sizeof(unsigned int);
196 pool->mmap_size = alloc_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200197
Jens Axboeec996e92008-05-27 11:39:39 +0200198 pool->nr_blocks = bitmap_blocks;
199 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
200
Jens Axboe8d5844e2011-06-29 09:50:08 +0200201 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE,
202 MAP_SHARED | OS_MAP_ANON, -1, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100203 if (ptr == MAP_FAILED)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200204 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100205
Jens Axboeec996e92008-05-27 11:39:39 +0200206 memset(ptr, 0, alloc_size);
207 pool->map = ptr;
208 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100209
Jens Axboe521da522012-08-02 11:21:36 +0200210 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboed24c33a2008-03-01 18:27:36 +0100211 if (!pool->lock)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200212 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100213
Jens Axboed24c33a2008-03-01 18:27:36 +0100214 nr_pools++;
215 return 0;
Jens Axboe8d5844e2011-06-29 09:50:08 +0200216out_fail:
Jens Axboeec996e92008-05-27 11:39:39 +0200217 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100218 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200219 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100220 return 1;
221}
222
223void sinit(void)
224{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100225 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100226
Jens Axboed7df1d12013-03-20 19:57:01 -0600227 lock = fio_rwlock_init();
Jens Axboeadf57092008-04-16 19:43:17 +0200228 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100229 assert(!ret);
230}
231
232static void cleanup_pool(struct pool *pool)
233{
Jens Axboe443bb112008-12-04 14:30:13 +0100234 /*
235 * This will also remove the temporary file we used as a backing
236 * store, it was already unlinked
237 */
Jens Axboeec996e92008-05-27 11:39:39 +0200238 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200239
240 if (pool->lock)
241 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100242}
243
244void scleanup(void)
245{
246 unsigned int i;
247
248 for (i = 0; i < nr_pools; i++)
249 cleanup_pool(&mp[i]);
250
Jens Axboe6548f472008-06-13 08:38:31 +0200251 if (lock)
Jens Axboed7df1d12013-03-20 19:57:01 -0600252 fio_rwlock_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100253}
254
Jens Axboe89da54e2008-05-27 20:49:29 +0200255#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200256static void *postred_ptr(struct block_hdr *hdr)
257{
Bruce Crane43606c2012-02-20 09:34:24 +0100258 uintptr_t ptr;
Jens Axboecf987082009-07-07 17:43:11 +0200259
Bruce Crane43606c2012-02-20 09:34:24 +0100260 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
Jens Axboecf987082009-07-07 17:43:11 +0200261 ptr = (ptr + int_mask) & ~int_mask;
262
263 return (void *) ptr;
264}
265
Jens Axboeec996e92008-05-27 11:39:39 +0200266static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200267{
Jens Axboecf987082009-07-07 17:43:11 +0200268 unsigned int *postred = postred_ptr(hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200269
Jens Axboeec996e92008-05-27 11:39:39 +0200270 hdr->prered = SMALLOC_PRE_RED;
271 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200272}
Jens Axboe55f64912008-05-26 09:37:21 +0200273
Jens Axboeec996e92008-05-27 11:39:39 +0200274static void sfree_check_redzone(struct block_hdr *hdr)
275{
Jens Axboecf987082009-07-07 17:43:11 +0200276 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200277
278 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200279 fprintf(stderr, "smalloc pre redzone destroyed!\n");
280 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200281 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200282 assert(0);
283 }
284 if (*postred != SMALLOC_POST_RED) {
285 fprintf(stderr, "smalloc post redzone destroyed!\n");
286 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200287 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200288 assert(0);
289 }
Jens Axboe55f64912008-05-26 09:37:21 +0200290}
Jens Axboe89da54e2008-05-27 20:49:29 +0200291#else
292static void fill_redzone(struct block_hdr *hdr)
293{
294}
295
296static void sfree_check_redzone(struct block_hdr *hdr)
297{
298}
299#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200300
Jens Axboed24c33a2008-03-01 18:27:36 +0100301static void sfree_pool(struct pool *pool, void *ptr)
302{
Jens Axboeec996e92008-05-27 11:39:39 +0200303 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200304 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200305 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100306
307 if (!ptr)
308 return;
309
Jens Axboeec996e92008-05-27 11:39:39 +0200310 ptr -= sizeof(*hdr);
311 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200312
Jens Axboed24c33a2008-03-01 18:27:36 +0100313 assert(ptr_valid(pool, ptr));
314
Jens Axboeec996e92008-05-27 11:39:39 +0200315 sfree_check_redzone(hdr);
316
317 offset = ptr - pool->map;
318 i = offset / SMALLOC_BPL;
319 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
320
Jens Axboed24c33a2008-03-01 18:27:36 +0100321 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200322 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200323 if (i < pool->next_non_full)
324 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200325 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100326 pool_unlock(pool);
327}
328
329void sfree(void *ptr)
330{
331 struct pool *pool = NULL;
332 unsigned int i;
333
Jens Axboe8e5732e2008-04-16 19:51:46 +0200334 if (!ptr)
335 return;
336
Jens Axboe65864cf2008-03-03 10:36:36 +0100337 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100338
339 for (i = 0; i < nr_pools; i++) {
340 if (ptr_valid(&mp[i], ptr)) {
341 pool = &mp[i];
342 break;
343 }
344 }
345
Jens Axboe65864cf2008-03-03 10:36:36 +0100346 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100347
348 assert(pool);
349 sfree_pool(pool, ptr);
350}
351
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100352static void *__smalloc_pool(struct pool *pool, size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100353{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100354 size_t nr_blocks;
Jens Axboeec996e92008-05-27 11:39:39 +0200355 unsigned int i;
356 unsigned int offset;
357 unsigned int last_idx;
358 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100359
Jens Axboed24c33a2008-03-01 18:27:36 +0100360 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200361
362 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200363 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100364 goto fail;
365
Jens Axboeec996e92008-05-27 11:39:39 +0200366 i = pool->next_non_full;
367 last_idx = 0;
368 offset = -1U;
369 while (i < pool->nr_blocks) {
370 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100371
Jens Axboeec996e92008-05-27 11:39:39 +0200372 if (pool->bitmap[i] == -1U) {
373 i++;
374 pool->next_non_full = i;
375 last_idx = 0;
376 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100377 }
Jens Axboeec996e92008-05-27 11:39:39 +0200378
379 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200380 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200381 idx += nr_blocks;
382 if (idx < SMALLOC_BPI)
383 last_idx = idx;
384 else {
385 last_idx = 0;
386 while (idx >= SMALLOC_BPI) {
387 i++;
388 idx -= SMALLOC_BPI;
389 }
390 }
391 continue;
392 }
Jens Axboedcb69092008-05-27 20:35:18 +0200393 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200394 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
395 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100396 }
Jens Axboeec996e92008-05-27 11:39:39 +0200397
398 if (i < pool->nr_blocks) {
399 pool->free_blocks -= nr_blocks;
400 ret = pool->map + offset;
401 }
402fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100403 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200404 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100405}
406
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100407static void *smalloc_pool(struct pool *pool, size_t size)
Jens Axboe55f64912008-05-26 09:37:21 +0200408{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100409 size_t alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200410 void *ptr;
411
Jens Axboecf987082009-07-07 17:43:11 +0200412 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200413 * Round to int alignment, so that the postred pointer will
414 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200415 */
Jens Axboeec996e92008-05-27 11:39:39 +0200416#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200417 alloc_size += sizeof(unsigned int);
418 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200419#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200420
421 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200422 if (ptr) {
423 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200424
Jens Axboe89da54e2008-05-27 20:49:29 +0200425 hdr->size = alloc_size;
426 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200427
Jens Axboe89da54e2008-05-27 20:49:29 +0200428 ptr += sizeof(*hdr);
429 memset(ptr, 0, size);
430 }
Jens Axboeec996e92008-05-27 11:39:39 +0200431
Jens Axboeec996e92008-05-27 11:39:39 +0200432 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200433}
434
Jens Axboe7982aa72012-11-02 16:35:18 +0100435void *smalloc(size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100436{
437 unsigned int i;
438
Jens Axboe7982aa72012-11-02 16:35:18 +0100439 if (size != (unsigned int) size)
440 return NULL;
441
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200442 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100443 i = last_pool;
444
445 do {
446 for (; i < nr_pools; i++) {
447 void *ptr = smalloc_pool(&mp[i], size);
448
449 if (ptr) {
450 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200451 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100452 return ptr;
453 }
454 }
455 if (last_pool) {
456 last_pool = 0;
457 continue;
458 }
459
Jens Axboeec996e92008-05-27 11:39:39 +0200460 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100461 break;
462 else {
463 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200464 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100465 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100466 }
467 } while (1);
468
Jens Axboe65864cf2008-03-03 10:36:36 +0100469out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200470 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100471 return NULL;
472}
473
474char *smalloc_strdup(const char *str)
475{
476 char *ptr;
477
478 ptr = smalloc(strlen(str) + 1);
479 strcpy(ptr, str);
480 return ptr;
481}