nlib
StackHeap.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_STACKHEAP_H_
17 #define INCLUDE_NN_NLIB_HEAP_STACKHEAP_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  NLIB_CEXPR StackHeap() NLIB_NOEXCEPT : cur_(nullptr), heap_option_(0),
32  start_(nullptr), end_(nullptr) {}
33  errno_t Init(void* start, size_t size, uint32_t heap_option) NLIB_NOEXCEPT;
34  void FreeAll() NLIB_NOEXCEPT { cur_ = start_; }
35  errno_t ResizeTop(void* p, size_t from_size, size_t to_size) NLIB_NOEXCEPT;
36  errno_t Adjust(void** start, size_t* size) NLIB_NOEXCEPT;
37 
38  void* Alloc(size_t n, size_t algn) NLIB_NOEXCEPT {
39  if (n == 0) return nullptr;
40  if (algn == 0 || 0 != (algn & (algn - 1))) return nullptr;
41  uint8_t* ret = this->RoundUp(cur_, algn);
42  uint8_t* new_top = ret + n;
43  if (new_top > end_) return nullptr;
44  cur_ = new_top;
45  return ret;
46  }
47  void* Alloc(size_t n) NLIB_NOEXCEPT { return this->Alloc(n, 8); }
48  errno_t Free(void* p) NLIB_NOEXCEPT {
49  uint8_t* ptr = static_cast<uint8_t*>(p);
50  if (ptr >= start_ && ptr < cur_) {
51  cur_ = ptr;
52  return 0;
53  } else {
54  return EFAULT;
55  }
56  }
57  void Dump() NLIB_NOEXCEPT;
58 
59  private:
60  static uint8_t* RoundUp(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
61  // algn is power of 2.
62  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
63  return reinterpret_cast<uint8_t*>(tmp + (-tmp & (algn - 1)));
64  }
65  void ChangeHeapRange(void* start, void* end) NLIB_NOEXCEPT {
66  start_ = static_cast<uint8_t*>(start);
67  end_ = static_cast<uint8_t*>(end);
68  }
69 
70  private:
71  uint8_t* cur_;
72 
73  uint32_t heap_option_;
74  uint8_t* start_;
75  uint8_t* end_;
77 };
78 
79 } // namespace heap
80 NLIB_NAMESPACE_END
81 
82 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
83 #undef NLIB_VIS_PUBLIC
84 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
85 #endif
86 
87 #endif // INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_
スタックに積み上げるようにメモリを確保していくアロケータです。
Definition: StackHeap.h:29
void * Alloc(size_t n) noexcept
メモリブロックを確保します。
Definition: StackHeap.h:47
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
void * Alloc(size_t n, size_t algn) noexcept
アライメントを指定してメモリブロックを確保します。
Definition: StackHeap.h:38
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
errno_t Free(void *p) noexcept
次のメモリ確保位置をp の位置まで戻します。
Definition: StackHeap.h:48
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:107
開発環境別の設定が書かれるファイルです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
void FreeAll() noexcept
メモリを全て解放し、ヒープを初期化直後の状態に戻します。
Definition: StackHeap.h:34
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37