blob: fdf7d22ec4bf2b256f755842d97ec578baaa9fb9 [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{
Jens Axboec0e75ab2010-10-14 14:34:05 +0200181 int fd, bitmap_blocks;
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
Bruce Cran9b836562011-01-08 19:49:54 +0100206 posix_fallocate(fd, 0, alloc_size);
Greg Edwards3a8600b2010-06-25 09:24:19 -0600207#endif
208
Jens Axboeec996e92008-05-27 11:39:39 +0200209 if (ftruncate(fd, alloc_size) < 0)
Jens Axboed24c33a2008-03-01 18:27:36 +0100210 goto out_unlink;
211
Jens Axboeec996e92008-05-27 11:39:39 +0200212 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100213 if (ptr == MAP_FAILED)
214 goto out_unlink;
215
Jens Axboeec996e92008-05-27 11:39:39 +0200216 memset(ptr, 0, alloc_size);
217 pool->map = ptr;
218 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100219
Jens Axboe6548f472008-06-13 08:38:31 +0200220 pool->lock = fio_mutex_init(1);
Jens Axboed24c33a2008-03-01 18:27:36 +0100221 if (!pool->lock)
222 goto out_unlink;
Jens Axboed24c33a2008-03-01 18:27:36 +0100223
Jens Axboe443bb112008-12-04 14:30:13 +0100224 /*
225 * Unlink pool file now. It wont get deleted until the fd is closed,
226 * which happens both for cleanup or unexpected quit. This way we
227 * don't leave temp files around in case of a crash.
228 */
Jens Axboeb8a65822008-12-04 14:33:41 +0100229 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100230 pool->fd = fd;
231
Jens Axboed24c33a2008-03-01 18:27:36 +0100232 nr_pools++;
233 return 0;
234out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200235 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100236 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200237 munmap(pool->map, pool->mmap_size);
Jens Axboeb8a65822008-12-04 14:33:41 +0100238 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100239out_close:
Jens Axboeb8a65822008-12-04 14:33:41 +0100240 close(fd);
Jens Axboed24c33a2008-03-01 18:27:36 +0100241 return 1;
242}
243
244void sinit(void)
245{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100246 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100247
Jens Axboe6548f472008-06-13 08:38:31 +0200248 lock = fio_mutex_rw_init();
Jens Axboeadf57092008-04-16 19:43:17 +0200249 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100250 assert(!ret);
251}
252
253static void cleanup_pool(struct pool *pool)
254{
Jens Axboe443bb112008-12-04 14:30:13 +0100255 /*
256 * This will also remove the temporary file we used as a backing
257 * store, it was already unlinked
258 */
Jens Axboed24c33a2008-03-01 18:27:36 +0100259 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200260 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200261
262 if (pool->lock)
263 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100264}
265
266void scleanup(void)
267{
268 unsigned int i;
269
270 for (i = 0; i < nr_pools; i++)
271 cleanup_pool(&mp[i]);
272
Jens Axboe6548f472008-06-13 08:38:31 +0200273 if (lock)
274 fio_mutex_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100275}
276
Jens Axboe89da54e2008-05-27 20:49:29 +0200277#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200278static void *postred_ptr(struct block_hdr *hdr)
279{
Jens Axboecf987082009-07-07 17:43:11 +0200280 unsigned long ptr;
281
282 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
283 ptr = (ptr + int_mask) & ~int_mask;
284
285 return (void *) ptr;
286}
287
Jens Axboeec996e92008-05-27 11:39:39 +0200288static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200289{
Jens Axboecf987082009-07-07 17:43:11 +0200290 unsigned int *postred = postred_ptr(hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200291
Jens Axboeec996e92008-05-27 11:39:39 +0200292 hdr->prered = SMALLOC_PRE_RED;
293 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200294}
Jens Axboe55f64912008-05-26 09:37:21 +0200295
Jens Axboeec996e92008-05-27 11:39:39 +0200296static void sfree_check_redzone(struct block_hdr *hdr)
297{
Jens Axboecf987082009-07-07 17:43:11 +0200298 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200299
300 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200301 fprintf(stderr, "smalloc pre redzone destroyed!\n");
302 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200303 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200304 assert(0);
305 }
306 if (*postred != SMALLOC_POST_RED) {
307 fprintf(stderr, "smalloc post redzone destroyed!\n");
308 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200309 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200310 assert(0);
311 }
Jens Axboe55f64912008-05-26 09:37:21 +0200312}
Jens Axboe89da54e2008-05-27 20:49:29 +0200313#else
314static void fill_redzone(struct block_hdr *hdr)
315{
316}
317
318static void sfree_check_redzone(struct block_hdr *hdr)
319{
320}
321#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200322
Jens Axboed24c33a2008-03-01 18:27:36 +0100323static void sfree_pool(struct pool *pool, void *ptr)
324{
Jens Axboeec996e92008-05-27 11:39:39 +0200325 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200326 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200327 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100328
329 if (!ptr)
330 return;
331
Jens Axboeec996e92008-05-27 11:39:39 +0200332 ptr -= sizeof(*hdr);
333 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200334
Jens Axboed24c33a2008-03-01 18:27:36 +0100335 assert(ptr_valid(pool, ptr));
336
Jens Axboeec996e92008-05-27 11:39:39 +0200337 sfree_check_redzone(hdr);
338
339 offset = ptr - pool->map;
340 i = offset / SMALLOC_BPL;
341 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
342
Jens Axboed24c33a2008-03-01 18:27:36 +0100343 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200344 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200345 if (i < pool->next_non_full)
346 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200347 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100348 pool_unlock(pool);
349}
350
351void sfree(void *ptr)
352{
353 struct pool *pool = NULL;
354 unsigned int i;
355
Jens Axboe8e5732e2008-04-16 19:51:46 +0200356 if (!ptr)
357 return;
358
Jens Axboe65864cf2008-03-03 10:36:36 +0100359 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100360
361 for (i = 0; i < nr_pools; i++) {
362 if (ptr_valid(&mp[i], ptr)) {
363 pool = &mp[i];
364 break;
365 }
366 }
367
Jens Axboe65864cf2008-03-03 10:36:36 +0100368 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100369
370 assert(pool);
371 sfree_pool(pool, ptr);
372}
373
Jens Axboe55f64912008-05-26 09:37:21 +0200374static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100375{
Jens Axboeec996e92008-05-27 11:39:39 +0200376 unsigned int nr_blocks;
377 unsigned int i;
378 unsigned int offset;
379 unsigned int last_idx;
380 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100381
Jens Axboed24c33a2008-03-01 18:27:36 +0100382 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200383
384 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200385 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100386 goto fail;
387
Jens Axboeec996e92008-05-27 11:39:39 +0200388 i = pool->next_non_full;
389 last_idx = 0;
390 offset = -1U;
391 while (i < pool->nr_blocks) {
392 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100393
Jens Axboeec996e92008-05-27 11:39:39 +0200394 if (pool->bitmap[i] == -1U) {
395 i++;
396 pool->next_non_full = i;
397 last_idx = 0;
398 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100399 }
Jens Axboeec996e92008-05-27 11:39:39 +0200400
401 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200402 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200403 idx += nr_blocks;
404 if (idx < SMALLOC_BPI)
405 last_idx = idx;
406 else {
407 last_idx = 0;
408 while (idx >= SMALLOC_BPI) {
409 i++;
410 idx -= SMALLOC_BPI;
411 }
412 }
413 continue;
414 }
Jens Axboedcb69092008-05-27 20:35:18 +0200415 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200416 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
417 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100418 }
Jens Axboeec996e92008-05-27 11:39:39 +0200419
420 if (i < pool->nr_blocks) {
421 pool->free_blocks -= nr_blocks;
422 ret = pool->map + offset;
423 }
424fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100425 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200426 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100427}
428
Jens Axboe55f64912008-05-26 09:37:21 +0200429static void *smalloc_pool(struct pool *pool, unsigned int size)
430{
Jens Axboe89da54e2008-05-27 20:49:29 +0200431 unsigned int alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200432 void *ptr;
433
Jens Axboecf987082009-07-07 17:43:11 +0200434 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200435 * Round to int alignment, so that the postred pointer will
436 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200437 */
Jens Axboeec996e92008-05-27 11:39:39 +0200438#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200439 alloc_size += sizeof(unsigned int);
440 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200441#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200442
443 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200444 if (ptr) {
445 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200446
Jens Axboe89da54e2008-05-27 20:49:29 +0200447 hdr->size = alloc_size;
448 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200449
Jens Axboe89da54e2008-05-27 20:49:29 +0200450 ptr += sizeof(*hdr);
451 memset(ptr, 0, size);
452 }
Jens Axboeec996e92008-05-27 11:39:39 +0200453
Jens Axboeec996e92008-05-27 11:39:39 +0200454 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200455}
456
Jens Axboed24c33a2008-03-01 18:27:36 +0100457void *smalloc(unsigned int size)
458{
459 unsigned int i;
460
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200461 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100462 i = last_pool;
463
464 do {
465 for (; i < nr_pools; i++) {
466 void *ptr = smalloc_pool(&mp[i], size);
467
468 if (ptr) {
469 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200470 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100471 return ptr;
472 }
473 }
474 if (last_pool) {
475 last_pool = 0;
476 continue;
477 }
478
Jens Axboeec996e92008-05-27 11:39:39 +0200479 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100480 break;
481 else {
482 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200483 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100484 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100485 }
486 } while (1);
487
Jens Axboe65864cf2008-03-03 10:36:36 +0100488out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200489 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100490 return NULL;
491}
492
493char *smalloc_strdup(const char *str)
494{
495 char *ptr;
496
497 ptr = smalloc(strlen(str) + 1);
498 strcpy(ptr, str);
499 return ptr;
500}