The Sparta Modeling Framework
Loading...
Searching...
No Matches
ObjectAllocator.hpp
Go to the documentation of this file.
1// <ObjectAllocator.h> -*- C++ -*-
2
3
10#pragma once
11
12#include <inttypes.h>
13#include <queue>
14#include <vector>
15#include <limits>
16#include <memory>
18#include <inttypes.h>
19
20
21namespace sparta
22{
23 template<typename ObjT>
25 {
26 private:
27 typedef std::unique_ptr<ObjT> SmartPtr;
28 std::queue<ObjT*> free_obj_list_;
29 std::vector<SmartPtr> allocated_objs_;
30 public:
32 {
33 }
34 //allow direct access to the allocator via create and Free
35 template<typename... Args>
36 ObjT * create(Args&&... args)
37 {
38 ObjT * obj = 0;
39 if(free_obj_list_.empty()) {
40 allocated_objs_.emplace_back(new ObjT(args...));
41 obj = allocated_objs_.back().get();
42 }
43 else {
44 obj = free_obj_list_.front();
45 free_obj_list_.pop();
46 }
47 return obj;
48 }
49
50 //When the obj is finished, it puts itself back on the free
51 //list
52 void free(ObjT * obj) {
53 free_obj_list_.push(obj);
54 }
55
56 void clear() {
57 allocated_objs_.clear();
58 free_obj_list_ = std::queue<ObjT *>();
59 }
60
61 //Make this allocator work as a stl compliant allocator
62 // public:
63 // typedef ObjT value_type;
64 // typedef ObjT* pointer;
65 // typedef const value_type* const_pointer;
66 // typedef value_type& reference;
67 // typedef const value_type& const_reference;
68 // typedef std::size_t size_type;
69 // typedef std::ptrdiff_t difference_type;
70
71 // //convert to a different template allocator???
72 // template<typename U>
73 // struct rebind{
74 // typedef ObjectAllocator<U> other;
75 // };
76
77 // //Allow construction of the ObjectAllocator
78 // explicit ObjectAllocator() {}
79 // ///Destruct
80 // ~ObjectAllocator()
81 // {
82 // //nothing to do since UniquePtrs were used. :)
83 // allocated_objs_.clear();
84 // }
85 // ///Allow copy construction
86 // explicit ObjectAllocator(ObjectAllocator<ObjT> const&){}
87 // template<typename U>
88 // inline explicit ObjectAllocator(ObjectAllocator<U> const&) {}
89
90 // //allow support to get the address of objects.
91 // pointer address(reference r) { return &r; }
92 // const_pointer address(const_reference r) { return &r; }
93
94
95 // ///either new up some memory or use some of our already created memory.
96 // pointer allocate(size_type size, typename std::allocator<void>::const_pointer = 0)
97 // {
98 // std::cout << "allocating memory" << std::endl;
99 // pointer p;
100 // if(free_obj_list_.empty())
101 // {
102 // //i don't think size should ever be greater than 1, if it is...
103 // //then i don't know what's going on.
104 // sparta_assert(size == 1);
105 // allocated_objs_.emplace_back(SmartPtr(new char[sizeof(ObjT)]));
106 // p = reinterpret_cast<pointer>(allocated_objs_.back().get());
107 // }
108 // else
109 // {
110 // p = free_obj_list_.front();
111 // free_obj_list_.pop();
112 // }
113 // return p;
114 // }
115
116 // ///instead of deleting the memory, store a pointer to the memory
117 // ///in a list of free memory.
118 // void deallocate(pointer p, size_type)
119 // {
120 // std::cout << "deallocating by pushing to free list" << std::endl;
121 // free_obj_list_.push(p);
122 // }
123
124 // ///max size?
125 // size_type max_size() const
126 // {
127 // return std::numeric_limits<size_type>::max() / sizeof(ObjT);
128 // }
129
130 // ///construct and destruct objects.
131 // void construct(pointer p, const ObjT& t)
132 // {
133 // //construct the object implace at p's location?
134 // new (p) ObjT(t);
135 // }
136 // ///destruct an object.
137 // void destroy(pointer p)
138 // {
139 // (void)p;
140 // //there's nothing to do since we are going to keep the memory at p around.
141 // //we don't care to destruct the object even? sounds ugly.
142 // }
143
144 // bool operator==(ObjectAllocator<ObjT> const&) { return false;}
145 // bool operator!=(ObjectAllocator<ObjT> const& a) {return !operator==(a); }
146
147 // public:
148 };
149}
Set of macros for Sparta assertions. Caught by the framework.
Macros for handling exponential backoff.