nlib
FrameHeap.h
Go to the documentation of this file.
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_(nullptr), end_(nullptr), handler_(nullptr) {
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_ = static_cast<uint8_t*>(start);
97  end_ = static_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_;
107 };
108 
109 } // namespace heap
110 NLIB_NAMESPACE_END
111 
112 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
113 #undef NLIB_VIS_PUBLIC
114 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
115 #endif
116 
117 #endif // INCLUDE_NN_NLIB_HEAP_FRAMEHEAP_H_
bool operator==(const State &rhs) noexcept
Compares heap states and returns true if they are equal.
Definition: FrameHeap.h:39
void FreeAll() noexcept
Frees all memory and restores the heap to its post-initialization state.
Definition: FrameHeap.h:56
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:179
void FreeBot() noexcept
Frees all memory regions that were allocated from bottom of the heap.
Definition: FrameHeap.h:63
void FreeTop() noexcept
Frees all memory regions that were allocated from top of the heap.
Definition: FrameHeap.h:62
State SaveState() noexcept
Saves the current heap state.
Definition: FrameHeap.h:70
Lists the state of the heap.
Definition: FrameHeap.h:37
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
bool operator!=(const State &rhs) noexcept
Compares heap states and returns true if they are unequal.
Definition: FrameHeap.h:42
Allows memory to be allocated from both sides of the heap.
Definition: FrameHeap.h:29
void RestoreState(State state) noexcept
Restores a saved heap state.
Definition: FrameHeap.h:71
#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.
void * Alloc(size_t n, size_t algn) noexcept
Allocates a memory block with a specific alignment.
Definition: FrameHeap.h:73
AllocOption
Specifies which side of the heap to allocate from.
Definition: FrameHeap.h:32
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
void * Alloc(size_t n) noexcept
Allocates a memory block.
Definition: FrameHeap.h:72
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37