blob: 0a86c43b0f17d58a5aa69587f92eff1d4f39c279 [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
14#include "mutex.h"
Jens Axboeb3268b92008-06-02 09:59:32 +020015#include "arch/arch.h"
Jens Axboed24c33a2008-03-01 18:27:36 +010016
Jens Axboe1f37f632008-05-27 11:40:37 +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 Axboec5dda9e2008-05-23 10:10:54 +020025#define MAX_POOLS 4 /* 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 {
33 struct fio_mutex *lock; /* protects this pool */
34 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;
54static struct fio_mutex *lock;
55
Jens Axboed24c33a2008-03-01 18:27:36 +010056static inline void pool_lock(struct pool *pool)
57{
58 if (pool->lock)
59 fio_mutex_down(pool->lock);
60}
61
62static inline void pool_unlock(struct pool *pool)
63{
64 if (pool->lock)
65 fio_mutex_up(pool->lock);
66}
67
Jens Axboe65864cf2008-03-03 10:36:36 +010068static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010069{
70 if (lock)
Jens Axboe65864cf2008-03-03 10:36:36 +010071 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{
76 if (lock)
Jens Axboe65864cf2008-03-03 10:36:36 +010077 fio_mutex_up_read(lock);
78}
79
80static inline void global_write_lock(void)
81{
82 if (lock)
83 fio_mutex_down_write(lock);
84}
85
86static inline void global_write_unlock(void)
87{
88 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
221#ifdef MP_SAFE
222 pool->lock = fio_mutex_init(1);
223 if (!pool->lock)
224 goto out_unlink;
225#endif
226
227 pool->fd = fd;
228
Jens Axboe65864cf2008-03-03 10:36:36 +0100229 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100230 nr_pools++;
Jens Axboe65864cf2008-03-03 10:36:36 +0100231 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100232 return 0;
233out_unlink:
Jens Axboeec996e92008-05-27 11:39:39 +0200234 fprintf(stderr, "smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100235 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200236 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100237 unlink(pool->file);
238out_close:
239 if (fd >= 0)
240 close(fd);
241 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
248#ifdef MP_SAFE
Jens Axboe9c5b5292008-03-03 11:08:19 +0100249 lock = fio_mutex_rw_init();
Jens Axboed24c33a2008-03-01 18:27:36 +0100250#endif
Jens Axboeadf57092008-04-16 19:43:17 +0200251 ret = add_pool(&mp[0], INITIAL_SIZE);
Jens Axboed24c33a2008-03-01 18:27:36 +0100252 assert(!ret);
253}
254
255static void cleanup_pool(struct pool *pool)
256{
257 unlink(pool->file);
258 close(pool->fd);
Jens Axboeec996e92008-05-27 11:39:39 +0200259 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100260
261 if (pool->lock)
262 fio_mutex_remove(pool->lock);
263}
264
265void scleanup(void)
266{
267 unsigned int i;
268
269 for (i = 0; i < nr_pools; i++)
270 cleanup_pool(&mp[i]);
271
272 if (lock)
273 fio_mutex_remove(lock);
274}
275
Jens Axboe89da54e2008-05-27 20:49:29 +0200276#ifdef SMALLOC_REDZONE
Jens Axboeec996e92008-05-27 11:39:39 +0200277static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200278{
Jens Axboeec996e92008-05-27 11:39:39 +0200279 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200280
Jens Axboeec996e92008-05-27 11:39:39 +0200281 hdr->prered = SMALLOC_PRE_RED;
282 *postred = SMALLOC_POST_RED;
Jens Axboeec996e92008-05-27 11:39:39 +0200283}
Jens Axboe55f64912008-05-26 09:37:21 +0200284
Jens Axboeec996e92008-05-27 11:39:39 +0200285static void sfree_check_redzone(struct block_hdr *hdr)
286{
Jens Axboeec996e92008-05-27 11:39:39 +0200287 unsigned int *postred = (void *) hdr + hdr->size - sizeof(unsigned int);
288
289 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboe55f64912008-05-26 09:37:21 +0200290 fprintf(stderr, "smalloc pre redzone destroyed!\n");
291 fprintf(stderr, " ptr=%p, prered=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200292 hdr, hdr->prered, SMALLOC_PRE_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200293 assert(0);
294 }
295 if (*postred != SMALLOC_POST_RED) {
296 fprintf(stderr, "smalloc post redzone destroyed!\n");
297 fprintf(stderr, " ptr=%p, postred=%x, expected %x\n",
Jens Axboeec996e92008-05-27 11:39:39 +0200298 hdr, *postred, SMALLOC_POST_RED);
Jens Axboe55f64912008-05-26 09:37:21 +0200299 assert(0);
300 }
Jens Axboe55f64912008-05-26 09:37:21 +0200301}
Jens Axboe89da54e2008-05-27 20:49:29 +0200302#else
303static void fill_redzone(struct block_hdr *hdr)
304{
305}
306
307static void sfree_check_redzone(struct block_hdr *hdr)
308{
309}
310#endif
Jens Axboe55f64912008-05-26 09:37:21 +0200311
Jens Axboed24c33a2008-03-01 18:27:36 +0100312static void sfree_pool(struct pool *pool, void *ptr)
313{
Jens Axboeec996e92008-05-27 11:39:39 +0200314 struct block_hdr *hdr;
Jens Axboe179446e2008-05-27 14:11:56 +0200315 unsigned int i, idx;
Jens Axboeec996e92008-05-27 11:39:39 +0200316 unsigned long offset;
Jens Axboed24c33a2008-03-01 18:27:36 +0100317
318 if (!ptr)
319 return;
320
Jens Axboeec996e92008-05-27 11:39:39 +0200321 ptr -= sizeof(*hdr);
322 hdr = ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200323
Jens Axboed24c33a2008-03-01 18:27:36 +0100324 assert(ptr_valid(pool, ptr));
325
Jens Axboeec996e92008-05-27 11:39:39 +0200326 sfree_check_redzone(hdr);
327
328 offset = ptr - pool->map;
329 i = offset / SMALLOC_BPL;
330 idx = (offset % SMALLOC_BPL) / SMALLOC_BPB;
331
Jens Axboed24c33a2008-03-01 18:27:36 +0100332 pool_lock(pool);
Jens Axboedcb69092008-05-27 20:35:18 +0200333 clear_blocks(pool, i, idx, size_to_blocks(hdr->size));
Jens Axboeec996e92008-05-27 11:39:39 +0200334 if (i < pool->next_non_full)
335 pool->next_non_full = i;
Jens Axboe179446e2008-05-27 14:11:56 +0200336 pool->free_blocks += size_to_blocks(hdr->size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100337 pool_unlock(pool);
338}
339
340void sfree(void *ptr)
341{
342 struct pool *pool = NULL;
343 unsigned int i;
344
Jens Axboe8e5732e2008-04-16 19:51:46 +0200345 if (!ptr)
346 return;
347
Jens Axboe65864cf2008-03-03 10:36:36 +0100348 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100349
350 for (i = 0; i < nr_pools; i++) {
351 if (ptr_valid(&mp[i], ptr)) {
352 pool = &mp[i];
353 break;
354 }
355 }
356
Jens Axboe65864cf2008-03-03 10:36:36 +0100357 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100358
359 assert(pool);
360 sfree_pool(pool, ptr);
361}
362
Jens Axboe55f64912008-05-26 09:37:21 +0200363static void *__smalloc_pool(struct pool *pool, unsigned int size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100364{
Jens Axboeec996e92008-05-27 11:39:39 +0200365 unsigned int nr_blocks;
366 unsigned int i;
367 unsigned int offset;
368 unsigned int last_idx;
369 void *ret = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100370
Jens Axboed24c33a2008-03-01 18:27:36 +0100371 pool_lock(pool);
Jens Axboe179446e2008-05-27 14:11:56 +0200372
373 nr_blocks = size_to_blocks(size);
Jens Axboeec996e92008-05-27 11:39:39 +0200374 if (nr_blocks > pool->free_blocks)
Jens Axboed24c33a2008-03-01 18:27:36 +0100375 goto fail;
376
Jens Axboeec996e92008-05-27 11:39:39 +0200377 i = pool->next_non_full;
378 last_idx = 0;
379 offset = -1U;
380 while (i < pool->nr_blocks) {
381 unsigned int idx;
Jens Axboed24c33a2008-03-01 18:27:36 +0100382
Jens Axboeec996e92008-05-27 11:39:39 +0200383 if (pool->bitmap[i] == -1U) {
384 i++;
385 pool->next_non_full = i;
386 last_idx = 0;
387 continue;
Jens Axboed24c33a2008-03-01 18:27:36 +0100388 }
Jens Axboeec996e92008-05-27 11:39:39 +0200389
390 idx = find_next_zero(pool->bitmap[i], last_idx);
Jens Axboedcb69092008-05-27 20:35:18 +0200391 if (!blocks_free(pool, i, idx, nr_blocks)) {
Jens Axboeec996e92008-05-27 11:39:39 +0200392 idx += nr_blocks;
393 if (idx < SMALLOC_BPI)
394 last_idx = idx;
395 else {
396 last_idx = 0;
397 while (idx >= SMALLOC_BPI) {
398 i++;
399 idx -= SMALLOC_BPI;
400 }
401 }
402 continue;
403 }
Jens Axboedcb69092008-05-27 20:35:18 +0200404 set_blocks(pool, i, idx, nr_blocks);
Jens Axboeec996e92008-05-27 11:39:39 +0200405 offset = i * SMALLOC_BPL + idx * SMALLOC_BPB;
406 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100407 }
Jens Axboeec996e92008-05-27 11:39:39 +0200408
409 if (i < pool->nr_blocks) {
410 pool->free_blocks -= nr_blocks;
411 ret = pool->map + offset;
412 }
413fail:
Jens Axboed24c33a2008-03-01 18:27:36 +0100414 pool_unlock(pool);
Jens Axboeec996e92008-05-27 11:39:39 +0200415 return ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100416}
417
Jens Axboe55f64912008-05-26 09:37:21 +0200418static void *smalloc_pool(struct pool *pool, unsigned int size)
419{
Jens Axboe89da54e2008-05-27 20:49:29 +0200420 unsigned int alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200421 void *ptr;
422
Jens Axboeec996e92008-05-27 11:39:39 +0200423#ifdef SMALLOC_REDZONE
424 alloc_size += sizeof(unsigned int);
Jens Axboe55f64912008-05-26 09:37:21 +0200425#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200426
427 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200428 if (ptr) {
429 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200430
Jens Axboe89da54e2008-05-27 20:49:29 +0200431 hdr->size = alloc_size;
432 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200433
Jens Axboe89da54e2008-05-27 20:49:29 +0200434 ptr += sizeof(*hdr);
435 memset(ptr, 0, size);
436 }
Jens Axboeec996e92008-05-27 11:39:39 +0200437
Jens Axboeec996e92008-05-27 11:39:39 +0200438 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200439}
440
Jens Axboed24c33a2008-03-01 18:27:36 +0100441void *smalloc(unsigned int size)
442{
443 unsigned int i;
444
Jens Axboe65864cf2008-03-03 10:36:36 +0100445 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100446 i = last_pool;
447
448 do {
449 for (; i < nr_pools; i++) {
450 void *ptr = smalloc_pool(&mp[i], size);
451
452 if (ptr) {
453 last_pool = i;
Jens Axboe65864cf2008-03-03 10:36:36 +0100454 global_read_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100455 return ptr;
456 }
457 }
458 if (last_pool) {
459 last_pool = 0;
460 continue;
461 }
462
Jens Axboeec996e92008-05-27 11:39:39 +0200463 if (nr_pools + 1 > MAX_POOLS)
Jens Axboed24c33a2008-03-01 18:27:36 +0100464 break;
465 else {
466 i = nr_pools;
Jens Axboe65864cf2008-03-03 10:36:36 +0100467 global_read_unlock();
Jens Axboeadf57092008-04-16 19:43:17 +0200468 if (add_pool(&mp[nr_pools], size))
Jens Axboe65864cf2008-03-03 10:36:36 +0100469 goto out;
470 global_read_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100471 }
472 } while (1);
473
Jens Axboe65864cf2008-03-03 10:36:36 +0100474 global_read_unlock();
475out:
Jens Axboed24c33a2008-03-01 18:27:36 +0100476 return NULL;
477}
478
479char *smalloc_strdup(const char *str)
480{
481 char *ptr;
482
483 ptr = smalloc(strlen(str) + 1);
484 strcpy(ptr, str);
485 return ptr;
486}