1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  * All rights reserved.
4  *
5  * 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.
14  *
15  * 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.
27  */
28 
29 #pragma once
30 
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <sys/cdefs.h>
34 #include <sys/mman.h>
35 #include <sys/prctl.h>
36 #include <stddef.h>
37 #include <unistd.h>
38 
39 const uint32_t kSmallObjectMaxSizeLog2 = 10;
40 const uint32_t kSmallObjectMinSizeLog2 = 4;
41 const uint32_t kSmallObjectAllocatorsCount = kSmallObjectMaxSizeLog2 - kSmallObjectMinSizeLog2 + 1;
42 
43 class BionicSmallObjectAllocator;
44 
45 // This structure is placed at the beginning of each addressable page
46 // and has all information we need to find the corresponding memory allocator.
47 struct page_info {
48   char signature[4];
49   uint32_t type;
50   union {
51     // we use allocated_size for large objects allocator
52     size_t allocated_size;
53     // and allocator_addr for small ones.
54     BionicSmallObjectAllocator* allocator_addr;
55   };
56 };
57 
58 struct small_object_block_record {
59   small_object_block_record* next;
60   size_t free_blocks_cnt;
61 };
62 
63 // This structure is placed at the beginning of each page managed by
64 // BionicSmallObjectAllocator.  Note that a page_info struct is expected at the
65 // beginning of each page as well, and therefore this structure contains a
66 // page_info as its *first* field.
67 struct small_object_page_info {
68   page_info info;  // Must be the first field.
69 
70   // Doubly linked list for traversing all pages allocated by a
71   // BionicSmallObjectAllocator.
72   small_object_page_info* next_page;
73   small_object_page_info* prev_page;
74 
75   // Linked list containing all free blocks in this page.
76   small_object_block_record* free_block_list;
77 
78   // Free blocks counter.
79   size_t free_blocks_cnt;
80 };
81 
82 class BionicSmallObjectAllocator {
83  public:
84   BionicSmallObjectAllocator(uint32_t type, size_t block_size);
85   void* alloc();
86   void free(void* ptr);
87 
get_block_size()88   size_t get_block_size() const { return block_size_; }
89  private:
90   void alloc_page();
91   void free_page(small_object_page_info* page);
92   void add_to_page_list(small_object_page_info* page);
93   void remove_from_page_list(small_object_page_info* page);
94 
95   const uint32_t type_;
96   const size_t block_size_;
97   const size_t blocks_per_page_;
98 
99   size_t free_pages_cnt_;
100 
101   small_object_page_info* page_list_;
102 };
103 
104 class BionicAllocator {
105  public:
BionicAllocator()106   constexpr BionicAllocator() : allocators_(nullptr), allocators_buf_() {}
107   void* alloc(size_t size);
108   void* memalign(size_t align, size_t size);
109 
110   // Note that this implementation of realloc never shrinks allocation
111   void* realloc(void* ptr, size_t size);
112   void free(void* ptr);
113 
114   // Returns the size of the given allocated heap chunk, if it is valid.
115   // Otherwise, this may return 0 or cause a segfault if the pointer is invalid.
116   size_t get_chunk_size(void* ptr);
117 
118  private:
119   void* alloc_mmap(size_t align, size_t size);
120   inline void* alloc_impl(size_t align, size_t size);
121   inline page_info* get_page_info_unchecked(void* ptr);
122   inline page_info* get_page_info(void* ptr);
123   BionicSmallObjectAllocator* get_small_object_allocator(uint32_t type);
124   void initialize_allocators();
125 
126   BionicSmallObjectAllocator* allocators_;
127   uint8_t allocators_buf_[sizeof(BionicSmallObjectAllocator)*kSmallObjectAllocatorsCount];
128 };
129