blob: b460d6573bbc4e081a394a76808a18e5ee705ab7 [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>
Bruce Crane43606c2012-02-20 09:34:24 +010011#include <inttypes.h>
Jens Axboed24c33a2008-03-01 18:27:36 +010012#include <sys/types.h>
13#include <limits.h>
Greg Edwards3a8600b2010-06-25 09:24:19 -060014#include <fcntl.h>
Jens Axboed24c33a2008-03-01 18:27:36 +010015
Jens Axboe6548f472008-06-13 08:38:31 +020016#include "mutex.h"
Jens Axboeb3268b92008-06-02 09:59:32 +020017#include "arch/arch.h"
Greg Edwards3a8600b2010-06-25 09:24:19 -060018#include "os/os.h"
Jens Axboe10aa1362014-04-01 21:10:36 -060019#include "smalloc.h"
Jens Axboef5c3fce2014-10-13 11:53:55 -060020#include "log.h"
Jens Axboed24c33a2008-03-01 18:27:36 +010021
Jens Axboe55f64912008-05-26 09:37:21 +020022#define SMALLOC_REDZONE /* define to detect memory corruption */
Jens Axboed24c33a2008-03-01 18:27:36 +010023
Jens Axboeec996e92008-05-27 11:39:39 +020024#define SMALLOC_BPB 32 /* block size, bytes-per-bit in bitmap */
25#define SMALLOC_BPI (sizeof(unsigned int) * 8)
26#define SMALLOC_BPL (SMALLOC_BPB * SMALLOC_BPI)
27
Jens Axboe016f32f2015-01-13 21:35:31 -070028#define INITIAL_SIZE 16*1024*1024 /* new pool size */
Jens Axboed19a46f2015-03-03 15:13:17 -070029#define MAX_POOLS 8 /* maximum number of pools to setup */
Jens Axboed24c33a2008-03-01 18:27:36 +010030
Jens Axboe55f64912008-05-26 09:37:21 +020031#define SMALLOC_PRE_RED 0xdeadbeefU
32#define SMALLOC_POST_RED 0x5aa55aa5U
Jens Axboe55f64912008-05-26 09:37:21 +020033
Jens Axboe2b386d22008-03-26 10:32:57 +010034unsigned int smalloc_pool_size = INITIAL_SIZE;
Jens Axboe10aa1362014-04-01 21:10:36 -060035static const int int_mask = sizeof(int) - 1;
Jens Axboe2b386d22008-03-26 10:32:57 +010036
Jens Axboed24c33a2008-03-01 18:27:36 +010037struct pool {
Jens Axboe6548f472008-06-13 08:38:31 +020038 struct fio_mutex *lock; /* protects this pool */
Jens Axboed24c33a2008-03-01 18:27:36 +010039 void *map; /* map of blocks */
Jens Axboeec996e92008-05-27 11:39:39 +020040 unsigned int *bitmap; /* blocks free/busy map */
Jens Axboea3ebe7e2012-11-02 16:47:03 +010041 size_t free_blocks; /* free blocks */
42 size_t nr_blocks; /* total blocks */
43 size_t next_non_full;
44 size_t mmap_size;
Jens Axboeec996e92008-05-27 11:39:39 +020045};
46
47struct block_hdr {
Jens Axboea3ebe7e2012-11-02 16:47:03 +010048 size_t size;
Jens Axboeec996e92008-05-27 11:39:39 +020049#ifdef SMALLOC_REDZONE
50 unsigned int prered;
51#endif
Jens Axboed24c33a2008-03-01 18:27:36 +010052};
53
54static struct pool mp[MAX_POOLS];
55static unsigned int nr_pools;
56static unsigned int last_pool;
Jens Axboed7df1d12013-03-20 19:57:01 -060057static struct fio_rwlock *lock;
Jens Axboed24c33a2008-03-01 18:27:36 +010058
Jens Axboed24c33a2008-03-01 18:27:36 +010059static inline void pool_lock(struct pool *pool)
60{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010061 fio_mutex_down(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010062}
63
64static inline void pool_unlock(struct pool *pool)
65{
Jens Axboe2e3e31e2009-01-06 11:30:08 +010066 fio_mutex_up(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010067}
68
Jens Axboe65864cf2008-03-03 10:36:36 +010069static inline void global_read_lock(void)
Jens Axboed24c33a2008-03-01 18:27:36 +010070{
Jens Axboed7df1d12013-03-20 19:57:01 -060071 fio_rwlock_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 Axboed7df1d12013-03-20 19:57:01 -060076 fio_rwlock_unlock(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010077}
78
79static inline void global_write_lock(void)
80{
Jens Axboed7df1d12013-03-20 19:57:01 -060081 fio_rwlock_write(lock);
Jens Axboe65864cf2008-03-03 10:36:36 +010082}
83
84static inline void global_write_unlock(void)
85{
Jens Axboed7df1d12013-03-20 19:57:01 -060086 fio_rwlock_unlock(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +010087}
88
Jens Axboed24c33a2008-03-01 18:27:36 +010089static inline int ptr_valid(struct pool *pool, void *ptr)
90{
Jens Axboedcb69092008-05-27 20:35:18 +020091 unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
Jens Axboeec996e92008-05-27 11:39:39 +020092
93 return (ptr >= pool->map) && (ptr < pool->map + pool_size);
Jens Axboed24c33a2008-03-01 18:27:36 +010094}
95
Jens Axboea3ebe7e2012-11-02 16:47:03 +010096static inline size_t size_to_blocks(size_t size)
Jens Axboe808e9ea2008-05-27 14:12:45 +020097{
98 return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
99}
100
Jens Axboedcb69092008-05-27 20:35:18 +0200101static int blocks_iter(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100102 unsigned int idx, size_t nr_blocks,
Jens Axboeec996e92008-05-27 11:39:39 +0200103 int (*func)(unsigned int *map, unsigned int mask))
Jens Axboed24c33a2008-03-01 18:27:36 +0100104{
Jens Axboedcb69092008-05-27 20:35:18 +0200105
Jens Axboeec996e92008-05-27 11:39:39 +0200106 while (nr_blocks) {
107 unsigned int this_blocks, mask;
Jens Axboedcb69092008-05-27 20:35:18 +0200108 unsigned int *map;
109
110 if (pool_idx >= pool->nr_blocks)
111 return 0;
112
113 map = &pool->bitmap[pool_idx];
Jens Axboed24c33a2008-03-01 18:27:36 +0100114
Jens Axboeec996e92008-05-27 11:39:39 +0200115 this_blocks = nr_blocks;
116 if (this_blocks + idx > SMALLOC_BPI) {
117 this_blocks = SMALLOC_BPI - idx;
118 idx = SMALLOC_BPI - this_blocks;
Jens Axboed24c33a2008-03-01 18:27:36 +0100119 }
Jens Axboeec996e92008-05-27 11:39:39 +0200120
121 if (this_blocks == SMALLOC_BPI)
122 mask = -1U;
123 else
124 mask = ((1U << this_blocks) - 1) << idx;
125
126 if (!func(map, mask))
127 return 0;
128
129 nr_blocks -= this_blocks;
130 idx = 0;
Jens Axboedcb69092008-05-27 20:35:18 +0200131 pool_idx++;
Jens Axboed24c33a2008-03-01 18:27:36 +0100132 }
133
Jens Axboeec996e92008-05-27 11:39:39 +0200134 return 1;
Jens Axboeec996e92008-05-27 11:39:39 +0200135}
136
137static int mask_cmp(unsigned int *map, unsigned int mask)
138{
139 return !(*map & mask);
140}
141
142static int mask_clear(unsigned int *map, unsigned int mask)
143{
Jens Axboedcb69092008-05-27 20:35:18 +0200144 assert((*map & mask) == mask);
Jens Axboeec996e92008-05-27 11:39:39 +0200145 *map &= ~mask;
146 return 1;
147}
148
149static int mask_set(unsigned int *map, unsigned int mask)
150{
Jens Axboedcb69092008-05-27 20:35:18 +0200151 assert(!(*map & mask));
Jens Axboeec996e92008-05-27 11:39:39 +0200152 *map |= mask;
153 return 1;
154}
155
Jens Axboedcb69092008-05-27 20:35:18 +0200156static int blocks_free(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100157 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200158{
Jens Axboedcb69092008-05-27 20:35:18 +0200159 return blocks_iter(pool, pool_idx, idx, nr_blocks, mask_cmp);
Jens Axboeec996e92008-05-27 11:39:39 +0200160}
161
Jens Axboedcb69092008-05-27 20:35:18 +0200162static void set_blocks(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100163 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200164{
Jens Axboedcb69092008-05-27 20:35:18 +0200165 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_set);
Jens Axboeec996e92008-05-27 11:39:39 +0200166}
167
Jens Axboedcb69092008-05-27 20:35:18 +0200168static void clear_blocks(struct pool *pool, unsigned int pool_idx,
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100169 unsigned int idx, size_t nr_blocks)
Jens Axboeec996e92008-05-27 11:39:39 +0200170{
Jens Axboedcb69092008-05-27 20:35:18 +0200171 blocks_iter(pool, pool_idx, idx, nr_blocks, mask_clear);
Jens Axboeec996e92008-05-27 11:39:39 +0200172}
173
Jens Axboeec996e92008-05-27 11:39:39 +0200174static int find_next_zero(int word, int start)
175{
176 assert(word != -1U);
Jiri Horky271067a2011-08-15 15:18:03 +0200177 word >>= start;
178 return ffz(word) + start;
Jens Axboed24c33a2008-03-01 18:27:36 +0100179}
180
Jens Axboeadf57092008-04-16 19:43:17 +0200181static int add_pool(struct pool *pool, unsigned int alloc_size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100182{
Jens Axboe8d5844e2011-06-29 09:50:08 +0200183 int bitmap_blocks;
Jens Axboeef7035a2014-06-18 15:30:09 -0700184 int mmap_flags;
Jens Axboeb8a65822008-12-04 14:33:41 +0100185 void *ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200186
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;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200199
Jens Axboeec996e92008-05-27 11:39:39 +0200200 pool->nr_blocks = bitmap_blocks;
201 pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
202
Jens Axboeef7035a2014-06-18 15:30:09 -0700203 mmap_flags = OS_MAP_ANON;
204#ifdef CONFIG_ESX
205 mmap_flags |= MAP_PRIVATE;
206#else
207 mmap_flags |= MAP_SHARED;
208#endif
209 ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0);
210
Jens Axboed24c33a2008-03-01 18:27:36 +0100211 if (ptr == MAP_FAILED)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200212 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100213
Jens Axboeec996e92008-05-27 11:39:39 +0200214 memset(ptr, 0, alloc_size);
215 pool->map = ptr;
216 pool->bitmap = (void *) ptr + (pool->nr_blocks * SMALLOC_BPL);
Jens Axboed24c33a2008-03-01 18:27:36 +0100217
Jens Axboe521da522012-08-02 11:21:36 +0200218 pool->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboed24c33a2008-03-01 18:27:36 +0100219 if (!pool->lock)
Jens Axboe8d5844e2011-06-29 09:50:08 +0200220 goto out_fail;
Jens Axboed24c33a2008-03-01 18:27:36 +0100221
Jens Axboed24c33a2008-03-01 18:27:36 +0100222 nr_pools++;
223 return 0;
Jens Axboe8d5844e2011-06-29 09:50:08 +0200224out_fail:
Jens Axboef5c3fce2014-10-13 11:53:55 -0600225 log_err("smalloc: failed adding pool\n");
Jens Axboed24c33a2008-03-01 18:27:36 +0100226 if (pool->map)
Jens Axboeec996e92008-05-27 11:39:39 +0200227 munmap(pool->map, pool->mmap_size);
Jens Axboed24c33a2008-03-01 18:27:36 +0100228 return 1;
229}
230
231void sinit(void)
232{
Jens Axboed19a46f2015-03-03 15:13:17 -0700233 int i, ret;
Jens Axboed24c33a2008-03-01 18:27:36 +0100234
Jens Axboed7df1d12013-03-20 19:57:01 -0600235 lock = fio_rwlock_init();
Jens Axboed19a46f2015-03-03 15:13:17 -0700236
237 for (i = 0; i < MAX_POOLS; i++) {
238 ret = add_pool(&mp[i], INITIAL_SIZE);
239 if (ret)
240 break;
241 }
242
243 /*
244 * If we added at least one pool, we should be OK for most
245 * cases.
246 */
247 assert(i);
Jens Axboed24c33a2008-03-01 18:27:36 +0100248}
249
250static void cleanup_pool(struct pool *pool)
251{
Jens Axboe443bb112008-12-04 14:30:13 +0100252 /*
253 * This will also remove the temporary file we used as a backing
254 * store, it was already unlinked
255 */
Jens Axboeec996e92008-05-27 11:39:39 +0200256 munmap(pool->map, pool->mmap_size);
Jens Axboe6548f472008-06-13 08:38:31 +0200257
258 if (pool->lock)
259 fio_mutex_remove(pool->lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100260}
261
262void scleanup(void)
263{
264 unsigned int i;
265
266 for (i = 0; i < nr_pools; i++)
267 cleanup_pool(&mp[i]);
268
Jens Axboe6548f472008-06-13 08:38:31 +0200269 if (lock)
Jens Axboed7df1d12013-03-20 19:57:01 -0600270 fio_rwlock_remove(lock);
Jens Axboed24c33a2008-03-01 18:27:36 +0100271}
272
Jens Axboe89da54e2008-05-27 20:49:29 +0200273#ifdef SMALLOC_REDZONE
Jens Axboecf987082009-07-07 17:43:11 +0200274static void *postred_ptr(struct block_hdr *hdr)
275{
Bruce Crane43606c2012-02-20 09:34:24 +0100276 uintptr_t ptr;
Jens Axboecf987082009-07-07 17:43:11 +0200277
Bruce Crane43606c2012-02-20 09:34:24 +0100278 ptr = (uintptr_t) hdr + hdr->size - sizeof(unsigned int);
Jens Axboecf987082009-07-07 17:43:11 +0200279 ptr = (ptr + int_mask) & ~int_mask;
280
281 return (void *) ptr;
282}
283
Jens Axboeec996e92008-05-27 11:39:39 +0200284static void fill_redzone(struct block_hdr *hdr)
Jens Axboe55f64912008-05-26 09:37:21 +0200285{
Jens Axboecf987082009-07-07 17:43:11 +0200286 unsigned int *postred = postred_ptr(hdr);
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 Axboecf987082009-07-07 17:43:11 +0200294 unsigned int *postred = postred_ptr(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200295
296 if (hdr->prered != SMALLOC_PRE_RED) {
Jens Axboef5c3fce2014-10-13 11:53:55 -0600297 log_err("smalloc pre redzone destroyed!\n"
298 " 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) {
Jens Axboef5c3fce2014-10-13 11:53:55 -0600303 log_err("smalloc post redzone destroyed!\n"
304 " 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 Axboea3ebe7e2012-11-02 16:47:03 +0100370static void *__smalloc_pool(struct pool *pool, size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100371{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100372 size_t nr_blocks;
Jens Axboeec996e92008-05-27 11:39:39 +0200373 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 Axboea3ebe7e2012-11-02 16:47:03 +0100425static void *smalloc_pool(struct pool *pool, size_t size)
Jens Axboe55f64912008-05-26 09:37:21 +0200426{
Jens Axboea3ebe7e2012-11-02 16:47:03 +0100427 size_t alloc_size = size + sizeof(struct block_hdr);
Jens Axboe55f64912008-05-26 09:37:21 +0200428 void *ptr;
429
Jens Axboecf987082009-07-07 17:43:11 +0200430 /*
Jens Axboe122426d2009-07-07 23:03:06 +0200431 * Round to int alignment, so that the postred pointer will
432 * be naturally aligned as well.
Jens Axboecf987082009-07-07 17:43:11 +0200433 */
Jens Axboeec996e92008-05-27 11:39:39 +0200434#ifdef SMALLOC_REDZONE
Jens Axboe122426d2009-07-07 23:03:06 +0200435 alloc_size += sizeof(unsigned int);
436 alloc_size = (alloc_size + int_mask) & ~int_mask;
Jens Axboe55f64912008-05-26 09:37:21 +0200437#endif
Jens Axboeec996e92008-05-27 11:39:39 +0200438
439 ptr = __smalloc_pool(pool, alloc_size);
Jens Axboe89da54e2008-05-27 20:49:29 +0200440 if (ptr) {
441 struct block_hdr *hdr = ptr;
Jens Axboeec996e92008-05-27 11:39:39 +0200442
Jens Axboe89da54e2008-05-27 20:49:29 +0200443 hdr->size = alloc_size;
444 fill_redzone(hdr);
Jens Axboeec996e92008-05-27 11:39:39 +0200445
Jens Axboe89da54e2008-05-27 20:49:29 +0200446 ptr += sizeof(*hdr);
447 memset(ptr, 0, size);
448 }
Jens Axboeec996e92008-05-27 11:39:39 +0200449
Jens Axboeec996e92008-05-27 11:39:39 +0200450 return ptr;
Jens Axboe55f64912008-05-26 09:37:21 +0200451}
452
Jens Axboe7982aa72012-11-02 16:35:18 +0100453void *smalloc(size_t size)
Jens Axboed24c33a2008-03-01 18:27:36 +0100454{
Jens Axboed19a46f2015-03-03 15:13:17 -0700455 unsigned int i, end_pool;
Jens Axboed24c33a2008-03-01 18:27:36 +0100456
Jens Axboe7982aa72012-11-02 16:35:18 +0100457 if (size != (unsigned int) size)
458 return NULL;
459
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200460 global_write_lock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100461 i = last_pool;
Jens Axboed19a46f2015-03-03 15:13:17 -0700462 end_pool = nr_pools;
Jens Axboed24c33a2008-03-01 18:27:36 +0100463
464 do {
Jens Axboed19a46f2015-03-03 15:13:17 -0700465 for (; i < end_pool; i++) {
Jens Axboed24c33a2008-03-01 18:27:36 +0100466 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) {
Jens Axboed19a46f2015-03-03 15:13:17 -0700475 end_pool = last_pool;
476 last_pool = i = 0;
Jens Axboed24c33a2008-03-01 18:27:36 +0100477 continue;
478 }
479
Jens Axboed19a46f2015-03-03 15:13:17 -0700480 break;
Jens Axboed24c33a2008-03-01 18:27:36 +0100481 } while (1);
482
Shaozhi Shawn Yed1271dc2008-09-10 09:02:51 +0200483 global_write_unlock();
Jens Axboed24c33a2008-03-01 18:27:36 +0100484 return NULL;
485}
486
Jens Axboedc068d52014-11-06 15:19:31 -0700487void *scalloc(size_t nmemb, size_t size)
488{
489 void *ret;
490
491 ret = smalloc(nmemb * size);
492 if (ret)
493 memset(ret, 0, nmemb * size);
494
495 return ret;
496}
497
Jens Axboed24c33a2008-03-01 18:27:36 +0100498char *smalloc_strdup(const char *str)
499{
Christian Ehrhardteb433fd2015-03-03 12:44:46 +0100500 char *ptr = NULL;
Jens Axboed24c33a2008-03-01 18:27:36 +0100501
502 ptr = smalloc(strlen(str) + 1);
Christian Ehrhardteb433fd2015-03-03 12:44:46 +0100503 if (ptr)
504 strcpy(ptr, str);
Jens Axboed24c33a2008-03-01 18:27:36 +0100505 return ptr;
506}