All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Allocator.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "TrackingAllocator.h"
7 #include "AllocationRecord.h"
8 
9 #include <stdlib.h>
10 
11 namespace Eegeo
12 {
13  class Allocator
14  {
15  public:
16  Allocator () {}
17  virtual ~Allocator () {}
18 
19  virtual void* Alloc ( size_t alignment, size_t size, const char* file, int line) { (void)alignment; (void)size; (void)file; (void)line; return NULL; }
20  virtual void Free ( void* data ) { (void)data; }
21  };
22 
23  Allocator* GetAllocator ();
24  Allocator* SetAllocator ( Allocator* pAllocator );
25 
26  Allocator* GetEndAllocator ();
27  Allocator* SetEndAllocator ( Allocator* pAllocator );
28 
29  Allocator* GetDefaultAllocator ();
30 
31 }
32 
33 namespace Eegeo
34 {
35 
36  class Mallocator : public Allocator
37  {
38  public:
39 #if defined(Eegeo_MEMORY_TRACKING_NEW)
40  virtual void* Alloc(size_t alignment, size_t size, const char* file, int line)
41  {
42  void* payload = TrackingAllocator::PerformAlloc((int)alignment, size);
43  AllocationRecord* record = AllocationRecord::GetFromPayload(payload);
44  record->site.file = file;
45  record->site.line = line;
46 
47  return payload;
48  }
49 #elif defined (EEGEO_IOS)
50  virtual void* Alloc ( size_t alignment, size_t size, const char* file, int line ) { void* pData; posix_memalign(&pData, alignment, size); return pData; }
51 #elif defined (EEGEO_OSX) || defined (EEGEO_WIN)
52  virtual void* Alloc( size_t alignment, size_t size, const char* file, int line )
53  {
54  void *mem = malloc( size );
55  Eegeo_ASSERT(mem != NULL, "Memory allocation failed!");
56  return mem;
57  }
58 #else
59  virtual void* Alloc ( size_t alignment, size_t size, const char* file, int line ) { void* pData = memalign(alignment, size); return pData; }
60 #endif
61 
62 #if defined(Eegeo_MEMORY_TRACKING_NEW)
63  virtual void Free(void* data)
64  {
65  TrackingAllocator::PerformFree(data);
66  }
67 #else
68  virtual void Free ( void* data ) { free(data); }
69 #endif
70  };
71 
72  extern Allocator* gAllocator;
73  extern Allocator* gEndAllocator;
74 
75  static Mallocator defaultAllocator;
76 }
77 
78 
79 
80 #define Eegeo_BASE_ALLOC(alloc, align, size) alloc->Alloc(align, size, __FILE__, __LINE__)
81 #define Eegeo_BASE_FREE(alloc, data) alloc->Free(data)
82