OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreMemoryTrackAlloc.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef _OgreMemoryTrackAlloc_H_
30 #define _OgreMemoryTrackAlloc_H_
31 
32 #if OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_TRACK
33 
34 #include <memory>
35 
36 #include "OgreAlignedAllocator.h"
37 #include "OgreHeaderPrefix.h"
38 
39 namespace Ogre
40 {
41 //1GB pool
42 #define OGRE_TRACK_POOL_SIZE 1024*1024*1024
43 
56  {
57  static char *MemoryPool;
58  static size_t CurrentOffset;
59  static char Magic[4];
60  static size_t RandomOffset;
61  public:
62  static DECL_MALLOC void* allocateBytes(size_t count,
64  const char* file = 0, int line = 0, const char* func = 0
65 #else
66  const char* = 0, int = 0, const char* = 0
67 #endif
68  );
69 
70  static void deallocateBytes(void* _ptr);
71 
73  static inline size_t getMaxAllocationSize()
74  {
75  return OGRE_TRACK_POOL_SIZE;
76  }
77  private:
78  // no instantiation
80  { }
81  };
82 
85  template <size_t Alignment = 0>
87  {
88  public:
89  // compile-time check alignment is available.
90  typedef int IsValidAlignment
91  [Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1];
92 
93  static inline DECL_MALLOC void* allocateBytes(size_t count,
95  const char* file = 0, int line = 0, const char* func = 0
96 #else
97  const char* = 0, int = 0, const char* = 0
98 #endif
99  )
100  {
101  if( !count )
102  return 0;
103 
104  size_t _alignment = Alignment ? Alignment : 16; //Should be OGRE_SIMD_ALIGNMENT, but compiler error
105  uint8 *tmp = (uint8*)TrackAllocPolicy::allocateBytes( count + _alignment
107  , file, line, func
108 #endif
109  );
110 
111  //Align...
112  uint8 *mem_block = (uint8*)( (uint32) (tmp + _alignment - 1) & (uint32)(~(_alignment - 1)) );
113 
114  //Special case where malloc have already satisfied the alignment
115  //We must add alignment to mem_block to mantain alignment AND
116  //to avoid that afree causes an ACCESS VIOLATION becuase
117  //(*(mem_block-1)) is beyond our visibility
118  if (mem_block == tmp)
119  mem_block += _alignment;
120 
121  //How far are from the real start of our memory
122  //block?
123  *(mem_block-1) = (uint8) (mem_block-tmp);
124 
125  return (void*)mem_block;
126  }
127 
128  static inline void deallocateBytes(void* ptr)
129  {
130  uint8 *realAddress;
131 
132  if( !ptr )
133  return;
134 
135  realAddress = (uint8*)ptr;
136  realAddress -= *(realAddress-1);
137 
138  TrackAllocPolicy::deallocateBytes( realAddress );
139  }
140 
142  static inline size_t getMaxAllocationSize()
143  {
144  return OGRE_TRACK_POOL_SIZE;
145  }
146  private:
147  // No instantiation
149  { }
150  };
154 }// namespace Ogre
155 
156 
157 #include "OgreHeaderSuffix.h"
158 
159 #endif
160 
161 #endif // _OgreMemoryTrackAlloc_H_
static void deallocateBytes(void *_ptr)
unsigned char uint8
Definition: OgrePlatform.h:422
unsigned int uint32
Definition: OgrePlatform.h:420
#define _OgreExport
Definition: OgrePlatform.h:255
#define OGRE_MEMORY_TRACKER
static size_t getMaxAllocationSize()
Get the maximum size of a single allocation.
static void deallocateBytes(void *ptr)
static size_t getMaxAllocationSize()
Get the maximum size of a single allocation.
An allocation policy that preallocates a fixed amount of memory and always returns a deterministic po...
static DECL_MALLOC void * allocateBytes(size_t count, const char *=0, int=0, const char *=0)
int IsValidAlignment[Alignment<=128 &&((Alignment &(Alignment-1))==0)?+1:-1]
#define DECL_MALLOC
Definition: OgrePlatform.h:385
static DECL_MALLOC void * allocateBytes(size_t count, const char *=0, int=0, const char *=0)
#define OGRE_TRACK_POOL_SIZE