Triton
Visual simulation library for ocean rendering.
MemAlloc.h
Go to the documentation of this file.
1// Copyright (c) 2010-2016 Sundog Software, LLC. All rights reserved worldwide.
2
3
8#ifndef TRITON_MEMALLOC_H
9#define TRITON_MEMALLOC_H
10
11#include <cstddef>
12#include <string>
13
14#ifdef _WIN32
15#define TRITONAPI __cdecl
16#else
17#define TRITONAPI
18#endif
19
20#define TRITON_NEW new
21#define TRITON_DELETE delete
22#define TRITON_MALLOC Allocator::GetAllocator()->alloc
23#define TRITON_FREE Allocator::GetAllocator()->dealloc
24
25namespace Triton
26{
35{
36public:
37 Allocator();
38 virtual ~Allocator();
39
41 virtual void * TRITONAPI alloc(size_t bytes);
42
44 virtual void TRITONAPI dealloc(void *p);
45
47 static Allocator * TRITONAPI GetAllocator() {
48 return sAllocator;
49 }
50
53 static void TRITONAPI SetAllocator(Allocator *a) {
54 if( a )
55 sAllocator = a;
56 else
57 sAllocator = &sDefaultAllocator;
58 }
59
60protected:
61 static Allocator *sAllocator;
62 static Allocator sDefaultAllocator;
63
64private:
65 void *heap;
66};
67
71{
72public:
73#if (!(_MSC_VER < 1300))
74
75 void *operator new(size_t bytes) {
76 return TRITON_MALLOC(bytes);
77 }
78
79 void operator delete(void *p) {
80 TRITON_FREE(p);
81 }
82#endif
83};
84}
85
86// intercepting STL allocation under VC6 is just hopeless.
87#if (_MSC_VER < 1300)
88
89#define TRITON_VECTOR(T) std::vector< T >
90#define TRITON_MAP(A, B) std::map< A, B >
91#define TRITON_SET(T) std::set< T >
92#define TRITON_LIST(T) std::list< T >
93#define TRITON_STRING std::string
94#define TRITON_STACK(T) std::stack< T >
95
96#else
97
98// Custom STL allocator
99
100#define MAX_ALLOCATOR_SIZE 100E6
101
102template <class T>
103class TritonAllocator
104{
105public:
106 // type definitions
107 typedef T value_type;
108 typedef T* pointer;
109 typedef const T* const_pointer;
110 typedef T& reference;
111 typedef const T& const_reference;
112
113 typedef std::size_t size_type;
114 typedef std::ptrdiff_t difference_type;
115
116 // rebind allocator to type U
117 template <class U >
118 struct rebind {
119 typedef TritonAllocator< U > other;
120 };
121
122 // return address of values
123 pointer address (reference value) const {
124 return &value;
125 }
126 const_pointer address (const_reference value) const {
127 return &value;
128 }
129
130 /* constructors and destructor
131 * - nothing to do because the allocator has no state
132 */
133 TritonAllocator() throw() {
134 }
135 TritonAllocator(const TritonAllocator& ) throw() {
136 }
137
138 template <class U >
139 TritonAllocator (const TritonAllocator< U > &) throw() {
140 }
141
142 ~TritonAllocator() throw() {
143 }
144
145 // return maximum number of elements that can be allocated
146 size_type max_size () const throw() {
147 return (size_type)(MAX_ALLOCATOR_SIZE / sizeof(T));
148 }
149
150 // allocate but don't initialize num elements of type T
151 pointer allocate (size_type num, const void* = 0) {
152 pointer ret = reinterpret_cast<T*>(::Triton::Allocator::GetAllocator()->alloc(num * sizeof(T)));
153 return ret;
154 }
155
156 // initialize elements of allocated storage p with value value
157 void construct (pointer p, const T& value) {
158 // initialize memory with placement new
159 ::new ((void*)p)T(value);
160 }
161
162 // destroy elements of initialized storage p
163 void destroy (pointer p) {
164 // destroy objects by calling their destructor
165 p->~T();
166 }
167
168 // deallocate storage p of deleted elements
169 void deallocate (pointer p, size_type ) {
171 }
172};
173
174// return that all specializations of this allocator are interchangeable
175template <class T1, class T2>
176bool operator== (const TritonAllocator<T1>&, const TritonAllocator<T2>&) throw()
177{
178 return true;
179}
180template <class T1, class T2>
181bool operator!= (const TritonAllocator<T1>&, const TritonAllocator<T2>&) throw()
182{
183 return false;
184}
185
186// Convenience macros for STL object using our allocator
187
188#define TRITON_VECTOR(T) std::vector<T, TritonAllocator< T > >
189#define TRITON_MAP(A, B) std::map<A, B, std::less< A >, TritonAllocator< std::pair<A const, B > > >
190#define TRITON_SET(T) std::set<T, std::less< T >, TritonAllocator< T > >
191#define TRITON_LIST(T) std::list<T, TritonAllocator< T > >
192#define TRITON_STRING std::basic_string<char, std::char_traits<char>, TritonAllocator<char> >
193#define TRITON_STACK(T) std::stack<T, std::deque< T, TritonAllocator< T > > >
194
195#endif
196
197// "Lazy" wrapper for handling STL objects that were static
198template <class T>
199class TRITON_LAZY
200{
201public:
202 TRITON_LAZY() : obj(0) {}
203 virtual ~TRITON_LAZY() {
204 if (obj) TRITON_DELETE obj;
205 }
206
207 T& Get() {
208 if (!obj) obj = TRITON_NEW T;
209 return *obj;
210 }
211
212private:
213 T* obj;
214};
215
216#endif
You may extend the Allocator class to hook your own memory management scheme into Triton.
Definition: MemAlloc.h:35
virtual void TRITONAPI dealloc(void *p)
Free a block of memory; defaults to free()
static void TRITONAPI SetAllocator(Allocator *a)
Sets a new static allocator object.
Definition: MemAlloc.h:53
static Allocator *TRITONAPI GetAllocator()
Retrieves the static allocator object.
Definition: MemAlloc.h:47
virtual void *TRITONAPI alloc(size_t bytes)
Allocate a block of memory; defaults to malloc()
This base class for all Triton objects intercepts the new and delete operators, routing them through ...
Definition: MemAlloc.h:71