nlib
CachedHeap.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_HEAP_CACHEDHEAP_H_
4 #define INCLUDE_NN_NLIB_HEAP_CACHEDHEAP_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 
9 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
10 #undef NLIB_VIS_PUBLIC
11 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
12 #endif
13 
14 NLIB_NAMESPACE_BEGIN
15 namespace heap {
16 
17 class TlsHeapCache;
18 
20  public:
21  CachedHeap() NLIB_NOEXCEPT : m_Body(NULL) {}
22  ~CachedHeap() NLIB_NOEXCEPT { this->Finalize(); }
23  NLIB_MOVE_MEMBER_HELPER_1(CachedHeap, m_Body);
25  using std::swap;
26  swap(m_Body, rhs.m_Body);
27  }
28 
29  void* Alloc(size_t n) NLIB_NOEXCEPT; // nmalloc()
30  void* Alloc(size_t n, size_t algn) NLIB_NOEXCEPT; // nmalloc_aligned()
31  size_t GetAllocSize(const void* p) NLIB_NOEXCEPT; // nmalloc_size()
32  errno_t Free(void* p) NLIB_NOEXCEPT; // nfree()
33  errno_t FreeWithSize(void* p, size_t size) NLIB_NOEXCEPT; // nfree_size()
34  errno_t Realloc(void* ptr, size_t size, void** p) NLIB_NOEXCEPT; // nrealloc() minus
35  errno_t SetMark1(void* ptr, uint16_t mark1) NLIB_NOEXCEPT;
36  errno_t SetMark2(void* ptr, uint16_t mark2) NLIB_NOEXCEPT;
37  errno_t GetMark(const void* ptr, uint16_t* mark1, uint16_t* mark2) NLIB_NOEXCEPT;
38  void* GetObjPtr(void* raw_ptr) NLIB_NOEXCEPT;
39 
40  void ReleaseAllCache() NLIB_NOEXCEPT; // nmalloc_clear_tls()
41  void Finalize() NLIB_NOEXCEPT; // nmalloc_finalize_tls()
42  bool CheckCache() NLIB_NOEXCEPT;
43 
44  // Internal use only
45  NLIB_VIS_HIDDEN void Reset(TlsHeapCache* body) NLIB_NOEXCEPT;
46  NLIB_VIS_HIDDEN TlsHeapCache* Release() NLIB_NOEXCEPT;
47 
48  NLIB_SAFE_BOOL(CachedHeap, (m_Body != NULL))
49 
50  private:
51  TlsHeapCache* m_Body;
53 };
54 
55 } // namespace heap
56 NLIB_NAMESPACE_END
57 
58 NLIB_DEFINE_STD_SWAP(::nlib_ns::heap::CachedHeap)
59 
60 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
61 #undef NLIB_VIS_PUBLIC
62 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
63 #endif
64 
65 #endif // INCLUDE_NN_NLIB_HEAP_CACHEDHEAP_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#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:126
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:141
~CachedHeap() noexcept
Destructor. Calls Finalize.
Definition: CachedHeap.h:22
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
void swap(CachedHeap &rhs) noexcept
Swaps the contents of an object.
Definition: CachedHeap.h:24
CachedHeap() noexcept
Instantiates the object with default parameters (default constructor).
Definition: CachedHeap.h:21
Implements common features and features that are highly platform-dependent. Also refer to nlib Platfo...
A file that contains the configuration information for each development environment.
Thread-specific cache class whose use is paired with CentralHeap.
Definition: CachedHeap.h:19
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24