nlib
UnitHeap.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HEAP_UNITHEAP_H_
4 #define INCLUDE_NN_NLIB_HEAP_UNITHEAP_H_
5 
6 #include "nn/nlib/Config.h"
8 
9 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
10 #undef NLIB_VIS_PUBLIC
11 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
12 #endif
13 
14 NLIB_NAMESPACE_BEGIN
15 namespace heap {
16 
18  public:
19  typedef void (*DestructorHandler)(void* start, void* end);
22  // Do not assert by GetAllocatedCount().
23  // Allow deallote without destroy.
24  if (m_Handler) {
25  (*m_Handler)(m_Start, m_End);
26  }
27  }
28  errno_t Initialize(void* start, size_t size, DestructorHandler handler, size_t unit,
29  size_t algn, uint32_t heap_option) NLIB_NOEXCEPT;
30  void* Alloc() NLIB_NOEXCEPT;
31  errno_t Free(void* p) NLIB_NOEXCEPT;
32  size_t GetUnitSize() const NLIB_NOEXCEPT { return m_UnitSize; }
33  size_t GetUnitCount() const NLIB_NOEXCEPT { return m_UnitCount; }
34  size_t GetAllocatedCount() const NLIB_NOEXCEPT { return m_AllocatedCount; }
35  void Dump() NLIB_NOEXCEPT;
36 
37  private:
38  struct Node {
39  Node* next;
40  };
41 
42  size_t m_UnitSize;
43  size_t m_BaseUnitCount;
44  Node* m_FreeList;
45  size_t m_UnitCount;
46  size_t m_AllocatedCount;
47 
48  uint32_t m_HeapOption;
49  uint8_t* m_Start;
50  uint8_t* m_End;
51  DestructorHandler m_Handler;
52 };
53 
54 } // namespace heap
55 NLIB_NAMESPACE_END
56 
57 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
58 #undef NLIB_VIS_PUBLIC
59 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
60 #endif
61 
62 #endif // INCLUDE_NN_NLIB_HEAP_UNITHEAP_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Heap for allocating and freeing fixed-size regions of memory. This is a so-called "pool-allocator...
Definition: UnitHeap.h:17
size_t GetUnitCount() const noexcept
Returns the number of regions that can be allocated from this heap.
Definition: UnitHeap.h:33
size_t GetAllocatedCount() const noexcept
Returns the number of regions that are currently allocated from this heap.
Definition: UnitHeap.h:34
A file that contains the configuration information for each development environment.
Used when aligned memory needs to be obtained.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24