nlib
FrameHeap.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 INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
17 #define INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
22 #undef NLIB_VIS_PUBLIC
23 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
24 #endif
25 
26 NLIB_NAMESPACE_BEGIN
27 namespace heap {
28 
30  public:
31  typedef void (*DestructorHandler)(void* start, void* end) NLIB_NOEXCEPT_FUNCPTR;
32  enum AllocOption {
33  kAllocOptionAllocHead = 0x00000000,
34  kAllocOptionAllocTail = 0x00000001
35  };
36 
37  struct State {
38  size_t _[2];
39  bool operator==(const State& rhs) NLIB_NOEXCEPT {
40  return _[0] == rhs._[0] && _[1] == rhs._[1];
41  }
42  bool operator!=(const State& rhs) NLIB_NOEXCEPT {
43  return _[0] != rhs._[0] || _[1] != rhs._[1];
44  }
45  };
46  FrameHeap() NLIB_NOEXCEPT : heap_option_(0), start_(NULL), end_(NULL), handler_(NULL) {
47  cur_.interlocked._[0] = cur_.interlocked._[1] = 0;
48  }
50  if (handler_) {
51  (*handler_)(start_, end_);
52  }
53  }
54  errno_t Init(void* start, size_t size, DestructorHandler handler,
55  uint32_t heap_option) NLIB_NOEXCEPT;
56  void FreeAll() NLIB_NOEXCEPT {
57  HeapStatus to;
58  to.tb.topidx = 0;
59  to.tb.botidx = end_ - start_;
60  cur_ = to;
61  }
62  void FreeTop() NLIB_NOEXCEPT { cur_.tb.topidx = 0; }
63  void FreeBot() NLIB_NOEXCEPT {
64  size_t bot = end_ - start_;
65  cur_.tb.botidx = bot;
66  }
67  errno_t ResizeTop(void* p, size_t from_size, size_t to_size) NLIB_NOEXCEPT;
68  errno_t Adjust(void** start, size_t* size) NLIB_NOEXCEPT;
69 
70  State SaveState() NLIB_NOEXCEPT { return cur_.interlocked; }
71  void RestoreState(State state) NLIB_NOEXCEPT { cur_.interlocked = state; }
72  void* Alloc(size_t n) NLIB_NOEXCEPT { return this->Alloc(n, 8, 0); }
73  void* Alloc(size_t n, size_t algn) NLIB_NOEXCEPT { return this->Alloc(n, algn, 0); }
74  void* Alloc(size_t n, size_t algn, uint32_t alloc_option) NLIB_NOEXCEPT;
75  void Dump() NLIB_NOEXCEPT;
76 
77  private:
78  union HeapStatus {
79  State interlocked;
80  struct topbot_ {
81  size_t topidx;
82  size_t botidx;
83  } tb;
84  };
85  static uint8_t* RoundUp(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
86  // algn is power of 2.
87  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
88  return reinterpret_cast<uint8_t*>(tmp + (-tmp & (algn - 1)));
89  }
90  static uint8_t* RoundDown(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
91  // algn is power of 2.
92  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
93  return reinterpret_cast<uint8_t*>(tmp & (~algn + 1));
94  }
95  void ChangeHeapRange(void* start, void* end) NLIB_NOEXCEPT {
96  start_ = reinterpret_cast<uint8_t*>(start);
97  end_ = reinterpret_cast<uint8_t*>(end);
98  }
99 
100  private:
101  HeapStatus cur_;
102  uint32_t heap_option_;
103  uint8_t* start_;
104  uint8_t* end_;
105  DestructorHandler handler_;
106 };
107 
108 } // namespace heap
109 NLIB_NAMESPACE_END
110 
111 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
112 #undef NLIB_VIS_PUBLIC
113 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
114 #endif
115 
116 #endif // INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
bool operator==(const State &rhs) noexcept
ヒープの状態を比較して、等価ならばtrueを返します。
Definition: FrameHeap.h:39
void FreeAll() noexcept
メモリを全て解放し、ヒープを初期化直後の状態に戻します。
Definition: FrameHeap.h:56
void FreeBot() noexcept
ヒープの下部から確保したメモリを全て解放します。
Definition: FrameHeap.h:63
void FreeTop() noexcept
ヒープの上部から確保したメモリを全て解放します。
Definition: FrameHeap.h:62
State SaveState() noexcept
現在のヒープの状態をセーブします。
Definition: FrameHeap.h:70
ヒープの状態を記録しています。
Definition: FrameHeap.h:37
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
bool operator!=(const State &rhs) noexcept
ヒープの状態を比較して、等価でなければtrueを返します。
Definition: FrameHeap.h:42
ヒープの両端からメモリを確保することができます。
Definition: FrameHeap.h:29
void RestoreState(State state) noexcept
セーブしたヒープの状態に戻します。
Definition: FrameHeap.h:71
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
開発環境別の設定が書かれるファイルです。
void * Alloc(size_t n, size_t algn) noexcept
アライメントを指定してメモリブロックを確保します。
Definition: FrameHeap.h:73
AllocOption
アロケートをヒープのどちらがわから行うかを指定します。
Definition: FrameHeap.h:32
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
void * Alloc(size_t n) noexcept
メモリブロックを確保します。
Definition: FrameHeap.h:72
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37