blob: da0f7d08be0f452e76928b9a352b8b9ab2055b9b [file] [log] [blame]
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070027 */
28
Ryan Prichard083d8502019-01-24 13:47:13 -080029#include "private/bionic_allocator.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070030
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070031#include <stdlib.h>
Ryan Prichard52165b32019-01-23 17:46:24 -080032#include <string.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070033#include <sys/mman.h>
Ryan Prichard96773a22019-01-24 15:22:50 -080034#include <sys/param.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070035#include <sys/prctl.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070036#include <unistd.h>
37
Ryan Prichard52165b32019-01-23 17:46:24 -080038#include <new>
39
Christopher Ferris7a3681e2017-04-24 17:48:32 -070040#include <async_safe/log.h>
Elliott Hughes3019d782019-02-13 12:39:07 -080041#include <async_safe/CHECK.h>
Christopher Ferris7a3681e2017-04-24 17:48:32 -070042
Ryan Prichard96773a22019-01-24 15:22:50 -080043#include "private/bionic_macros.h"
Ryan Prichard52165b32019-01-23 17:46:24 -080044#include "private/bionic_page.h"
45
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070046//
Ryan Prichard083d8502019-01-24 13:47:13 -080047// BionicAllocator is a general purpose allocator designed to provide the same
48// functionality as the malloc/free/realloc libc functions.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070049//
50// On alloc:
51// If size is >= 1k allocator proxies malloc call directly to mmap
52// If size < 1k allocator uses SmallObjectAllocator for the size
53// rounded up to the nearest power of two.
54//
55// On free:
56//
57// For a pointer allocated using proxy-to-mmap allocator unmaps
58// the memory.
59//
60// For a pointer allocated using SmallObjectAllocator it adds
Vic Yang54938512018-12-02 23:46:26 -080061// the block to free_blocks_list in the corresponding page. If the number of
62// free pages reaches 2, SmallObjectAllocator munmaps one of the pages keeping
63// the other one in reserve.
64
65// Memory management for large objects is fairly straightforward, but for small
66// objects it is more complicated. If you are changing this code, one simple
67// way to evaluate the memory usage change is by running 'dd' and examine the
68// memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that:
69// 1. It links in quite a few libraries, so you get some linker memory use.
70// 2. When run with no arguments, it sits waiting for input, so it is easy to
71// examine its memory usage with showmap.
72// 3. Since it does nothing while waiting for input, the memory usage is
73// determinisitic.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070074
75static const char kSignature[4] = {'L', 'M', 'A', 1};
76
77static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
78
79// This type is used for large allocations (with size >1k)
80static const uint32_t kLargeObject = 111;
81
Vic Yang259429b2018-12-04 23:59:57 -080082// Allocated pointers must be at least 16-byte aligned. Round up the size of
83// page_info to multiple of 16.
84static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16);
85
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070086static inline uint16_t log2(size_t number) {
87 uint16_t result = 0;
88 number--;
89
90 while (number != 0) {
91 result++;
92 number >>= 1;
93 }
94
95 return result;
96}
97
Ryan Prichard083d8502019-01-24 13:47:13 -080098BionicSmallObjectAllocator::BionicSmallObjectAllocator(uint32_t type,
Vic Yang54938512018-12-02 23:46:26 -080099 size_t block_size)
100 : type_(type),
101 block_size_(block_size),
Vic Yang259429b2018-12-04 23:59:57 -0800102 blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) /
103 block_size),
Vic Yang54938512018-12-02 23:46:26 -0800104 free_pages_cnt_(0),
105 page_list_(nullptr) {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700106
Ryan Prichard083d8502019-01-24 13:47:13 -0800107void* BionicSmallObjectAllocator::alloc() {
Dimitry Ivanovf8572112016-07-13 10:24:06 -0700108 CHECK(block_size_ != 0);
109
Vic Yang54938512018-12-02 23:46:26 -0800110 if (page_list_ == nullptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700111 alloc_page();
112 }
113
Vic Yang54938512018-12-02 23:46:26 -0800114 // Fully allocated pages are de-managed and removed from the page list, so
115 // every page from the page list must be useable. Let's just take the first
116 // one.
117 small_object_page_info* page = page_list_;
118 CHECK(page->free_block_list != nullptr);
119
120 small_object_block_record* const block_record = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700121 if (block_record->free_blocks_cnt > 1) {
Vic Yang54938512018-12-02 23:46:26 -0800122 small_object_block_record* next_free =
123 reinterpret_cast<small_object_block_record*>(
124 reinterpret_cast<uint8_t*>(block_record) + block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700125 next_free->next = block_record->next;
126 next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
Vic Yang54938512018-12-02 23:46:26 -0800127 page->free_block_list = next_free;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700128 } else {
Vic Yang54938512018-12-02 23:46:26 -0800129 page->free_block_list = block_record->next;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700130 }
131
Vic Yang259429b2018-12-04 23:59:57 -0800132 if (page->free_blocks_cnt == blocks_per_page_) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700133 free_pages_cnt_--;
134 }
135
Vic Yang54938512018-12-02 23:46:26 -0800136 page->free_blocks_cnt--;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700137
138 memset(block_record, 0, block_size_);
139
Vic Yang54938512018-12-02 23:46:26 -0800140 if (page->free_blocks_cnt == 0) {
141 // De-manage fully allocated pages. These pages will be managed again if
142 // a block is freed.
143 remove_from_page_list(page);
144 }
145
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700146 return block_record;
147}
148
Ryan Prichard083d8502019-01-24 13:47:13 -0800149void BionicSmallObjectAllocator::free_page(small_object_page_info* page) {
Vic Yang259429b2018-12-04 23:59:57 -0800150 CHECK(page->free_blocks_cnt == blocks_per_page_);
Vic Yang54938512018-12-02 23:46:26 -0800151 if (page->prev_page) {
152 page->prev_page->next_page = page->next_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700153 }
Vic Yang54938512018-12-02 23:46:26 -0800154 if (page->next_page) {
155 page->next_page->prev_page = page->prev_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700156 }
Vic Yang54938512018-12-02 23:46:26 -0800157 if (page_list_ == page) {
158 page_list_ = page->next_page;
159 }
160 munmap(page, PAGE_SIZE);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700161 free_pages_cnt_--;
162}
163
Ryan Prichard083d8502019-01-24 13:47:13 -0800164void BionicSmallObjectAllocator::free(void* ptr) {
Vic Yang54938512018-12-02 23:46:26 -0800165 small_object_page_info* const page =
166 reinterpret_cast<small_object_page_info*>(
167 PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700168
Vic Yang259429b2018-12-04 23:59:57 -0800169 if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700170 async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700171 }
172
173 memset(ptr, 0, block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800174 small_object_block_record* const block_record =
175 reinterpret_cast<small_object_block_record*>(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700176
Vic Yang54938512018-12-02 23:46:26 -0800177 block_record->next = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700178 block_record->free_blocks_cnt = 1;
179
Vic Yang54938512018-12-02 23:46:26 -0800180 page->free_block_list = block_record;
181 page->free_blocks_cnt++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700182
Vic Yang259429b2018-12-04 23:59:57 -0800183 if (page->free_blocks_cnt == blocks_per_page_) {
Vic Yang54938512018-12-02 23:46:26 -0800184 if (++free_pages_cnt_ > 1) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700185 // if we already have a free page - unmap this one.
Vic Yang54938512018-12-02 23:46:26 -0800186 free_page(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700187 }
Vic Yang54938512018-12-02 23:46:26 -0800188 } else if (page->free_blocks_cnt == 1) {
189 // We just freed from a full page. Add this page back to the list.
190 add_to_page_list(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700191 }
192}
193
Ryan Prichard083d8502019-01-24 13:47:13 -0800194void BionicSmallObjectAllocator::alloc_page() {
Vic Yang54938512018-12-02 23:46:26 -0800195 void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE,
196 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700197 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700198 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700199 }
200
Vic Yang54938512018-12-02 23:46:26 -0800201 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE,
Ryan Prichard083d8502019-01-24 13:47:13 -0800202 "bionic_alloc_small_objects");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700203
Vic Yang54938512018-12-02 23:46:26 -0800204 small_object_page_info* const page =
205 reinterpret_cast<small_object_page_info*>(map_ptr);
206 memcpy(page->info.signature, kSignature, sizeof(kSignature));
207 page->info.type = type_;
208 page->info.allocator_addr = this;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700209
Vic Yang259429b2018-12-04 23:59:57 -0800210 page->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700211
Vic Yang259429b2018-12-04 23:59:57 -0800212 // Align the first block to block_size_.
213 const uintptr_t first_block_addr =
214 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800215 small_object_block_record* const first_block =
Vic Yang259429b2018-12-04 23:59:57 -0800216 reinterpret_cast<small_object_block_record*>(first_block_addr);
217
Vic Yang54938512018-12-02 23:46:26 -0800218 first_block->next = nullptr;
Vic Yang259429b2018-12-04 23:59:57 -0800219 first_block->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700220
Vic Yang54938512018-12-02 23:46:26 -0800221 page->free_block_list = first_block;
222
223 add_to_page_list(page);
Vic Yangde696602018-11-27 13:34:44 -0800224
225 free_pages_cnt_++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700226}
227
Ryan Prichard083d8502019-01-24 13:47:13 -0800228void BionicSmallObjectAllocator::add_to_page_list(small_object_page_info* page) {
Vic Yang54938512018-12-02 23:46:26 -0800229 page->next_page = page_list_;
230 page->prev_page = nullptr;
231 if (page_list_) {
232 page_list_->prev_page = page;
233 }
234 page_list_ = page;
235}
236
Ryan Prichard083d8502019-01-24 13:47:13 -0800237void BionicSmallObjectAllocator::remove_from_page_list(
Vic Yang54938512018-12-02 23:46:26 -0800238 small_object_page_info* page) {
239 if (page->prev_page) {
240 page->prev_page->next_page = page->next_page;
241 }
242 if (page->next_page) {
243 page->next_page->prev_page = page->prev_page;
244 }
245 if (page_list_ == page) {
246 page_list_ = page->next_page;
247 }
248 page->prev_page = nullptr;
249 page->next_page = nullptr;
250}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700251
Ryan Prichard083d8502019-01-24 13:47:13 -0800252void BionicAllocator::initialize_allocators() {
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700253 if (allocators_ != nullptr) {
254 return;
255 }
256
Ryan Prichard083d8502019-01-24 13:47:13 -0800257 BionicSmallObjectAllocator* allocators =
258 reinterpret_cast<BionicSmallObjectAllocator*>(allocators_buf_);
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700259
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700260 for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
261 uint32_t type = i + kSmallObjectMinSizeLog2;
Ryan Prichard083d8502019-01-24 13:47:13 -0800262 new (allocators + i) BionicSmallObjectAllocator(type, 1 << type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700263 }
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700264
265 allocators_ = allocators;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700266}
267
Ryan Prichard96773a22019-01-24 15:22:50 -0800268void* BionicAllocator::alloc_mmap(size_t align, size_t size) {
269 size_t header_size = __BIONIC_ALIGN(kPageInfoSize, align);
270 size_t allocated_size;
271 if (__builtin_add_overflow(header_size, size, &allocated_size) ||
272 PAGE_END(allocated_size) < allocated_size) {
273 async_safe_fatal("overflow trying to alloc %zu bytes", size);
274 }
275 allocated_size = PAGE_END(allocated_size);
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700276 void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
277 -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700278
279 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700280 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700281 }
282
Ryan Prichard083d8502019-01-24 13:47:13 -0800283 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700284
Ryan Prichard96773a22019-01-24 15:22:50 -0800285 void* result = static_cast<char*>(map_ptr) + header_size;
286 page_info* info = get_page_info_unchecked(result);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700287 memcpy(info->signature, kSignature, sizeof(kSignature));
288 info->type = kLargeObject;
289 info->allocated_size = allocated_size;
290
Ryan Prichard96773a22019-01-24 15:22:50 -0800291 return result;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700292}
293
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700294
Ryan Prichard96773a22019-01-24 15:22:50 -0800295inline void* BionicAllocator::alloc_impl(size_t align, size_t size) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700296 if (size > kSmallObjectMaxSize) {
Ryan Prichard96773a22019-01-24 15:22:50 -0800297 return alloc_mmap(align, size);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700298 }
299
300 uint16_t log2_size = log2(size);
301
302 if (log2_size < kSmallObjectMinSizeLog2) {
303 log2_size = kSmallObjectMinSizeLog2;
304 }
305
306 return get_small_object_allocator(log2_size)->alloc();
307}
308
Ryan Prichard96773a22019-01-24 15:22:50 -0800309void* BionicAllocator::alloc(size_t size) {
310 // treat alloc(0) as alloc(1)
311 if (size == 0) {
312 size = 1;
313 }
314 return alloc_impl(16, size);
315}
316
317void* BionicAllocator::memalign(size_t align, size_t size) {
318 // The Bionic allocator only supports alignment up to one page, which is good
319 // enough for ELF TLS.
320 align = MIN(align, PAGE_SIZE);
321 align = MAX(align, 16);
322 if (!powerof2(align)) {
323 align = BIONIC_ROUND_UP_POWER_OF_2(align);
324 }
325 size = MAX(size, align);
326 return alloc_impl(align, size);
327}
328
329inline page_info* BionicAllocator::get_page_info_unchecked(void* ptr) {
330 uintptr_t header_page = PAGE_START(reinterpret_cast<size_t>(ptr) - kPageInfoSize);
331 return reinterpret_cast<page_info*>(header_page);
332}
333
334inline page_info* BionicAllocator::get_page_info(void* ptr) {
335 page_info* info = get_page_info_unchecked(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700336 if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700337 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700338 }
339
340 return info;
341}
342
Ryan Prichard083d8502019-01-24 13:47:13 -0800343void* BionicAllocator::realloc(void* ptr, size_t size) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700344 if (ptr == nullptr) {
345 return alloc(size);
346 }
347
348 if (size == 0) {
349 free(ptr);
350 return nullptr;
351 }
352
353 page_info* info = get_page_info(ptr);
354
355 size_t old_size = 0;
356
357 if (info->type == kLargeObject) {
Ryan Prichard96773a22019-01-24 15:22:50 -0800358 old_size = info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700359 } else {
Ryan Prichard083d8502019-01-24 13:47:13 -0800360 BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700361 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700362 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700363 }
364
365 old_size = allocator->get_block_size();
366 }
367
368 if (old_size < size) {
369 void *result = alloc(size);
370 memcpy(result, ptr, old_size);
371 free(ptr);
372 return result;
373 }
374
375 return ptr;
376}
377
Ryan Prichard083d8502019-01-24 13:47:13 -0800378void BionicAllocator::free(void* ptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700379 if (ptr == nullptr) {
380 return;
381 }
382
383 page_info* info = get_page_info(ptr);
384
385 if (info->type == kLargeObject) {
386 munmap(info, info->allocated_size);
387 } else {
Ryan Prichard083d8502019-01-24 13:47:13 -0800388 BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700389 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700390 async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700391 }
392
393 allocator->free(ptr);
394 }
395}
396
Ryan Prichard083d8502019-01-24 13:47:13 -0800397BionicSmallObjectAllocator* BionicAllocator::get_small_object_allocator(uint32_t type) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700398 if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700399 async_safe_fatal("invalid type: %u", type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700400 }
401
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700402 initialize_allocators();
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700403 return &allocators_[type - kSmallObjectMinSizeLog2];
404}