forked from shokunin000/te120
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvallocator.h
84 lines (66 loc) · 1.81 KB
/
vallocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// These classes let you write your own allocators to be used with new and delete.
// If you have an allocator: VAllocator *pAlloc, you can call new and delete like this:
//
// ptr = VNew(pAlloc) ClassName;
// VDelete(pAlloc, ptr);
//
// Note: allocating and freeing arrays of objects will not work using VAllocators.
#ifndef VALLOCATOR_H
#define VALLOCATOR_H
class VAllocator
{
public:
virtual void* Alloc(unsigned long size)=0;
virtual void Free(void *ptr)=0;
};
// This allocator just uses malloc and free.
class VStdAllocator : public VAllocator
{
public:
virtual void* Alloc(unsigned long size);
virtual void Free(void *ptr);
};
extern VStdAllocator g_StdAllocator;
// Use these to allocate classes through VAllocator.
// Allocating arrays of classes is not supported.
#define VNew(pAlloc) new
#define VDelete(pAlloc, ptr) delete ptr
// Used internally.. just makes sure we call the right operator new.
class DummyAllocatorHelper
{
public:
int x;
};
inline void* operator new(size_t size, void *ptr, DummyAllocatorHelper *asdf)
{
asdf=asdf; // compiler warning.
size=size;
return ptr;
}
inline void operator delete(void *ptrToDelete, void *ptr, DummyAllocatorHelper *asdf)
{
asdf=asdf; // compiler warning.
ptr=ptr;
ptrToDelete=ptrToDelete;
}
// Use these to manually construct and destruct lists of objects.
template<class T>
inline void VAllocator_CallConstructors(T *pObjects, int count=1)
{
for(int i=0; i < count; i++)
new(&pObjects[i], (DummyAllocatorHelper*)0) T;
}
template<class T>
inline void VAllocator_CallDestructors(T *pObjects, int count)
{
for(int i=0; i < count; i++)
pObjects[i].~T();
}
#endif