Nebula
Loading...
Searching...
No Matches
posixheap.h
Go to the documentation of this file.
1#pragma once
2#ifndef POSIX_POSIXHEAP_H
3#define POSIX_POSIXHEAP_H
4//------------------------------------------------------------------------------
15#include "core/types.h"
18#include "util/array.h"
19#include "util/list.h"
20
21//------------------------------------------------------------------------------
22namespace Posix
23{
25{
26public:
28 static void Setup();
30 PosixHeap(const char* name);
32 ~PosixHeap();
34 const char* GetName() const;
36 void* Alloc(size_t size);
38 void* Realloc(void* ptr, size_t newSize);
40 void Free(void* ptr);
41
42 #if NEBULA_MEMORY_STATS
44 struct Stats
45 {
46 const char* name;
47 int allocCount;
48 int allocSize;
49 };
51 static Util::Array<Stats> GetAllHeapStats();
53 static bool ValidateAllHeaps();
55 bool ValidateHeap() const;
57 int GetAllocCount() const;
59 int GetAllocSize() const;
60 #endif
61
62private:
65
66 const char* name;
67
68 #if NEBULA_MEMORY_STATS
69 int volatile allocCount;
70 int volatile allocSize;
71 static Threading::CriticalSection* criticalSection;
72 static Util::List<PosixHeap*>* list;
74 #endif
75};
76
77//------------------------------------------------------------------------------
80inline const char*
82{
83 n_assert(0 != this->name);
84 return this->name;
85}
86
87//------------------------------------------------------------------------------
90__forceinline void*
91PosixHeap::Alloc(size_t size)
92{
93 #if NEBULA_MEMORY_STATS
94 Threading::Interlocked::Increment(this->allocCount);
95 Threading::Interlocked::Add(this->allocSize, int(size));
96 #endif
97 return malloc(size);
98}
99
100//------------------------------------------------------------------------------
103__forceinline void*
104PosixHeap::Realloc(void* ptr, size_t size)
105{
106 #if NEBULA_MEMORY_STATS
107 size_t curSize = HeapSize(this->heap, 0, ptr);
108 Threading::Interlocked::Add(this->allocSize, int(size - curSize));
109 #endif
110 return realloc(ptr, size);
111}
112
113//------------------------------------------------------------------------------
116__forceinline void
118{
119 n_assert(0 != ptr);
120 #if NEBULA_MEMORY_STATS
121 size_t size = HeapSize(this->heap, 0, ptr);
122 Threading::Interlocked::Add(this->allocSize, -int(size));
123 Threading::Interlocked::Decrement(this->allocCount);
124 #endif
125 free(ptr);
126}
127
128} // namespace Posix
129//------------------------------------------------------------------------------
130#endif
Posix implementation of the class Memory::Heap using the Posix-Heap functions.
Definition posixheap.h:25
const char * name
Definition posixheap.h:66
const char * GetName() const
get heap name
Definition posixheap.h:81
~PosixHeap()
destructor
Definition posixheap.cc:57
PosixHeap()
default constructor not allowed
void * Alloc(size_t size)
allocate a block of memory from the heap
Definition posixheap.h:91
static void Setup()
static setup method (called by Util::Setup)
Definition posixheap.cc:25
void Free(void *ptr)
free a block of memory which has been allocated from this heap
Definition posixheap.h:117
void * Realloc(void *ptr, size_t newSize)
re-allocate a block of memory
Definition posixheap.h:104
Critical section objects are used to protect a portion of code from parallel execution.
Nebula's dynamic array class.
Definition array.h:60
the list iterator
Definition list.h:76
Implements a doubly linked list.
Definition list.h:20
#define n_assert(exp)
Definition debug.h:50
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
int Decrement(int volatile *var)
interlocked decrement, return result
Definition gccinterlocked.cc:157
int Add(int volatile *var, int add)
interlocked add
Definition gccinterlocked.cc:22
int Increment(int volatile *var)
interlocked increment, return result
Definition gccinterlocked.cc:148