Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
allocationmanager.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_ALLOCATIONMANAGER_H_
19 #define ION_BASE_ALLOCATIONMANAGER_H_
20 
21 #include "base/macros.h"
22 #include "ion/base/allocator.h"
23 
24 namespace ion {
25 namespace base {
26 
29 class ION_API AllocationManager {
30  public:
32 
36  GetInstance()->default_allocation_lifetime_ = lifetime;
37  }
39  return GetInstance()->default_allocation_lifetime_;
40  }
41 
57  const AllocatorPtr& allocator) {
58  AllocationManager* mgr = GetInstance();
59  mgr->default_allocators_[lifetime] =
60  allocator.Get() ? allocator : mgr->malloc_allocator_;
61  }
63  AllocationLifetime lifetime) {
64  return GetInstance()->default_allocators_[lifetime];
65  }
66 
71  AllocationManager* mgr = GetInstance();
72  return mgr->default_allocators_[mgr->default_allocation_lifetime_];
73  }
74 
77  static const AllocatorPtr& GetMallocAllocator() {
78  return GetInstance()->malloc_allocator_;
79  }
80 
85  const AllocatorPtr& allocator) {
86  return allocator.Get() ? allocator : GetDefaultAllocator();
87  }
88 
89  private:
92  class MallocAllocator;
93 
96 
98  static AllocationManager* GetInstance();
99 
101  AllocationLifetime default_allocation_lifetime_;
102 
104  AllocatorPtr default_allocators_[kNumAllocationLifetimes];
105 
107  AllocatorPtr malloc_allocator_;
108 
109  DISALLOW_COPY_AND_ASSIGN(AllocationManager);
110 };
111 
112 } // namespace base
113 } // namespace ion
114 
115 #endif // ION_BASE_ALLOCATIONMANAGER_H_
AllocationLifetime
All memory allocated within Ion uses an Allocator chosen based on the predicted lifetime of the targe...
Definition: allocator.h:33
AllocationManager is a singleton class that is used to manage Allocators used to allocate Ion objects...
static const AllocatorPtr & GetDefaultAllocator()
Convenience function that returns the default allocator to use when no lifetime is specified...
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
static const AllocatorPtr & GetNonNullAllocator(const AllocatorPtr &allocator)
This convenience function can be used where a non-NULL Allocator pointer is needed.
static void SetDefaultAllocatorForLifetime(AllocationLifetime lifetime, const AllocatorPtr &allocator)
Sets/returns the default Allocator to use for a specific AllocationLifetime.
static const AllocatorPtr & GetMallocAllocator()
Returns an allocator that performs conventional allocation and deallocation with malloc() and free()...
static AllocationLifetime GetDefaultAllocationLifetime()
static void SetDefaultAllocationLifetime(AllocationLifetime lifetime)
Sets/returns the AllocationLifetime that is assumed when a default new() operator is used for an Allo...
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)