blob: 4cd8298eaa004d7a56d861aefcb1373ae7fd7001 [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>
11#include <sys/types.h>
12#include <limits.h>
Greg Edwards3a8600b2010-06-25 09:24:19 -060013#include <fcntl.h>
Jens Axboed24c33a2008-03-01 18:27:36 +010014
Jens Axboe6548f472008-06-13 08:38:31 +020015#include "mutex.h"
Jens Axboeb3268b92008-06-02 09:59:32 +020016#include "arch/arch.h"
Greg Edwards3a8600b2010-06-25 09:24:19 -060017#include "os/os.h"
Jens Axboed24c33a2008-03-01 18:27:36 +010018
Jens Axboe55f64912008-05-26 09:37:21 +020019#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010020
Jens Axboeec996e92008-05-27 11:39:39 +020021#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
22#define SMALLOC_BPI (sizeof(unsigned int) * 8)
23#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
24
Jens Axboe5e012982009-07-31 11:23:19 +020025#define INITIAL_SIZE 8192*1024 /* new pool size */
Jens Axboe68857682008-07-31 19:50:54 +020026#define MAX_POOLS 128 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010027
Jens Axboe55f64912008-05-26 09:37:21 +020028#define SMALLOC_PRE_RED 0xdeadbeefU
29#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020030
Jens Axboe2b386d22008-03-26 10:32:57 +010031unsigned int smalloc_pool_size = INITIAL_SIZE;
Jens Axboe122426d2009-07-07 23:03:06 +020032const int int_mask = sizeof(int) - 1;
Jens Axboe2b386d22008-03-26 10:32:57 +010033
Jens Axboed24c33a2008-03-01 18:27:36 +010034struct pool {
Jens Axboe6548f472008-06-13 08:38:31 +020035 struct fio_mutex *lock; /* protects this pool */
Jens Axboed24c33a2008-03-01 18:27:36 +010036 void *map; /* map of blocks */
Jens Axboeec996e92008-05-27 11:39:39 +020037 unsigned int *bitmap; /* blocks free/busy map */
38 unsigned int free_blocks; /* free blocks */
39 unsigned int nr_blocks; /* total blocks */
40 unsigned int next_non_full;
Jens Axboed24c33a2008-03-01 18:27:36 +010041 int fd; /* memory backing fd */
Jens Axboeec996e92008-05-27 11:39:39 +020042 unsigned int mmap_size;
43};
44
45struct block_hdr {
46 unsigned int size;
47#ifdef SMALLOC_REDZONE
48 unsigned int prered;
49#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010050};
51
52static struct pool mp[MAX_POOLS];
53static unsigned int nr_pools;
54static unsigned int last_pool;
Jens Axboe6548f472008-06-13 08:38:31 +020055static struct fio_mutex *lock;
Jens Axboed24c33a2008-03-01 18:27:36 +010056
Jens Axboed24c33a2008-03-01 18:27:36 +010057static inline void pool_lock(struct pool *pool)
58{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010059 fio_mutex_down(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010060}
61
62static inline void pool_unlock(struct pool *pool)
63{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010064 fio_mutex_up(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010065}
66
Jens Axboe65864cf2008-03-03 10:36:36 +010067static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010068{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010069 fio_mutex_down_read(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010070}
71
Jens Axboe65864cf2008-03-03 10:36:36 +010072static inline void global_read_unlock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010073{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010074 fio_mutex_up_read(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010075}
76
77static inline void global_write_lock(void)
78{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010079 fio_mutex_down_write(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010080}
81
82static inline void global_write_unlock(void)
83{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010084 fio_mutex_up_write(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010085}
86
Jens Axboed24c33a2008-03-01 18:27:36 +010087static inline int ptr_valid(struct pool *pool, void *ptr)
88{
Jens Axboedcb69092008-05-27 20:35:18 +020089 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
Jens Axboeec996e92008-05-27 11:39:39 +020090
91 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010092}
93
Jens Axboe808e9ea2008-05-27 14:12:45 +020094static inline unsigned int size_to_blocks(unsigned int size)
95{
96 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
97}
98
Jens Axboedcb69092008-05-27 20:35:18 +020099static int blocks_iter(struct pool *pool, unsigned int pool_idx,
100 unsigned int idx, unsigned int nr_blocks,
Jens Axboeec996e92008-05-27 11:39:39 +0200101 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100102{
Jens Axboedcb69092008-05-27 20:35:18 +0200103
Jens Axboeec996e92008-05-27 11:39:39 +0200104 while (nr_blocks) {
105 unsigned int this_blocks, mask;
Jens Axboedcb69092008-05-27 20:35:18 +0200106 unsigned int *map;
107
108 if (pool_idx >= pool->nr_blocks)
109 return 0;
110
111 map = &pool->bitmap[pool_idx];
Jens Axboed24c33a2008-03-01 18:27:36 +0100112
Jens Axboeec996e92008-05-27 11:39:39 +0200113 this_blocks = nr_blocks;
114 if (this_blocks + idx > SMALLOC_BPI) {
115 this_blocks = SMALLOC_BPI - idx;
116 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100117 }
Jens Axboeec996e92008-05-27 11:39:39 +0200118
119 if (this_blocks == SMALLOC_BPI)
120 mask = -1U;
121 else
122 mask = ((1U << this_blocks) - 1) << idx;
123
124 if (!func(map, mask))
125 return 0;
126
127 nr_blocks -= this_blocks;
128 idx = 0;
Jens Axboedcb69092008-05-27 20:35:18 +0200129 pool_idx++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100130 }
131
Jens Axboeec996e92008-05-27 11:39:39 +0200132 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200133}
134
135static int mask_cmp(unsigned int *map, unsigned int mask)
136{
137 return !(*map & mask);
138}
139
140static int mask_clear(unsigned int *map, unsigned int mask)
141{
Jens Axboedcb69092008-05-27 20:35:18 +0200142 assert((*map & mask) == mask);
Jens Axboeec996e92008-05-27 11:39:39 +0200143 *map &= ~mask;
144 return 1;
145}
146
147static int mask_set(unsigned int *map, unsigned int mask)
148{
Jens Axboedcb69092008-05-27 20:35:18 +0200149 assert(!(*map & mask));
Jens Axboeec996e92008-05-27 11:39:39 +0200150 *map |= mask;
151 return 1;
152}
153
Jens Axboedcb69092008-05-27 20:35:18 +0200154static int blocks_free(struct pool *pool, unsigned int pool_idx,
155 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200156{
Jens Axboedcb69092008-05-27 20:35:18 +0200157 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
Jens Axboeec996e92008-05-27 11:39:39 +0200158}
159
Jens Axboedcb69092008-05-27 20:35:18 +0200160static void set_blocks(struct pool *pool, unsigned int pool_idx,
161 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200162{
Jens Axboedcb69092008-05-27 20:35:18 +0200163 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
Jens Axboeec996e92008-05-27 11:39:39 +0200164}
165
Jens Axboedcb69092008-05-27 20:35:18 +0200166static void clear_blocks(struct pool *pool, unsigned int pool_idx,
167 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200168{
Jens Axboedcb69092008-05-27 20:35:18 +0200169 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
Jens Axboeec996e92008-05-27 11:39:39 +0200170}
171
Jens Axboeec996e92008-05-27 11:39:39 +0200172static int find_next_zero(int word, int start)
173{
174 assert(word != -1U);
175 word >>= (start + 1);
Jens Axboeb3268b92008-06-02 09:59:32 +0200176 return ffz(word) + start + 1;
Jens Axboed24c33a2008-03-01 18:27:36 +0100177}
178
Jens Axboeadf57092008-04-16 19:43:17 +0200179static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100180{
Greg Edwards3a8600b2010-06-25 09:24:19 -0600181 int fd, bitmap_blocks, ret;
Jens Axboeb8a65822008-12-04 14:33:41 +0100182 char file[] = "/tmp/.fio_smalloc.XXXXXX";
183 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200184
Jens Axboeb8a65822008-12-04 14:33:41 +0100185 fd = mkstemp(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100186 if (fd < 0)
187 goto out_close;
188
Jens Axboe55f64912008-05-26 09:37:21 +0200189#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200190 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200191#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200192 alloc_size += sizeof(struct block_hdr);
193 if (alloc_size < INITIAL_SIZE)
194 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200195
Jens Axboeec996e92008-05-27 11:39:39 +0200196 /* round up to nearest full number of blocks */
197 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
198 bitmap_blocks = alloc_size / SMALLOC_BPL;
199 alloc_size += bitmap_blocks * sizeof(unsigned int);
200 pool->mmap_size = alloc_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200201
Jens Axboeec996e92008-05-27 11:39:39 +0200202 pool->nr_blocks = bitmap_blocks;
203 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
204
Greg Edwards3a8600b2010-06-25 09:24:19 -0600205#ifdef FIO_HAVE_FALLOCATE
206 ret = posix_fallocate(fd, 0, alloc_size);
207 if (ret > 0) {
208 fprintf(stderr, "posix_fallocate pool file failed: %s\n", strerror(ret));
209 goto out_unlink;
210 }
211#endif
212
Jens Axboeec996e92008-05-27 11:39:39 +0200213 if (ftruncate(fd, alloc_size) < 0)
Jens Axboed24c33a2008-03-01 18:27:36 +0100214 goto out_unlink;
215
Jens Axboeec996e92008-05-27 11:39:39 +0200216 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100217 if (ptr == MAP_FAILED)
218 goto out_unlink;
219
Jens Axboeec996e92008-05-27 11:39:39 +0200220 memset(ptr, 0, alloc_size);
221 pool->map = ptr;
222 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100223
Jens Axboe6548f472008-06-13 08:38:31 +0200224 pool->lock = fio_mutex_init(1);
Jens Axboed24c33a2008-03-01 18:27:36 +0100225 if (!pool->lock)
226 goto out_unlink;
Jens Axboed24c33a2008-03-01 18:27:36 +0100227
Jens Axboe443bb112008-12-04 14:30:13 +0100228 /*
229 * Unlink pool file now. It wont get deleted until the fd is closed,
230 * which happens both for cleanup or unexpected quit. This way we
231 * don't leave temp files around in case of a crash.
232 */
Jens Axboeb8a65822008-12-04 14:33:41 +0100233 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100234 pool->fd = fd;
235
Jens Axboed24c33a2008-03-01 18:27:36 +0100236 nr_pools++;
237 return 0;
238out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200239 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100240 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200241 munmap(pool->map, pool->mmap_size);
Jens Axboeb8a65822008-12-04 14:33:41 +0100242 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100243out_close:
Jens Axboeb8a65822008-12-04 14:33:41 +0100244 close(fd);
Jens Axboed24c33a2008-03-01 18:27:36 +0100245 return 1;
246}
247
248void sinit(void)
249{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100250 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100251
Jens Axboe6548f472008-06-13 08:38:31 +0200252 lock = fio_mutex_rw_init();
Jens Axboeadf57092008-04-16 19:43:17 +0200253 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100254 assert(!ret);
255}
256
257static void cleanup_pool(struct pool *pool)
258{
Jens Axboe443bb112008-12-04 14:30:13 +0100259 /*
260 * This will also remove the temporary file we used as a backing
261 * store, it was already unlinked
262 */
Jens Axboed24c33a2008-03-01 18:27:36 +0100263 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200264 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200265
266 if (pool->lock)
267 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100268}
269
270void scleanup(void)
271{
272 unsigned int i;
273
274 for (i = 0; i < nr_pools; i++)
275 cleanup_pool(&mp[i]);
276
Jens Axboe6548f472008-06-13 08:38:31 +0200277 if (lock)
278 fio_mutex_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100279}
280
Jens Axboe89da54e2008-05-27 20:49:29 +0200281#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200282static void *postred_ptr(struct block_hdr *hdr)
283{
Jens Axboecf987082009-07-07 17:43:11 +0200284 unsigned long ptr;
285
286 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
287 ptr = (ptr + int_mask) & ~int_mask;
288
289 return (void *) ptr;
290}
291
Jens Axboeec996e92008-05-27 11:39:39 +0200292static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200293{
Jens Axboecf987082009-07-07 17:43:11 +0200294 unsigned int *postred = postred_ptr(hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200295
Jens Axboeec996e92008-05-27 11:39:39 +0200296 hdr->prered = SMALLOC_PRE_RED;
297 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200298}
Jens Axboe55f64912008-05-26 09:37:21 +0200299
Jens Axboeec996e92008-05-27 11:39:39 +0200300static void sfree_check_redzone(struct block_hdr *hdr)
301{
Jens Axboecf987082009-07-07 17:43:11 +0200302 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200303
304 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200305 fprintf(stderr, "smalloc pre redzone destroyed!\n");
306 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200307 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200308 assert(0);
309 }
310 if (*postred != SMALLOC_POST_RED) {
311 fprintf(stderr, "smalloc post redzone destroyed!\n");
312 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200313 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200314 assert(0);
315 }
Jens Axboe55f64912008-05-26 09:37:21 +0200316}
Jens Axboe89da54e2008-05-27 20:49:29 +0200317#else
318static void fill_redzone(struct block_hdr *hdr)
319{
320}
321
322static void sfree_check_redzone(struct block_hdr *hdr)
323{
324}
325#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200326
Jens Axboed24c33a2008-03-01 18:27:36 +0100327static void sfree_pool(struct pool *pool, void *ptr)
328{
Jens Axboeec996e92008-05-27 11:39:39 +0200329 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200330 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200331 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100332
333 if (!ptr)
334 return;
335
Jens Axboeec996e92008-05-27 11:39:39 +0200336 ptr -= sizeof(*hdr);
337 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200338
Jens Axboed24c33a2008-03-01 18:27:36 +0100339 assert(ptr_valid(pool, ptr));
340
Jens Axboeec996e92008-05-27 11:39:39 +0200341 sfree_check_redzone(hdr);
342
343 offset = ptr - pool->map;
344 i = offset / SMALLOC_BPL;
345 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
346
Jens Axboed24c33a2008-03-01 18:27:36 +0100347 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200348 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200349 if (i < pool->next_non_full)
350 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200351 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100352 pool_unlock(pool);
353}
354
355void sfree(void *ptr)
356{
357 struct pool *pool = NULL;
358 unsigned int i;
359
Jens Axboe8e5732e2008-04-16 19:51:46 +0200360 if (!ptr)
361 return;
362
Jens Axboe65864cf2008-03-03 10:36:36 +0100363 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100364
365 for (i = 0; i < nr_pools; i++) {
366 if (ptr_valid(&mp[i], ptr)) {
367 pool = &mp[i];
368 break;
369 }
370 }
371
Jens Axboe65864cf2008-03-03 10:36:36 +0100372 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100373
374 assert(pool);
375 sfree_pool(pool, ptr);
376}
377
Jens Axboe55f64912008-05-26 09:37:21 +0200378static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100379{
Jens Axboeec996e92008-05-27 11:39:39 +0200380 unsigned int nr_blocks;
381 unsigned int i;
382 unsigned int offset;
383 unsigned int last_idx;
384 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100385
Jens Axboed24c33a2008-03-01 18:27:36 +0100386 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200387
388 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200389 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100390 goto fail;
391
Jens Axboeec996e92008-05-27 11:39:39 +0200392 i = pool->next_non_full;
393 last_idx = 0;
394 offset = -1U;
395 while (i < pool->nr_blocks) {
396 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100397
Jens Axboeec996e92008-05-27 11:39:39 +0200398 if (pool->bitmap[i] == -1U) {
399 i++;
400 pool->next_non_full = i;
401 last_idx = 0;
402 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100403 }
Jens Axboeec996e92008-05-27 11:39:39 +0200404
405 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200406 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200407 idx += nr_blocks;
408 if (idx < SMALLOC_BPI)
409 last_idx = idx;
410 else {
411 last_idx = 0;
412 while (idx >= SMALLOC_BPI) {
413 i++;
414 idx -= SMALLOC_BPI;
415 }
416 }
417 continue;
418 }
Jens Axboedcb69092008-05-27 20:35:18 +0200419 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200420 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
421 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100422 }
Jens Axboeec996e92008-05-27 11:39:39 +0200423
424 if (i < pool->nr_blocks) {
425 pool->free_blocks -= nr_blocks;
426 ret = pool->map + offset;
427 }
428fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100429 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200430 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100431}
432
Jens Axboe55f64912008-05-26 09:37:21 +0200433static void *smalloc_pool(struct pool *pool, unsigned int size)
434{
Jens Axboe89da54e2008-05-27 20:49:29 +0200435 unsigned int alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200436 void *ptr;
437
Jens Axboecf987082009-07-07 17:43:11 +0200438 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200439 * Round to int alignment, so that the postred pointer will
440 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200441 */
Jens Axboeec996e92008-05-27 11:39:39 +0200442#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200443 alloc_size += sizeof(unsigned int);
444 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200445#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200446
447 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200448 if (ptr) {
449 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200450
Jens Axboe89da54e2008-05-27 20:49:29 +0200451 hdr->size = alloc_size;
452 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200453
Jens Axboe89da54e2008-05-27 20:49:29 +0200454 ptr += sizeof(*hdr);
455 memset(ptr, 0, size);
456 }
Jens Axboeec996e92008-05-27 11:39:39 +0200457
Jens Axboeec996e92008-05-27 11:39:39 +0200458 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200459}
460
Jens Axboed24c33a2008-03-01 18:27:36 +0100461void *smalloc(unsigned int size)
462{
463 unsigned int i;
464
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200465 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100466 i = last_pool;
467
468 do {
469 for (; i < nr_pools; i++) {
470 void *ptr = smalloc_pool(&mp[i], size);
471
472 if (ptr) {
473 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200474 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100475 return ptr;
476 }
477 }
478 if (last_pool) {
479 last_pool = 0;
480 continue;
481 }
482
Jens Axboeec996e92008-05-27 11:39:39 +0200483 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100484 break;
485 else {
486 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200487 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100488 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100489 }
490 } while (1);
491
Jens Axboe65864cf2008-03-03 10:36:36 +0100492out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200493 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100494 return NULL;
495}
496
497char *smalloc_strdup(const char *str)
498{
499 char *ptr;
500
501 ptr = smalloc(strlen(str) + 1);
502 strcpy(ptr, str);
503 return ptr;
504}