blob: d0f732baee6cc6515e26f554432fae87958c0bf6 [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 Axboeef7035a2014-06-18 15:30:09 -0700183 int mmap_flags;
Jens Axboeb8a65822008-12-04 14:33:41 +0100184 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200185
Jens Axboe55f64912008-05-26 09:37:21 +0200186#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200187 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200188#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200189 alloc_size += sizeof(struct block_hdr);
190 if (alloc_size < INITIAL_SIZE)
191 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200192
Jens Axboeec996e92008-05-27 11:39:39 +0200193 /* round up to nearest full number of blocks */
194 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
195 bitmap_blocks = alloc_size / SMALLOC_BPL;
196 alloc_size += bitmap_blocks * sizeof(unsigned int);
197 pool->mmap_size = alloc_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200198
Jens Axboeec996e92008-05-27 11:39:39 +0200199 pool->nr_blocks = bitmap_blocks;
200 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
201
Jens Axboeef7035a2014-06-18 15:30:09 -0700202 mmap_flags = OS_MAP_ANON;
203#ifdef CONFIG_ESX
204 mmap_flags |= MAP_PRIVATE;
205#else
206 mmap_flags |= MAP_SHARED;
207#endif
208 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
209
Jens Axboed24c33a2008-03-01 18:27:36 +0100210 if (ptr == MAP_FAILED)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200211 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100212
Jens Axboeec996e92008-05-27 11:39:39 +0200213 memset(ptr, 0, alloc_size);
214 pool->map = ptr;
215 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100216
Jens Axboe521da522012-08-02 11:21:36 +0200217 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboed24c33a2008-03-01 18:27:36 +0100218 if (!pool->lock)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200219 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100220
Jens Axboed24c33a2008-03-01 18:27:36 +0100221 nr_pools++;
222 return 0;
Jens Axboe8d5844e2011-06-29 09:50:08 +0200223out_fail:
Jens Axboeec996e92008-05-27 11:39:39 +0200224 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100225 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200226 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100227 return 1;
228}
229
230void sinit(void)
231{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100232 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100233
Jens Axboed7df1d12013-03-20 19:57:01 -0600234 lock = fio_rwlock_init();
Jens Axboeadf57092008-04-16 19:43:17 +0200235 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100236 assert(!ret);
237}
238
239static void cleanup_pool(struct pool *pool)
240{
Jens Axboe443bb112008-12-04 14:30:13 +0100241 /*
242 * This will also remove the temporary file we used as a backing
243 * store, it was already unlinked
244 */
Jens Axboeec996e92008-05-27 11:39:39 +0200245 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200246
247 if (pool->lock)
248 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100249}
250
251void scleanup(void)
252{
253 unsigned int i;
254
255 for (i = 0; i < nr_pools; i++)
256 cleanup_pool(&mp[i]);
257
Jens Axboe6548f472008-06-13 08:38:31 +0200258 if (lock)
Jens Axboed7df1d12013-03-20 19:57:01 -0600259 fio_rwlock_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100260}
261
Jens Axboe89da54e2008-05-27 20:49:29 +0200262#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200263static void *postred_ptr(struct block_hdr *hdr)
264{
Bruce Crane43606c2012-02-20 09:34:24 +0100265 uintptr_t ptr;
Jens Axboecf987082009-07-07 17:43:11 +0200266
Bruce Crane43606c2012-02-20 09:34:24 +0100267 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
Jens Axboecf987082009-07-07 17:43:11 +0200268 ptr = (ptr + int_mask) & ~int_mask;
269
270 return (void *) ptr;
271}
272
Jens Axboeec996e92008-05-27 11:39:39 +0200273static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200274{
Jens Axboecf987082009-07-07 17:43:11 +0200275 unsigned int *postred = postred_ptr(hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200276
Jens Axboeec996e92008-05-27 11:39:39 +0200277 hdr->prered = SMALLOC_PRE_RED;
278 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200279}
Jens Axboe55f64912008-05-26 09:37:21 +0200280
Jens Axboeec996e92008-05-27 11:39:39 +0200281static void sfree_check_redzone(struct block_hdr *hdr)
282{
Jens Axboecf987082009-07-07 17:43:11 +0200283 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200284
285 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200286 fprintf(stderr, "smalloc pre redzone destroyed!\n");
287 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200288 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200289 assert(0);
290 }
291 if (*postred != SMALLOC_POST_RED) {
292 fprintf(stderr, "smalloc post redzone destroyed!\n");
293 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200294 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200295 assert(0);
296 }
Jens Axboe55f64912008-05-26 09:37:21 +0200297}
Jens Axboe89da54e2008-05-27 20:49:29 +0200298#else
299static void fill_redzone(struct block_hdr *hdr)
300{
301}
302
303static void sfree_check_redzone(struct block_hdr *hdr)
304{
305}
306#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200307
Jens Axboed24c33a2008-03-01 18:27:36 +0100308static void sfree_pool(struct pool *pool, void *ptr)
309{
Jens Axboeec996e92008-05-27 11:39:39 +0200310 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200311 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200312 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100313
314 if (!ptr)
315 return;
316
Jens Axboeec996e92008-05-27 11:39:39 +0200317 ptr -= sizeof(*hdr);
318 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200319
Jens Axboed24c33a2008-03-01 18:27:36 +0100320 assert(ptr_valid(pool, ptr));
321
Jens Axboeec996e92008-05-27 11:39:39 +0200322 sfree_check_redzone(hdr);
323
324 offset = ptr - pool->map;
325 i = offset / SMALLOC_BPL;
326 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
327
Jens Axboed24c33a2008-03-01 18:27:36 +0100328 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200329 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200330 if (i < pool->next_non_full)
331 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200332 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100333 pool_unlock(pool);
334}
335
336void sfree(void *ptr)
337{
338 struct pool *pool = NULL;
339 unsigned int i;
340
Jens Axboe8e5732e2008-04-16 19:51:46 +0200341 if (!ptr)
342 return;
343
Jens Axboe65864cf2008-03-03 10:36:36 +0100344 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100345
346 for (i = 0; i < nr_pools; i++) {
347 if (ptr_valid(&mp[i], ptr)) {
348 pool = &mp[i];
349 break;
350 }
351 }
352
Jens Axboe65864cf2008-03-03 10:36:36 +0100353 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100354
355 assert(pool);
356 sfree_pool(pool, ptr);
357}
358
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100359static void *__smalloc_pool(struct pool *pool, size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100360{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100361 size_t nr_blocks;
Jens Axboeec996e92008-05-27 11:39:39 +0200362 unsigned int i;
363 unsigned int offset;
364 unsigned int last_idx;
365 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100366
Jens Axboed24c33a2008-03-01 18:27:36 +0100367 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200368
369 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200370 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100371 goto fail;
372
Jens Axboeec996e92008-05-27 11:39:39 +0200373 i = pool->next_non_full;
374 last_idx = 0;
375 offset = -1U;
376 while (i < pool->nr_blocks) {
377 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100378
Jens Axboeec996e92008-05-27 11:39:39 +0200379 if (pool->bitmap[i] == -1U) {
380 i++;
381 pool->next_non_full = i;
382 last_idx = 0;
383 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100384 }
Jens Axboeec996e92008-05-27 11:39:39 +0200385
386 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200387 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200388 idx += nr_blocks;
389 if (idx < SMALLOC_BPI)
390 last_idx = idx;
391 else {
392 last_idx = 0;
393 while (idx >= SMALLOC_BPI) {
394 i++;
395 idx -= SMALLOC_BPI;
396 }
397 }
398 continue;
399 }
Jens Axboedcb69092008-05-27 20:35:18 +0200400 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200401 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
402 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100403 }
Jens Axboeec996e92008-05-27 11:39:39 +0200404
405 if (i < pool->nr_blocks) {
406 pool->free_blocks -= nr_blocks;
407 ret = pool->map + offset;
408 }
409fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100410 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200411 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100412}
413
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100414static void *smalloc_pool(struct pool *pool, size_t size)
Jens Axboe55f64912008-05-26 09:37:21 +0200415{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100416 size_t alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200417 void *ptr;
418
Jens Axboecf987082009-07-07 17:43:11 +0200419 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200420 * Round to int alignment, so that the postred pointer will
421 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200422 */
Jens Axboeec996e92008-05-27 11:39:39 +0200423#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200424 alloc_size += sizeof(unsigned int);
425 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200426#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200427
428 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200429 if (ptr) {
430 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200431
Jens Axboe89da54e2008-05-27 20:49:29 +0200432 hdr->size = alloc_size;
433 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200434
Jens Axboe89da54e2008-05-27 20:49:29 +0200435 ptr += sizeof(*hdr);
436 memset(ptr, 0, size);
437 }
Jens Axboeec996e92008-05-27 11:39:39 +0200438
Jens Axboeec996e92008-05-27 11:39:39 +0200439 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200440}
441
Jens Axboe7982aa72012-11-02 16:35:18 +0100442void *smalloc(size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100443{
444 unsigned int i;
445
Jens Axboe7982aa72012-11-02 16:35:18 +0100446 if (size != (unsigned int) size)
447 return NULL;
448
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200449 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100450 i = last_pool;
451
452 do {
453 for (; i < nr_pools; i++) {
454 void *ptr = smalloc_pool(&mp[i], size);
455
456 if (ptr) {
457 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200458 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100459 return ptr;
460 }
461 }
462 if (last_pool) {
463 last_pool = 0;
464 continue;
465 }
466
Jens Axboeec996e92008-05-27 11:39:39 +0200467 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100468 break;
469 else {
470 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200471 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100472 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100473 }
474 } while (1);
475
Jens Axboe65864cf2008-03-03 10:36:36 +0100476out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200477 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100478 return NULL;
479}
480
481char *smalloc_strdup(const char *str)
482{
483 char *ptr;
484
485 ptr = smalloc(strlen(str) + 1);
486 strcpy(ptr, str);
487 return ptr;
488}