nlib
gameheap.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
17 #define SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 class HeapMgr;
22 class HeapHandle {
23 public:
24  static HeapHandle CreateHeap(size_t size) NLIB_NOEXCEPT;
25  static HeapHandle CreateHeap(void* mem, size_t size) NLIB_NOEXCEPT;
26  static void DestroyHeap(HeapHandle h) NLIB_NOEXCEPT;
27 
28  HeapHandle() NLIB_NOEXCEPT : idx_(0) {}
29  HeapHandle(const HeapHandle& h) NLIB_NOEXCEPT : idx_(h.idx_) {}
30  HeapHandle& operator=(const HeapHandle& rhs) NLIB_NOEXCEPT {
31  idx_ = rhs.idx_;
32  return *this;
33  }
34 
35  void* Alloc(size_t size, size_t alignment = 8) NLIB_NOEXCEPT;
36  void Free(void* ptr) NLIB_NOEXCEPT;
37  // void* realloc(void* ptr, size_t new_size) NLIB_NOEXCEPT;
38  void FreeAll() NLIB_NOEXCEPT;
39  // const void* getStartAddress() const NLIB_NOEXCEPT;
40  // const void* getEndAddress() const NLIB_NOEXCEPT;
41  // size_t getSize() const NLIB_NOEXCEPT;
42  // size_t getFreeSize() const NLIB_NOEXCEPT;
43  // size_t getMaxAllocatableSize(size_t alignment = 8) NLIB_NOEXCEPT;
44  // bool isInclude(const void* ptr) const NLIB_NOEXCEPT;
45  // void dump() const NLIB_NOEXCEPT;
46  bool IsValid() NLIB_NOEXCEPT;
47  bool IsClean() NLIB_NOEXCEPT;
48 
49 private:
50  explicit HeapHandle(int h) NLIB_NOEXCEPT : idx_(h) {}
51 
52  int32_t idx_;
53  friend class HeapMgr;
54  friend HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
55  friend void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
56  friend bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
57  friend bool operator!=(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT;
58 };
59 
60 inline bool operator==(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT {
61  return lhs.idx_ == rhs.idx_;
62 }
63 
64 inline bool operator!=(const HeapHandle& lhs, const HeapHandle& rhs) NLIB_NOEXCEPT {
65  return lhs.idx_ != rhs.idx_;
66 }
67 
68 // You can register default heap handle per a thread.
69 // To make a different thread has a different heap handle,
70 // you can avoid the overhead of locking in memory allocation.
71 // Also, freeing memory from different thread is possible,
72 // and you can move the ownership of memory from a thread to another thread.
73 HeapHandle GetThreadDefaultHeapHandle() NLIB_NOEXCEPT;
74 void SetThreadDefaultHeapHandle(HeapHandle h) NLIB_NOEXCEPT;
75 
76 class IDisposer {
77 public:
78  IDisposer(HeapHandle h) NLIB_NOEXCEPT : handle_(h) {}
79  void Destroy() NLIB_NOEXCEPT {
80  nlib_printf("IDisposer::Destroy() begin\n");
81  this->~IDisposer();
82  nlib_printf("IDisposer::Destroy() end\n");
83  }
84  HeapHandle GetHeapHandle() const NLIB_NOEXCEPT { return handle_; }
85 
86 protected:
87  virtual ~IDisposer() NLIB_NOEXCEPT {
88  // Multpiple inheritance is not available.
89  handle_.Free(this);
90  }
91 
92 private:
93  HeapHandle handle_;
94 };
95 
96 
97 #endif // SAMPLES_SOURCES_HEAP_GAMEHEAP_GAMEHEAP_H_
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
Definition: NMalloc.h:154
int nlib_printf(const char *fmt,...)
The substitute for the printf function.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
A file that contains the configuration information for each development environment.