blob: ac5754d5f496119a342180eeae58bb4a229e4cf0 [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>
13
Jens Axboe6548f472008-06-13 08:38:31 +020014#include "mutex.h"
Jens Axboeb3268b92008-06-02 09:59:32 +020015#include "arch/arch.h"
Jens Axboed24c33a2008-03-01 18:27:36 +010016
Jens Axboe55f64912008-05-26 09:37:21 +020017#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010018
Jens Axboeec996e92008-05-27 11:39:39 +020019#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
20#define SMALLOC_BPI (sizeof(unsigned int) * 8)
21#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
22
23#define INITIAL_SIZE 1024*1024 /* new pool size */
Jens Axboe68857682008-07-31 19:50:54 +020024#define MAX_POOLS 128 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010025
Jens Axboe55f64912008-05-26 09:37:21 +020026#define SMALLOC_PRE_RED 0xdeadbeefU
27#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020028
Jens Axboe2b386d22008-03-26 10:32:57 +010029unsigned int smalloc_pool_size = INITIAL_SIZE;
Jens Axboe122426d2009-07-07 23:03:06 +020030const int int_mask = sizeof(int) - 1;
Jens Axboe2b386d22008-03-26 10:32:57 +010031
Jens Axboed24c33a2008-03-01 18:27:36 +010032struct pool {
Jens Axboe6548f472008-06-13 08:38:31 +020033 struct fio_mutex *lock; /* protects this pool */
Jens Axboed24c33a2008-03-01 18:27:36 +010034 void *map; /* map of blocks */
Jens Axboeec996e92008-05-27 11:39:39 +020035 unsigned int *bitmap; /* blocks free/busy map */
36 unsigned int free_blocks; /* free blocks */
37 unsigned int nr_blocks; /* total blocks */
38 unsigned int next_non_full;
Jens Axboed24c33a2008-03-01 18:27:36 +010039 int fd; /* memory backing fd */
Jens Axboeec996e92008-05-27 11:39:39 +020040 unsigned int mmap_size;
41};
42
43struct block_hdr {
44 unsigned int size;
45#ifdef SMALLOC_REDZONE
46 unsigned int prered;
47#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010048};
49
50static struct pool mp[MAX_POOLS];
51static unsigned int nr_pools;
52static unsigned int last_pool;
Jens Axboe6548f472008-06-13 08:38:31 +020053static struct fio_mutex *lock;
Jens Axboed24c33a2008-03-01 18:27:36 +010054
Jens Axboed24c33a2008-03-01 18:27:36 +010055static inline void pool_lock(struct pool *pool)
56{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010057 fio_mutex_down(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010058}
59
60static inline void pool_unlock(struct pool *pool)
61{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010062 fio_mutex_up(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010063}
64
Jens Axboe65864cf2008-03-03 10:36:36 +010065static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010066{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010067 fio_mutex_down_read(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010068}
69
Jens Axboe65864cf2008-03-03 10:36:36 +010070static inline void global_read_unlock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010071{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010072 fio_mutex_up_read(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010073}
74
75static inline void global_write_lock(void)
76{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010077 fio_mutex_down_write(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010078}
79
80static inline void global_write_unlock(void)
81{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010082 fio_mutex_up_write(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010083}
84
Jens Axboed24c33a2008-03-01 18:27:36 +010085static inline int ptr_valid(struct pool *pool, void *ptr)
86{
Jens Axboedcb69092008-05-27 20:35:18 +020087 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
Jens Axboeec996e92008-05-27 11:39:39 +020088
89 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010090}
91
Jens Axboe808e9ea2008-05-27 14:12:45 +020092static inline unsigned int size_to_blocks(unsigned int size)
93{
94 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
95}
96
Jens Axboedcb69092008-05-27 20:35:18 +020097static int blocks_iter(struct pool *pool, unsigned int pool_idx,
98 unsigned int idx, unsigned int nr_blocks,
Jens Axboeec996e92008-05-27 11:39:39 +020099 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100100{
Jens Axboedcb69092008-05-27 20:35:18 +0200101
Jens Axboeec996e92008-05-27 11:39:39 +0200102 while (nr_blocks) {
103 unsigned int this_blocks, mask;
Jens Axboedcb69092008-05-27 20:35:18 +0200104 unsigned int *map;
105
106 if (pool_idx >= pool->nr_blocks)
107 return 0;
108
109 map = &pool->bitmap[pool_idx];
Jens Axboed24c33a2008-03-01 18:27:36 +0100110
Jens Axboeec996e92008-05-27 11:39:39 +0200111 this_blocks = nr_blocks;
112 if (this_blocks + idx > SMALLOC_BPI) {
113 this_blocks = SMALLOC_BPI - idx;
114 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100115 }
Jens Axboeec996e92008-05-27 11:39:39 +0200116
117 if (this_blocks == SMALLOC_BPI)
118 mask = -1U;
119 else
120 mask = ((1U << this_blocks) - 1) << idx;
121
122 if (!func(map, mask))
123 return 0;
124
125 nr_blocks -= this_blocks;
126 idx = 0;
Jens Axboedcb69092008-05-27 20:35:18 +0200127 pool_idx++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100128 }
129
Jens Axboeec996e92008-05-27 11:39:39 +0200130 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200131}
132
133static int mask_cmp(unsigned int *map, unsigned int mask)
134{
135 return !(*map & mask);
136}
137
138static int mask_clear(unsigned int *map, unsigned int mask)
139{
Jens Axboedcb69092008-05-27 20:35:18 +0200140 assert((*map & mask) == mask);
Jens Axboeec996e92008-05-27 11:39:39 +0200141 *map &= ~mask;
142 return 1;
143}
144
145static int mask_set(unsigned int *map, unsigned int mask)
146{
Jens Axboedcb69092008-05-27 20:35:18 +0200147 assert(!(*map & mask));
Jens Axboeec996e92008-05-27 11:39:39 +0200148 *map |= mask;
149 return 1;
150}
151
Jens Axboedcb69092008-05-27 20:35:18 +0200152static int blocks_free(struct pool *pool, unsigned int pool_idx,
153 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200154{
Jens Axboedcb69092008-05-27 20:35:18 +0200155 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
Jens Axboeec996e92008-05-27 11:39:39 +0200156}
157
Jens Axboedcb69092008-05-27 20:35:18 +0200158static void set_blocks(struct pool *pool, unsigned int pool_idx,
159 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200160{
Jens Axboedcb69092008-05-27 20:35:18 +0200161 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
Jens Axboeec996e92008-05-27 11:39:39 +0200162}
163
Jens Axboedcb69092008-05-27 20:35:18 +0200164static void clear_blocks(struct pool *pool, unsigned int pool_idx,
165 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200166{
Jens Axboedcb69092008-05-27 20:35:18 +0200167 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
Jens Axboeec996e92008-05-27 11:39:39 +0200168}
169
Jens Axboeec996e92008-05-27 11:39:39 +0200170static int find_next_zero(int word, int start)
171{
172 assert(word != -1U);
173 word >>= (start + 1);
Jens Axboeb3268b92008-06-02 09:59:32 +0200174 return ffz(word) + start + 1;
Jens Axboed24c33a2008-03-01 18:27:36 +0100175}
176
Jens Axboeadf57092008-04-16 19:43:17 +0200177static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100178{
Jens Axboeec996e92008-05-27 11:39:39 +0200179 int fd, bitmap_blocks;
Jens Axboeb8a65822008-12-04 14:33:41 +0100180 char file[] = "/tmp/.fio_smalloc.XXXXXX";
181 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200182
Jens Axboeb8a65822008-12-04 14:33:41 +0100183 fd = mkstemp(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100184 if (fd < 0)
185 goto out_close;
186
Jens Axboe55f64912008-05-26 09:37:21 +0200187#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200188 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200189#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200190 alloc_size += sizeof(struct block_hdr);
191 if (alloc_size < INITIAL_SIZE)
192 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200193
Jens Axboeec996e92008-05-27 11:39:39 +0200194 /* round up to nearest full number of blocks */
195 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
196 bitmap_blocks = alloc_size / SMALLOC_BPL;
197 alloc_size += bitmap_blocks * sizeof(unsigned int);
198 pool->mmap_size = alloc_size;
199
200 pool->nr_blocks = bitmap_blocks;
201 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
202
203 if (ftruncate(fd, alloc_size) < 0)
Jens Axboed24c33a2008-03-01 18:27:36 +0100204 goto out_unlink;
205
Jens Axboeec996e92008-05-27 11:39:39 +0200206 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100207 if (ptr == MAP_FAILED)
208 goto out_unlink;
209
Jens Axboeec996e92008-05-27 11:39:39 +0200210 memset(ptr, 0, alloc_size);
211 pool->map = ptr;
212 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100213
Jens Axboe6548f472008-06-13 08:38:31 +0200214 pool->lock = fio_mutex_init(1);
Jens Axboed24c33a2008-03-01 18:27:36 +0100215 if (!pool->lock)
216 goto out_unlink;
Jens Axboed24c33a2008-03-01 18:27:36 +0100217
Jens Axboe443bb112008-12-04 14:30:13 +0100218 /*
219 * Unlink pool file now. It wont get deleted until the fd is closed,
220 * which happens both for cleanup or unexpected quit. This way we
221 * don't leave temp files around in case of a crash.
222 */
Jens Axboeb8a65822008-12-04 14:33:41 +0100223 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100224 pool->fd = fd;
225
Jens Axboed24c33a2008-03-01 18:27:36 +0100226 nr_pools++;
227 return 0;
228out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200229 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100230 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200231 munmap(pool->map, pool->mmap_size);
Jens Axboeb8a65822008-12-04 14:33:41 +0100232 unlink(file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100233out_close:
Jens Axboeb8a65822008-12-04 14:33:41 +0100234 close(fd);
Jens Axboed24c33a2008-03-01 18:27:36 +0100235 return 1;
236}
237
238void sinit(void)
239{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100240 int ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100241
Jens Axboe6548f472008-06-13 08:38:31 +0200242 lock = fio_mutex_rw_init();
Jens Axboeadf57092008-04-16 19:43:17 +0200243 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100244 assert(!ret);
245}
246
247static void cleanup_pool(struct pool *pool)
248{
Jens Axboe443bb112008-12-04 14:30:13 +0100249 /*
250 * This will also remove the temporary file we used as a backing
251 * store, it was already unlinked
252 */
Jens Axboed24c33a2008-03-01 18:27:36 +0100253 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200254 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200255
256 if (pool->lock)
257 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100258}
259
260void scleanup(void)
261{
262 unsigned int i;
263
264 for (i = 0; i < nr_pools; i++)
265 cleanup_pool(&mp[i]);
266
Jens Axboe6548f472008-06-13 08:38:31 +0200267 if (lock)
268 fio_mutex_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100269}
270
Jens Axboe89da54e2008-05-27 20:49:29 +0200271#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200272static void *postred_ptr(struct block_hdr *hdr)
273{
Jens Axboecf987082009-07-07 17:43:11 +0200274 unsigned long ptr;
275
276 ptr = (unsigned long) hdr + hdr->size - sizeof(unsigned int);
277 ptr = (ptr + int_mask) & ~int_mask;
278
279 return (void *) ptr;
280}
281
Jens Axboeec996e92008-05-27 11:39:39 +0200282static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200283{
Jens Axboecf987082009-07-07 17:43:11 +0200284 unsigned int *postred = postred_ptr(hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200285
Jens Axboeec996e92008-05-27 11:39:39 +0200286 hdr->prered = SMALLOC_PRE_RED;
287 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200288}
Jens Axboe55f64912008-05-26 09:37:21 +0200289
Jens Axboeec996e92008-05-27 11:39:39 +0200290static void sfree_check_redzone(struct block_hdr *hdr)
291{
Jens Axboecf987082009-07-07 17:43:11 +0200292 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200293
294 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200295 fprintf(stderr, "smalloc pre redzone destroyed!\n");
296 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200297 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200298 assert(0);
299 }
300 if (*postred != SMALLOC_POST_RED) {
301 fprintf(stderr, "smalloc post redzone destroyed!\n");
302 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200303 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200304 assert(0);
305 }
Jens Axboe55f64912008-05-26 09:37:21 +0200306}
Jens Axboe89da54e2008-05-27 20:49:29 +0200307#else
308static void fill_redzone(struct block_hdr *hdr)
309{
310}
311
312static void sfree_check_redzone(struct block_hdr *hdr)
313{
314}
315#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200316
Jens Axboed24c33a2008-03-01 18:27:36 +0100317static void sfree_pool(struct pool *pool, void *ptr)
318{
Jens Axboeec996e92008-05-27 11:39:39 +0200319 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200320 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200321 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100322
323 if (!ptr)
324 return;
325
Jens Axboeec996e92008-05-27 11:39:39 +0200326 ptr -= sizeof(*hdr);
327 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200328
Jens Axboed24c33a2008-03-01 18:27:36 +0100329 assert(ptr_valid(pool, ptr));
330
Jens Axboeec996e92008-05-27 11:39:39 +0200331 sfree_check_redzone(hdr);
332
333 offset = ptr - pool->map;
334 i = offset / SMALLOC_BPL;
335 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
336
Jens Axboed24c33a2008-03-01 18:27:36 +0100337 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200338 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200339 if (i < pool->next_non_full)
340 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200341 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100342 pool_unlock(pool);
343}
344
345void sfree(void *ptr)
346{
347 struct pool *pool = NULL;
348 unsigned int i;
349
Jens Axboe8e5732e2008-04-16 19:51:46 +0200350 if (!ptr)
351 return;
352
Jens Axboe65864cf2008-03-03 10:36:36 +0100353 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100354
355 for (i = 0; i < nr_pools; i++) {
356 if (ptr_valid(&mp[i], ptr)) {
357 pool = &mp[i];
358 break;
359 }
360 }
361
Jens Axboe65864cf2008-03-03 10:36:36 +0100362 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100363
364 assert(pool);
365 sfree_pool(pool, ptr);
366}
367
Jens Axboe55f64912008-05-26 09:37:21 +0200368static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100369{
Jens Axboeec996e92008-05-27 11:39:39 +0200370 unsigned int nr_blocks;
371 unsigned int i;
372 unsigned int offset;
373 unsigned int last_idx;
374 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100375
Jens Axboed24c33a2008-03-01 18:27:36 +0100376 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200377
378 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200379 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100380 goto fail;
381
Jens Axboeec996e92008-05-27 11:39:39 +0200382 i = pool->next_non_full;
383 last_idx = 0;
384 offset = -1U;
385 while (i < pool->nr_blocks) {
386 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100387
Jens Axboeec996e92008-05-27 11:39:39 +0200388 if (pool->bitmap[i] == -1U) {
389 i++;
390 pool->next_non_full = i;
391 last_idx = 0;
392 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100393 }
Jens Axboeec996e92008-05-27 11:39:39 +0200394
395 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200396 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200397 idx += nr_blocks;
398 if (idx < SMALLOC_BPI)
399 last_idx = idx;
400 else {
401 last_idx = 0;
402 while (idx >= SMALLOC_BPI) {
403 i++;
404 idx -= SMALLOC_BPI;
405 }
406 }
407 continue;
408 }
Jens Axboedcb69092008-05-27 20:35:18 +0200409 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200410 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
411 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100412 }
Jens Axboeec996e92008-05-27 11:39:39 +0200413
414 if (i < pool->nr_blocks) {
415 pool->free_blocks -= nr_blocks;
416 ret = pool->map + offset;
417 }
418fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100419 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200420 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100421}
422
Jens Axboe55f64912008-05-26 09:37:21 +0200423static void *smalloc_pool(struct pool *pool, unsigned int size)
424{
Jens Axboe89da54e2008-05-27 20:49:29 +0200425 unsigned int alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200426 void *ptr;
427
Jens Axboecf987082009-07-07 17:43:11 +0200428 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200429 * Round to int alignment, so that the postred pointer will
430 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200431 */
Jens Axboeec996e92008-05-27 11:39:39 +0200432#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200433 alloc_size += sizeof(unsigned int);
434 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200435#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200436
437 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200438 if (ptr) {
439 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200440
Jens Axboe89da54e2008-05-27 20:49:29 +0200441 hdr->size = alloc_size;
442 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200443
Jens Axboe89da54e2008-05-27 20:49:29 +0200444 ptr += sizeof(*hdr);
445 memset(ptr, 0, size);
446 }
Jens Axboeec996e92008-05-27 11:39:39 +0200447
Jens Axboeec996e92008-05-27 11:39:39 +0200448 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200449}
450
Jens Axboed24c33a2008-03-01 18:27:36 +0100451void *smalloc(unsigned int size)
452{
453 unsigned int i;
454
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200455 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100456 i = last_pool;
457
458 do {
459 for (; i < nr_pools; i++) {
460 void *ptr = smalloc_pool(&mp[i], size);
461
462 if (ptr) {
463 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200464 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100465 return ptr;
466 }
467 }
468 if (last_pool) {
469 last_pool = 0;
470 continue;
471 }
472
Jens Axboeec996e92008-05-27 11:39:39 +0200473 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100474 break;
475 else {
476 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200477 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100478 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100479 }
480 } while (1);
481
Jens Axboe65864cf2008-03-03 10:36:36 +0100482out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200483 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100484 return NULL;
485}
486
487char *smalloc_strdup(const char *str)
488{
489 char *ptr;
490
491 ptr = smalloc(strlen(str) + 1);
492 strcpy(ptr, str);
493 return ptr;
494}