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"
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);
21  // Do not assert by GetAllocatedCount().
22  // Allow deallote without destroy.
23  if (handler_) {
24  (*handler_)(start_, end_);
25  }
26  }
27  errno_t Init(void* start, size_t size, DestructorHandler handler, size_t unit,
28  size_t algn, uint32_t heap_option) NLIB_NOEXCEPT;
29  void* Alloc() NLIB_NOEXCEPT;
30  errno_t Free(void* p) NLIB_NOEXCEPT;
31  size_t GetUnitSize() const NLIB_NOEXCEPT { return unit_size_; }
32  size_t GetUnitCount() const NLIB_NOEXCEPT { return unit_count_; }
33  size_t GetAllocatedCount() const NLIB_NOEXCEPT { return allocated_count_; }
34  void Dump() NLIB_NOEXCEPT;
35 
36  private:
37  struct Node {
38  Node* next;
39  };
40 
41  size_t unit_size_;
42  size_t baseunit_count_;
43  Node* free_list_;
44  size_t unit_count_;
45  size_t allocated_count_;
46 
47  uint32_t heap_option_;
48  uint8_t* start_;
49  uint8_t* end_;
50  DestructorHandler handler_;
51 };
52 
53 } // namespace heap
54 NLIB_NAMESPACE_END
55 
56 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
57 #undef NLIB_VIS_PUBLIC
58 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
59 #endif
60 
61 #endif // INCLUDE_NN_NLIB_HEAP_UNITHEAP_H_
size_t GetUnitCount() const noexcept
Returns the number of regions that can be allocated from this heap.
Definition: UnitHeap.h:32
Heap for allocating and freeing fixed-size regions of memory. This is a so-called "pool-allocator...
Definition: UnitHeap.h:16
size_t GetAllocatedCount() const noexcept
Returns the number of regions that are currently allocated from this heap.
Definition: UnitHeap.h:33
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
size_t GetUnitSize() const noexcept
Returns the size of each region that to be allocated from this heap.
Definition: UnitHeap.h:31
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24