aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/mem_pool/mem_pool.h
blob: 4189638648130d406d4f57331d5812b1871c6065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_MEM_POOL_H_
#define BOTAN_MEM_POOL_H_

#include <botan/types.h>
#include <botan/mutex.h>
#include <vector>

namespace Botan {

class Memory_Pool final
   {
   public:
      /**
      * Initialize a memory pool. The memory is not owned by *this,
      * it must be freed by the caller.
      * @param pool the pool
      * @param pool_size size of pool
      * @param page_size some nominal page size (does not need to match
      *        the system page size)
      * @param min_allocation return null for allocs for smaller amounts
      * @param max_allocation return null for allocs of larger amounts
      * @param align_bit align all returned memory to (1<<align_bit) bytes
      */
      Memory_Pool(uint8_t* pool,
                  size_t pool_size,
                  size_t page_size,
                  size_t min_allocation,
                  size_t max_allocation,
                  uint8_t align_bit);

      void* allocate(size_t size);

      bool deallocate(void* p, size_t size) BOTAN_NOEXCEPT;

      Memory_Pool(const Memory_Pool&) = delete;

      Memory_Pool& operator=(const Memory_Pool&) = delete;

   private:
      const size_t m_page_size = 0;
      const size_t m_min_alloc = 0;
      const size_t m_max_alloc = 0;
      const uint8_t m_align_bit = 0;

      mutex_type m_mutex;

      std::vector<std::pair<size_t, size_t>> m_freelist;
      uint8_t* m_pool = nullptr;
      size_t m_pool_size = 0;
   };

}

#endif