nlib
FrameHeap.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
4 #define INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
9 #undef NLIB_VIS_PUBLIC
10 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
11 #endif
12 
13 NLIB_NAMESPACE_BEGIN
14 namespace heap {
15 
17  public:
18  typedef void (*DestructorHandler)(void* start, void* end);
19  enum AllocOption {
20  ALLOCOPTION_ALLOCHEAD = 0x00000000,
21  ALLOCOPTION_ALLOCTAIL = 0x00000001
22  };
23 
24  struct State {
25  size_t _[2];
26  bool operator==(const State& rhs) NLIB_NOEXCEPT {
27  return _[0] == rhs._[0] && _[1] == rhs._[1];
28  }
29  bool operator!=(const State& rhs) NLIB_NOEXCEPT {
30  return _[0] != rhs._[0] || _[1] != rhs._[1];
31  }
32  };
33  FrameHeap() NLIB_NOEXCEPT : m_HeapOption(0), m_Start(NULL), m_End(NULL), m_Handler(NULL) {
34  m_Cur.interlocked._[0] = m_Cur.interlocked._[1] = 0;
35  }
36  ~FrameHeap() NLIB_NOEXCEPT {
37  if (m_Handler) {
38  (*m_Handler)(m_Start, m_End);
39  }
40  }
41  errno_t Initialize(void* start, size_t size, DestructorHandler handler,
42  uint32_t heap_option) NLIB_NOEXCEPT;
43  void FreeAll() NLIB_NOEXCEPT {
44  HeapStatus to;
45  to.tb.topidx = 0;
46  to.tb.botidx = m_End - m_Start;
47  m_Cur = to;
48  }
49  void FreeTop() NLIB_NOEXCEPT { m_Cur.tb.topidx = 0; }
50  void FreeBot() NLIB_NOEXCEPT {
51  size_t bot = m_End - m_Start;
52  m_Cur.tb.botidx = bot;
53  }
54  errno_t ResizeTop(void* p, size_t from_size, size_t to_size) NLIB_NOEXCEPT;
55  errno_t Adjust(void** start, size_t* size) NLIB_NOEXCEPT;
56 
57  State SaveState() NLIB_NOEXCEPT { return m_Cur.interlocked; }
58  void RestoreState(State state) NLIB_NOEXCEPT { m_Cur.interlocked = state; }
59  void* Alloc(size_t n) NLIB_NOEXCEPT { return this->Alloc(n, 8, 0); }
60  void* Alloc(size_t n, size_t algn) NLIB_NOEXCEPT { return this->Alloc(n, algn, 0); }
61  void* Alloc(size_t n, size_t algn, uint32_t alloc_option) NLIB_NOEXCEPT;
62  void Dump() NLIB_NOEXCEPT;
63 
64  private:
65  union HeapStatus {
66  State interlocked;
67  struct topbot_ {
68  size_t topidx;
69  size_t botidx;
70  } tb;
71  };
72  static uint8_t* RoundUp(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
73  // algn is power of 2.
74  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
75  return reinterpret_cast<uint8_t*>(tmp + (-tmp & (algn - 1)));
76  }
77  static uint8_t* RoundDown(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
78  // algn is power of 2.
79  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
80  return reinterpret_cast<uint8_t*>(tmp & (~algn + 1));
81  }
82  void ChangeHeapRange(void* start, void* end) NLIB_NOEXCEPT {
83  m_Start = reinterpret_cast<uint8_t*>(start);
84  m_End = reinterpret_cast<uint8_t*>(end);
85  }
86 
87  private:
88  HeapStatus m_Cur;
89 
90  uint32_t m_HeapOption;
91  uint8_t* m_Start;
92  uint8_t* m_End;
93  DestructorHandler m_Handler;
94 };
95 
96 } // namespace heap
97 NLIB_NAMESPACE_END
98 
99 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
100 #undef NLIB_VIS_PUBLIC
101 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
102 #endif
103 
104 #endif // INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
bool operator==(const State &rhs) noexcept
Compares heap states and returns true if they are equal.
Definition: FrameHeap.h:26
void FreeAll() noexcept
Frees all memory and restores the heap to its post-initialization state.
Definition: FrameHeap.h:43
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
void FreeBot() noexcept
Frees all memory regions that were allocated from bottom of the heap.
Definition: FrameHeap.h:50
void FreeTop() noexcept
Frees all memory regions that were allocated from top of the heap.
Definition: FrameHeap.h:49
State SaveState() noexcept
Saves the current heap state.
Definition: FrameHeap.h:57
Lists the state of the heap.
Definition: FrameHeap.h:24
bool operator!=(const State &rhs) noexcept
Compares heap states and returns true if they are unequal.
Definition: FrameHeap.h:29
Allows memory to be allocated from both sides of the heap.
Definition: FrameHeap.h:16
void RestoreState(State state) noexcept
Restores a saved heap state.
Definition: FrameHeap.h:58
A file that contains the configuration information for each development environment.
void * Alloc(size_t n, size_t algn) noexcept
Allocates a memory block with a specific alignment.
Definition: FrameHeap.h:60
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
AllocOption
Specifies which side of the heap to allocate from.
Definition: FrameHeap.h:19
void * Alloc(size_t n) noexcept
Allocates a memory block.
Definition: FrameHeap.h:59
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24