blob: fc6ac526c33d95cac6c88f2bfe371f724f0d426c [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 Axboe6548f472008-06-13 08:38:31 +020017#define MP_SAFE /* define to make thread safe */
Jens Axboe55f64912008-05-26 09:37:21 +020018#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010019
Jens Axboeec996e92008-05-27 11:39:39 +020020#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
21#define SMALLOC_BPI (sizeof(unsigned int) * 8)
22#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
23
24#define INITIAL_SIZE 1024*1024 /* new pool size */
Jens Axboe68857682008-07-31 19:50:54 +020025#define MAX_POOLS 128 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010026
Jens Axboe55f64912008-05-26 09:37:21 +020027#define SMALLOC_PRE_RED 0xdeadbeefU
28#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020029
Jens Axboe2b386d22008-03-26 10:32:57 +010030unsigned int smalloc_pool_size = INITIAL_SIZE;
31
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 */
40 char file[PATH_MAX]; /* filename for fd */
Jens Axboeec996e92008-05-27 11:39:39 +020041 unsigned int mmap_size;
42};
43
44struct block_hdr {
45 unsigned int size;
46#ifdef SMALLOC_REDZONE
47 unsigned int prered;
48#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010049};
50
51static struct pool mp[MAX_POOLS];
52static unsigned int nr_pools;
53static unsigned int last_pool;
Jens Axboe6548f472008-06-13 08:38:31 +020054static struct fio_mutex *lock;
Jens Axboed24c33a2008-03-01 18:27:36 +010055
Jens Axboed24c33a2008-03-01 18:27:36 +010056static inline void pool_lock(struct pool *pool)
57{
Jens Axboe6548f472008-06-13 08:38:31 +020058 if (pool->lock)
59 fio_mutex_down(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010060}
61
62static inline void pool_unlock(struct pool *pool)
63{
Jens Axboe6548f472008-06-13 08:38:31 +020064 if (pool->lock)
65 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 Axboe6548f472008-06-13 08:38:31 +020070 if (lock)
71 fio_mutex_down_read(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010072}
73
Jens Axboe65864cf2008-03-03 10:36:36 +010074static inline void global_read_unlock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010075{
Jens Axboe6548f472008-06-13 08:38:31 +020076 if (lock)
77 fio_mutex_up_read(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010078}
79
80static inline void global_write_lock(void)
81{
Jens Axboe6548f472008-06-13 08:38:31 +020082 if (lock)
83 fio_mutex_down_write(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010084}
85
86static inline void global_write_unlock(void)
87{
Jens Axboe6548f472008-06-13 08:38:31 +020088 if (lock)
89 fio_mutex_up_write(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010090}
91
Jens Axboed24c33a2008-03-01 18:27:36 +010092static inline int ptr_valid(struct pool *pool, void *ptr)
93{
Jens Axboedcb69092008-05-27 20:35:18 +020094 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
Jens Axboeec996e92008-05-27 11:39:39 +020095
96 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010097}
98
Jens Axboe808e9ea2008-05-27 14:12:45 +020099static inline unsigned int size_to_blocks(unsigned int size)
100{
101 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
102}
103
Jens Axboedcb69092008-05-27 20:35:18 +0200104static int blocks_iter(struct pool *pool, unsigned int pool_idx,
105 unsigned int idx, unsigned int nr_blocks,
Jens Axboeec996e92008-05-27 11:39:39 +0200106 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100107{
Jens Axboedcb69092008-05-27 20:35:18 +0200108
Jens Axboeec996e92008-05-27 11:39:39 +0200109 while (nr_blocks) {
110 unsigned int this_blocks, mask;
Jens Axboedcb69092008-05-27 20:35:18 +0200111 unsigned int *map;
112
113 if (pool_idx >= pool->nr_blocks)
114 return 0;
115
116 map = &pool->bitmap[pool_idx];
Jens Axboed24c33a2008-03-01 18:27:36 +0100117
Jens Axboeec996e92008-05-27 11:39:39 +0200118 this_blocks = nr_blocks;
119 if (this_blocks + idx > SMALLOC_BPI) {
120 this_blocks = SMALLOC_BPI - idx;
121 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100122 }
Jens Axboeec996e92008-05-27 11:39:39 +0200123
124 if (this_blocks == SMALLOC_BPI)
125 mask = -1U;
126 else
127 mask = ((1U << this_blocks) - 1) << idx;
128
129 if (!func(map, mask))
130 return 0;
131
132 nr_blocks -= this_blocks;
133 idx = 0;
Jens Axboedcb69092008-05-27 20:35:18 +0200134 pool_idx++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100135 }
136
Jens Axboeec996e92008-05-27 11:39:39 +0200137 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200138}
139
140static int mask_cmp(unsigned int *map, unsigned int mask)
141{
142 return !(*map & mask);
143}
144
145static int mask_clear(unsigned int *map, unsigned int mask)
146{
Jens Axboedcb69092008-05-27 20:35:18 +0200147 assert((*map & mask) == mask);
Jens Axboeec996e92008-05-27 11:39:39 +0200148 *map &= ~mask;
149 return 1;
150}
151
152static int mask_set(unsigned int *map, unsigned int mask)
153{
Jens Axboedcb69092008-05-27 20:35:18 +0200154 assert(!(*map & mask));
Jens Axboeec996e92008-05-27 11:39:39 +0200155 *map |= mask;
156 return 1;
157}
158
Jens Axboedcb69092008-05-27 20:35:18 +0200159static int blocks_free(struct pool *pool, unsigned int pool_idx,
160 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200161{
Jens Axboedcb69092008-05-27 20:35:18 +0200162 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
Jens Axboeec996e92008-05-27 11:39:39 +0200163}
164
Jens Axboedcb69092008-05-27 20:35:18 +0200165static void set_blocks(struct pool *pool, unsigned int pool_idx,
166 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200167{
Jens Axboedcb69092008-05-27 20:35:18 +0200168 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
Jens Axboeec996e92008-05-27 11:39:39 +0200169}
170
Jens Axboedcb69092008-05-27 20:35:18 +0200171static void clear_blocks(struct pool *pool, unsigned int pool_idx,
172 unsigned int idx, unsigned int nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200173{
Jens Axboedcb69092008-05-27 20:35:18 +0200174 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
Jens Axboeec996e92008-05-27 11:39:39 +0200175}
176
Jens Axboeec996e92008-05-27 11:39:39 +0200177static int find_next_zero(int word, int start)
178{
179 assert(word != -1U);
180 word >>= (start + 1);
Jens Axboeb3268b92008-06-02 09:59:32 +0200181 return ffz(word) + start + 1;
Jens Axboed24c33a2008-03-01 18:27:36 +0100182}
183
Jens Axboeadf57092008-04-16 19:43:17 +0200184static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100185{
Jens Axboed24c33a2008-03-01 18:27:36 +0100186 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200187 int fd, bitmap_blocks;
188
Jens Axboed24c33a2008-03-01 18:27:36 +0100189 strcpy(pool->file, "/tmp/.fio_smalloc.XXXXXX");
190 fd = mkstemp(pool->file);
191 if (fd < 0)
192 goto out_close;
193
Jens Axboe55f64912008-05-26 09:37:21 +0200194#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200195 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200196#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200197 alloc_size += sizeof(struct block_hdr);
198 if (alloc_size < INITIAL_SIZE)
199 alloc_size = INITIAL_SIZE;
Jens Axboeadf57092008-04-16 19:43:17 +0200200
Jens Axboeec996e92008-05-27 11:39:39 +0200201 /* round up to nearest full number of blocks */
202 alloc_size = (alloc_size + SMALLOC_BPL - 1) & ~(SMALLOC_BPL - 1);
203 bitmap_blocks = alloc_size / SMALLOC_BPL;
204 alloc_size += bitmap_blocks * sizeof(unsigned int);
205 pool->mmap_size = alloc_size;
206
207 pool->nr_blocks = bitmap_blocks;
208 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
209
210 if (ftruncate(fd, alloc_size) < 0)
Jens Axboed24c33a2008-03-01 18:27:36 +0100211 goto out_unlink;
212
Jens Axboeec996e92008-05-27 11:39:39 +0200213 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboed24c33a2008-03-01 18:27:36 +0100214 if (ptr == MAP_FAILED)
215 goto out_unlink;
216
Jens Axboeec996e92008-05-27 11:39:39 +0200217 memset(ptr, 0, alloc_size);
218 pool->map = ptr;
219 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100220
Jens Axboe6548f472008-06-13 08:38:31 +0200221#ifdef MP_SAFE
222 pool->lock = fio_mutex_init(1);
Jens Axboed24c33a2008-03-01 18:27:36 +0100223 if (!pool->lock)
224 goto out_unlink;
Jens Axboe6548f472008-06-13 08:38:31 +0200225#endif
Jens Axboed24c33a2008-03-01 18:27:36 +0100226
Jens Axboe443bb112008-12-04 14:30:13 +0100227 /*
228 * Unlink pool file now. It wont get deleted until the fd is closed,
229 * which happens both for cleanup or unexpected quit. This way we
230 * don't leave temp files around in case of a crash.
231 */
Jens Axboed24c33a2008-03-01 18:27:36 +0100232 pool->fd = fd;
Jens Axboe443bb112008-12-04 14:30:13 +0100233 unlink(pool->file);
Jens Axboed24c33a2008-03-01 18:27:36 +0100234
Jens Axboed24c33a2008-03-01 18:27:36 +0100235 nr_pools++;
236 return 0;
237out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200238 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100239 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200240 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100241 unlink(pool->file);
242out_close:
243 if (fd >= 0)
244 close(fd);
245 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#ifdef MP_SAFE
253 lock = fio_mutex_rw_init();
254#endif
Jens Axboeadf57092008-04-16 19:43:17 +0200255 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100256 assert(!ret);
257}
258
259static void cleanup_pool(struct pool *pool)
260{
Jens Axboe443bb112008-12-04 14:30:13 +0100261 /*
262 * This will also remove the temporary file we used as a backing
263 * store, it was already unlinked
264 */
Jens Axboed24c33a2008-03-01 18:27:36 +0100265 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200266 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200267
268 if (pool->lock)
269 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100270}
271
272void scleanup(void)
273{
274 unsigned int i;
275
276 for (i = 0; i < nr_pools; i++)
277 cleanup_pool(&mp[i]);
278
Jens Axboe6548f472008-06-13 08:38:31 +0200279 if (lock)
280 fio_mutex_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100281}
282
Jens Axboe89da54e2008-05-27 20:49:29 +0200283#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200284static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200285{
Jens Axboeec996e92008-05-27 11:39:39 +0200286 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200287
Jens Axboeec996e92008-05-27 11:39:39 +0200288 hdr->prered = SMALLOC_PRE_RED;
289 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200290}
Jens Axboe55f64912008-05-26 09:37:21 +0200291
Jens Axboeec996e92008-05-27 11:39:39 +0200292static void sfree_check_redzone(struct block_hdr *hdr)
293{
Jens Axboeec996e92008-05-27 11:39:39 +0200294 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
295
296 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200297 fprintf(stderr, "smalloc pre redzone destroyed!\n");
298 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200299 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200300 assert(0);
301 }
302 if (*postred != SMALLOC_POST_RED) {
303 fprintf(stderr, "smalloc post redzone destroyed!\n");
304 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200305 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200306 assert(0);
307 }
Jens Axboe55f64912008-05-26 09:37:21 +0200308}
Jens Axboe89da54e2008-05-27 20:49:29 +0200309#else
310static void fill_redzone(struct block_hdr *hdr)
311{
312}
313
314static void sfree_check_redzone(struct block_hdr *hdr)
315{
316}
317#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200318
Jens Axboed24c33a2008-03-01 18:27:36 +0100319static void sfree_pool(struct pool *pool, void *ptr)
320{
Jens Axboeec996e92008-05-27 11:39:39 +0200321 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200322 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200323 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100324
325 if (!ptr)
326 return;
327
Jens Axboeec996e92008-05-27 11:39:39 +0200328 ptr -= sizeof(*hdr);
329 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200330
Jens Axboed24c33a2008-03-01 18:27:36 +0100331 assert(ptr_valid(pool, ptr));
332
Jens Axboeec996e92008-05-27 11:39:39 +0200333 sfree_check_redzone(hdr);
334
335 offset = ptr - pool->map;
336 i = offset / SMALLOC_BPL;
337 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
338
Jens Axboed24c33a2008-03-01 18:27:36 +0100339 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200340 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200341 if (i < pool->next_non_full)
342 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200343 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100344 pool_unlock(pool);
345}
346
347void sfree(void *ptr)
348{
349 struct pool *pool = NULL;
350 unsigned int i;
351
Jens Axboe8e5732e2008-04-16 19:51:46 +0200352 if (!ptr)
353 return;
354
Jens Axboe65864cf2008-03-03 10:36:36 +0100355 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100356
357 for (i = 0; i < nr_pools; i++) {
358 if (ptr_valid(&mp[i], ptr)) {
359 pool = &mp[i];
360 break;
361 }
362 }
363
Jens Axboe65864cf2008-03-03 10:36:36 +0100364 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100365
366 assert(pool);
367 sfree_pool(pool, ptr);
368}
369
Jens Axboe55f64912008-05-26 09:37:21 +0200370static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100371{
Jens Axboeec996e92008-05-27 11:39:39 +0200372 unsigned int nr_blocks;
373 unsigned int i;
374 unsigned int offset;
375 unsigned int last_idx;
376 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100377
Jens Axboed24c33a2008-03-01 18:27:36 +0100378 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200379
380 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200381 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100382 goto fail;
383
Jens Axboeec996e92008-05-27 11:39:39 +0200384 i = pool->next_non_full;
385 last_idx = 0;
386 offset = -1U;
387 while (i < pool->nr_blocks) {
388 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100389
Jens Axboeec996e92008-05-27 11:39:39 +0200390 if (pool->bitmap[i] == -1U) {
391 i++;
392 pool->next_non_full = i;
393 last_idx = 0;
394 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100395 }
Jens Axboeec996e92008-05-27 11:39:39 +0200396
397 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200398 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200399 idx += nr_blocks;
400 if (idx < SMALLOC_BPI)
401 last_idx = idx;
402 else {
403 last_idx = 0;
404 while (idx >= SMALLOC_BPI) {
405 i++;
406 idx -= SMALLOC_BPI;
407 }
408 }
409 continue;
410 }
Jens Axboedcb69092008-05-27 20:35:18 +0200411 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200412 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
413 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100414 }
Jens Axboeec996e92008-05-27 11:39:39 +0200415
416 if (i < pool->nr_blocks) {
417 pool->free_blocks -= nr_blocks;
418 ret = pool->map + offset;
419 }
420fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100421 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200422 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100423}
424
Jens Axboe55f64912008-05-26 09:37:21 +0200425static void *smalloc_pool(struct pool *pool, unsigned int size)
426{
Jens Axboe89da54e2008-05-27 20:49:29 +0200427 unsigned int alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200428 void *ptr;
429
Jens Axboeec996e92008-05-27 11:39:39 +0200430#ifdef SMALLOC_REDZONE
431 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200432#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200433
434 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200435 if (ptr) {
436 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200437
Jens Axboe89da54e2008-05-27 20:49:29 +0200438 hdr->size = alloc_size;
439 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200440
Jens Axboe89da54e2008-05-27 20:49:29 +0200441 ptr += sizeof(*hdr);
442 memset(ptr, 0, size);
443 }
Jens Axboeec996e92008-05-27 11:39:39 +0200444
Jens Axboeec996e92008-05-27 11:39:39 +0200445 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200446}
447
Jens Axboed24c33a2008-03-01 18:27:36 +0100448void *smalloc(unsigned int size)
449{
450 unsigned int i;
451
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200452 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100453 i = last_pool;
454
455 do {
456 for (; i < nr_pools; i++) {
457 void *ptr = smalloc_pool(&mp[i], size);
458
459 if (ptr) {
460 last_pool = i;
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200461 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100462 return ptr;
463 }
464 }
465 if (last_pool) {
466 last_pool = 0;
467 continue;
468 }
469
Jens Axboeec996e92008-05-27 11:39:39 +0200470 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100471 break;
472 else {
473 i = nr_pools;
Jens Axboeadf57092008-04-16 19:43:17 +0200474 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100475 goto out;
Jens Axboed24c33a2008-03-01 18:27:36 +0100476 }
477 } while (1);
478
Jens Axboe65864cf2008-03-03 10:36:36 +0100479out:
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200480 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100481 return NULL;
482}
483
484char *smalloc_strdup(const char *str)
485{
486 char *ptr;
487
488 ptr = smalloc(strlen(str) + 1);
489 strcpy(ptr, str);
490 return ptr;
491}