CppCommon  1.0.4.1
C++ Common Library
allocator_heap.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 inline void* HeapMemoryManager::malloc(size_t size, size_t alignment)
12 {
13  assert((size > 0) && "Allocated block size must be greater than zero!");
14  assert(Memory::IsValidAlignment(alignment) && "Alignment must be valid!");
15 
16 #if defined(_WIN32) || defined(_WIN64)
17  void* result = HeapAlloc(GetProcessHeap(), 0, size);
18 #else
19  void* result = std::malloc(size);
20 #endif
21  if (result != nullptr)
22  {
23  // Update allocation statistics
24  _allocated += size;
25  ++_allocations;
26  }
27  return result;
28 }
29 
30 inline void HeapMemoryManager::free(void* ptr, size_t size)
31 {
32  assert((ptr != nullptr) && "Deallocated block must be valid!");
33 
34  if (ptr != nullptr)
35  {
36 #if defined(_WIN32) || defined(_WIN64)
37  HeapFree(GetProcessHeap(), 0, (LPVOID)ptr);
38 #else
39  std::free(ptr);
40 #endif
41 
42  // Update allocation statistics
43  _allocated -= size;
44  --_allocations;
45  }
46 }
47 
49 {
50  assert((_allocated == 0) && "Memory leak detected! Allocated memory size must be zero!");
51  assert((_allocations == 0) && "Memory leak detected! Count of active memory allocations must be zero!");
52 }
53 
54 } // namespace CppCommon
void reset()
Reset the memory manager.
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
void free(void *ptr, size_t size)
Free the previously allocated memory block.
static bool IsValidAlignment(size_t alignment) noexcept
Is the given alignment valid?
Definition: memory.inl:13
C++ Common project definitions.
Definition: token_bucket.h:15