7#include <botan/internal/mem_pool.h>
9#include <botan/mem_ops.h>
12#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
13 #include <botan/internal/os_utils.h>
92size_t choose_bucket(
size_t n) {
93 const size_t MINIMUM_ALLOCATION = 16;
94 const size_t MAXIMUM_ALLOCATION = 256;
96 if(n < MINIMUM_ALLOCATION || n > MAXIMUM_ALLOCATION) {
102 const size_t buckets[] = {
118 for(
size_t i = 0; buckets[i]; ++i) {
119 if(n <= buckets[i]) {
127inline bool ptr_in_pool(
const void* pool_ptr,
size_t poolsize,
const void* buf_ptr,
size_t bufsize) {
128 const uintptr_t pool =
reinterpret_cast<uintptr_t
>(pool_ptr);
129 const uintptr_t buf =
reinterpret_cast<uintptr_t
>(buf_ptr);
130 return (buf >= pool) && (buf + bufsize <= pool + poolsize);
135size_t find_set_bit(
T b) {
136 size_t s = 8 *
sizeof(
T) / 2;
141 const T mask = (
static_cast<T>(1) << s) - 1;
142 if((
b & mask) == 0) {
154 explicit BitMap(
size_t bits) : m_len(bits) {
155 m_bits.resize((bits + BITMASK_BITS - 1) / BITMASK_BITS);
157 m_main_mask =
static_cast<bitmask_type
>(~0);
158 m_last_mask = m_main_mask;
160 if(bits % BITMASK_BITS != 0) {
161 m_last_mask = (
static_cast<bitmask_type
>(1) << (bits % BITMASK_BITS)) - 1;
165 bool find_free(
size_t* bit);
167 void free(
size_t bit) {
169 const size_t w = bit / BITMASK_BITS;
171 const bitmask_type mask =
static_cast<bitmask_type
>(1) << (bit % BITMASK_BITS);
172 m_bits[w] = m_bits[w] & (~mask);
176 for(
auto bitset : m_bits) {
186#if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
187 using bitmask_type = uint8_t;
189 using bitmask_type = word;
192 static const size_t BITMASK_BITS =
sizeof(bitmask_type) * 8;
195 bitmask_type m_main_mask;
196 bitmask_type m_last_mask;
197 std::vector<bitmask_type> m_bits;
200bool BitMap::find_free(
size_t* bit) {
201 for(
size_t i = 0; i != m_bits.size(); ++i) {
202 const bitmask_type mask = (i == m_bits.size() - 1) ? m_last_mask : m_main_mask;
203 if((m_bits[i] & mask) != mask) {
204 const size_t free_bit = find_set_bit(~m_bits[i]);
205 const bitmask_type bmask =
static_cast<bitmask_type
>(1) << (free_bit % BITMASK_BITS);
208 *bit = BITMASK_BITS * i + free_bit;
220 Bucket(uint8_t* mem,
size_t mem_size,
size_t item_size) :
221 m_item_size(item_size),
222 m_page_size(mem_size),
224 m_bitmap(mem_size / item_size),
234 if(!m_bitmap.find_free(&offset)) {
240 BOTAN_ASSERT(offset * m_item_size < m_page_size,
"Offset is in range");
241 return m_range + m_item_size * offset;
245 if(!in_this_bucket(p)) {
253 std::memset(p, 0, m_item_size);
255 const size_t offset = (
reinterpret_cast<uintptr_t
>(p) -
reinterpret_cast<uintptr_t
>(m_range)) / m_item_size;
257 m_bitmap.free(offset);
263 bool in_this_bucket(
void* p)
const {
return ptr_in_pool(m_range, m_page_size, p, m_item_size); }
265 bool empty()
const {
return m_bitmap.empty(); }
267 uint8_t* ptr()
const {
return m_range; }
278 m_min_page_ptr = ~static_cast<uintptr_t>(0);
281 for(
auto page : pages) {
282 const uintptr_t p =
reinterpret_cast<uintptr_t
>(page);
284 m_min_page_ptr = std::min(p, m_min_page_ptr);
285 m_max_page_ptr = std::max(p, m_max_page_ptr);
288#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
291 m_free_pages.push_back(
static_cast<uint8_t*
>(page));
298 m_max_page_ptr += page_size;
303#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
304 for(
size_t i = 0; i != m_free_pages.size(); ++i) {
311 if(n > m_page_size) {
315 const size_t n_bucket = choose_bucket(n);
320 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
328 for(
auto& bucket : buckets) {
329 if(uint8_t* p = bucket.alloc()) {
337 if(!m_free_pages.empty()) {
338 uint8_t* ptr = m_free_pages[0];
339 m_free_pages.pop_front();
340#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
343 buckets.push_front(Bucket(ptr, m_page_size, n_bucket));
344 void* p = buckets[0].alloc();
356 const uintptr_t p_val =
reinterpret_cast<uintptr_t
>(p);
357 if(p_val < m_min_page_ptr || p_val > m_max_page_ptr) {
361 const size_t n_bucket = choose_bucket(len);
367 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
369 for(
size_t i = 0; i != buckets.size(); ++i) {
370 Bucket& bucket = buckets[i];
373#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
376 m_free_pages.push_back(bucket.ptr());
378 if(i != buckets.size() - 1) {
379 std::swap(buckets.back(), buckets[i]);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT(expr, assertion_made)
Memory_Pool(const std::vector< void * > &pages, size_t page_size)
bool deallocate(void *p, size_t size) noexcept
void * allocate(size_t size)
int(* final)(unsigned char *, CTX *)
void page_allow_access(void *page)
void page_prohibit_access(void *page)
secure_vector< T > lock(const std::vector< T > &in)
constexpr void clear_bytes(void *ptr, size_t bytes)