Nebula
Toggle main menu visibility
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
"
16
#include "
threading/interlocked.h
"
17
#include "
threading/criticalsection.h
"
18
#include "
util/array.h
"
19
#include "
util/list.h
"
20
21
//------------------------------------------------------------------------------
22
namespace
Posix
23
{
24
class
PosixHeap
25
{
26
public
:
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
62
private
:
64
PosixHeap
();
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;
73
Util::List<PosixHeap*>::Iterator
listIterator;
74
#endif
75
};
76
77
//------------------------------------------------------------------------------
80
inline
const
char
*
81
PosixHeap::GetName
()
const
82
{
83
n_assert
(0 != this->
name
);
84
return
this->
name
;
85
}
86
87
//------------------------------------------------------------------------------
90
__forceinline
void
*
91
PosixHeap::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
*
104
PosixHeap::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
117
PosixHeap::Free
(
void
* ptr)
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
array.h
Posix::PosixHeap::PosixHeap
PosixHeap(const char *name)
constructor (name must be static string!)
Definition
posixheap.cc:38
Posix::PosixHeap::name
const char * name
Definition
posixheap.h:66
Posix::PosixHeap::GetName
const char * GetName() const
get heap name
Definition
posixheap.h:81
Posix::PosixHeap::~PosixHeap
~PosixHeap()
destructor
Definition
posixheap.cc:57
Posix::PosixHeap::PosixHeap
PosixHeap()
default constructor not allowed
Posix::PosixHeap::Alloc
void * Alloc(size_t size)
allocate a block of memory from the heap
Definition
posixheap.h:91
Posix::PosixHeap::Setup
static void Setup()
static setup method (called by Util::Setup)
Definition
posixheap.cc:25
Posix::PosixHeap::Free
void Free(void *ptr)
free a block of memory which has been allocated from this heap
Definition
posixheap.h:117
Posix::PosixHeap::Realloc
void * Realloc(void *ptr, size_t newSize)
re-allocate a block of memory
Definition
posixheap.h:104
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
Util::List::Iterator
the list iterator
Definition
list.h:76
Util::List
Implements a doubly linked list.
Definition
list.h:20
criticalsection.h
n_assert
#define n_assert(exp)
Definition
debug.h:50
interlocked.h
list.h
Posix
Posix implemention of a read-many write-few lock.
Definition
posixsysfunc.cc:21
Threading::Interlocked::Decrement
int Decrement(int volatile *var)
interlocked decrement, return result
Definition
gccinterlocked.cc:157
Threading::Interlocked::Add
int Add(int volatile *var, int add)
interlocked add
Definition
gccinterlocked.cc:22
Threading::Interlocked::Increment
int Increment(int volatile *var)
interlocked increment, return result
Definition
gccinterlocked.cc:148
types.h
code
foundation
memory
posix
posixheap.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.