nlib
StackHeap.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_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  StackHeap() NLIB_NOEXCEPT : cur_(NULL), heap_option_(0), start_(NULL), end_(NULL) {}
32  errno_t Init(void* start, size_t size, uint32_t heap_option) NLIB_NOEXCEPT;
33  void FreeAll() NLIB_NOEXCEPT { cur_ = start_; }
34  errno_t ResizeTop(void* p, size_t from_size, size_t to_size) NLIB_NOEXCEPT;
35  errno_t Adjust(void** start, size_t* size) NLIB_NOEXCEPT;
36 
37  void* Alloc(size_t n, size_t algn) NLIB_NOEXCEPT {
38  if (n == 0) return NULL;
39  if (algn == 0 || 0 != (algn & (algn - 1))) return NULL;
40  uint8_t* ret = this->RoundUp(cur_, algn);
41  uint8_t* new_top = ret + n;
42  if (new_top > end_) return NULL;
43  cur_ = new_top;
44  return ret;
45  }
46  void* Alloc(size_t n) NLIB_NOEXCEPT { return this->Alloc(n, 8); }
47  errno_t Free(void* p) NLIB_NOEXCEPT {
48  uint8_t* ptr = reinterpret_cast<uint8_t*>(p);
49  if (ptr >= start_ && ptr < cur_) {
50  cur_ = ptr;
51  return 0;
52  } else {
53  return EFAULT;
54  }
55  }
56  void Dump() NLIB_NOEXCEPT;
57 
58  private:
59  static uint8_t* RoundUp(uint8_t* ptr, size_t algn) NLIB_NOEXCEPT {
60  // algn is power of 2.
61  intptr_t tmp = reinterpret_cast<intptr_t>(ptr);
62  return reinterpret_cast<uint8_t*>(tmp + (-tmp & (algn - 1)));
63  }
64  void ChangeHeapRange(void* start, void* end) NLIB_NOEXCEPT {
65  start_ = reinterpret_cast<uint8_t*>(start);
66  end_ = reinterpret_cast<uint8_t*>(end);
67  }
68 
69  private:
70  uint8_t* cur_;
71 
72  uint32_t heap_option_;
73  uint8_t* start_;
74  uint8_t* end_;
75 };
76 
77 } // namespace heap
78 NLIB_NAMESPACE_END
79 
80 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
81 #undef NLIB_VIS_PUBLIC
82 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
83 #endif
84 
85 #endif // INCLUDE_NN_NLIB_HEAP_STACKHEAP_H_
Allocator that allocates memory from the top like a stack.
Definition: StackHeap.h:29
void * Alloc(size_t n) noexcept
Allocates a memory block.
Definition: StackHeap.h:46
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
void * Alloc(size_t n, size_t algn) noexcept
Allocates a memory block with a specific alignment.
Definition: StackHeap.h:37
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
errno_t Free(void *p) noexcept
Moves the location of the next memory allocation back to the address that p points to...
Definition: StackHeap.h:47
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
void FreeAll() noexcept
Frees all memory and restores the heap to its post-initialization state.
Definition: StackHeap.h:33
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37