xref: /aosp_15_r20/bionic/libc/private/bionic_allocator.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
32*8d67ca89SAndroid Build Coastguard Worker #include <stddef.h>
33*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
34*8d67ca89SAndroid Build Coastguard Worker 
35*8d67ca89SAndroid Build Coastguard Worker const uint32_t kSmallObjectMaxSizeLog2 = 10;
36*8d67ca89SAndroid Build Coastguard Worker const uint32_t kSmallObjectMinSizeLog2 = 4;
37*8d67ca89SAndroid Build Coastguard Worker const uint32_t kSmallObjectAllocatorsCount = kSmallObjectMaxSizeLog2 - kSmallObjectMinSizeLog2 + 1;
38*8d67ca89SAndroid Build Coastguard Worker 
39*8d67ca89SAndroid Build Coastguard Worker class BionicSmallObjectAllocator;
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker // This structure is placed at the beginning of each addressable page
42*8d67ca89SAndroid Build Coastguard Worker // and has all information we need to find the corresponding memory allocator.
43*8d67ca89SAndroid Build Coastguard Worker struct page_info {
44*8d67ca89SAndroid Build Coastguard Worker   char signature[4];
45*8d67ca89SAndroid Build Coastguard Worker   uint32_t type;
46*8d67ca89SAndroid Build Coastguard Worker   union {
47*8d67ca89SAndroid Build Coastguard Worker     // we use allocated_size for large objects allocator
48*8d67ca89SAndroid Build Coastguard Worker     size_t allocated_size;
49*8d67ca89SAndroid Build Coastguard Worker     // and allocator_addr for small ones.
50*8d67ca89SAndroid Build Coastguard Worker     BionicSmallObjectAllocator* allocator_addr;
51*8d67ca89SAndroid Build Coastguard Worker   };
52*8d67ca89SAndroid Build Coastguard Worker };
53*8d67ca89SAndroid Build Coastguard Worker 
54*8d67ca89SAndroid Build Coastguard Worker struct small_object_block_record {
55*8d67ca89SAndroid Build Coastguard Worker   small_object_block_record* next;
56*8d67ca89SAndroid Build Coastguard Worker   size_t free_blocks_cnt;
57*8d67ca89SAndroid Build Coastguard Worker };
58*8d67ca89SAndroid Build Coastguard Worker 
59*8d67ca89SAndroid Build Coastguard Worker // This structure is placed at the beginning of each page managed by
60*8d67ca89SAndroid Build Coastguard Worker // BionicSmallObjectAllocator.  Note that a page_info struct is expected at the
61*8d67ca89SAndroid Build Coastguard Worker // beginning of each page as well, and therefore this structure contains a
62*8d67ca89SAndroid Build Coastguard Worker // page_info as its *first* field.
63*8d67ca89SAndroid Build Coastguard Worker struct small_object_page_info {
64*8d67ca89SAndroid Build Coastguard Worker   page_info info;  // Must be the first field.
65*8d67ca89SAndroid Build Coastguard Worker 
66*8d67ca89SAndroid Build Coastguard Worker   // Doubly linked list for traversing all pages allocated by a
67*8d67ca89SAndroid Build Coastguard Worker   // BionicSmallObjectAllocator.
68*8d67ca89SAndroid Build Coastguard Worker   small_object_page_info* next_page;
69*8d67ca89SAndroid Build Coastguard Worker   small_object_page_info* prev_page;
70*8d67ca89SAndroid Build Coastguard Worker 
71*8d67ca89SAndroid Build Coastguard Worker   // Linked list containing all free blocks in this page.
72*8d67ca89SAndroid Build Coastguard Worker   small_object_block_record* free_block_list;
73*8d67ca89SAndroid Build Coastguard Worker 
74*8d67ca89SAndroid Build Coastguard Worker   // Free blocks counter.
75*8d67ca89SAndroid Build Coastguard Worker   size_t free_blocks_cnt;
76*8d67ca89SAndroid Build Coastguard Worker };
77*8d67ca89SAndroid Build Coastguard Worker 
78*8d67ca89SAndroid Build Coastguard Worker class BionicSmallObjectAllocator {
79*8d67ca89SAndroid Build Coastguard Worker  public:
80*8d67ca89SAndroid Build Coastguard Worker   BionicSmallObjectAllocator(uint32_t type, size_t block_size);
81*8d67ca89SAndroid Build Coastguard Worker   void* alloc();
82*8d67ca89SAndroid Build Coastguard Worker   void free(void* ptr);
83*8d67ca89SAndroid Build Coastguard Worker 
get_block_size()84*8d67ca89SAndroid Build Coastguard Worker   size_t get_block_size() const { return block_size_; }
85*8d67ca89SAndroid Build Coastguard Worker  private:
86*8d67ca89SAndroid Build Coastguard Worker   void alloc_page();
87*8d67ca89SAndroid Build Coastguard Worker   void free_page(small_object_page_info* page);
88*8d67ca89SAndroid Build Coastguard Worker   void add_to_page_list(small_object_page_info* page);
89*8d67ca89SAndroid Build Coastguard Worker   void remove_from_page_list(small_object_page_info* page);
90*8d67ca89SAndroid Build Coastguard Worker 
91*8d67ca89SAndroid Build Coastguard Worker   const uint32_t type_;
92*8d67ca89SAndroid Build Coastguard Worker   const size_t block_size_;
93*8d67ca89SAndroid Build Coastguard Worker   const size_t blocks_per_page_;
94*8d67ca89SAndroid Build Coastguard Worker 
95*8d67ca89SAndroid Build Coastguard Worker   size_t free_pages_cnt_;
96*8d67ca89SAndroid Build Coastguard Worker 
97*8d67ca89SAndroid Build Coastguard Worker   small_object_page_info* page_list_;
98*8d67ca89SAndroid Build Coastguard Worker };
99*8d67ca89SAndroid Build Coastguard Worker 
100*8d67ca89SAndroid Build Coastguard Worker class BionicAllocator {
101*8d67ca89SAndroid Build Coastguard Worker  public:
BionicAllocator()102*8d67ca89SAndroid Build Coastguard Worker   constexpr BionicAllocator() : allocators_(nullptr), allocators_buf_() {}
103*8d67ca89SAndroid Build Coastguard Worker   void* alloc(size_t size);
104*8d67ca89SAndroid Build Coastguard Worker   void* memalign(size_t align, size_t size);
105*8d67ca89SAndroid Build Coastguard Worker 
106*8d67ca89SAndroid Build Coastguard Worker   // Note that this implementation of realloc never shrinks allocation
107*8d67ca89SAndroid Build Coastguard Worker   void* realloc(void* ptr, size_t size);
108*8d67ca89SAndroid Build Coastguard Worker   void free(void* ptr);
109*8d67ca89SAndroid Build Coastguard Worker 
110*8d67ca89SAndroid Build Coastguard Worker   // Returns the size of the given allocated heap chunk, if it is valid.
111*8d67ca89SAndroid Build Coastguard Worker   // Otherwise, this may return 0 or cause a segfault if the pointer is invalid.
112*8d67ca89SAndroid Build Coastguard Worker   size_t get_chunk_size(void* ptr);
113*8d67ca89SAndroid Build Coastguard Worker 
114*8d67ca89SAndroid Build Coastguard Worker  private:
115*8d67ca89SAndroid Build Coastguard Worker   void* alloc_mmap(size_t align, size_t size);
116*8d67ca89SAndroid Build Coastguard Worker   inline void* alloc_impl(size_t align, size_t size);
117*8d67ca89SAndroid Build Coastguard Worker   inline page_info* get_page_info_unchecked(void* ptr);
118*8d67ca89SAndroid Build Coastguard Worker   inline page_info* get_page_info(void* ptr);
119*8d67ca89SAndroid Build Coastguard Worker   BionicSmallObjectAllocator* get_small_object_allocator_unchecked(uint32_t type);
120*8d67ca89SAndroid Build Coastguard Worker   BionicSmallObjectAllocator* get_small_object_allocator(page_info* pi, void* ptr);
121*8d67ca89SAndroid Build Coastguard Worker   void initialize_allocators();
122*8d67ca89SAndroid Build Coastguard Worker 
123*8d67ca89SAndroid Build Coastguard Worker   BionicSmallObjectAllocator* allocators_;
124*8d67ca89SAndroid Build Coastguard Worker   uint8_t allocators_buf_[sizeof(BionicSmallObjectAllocator)*kSmallObjectAllocatorsCount];
125*8d67ca89SAndroid Build Coastguard Worker };
126