blob: beffc52dbacf30990509228e3d8e5926c3b42bba [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
29#ifndef __LINKER_ALLOCATOR_H
30#define __LINKER_ALLOCATOR_H
31
32#include <stdlib.h>
33#include <sys/cdefs.h>
34#include <sys/mman.h>
35#include <stddef.h>
36#include <unistd.h>
37
38#include <vector>
39
40#include "private/bionic_prctl.h"
41#include "private/libc_logging.h"
42
43const uint32_t kSmallObjectMaxSizeLog2 = 10;
44const uint32_t kSmallObjectMinSizeLog2 = 4;
45const uint32_t kSmallObjectAllocatorsCount = kSmallObjectMaxSizeLog2 - kSmallObjectMinSizeLog2 + 1;
46
47class LinkerSmallObjectAllocator;
48
49// This structure is placed at the beginning of each addressable page
50// and has all information we need to find the corresponding memory allocator.
51struct page_info {
52 char signature[4];
53 uint32_t type;
54 union {
55 // we use allocated_size for large objects allocator
56 size_t allocated_size;
57 // and allocator_addr for small ones.
58 LinkerSmallObjectAllocator* allocator_addr;
59 };
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -080060} __attribute__((aligned(16)));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070061
62struct small_object_page_record {
63 void* page_addr;
64 size_t free_blocks_cnt;
65 size_t allocated_blocks_cnt;
66};
67
68// for lower_bound...
69bool operator<(const small_object_page_record& one, const small_object_page_record& two);
70
71struct small_object_block_record {
72 small_object_block_record* next;
73 size_t free_blocks_cnt;
74};
75
76// This is implementation for std::vector allocator
77template <typename T>
78class linker_vector_allocator {
79 public:
80 typedef T value_type;
81 typedef T* pointer;
82 typedef const T* const_pointer;
83 typedef T& reference;
84 typedef const T& const_reference;
85 typedef size_t size_type;
86 typedef ptrdiff_t difference_type;
87
88 T* allocate(size_t n, const T* hint = nullptr) {
89 size_t size = n * sizeof(T);
90 void* ptr = mmap(const_cast<T*>(hint), size,
91 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
92 if (ptr == MAP_FAILED) {
93 // Spec says we need to throw std::bad_alloc here but because our
94 // code does not support exception handling anyways - we are going to abort.
95 __libc_fatal("mmap failed");
96 }
97
98 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "linker_alloc_vector");
99
100 return reinterpret_cast<T*>(ptr);
101 }
102
103 void deallocate(T* ptr, size_t n) {
104 munmap(ptr, n * sizeof(T));
105 }
106};
107
108typedef
109 std::vector<small_object_page_record, linker_vector_allocator<small_object_page_record>>
110 linker_vector_t;
111
112
113class LinkerSmallObjectAllocator {
114 public:
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700115 LinkerSmallObjectAllocator(uint32_t type, size_t block_size);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700116 void* alloc();
117 void free(void* ptr);
118
119 size_t get_block_size() const { return block_size_; }
120 private:
121 void alloc_page();
122 void free_page(linker_vector_t::iterator page_record);
123 linker_vector_t::iterator find_page_record(void* ptr);
124 void create_page_record(void* page_addr, size_t free_blocks_cnt);
125
126 uint32_t type_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700127 size_t block_size_;
128
129 size_t free_pages_cnt_;
130 small_object_block_record* free_blocks_list_;
131
132 // sorted vector of page records
133 linker_vector_t page_records_;
134};
135
136class LinkerMemoryAllocator {
137 public:
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700138 constexpr LinkerMemoryAllocator() : allocators_(nullptr), allocators_buf_() {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700139 void* alloc(size_t size);
140
141 // Note that this implementation of realloc never shrinks allocation
142 void* realloc(void* ptr, size_t size);
143 void free(void* ptr);
144 private:
145 void* alloc_mmap(size_t size);
146 page_info* get_page_info(void* ptr);
147 LinkerSmallObjectAllocator* get_small_object_allocator(uint32_t type);
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700148 void initialize_allocators();
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700149
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700150 LinkerSmallObjectAllocator* allocators_;
151 uint8_t allocators_buf_[sizeof(LinkerSmallObjectAllocator)*kSmallObjectAllocatorsCount];
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700152};
153
154
155#endif /* __LINKER_ALLOCATOR_H */