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 : body_(NULL) {}
22  ~CachedHeap() NLIB_NOEXCEPT { this->Finalize(); }
23  NLIB_MOVE_MEMBER_HELPER_1(CachedHeap, body_);
25  using std::swap;
26  swap(body_, rhs.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 
36  void ReleaseAllCache() NLIB_NOEXCEPT; // nmalloc_query(NMALLOC_QUERY_CLEAR_CACHE);
37  void Finalize() NLIB_NOEXCEPT; // nmalloc_query(NMALLOC_QUERY_FINALIZE_CACHE);
38  bool CheckCache() NLIB_NOEXCEPT;
39  errno_t QueryV(int query, va_list ap) NLIB_NOEXCEPT;
40  errno_t Query(int query, ...) NLIB_NOEXCEPT;
41 
42  // Internal use only
43  NLIB_VIS_HIDDEN void Reset(TlsHeapCache* body) NLIB_NOEXCEPT;
44  NLIB_VIS_HIDDEN TlsHeapCache* Release() NLIB_NOEXCEPT;
45 
46  NLIB_SAFE_BOOL(CachedHeap, (body_ != NULL))
47 
48  private:
49  TlsHeapCache* body_;
51 };
52 
53 } // namespace heap
54 NLIB_NAMESPACE_END
55 
56 NLIB_DEFINE_STD_SWAP(::nlib_ns::heap::CachedHeap)
57 
58 #if defined(_MSC_VER) && defined(nx_heap_EXPORTS)
59 #undef NLIB_VIS_PUBLIC
60 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
61 #endif
62 
63 #endif // INCLUDE_NN_NLIB_HEAP_CACHEDHEAP_H_
#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:145
#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:160
~CachedHeap() noexcept
Destructor. Calls Finalize.
Definition: CachedHeap.h:22
void swap(CachedHeap &rhs) noexcept
Swaps the contents of an object.
Definition: CachedHeap.h:24
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
CachedHeap() noexcept
Instantiates the object with default parameters (default constructor).
Definition: CachedHeap.h:21
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
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_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24