CommonLibVR
NiTCollection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/M/MemoryManager.h"
4 
5 namespace RE
6 {
7  void* NiMalloc(std::size_t a_sizeInBytes);
8  void* NiAlignedMalloc(std::size_t a_sizeInBytes, std::size_t a_alignment);
9  void* NiRealloc(void* a_mem, std::size_t a_sizeInBytes);
10  void* NiAlignedRealloc(void* a_mem, std::size_t a_sizeInBytes, std::size_t a_alignment);
11  void NiFree(void* a_mem);
12  void NiAlignedFree(void* a_mem);
13  constexpr bool NiTrackAlloc([[maybe_unused]] void* a_mem, [[maybe_unused]] std::size_t a_sizeInBytes) { return false; }
14  constexpr bool NiTrackFree([[maybe_unused]] void* a_mem) { return false; }
15 
16  // calloc
17  template <class T>
18  T* NiAlloc(std::size_t a_count)
19  {
20  return static_cast<T*>(NiMalloc(sizeof(T) * a_count));
21  }
22 
23  // aligned calloc
24  template <class T>
25  T* NiAlignedAlloc(std::size_t a_count, std::size_t a_alignment)
26  {
27  return static_cast<T*>(NiAlignedMalloc(sizeof(T) * a_count, a_alignment));
28  }
29 
30  template <class T>
32  {
33  public:
34  inline static T* Allocate(std::size_t a_numElements)
35  {
36  return static_cast<T*>(NiMalloc(sizeof(T) * a_numElements));
37  };
38 
39  inline static void Deallocate(T* a_array)
40  {
41  NiFree(a_array);
42  };
43  };
44 
45  template <class T>
47  {
48  public:
49  inline static T* Allocate(std::size_t a_numElements)
50  {
51  auto mem = malloc(sizeof(std::size_t) + sizeof(T) * a_numElements);
52  auto head = static_cast<std::size_t*>(mem);
53  *head = a_numElements;
54  mem = head + 1;
55  return static_cast<T*>(mem);
56  };
57 
58  inline static void Deallocate(T* a_array)
59  {
60  if (a_array) {
61  auto head = stl::adjust_pointer<std::size_t>(a_array, -stl::ssizeof_v<std::uintptr_t>);
62  for (std::size_t i = 0; i < *head; ++i) {
63  a_array[i].~T();
64  }
65  free(head);
66  }
67  };
68  };
69 }
Definition: NiTCollection.h:32
static T * Allocate(std::size_t a_numElements)
Definition: NiTCollection.h:34
static void Deallocate(T *a_array)
Definition: NiTCollection.h:39
Definition: NiTCollection.h:47
static void Deallocate(T *a_array)
Definition: NiTCollection.h:58
static T * Allocate(std::size_t a_numElements)
Definition: NiTCollection.h:49
Definition: AbsorbEffect.h:6
void * NiMalloc(std::size_t a_sizeInBytes)
void * NiAlignedRealloc(void *a_mem, std::size_t a_sizeInBytes, std::size_t a_alignment)
void NiFree(void *a_mem)
void NiAlignedFree(void *a_mem)
void * NiRealloc(void *a_mem, std::size_t a_sizeInBytes)
T * NiAlloc(std::size_t a_count)
Definition: NiTCollection.h:18
constexpr bool NiTrackFree([[maybe_unused]] void *a_mem)
Definition: NiTCollection.h:14
void * NiAlignedMalloc(std::size_t a_sizeInBytes, std::size_t a_alignment)
void * malloc(std::size_t a_size)
Definition: MemoryManager.h:98
T * NiAlignedAlloc(std::size_t a_count, std::size_t a_alignment)
Definition: NiTCollection.h:25
void free(void *a_ptr)
Definition: MemoryManager.h:187
constexpr bool NiTrackAlloc([[maybe_unused]] void *a_mem, [[maybe_unused]] std::size_t a_sizeInBytes)
Definition: NiTCollection.h:13