nlib
gameheap.h
1 
2 #pragma once
3 #ifndef SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
4 #define SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 class HeapMgr;
9 class HeapHandle {
10 public:
11  static HeapHandle createHeap(size_t size) NLIB_NOEXCEPT;
12  static HeapHandle createHeap(void* mem, size_t size) NLIB_NOEXCEPT;
13  static void destroyHeap(HeapHandle h) NLIB_NOEXCEPT;
14 
15  HeapHandle() NLIB_NOEXCEPT : m_Idx(0) {}
16  HeapHandle(const HeapHandle& h) NLIB_NOEXCEPT : m_Idx(h.m_Idx) {}
17  HeapHandle& operator=(const HeapHandle& rhs) NLIB_NOEXCEPT {
18  m_Idx = rhs.m_Idx;
19  return *this;
20  }
21 
22  void* alloc(size_t size, size_t alignment = 8) NLIB_NOEXCEPT;
23  void free(void* ptr) NLIB_NOEXCEPT;
24  // void* realloc(void* ptr, size_t new_size) NLIB_NOEXCEPT;
25  void freeAll() NLIB_NOEXCEPT;
26  // const void* getStartAddress() const NLIB_NOEXCEPT;
27  // const void* getEndAddress() const NLIB_NOEXCEPT;
28  // size_t getSize() const NLIB_NOEXCEPT;
29  // size_t getFreeSize() const NLIB_NOEXCEPT;
30  // size_t getMaxAllocatableSize(size_t alignment = 8) NLIB_NOEXCEPT;
31  // bool isInclude(const void* ptr) const NLIB_NOEXCEPT;
32  // void dump() const NLIB_NOEXCEPT;
33  bool isValid() NLIB_NOEXCEPT;
34  bool isClean() NLIB_NOEXCEPT;
35 
36 private:
37  explicit HeapHandle(int h) NLIB_NOEXCEPT : m_Idx(h) {}
38 
39  int32_t m_Idx;
40  friend class HeapMgr;
41  friend HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
42  friend void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
43  friend bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
44  friend bool operator!=(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
45 };
46 
47 inline bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT {
48  return lhs.m_Idx == rhs.m_Idx;
49 }
50 
51 inline bool operator!=(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT {
52  return lhs.m_Idx != rhs.m_Idx;
53 }
54 
55 // You can register default heap handle per a thread.
56 // To make a different thread has a different heap handle,
57 // you can avoid the overhead of locking in memory allocation.
58 // Also, freeing memory from different thread is possible,
59 // and you can move the ownership of memory from a thread to another thread.
60 HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
61 void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
62 
63 class IDisposer {
64 public:
65  IDisposer(HeapHandle h) NLIB_NOEXCEPT : m_Handle(h) {}
66  void Destroy() NLIB_NOEXCEPT {
67  nlib_printf("IDisposer::Destroy() begin\n");
68  this->~IDisposer();
69  nlib_printf("IDisposer::Destroy() end\n");
70  }
71  HeapHandle GetHeapHandle() const NLIB_NOEXCEPT { return m_Handle; }
72 
73 protected:
74  virtual ~IDisposer() NLIB_NOEXCEPT {
75  // Multpiple inheritance is not available.
76  m_Handle.free(this);
77  }
78 
79 private:
80  HeapHandle m_Handle;
81 };
82 
83 
84 #endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
Definition: NMalloc.h:106
int nlib_printf(const char *fmt,...)
The substitute for the printf function.
A file that contains the configuration information for each development environment.