set allocator as it should be

That stuff drunk my blood during last week. Take plese a look and say what else is wrong there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// set::insert
#include <iostream> 
#include <set>
#include <vector>
#include <memory>
#include <functional>

//using namespace std;
  ///////////////////////1
  class TInternalPool
  {
  public:
    TInternalPool()
    {
      buf=NULL;
      mem_cnt=0;
    }
    
    //  I don't want objects of this class to be copied or assigned
    TInternalPool(const TInternalPool&) = delete;
    TInternalPool& operator=(const TInternalPool&) = delete;
    
    ~TInternalPool()
    {
      FreeBuf();
    }
    
    void FreeBuf()
    {
      if (buf)
      {
        delete [] buf;
        buf=NULL;
      }
    }
    
    void BufReserve(size_t count, size_t align)
    {
      count+=align-1; // to make available satisfy alignment for elements in future
      if (mem_cnt<count)
      {
        FreeBuf();
        mem_cnt=count;
        buf = new char[count]; //>=count bytes, not aligned, aligning will be later
        if (!buf)
          throw std::bad_alloc();
        last=buf+count;
      }
      curr=last;
    }
    
    inline char* allocate(size_t sz, size_t align)
    {
      curr-=sz;
      curr=reinterpret_cast<char*>(reinterpret_cast<size_t>(curr) & (-align));
      if (curr<buf)
        throw("Space supposed to be pre-allocated!");
      return curr;
    }
    
    inline void deallocate(size_t n)
    {
      // curr+=n; // no align control analysis so it's not invert to "allocate"
    }
    
    void Clear()
    {
      curr=last; // allocate from back as in stack
    }
  private:
    char *buf, *last, *curr;
    size_t mem_cnt;
  };
  ///////////////////////2
  template <class T> 
  class TBufferedAllocator: public std::allocator<T>
  {
  public:
    typedef T* pointer;
    typedef size_t size_type;
    
    template<class _Other>
    struct rebind
    {
      typedef TBufferedAllocator<_Other> other; 
    };
    
    explicit TBufferedAllocator(TInternalPool &Apool): std::allocator<T>(), pool(Apool)
    {
      
    }
    
    TBufferedAllocator(const TBufferedAllocator& copy_from_me) : pool(copy_from_me.pool)
    {
    }
    
    template <typename A> 
    TBufferedAllocator(const A& copy_from_me) : pool(copy_from_me.pool)
    {
    }
    
    virtual ~TBufferedAllocator()
    {
    }
    
    virtual pointer allocate(size_type n, const void * hint=0)
    {
      std::cout << "TBufferedAllocator::allocate called, size="<<n <<std::endl;
      std::cout << "size="<<sizeof(T) <<", name=" << std::endl;
      pool.allocate(n*sizeof(T), alignof(T));
    }
    
    virtual void deallocate(pointer p, size_type n)
    {
      std::cout << "TBufferedAllocator::deallocate called, size="<<n <<std::endl;
      pool.deallocate(n*sizeof(T));
      // no implementation, shouldn't actually be. Memory REALLY deallocates only in destructor or via mem change
    }
    
    void reserve(size_t n_bytes)
    {
      pool.BufReserve(n_bytes, alignof(T));
    }
  protected:
    TInternalPool &pool;
  private:
    // no default constructor accessable:
    TBufferedAllocator();
    
    template <typename U>
    friend class TBufferedAllocator;
  };
  ///////////////////////3
  template <class T> 
  class TBufferedStdSetAllocator : public TBufferedAllocator<T>
  {
  public:
    typedef T* pointer;
    typedef size_t size_type;
    
    template<class _Other>
    struct rebind
    {
      typedef TBufferedStdSetAllocator<_Other> other; 
    };
    
    explicit TBufferedStdSetAllocator(TInternalPool &Apool): TBufferedAllocator<T>(Apool)
    {
      
    }
    
    TBufferedStdSetAllocator(const TBufferedStdSetAllocator& copy_from_me) : TBufferedAllocator<T>(copy_from_me)
    {
    }
    
    template <typename A> 
    TBufferedStdSetAllocator(const A& copy_from_me) : TBufferedAllocator<T>(copy_from_me)
    {
    }
    
  private:
    template <class U>
    class TBufferedStdSetAllocatorHelper : public std::allocator<U>
    {
    public:
      size_t node_type_size;
      typedef U* pointer;
      typedef size_t size_type;
      
      template<class _Other>
      struct rebind
      {
        typedef TBufferedStdSetAllocatorHelper<_Other> other; 
      };
      
      explicit TBufferedStdSetAllocatorHelper(): std::allocator<U>()
      {
      }
      
      TBufferedStdSetAllocatorHelper(const TBufferedStdSetAllocatorHelper& copy_from_me)
      {
      }
      
      template <typename A> 
      TBufferedStdSetAllocatorHelper(const A& copy_from_me)
      {
      }
      
      virtual pointer allocate(size_type n, const void * hint=0)
      {
        node_type_size=sizeof(U);
        return std::allocator<U>::allocate(n,hint);
      }
    };
    
  public:
    void reserve_set_items_cnt(size_t n_items, const T &dummy_val)
    {
      // hack to get node size in std::set :
      TBufferedStdSetAllocatorHelper<T> hlpAlloc(this->pool);
      std::set<T, std::less<T>, TBufferedStdSetAllocatorHelper<T>> help_set(std::less<T>(), hlpAlloc);
      //
      try
      {
        help_set.insert(dummy_val); // could be exception there, just ignore it
      }
      catch (...)
      {
        std::cout<<"gotcha!"<<std::endl;
      }
      this->pool.BufReserve(n_items*hlpAlloc.node_type_size, alignof(T));
    }
  };
  
  class foo 
  {
    int a;
    double b;
    void *c;
    const char *d;
    int *e();
    int g;
    
  public: 
    foo(int Aa, int Ag)
    {
      a=Aa;
      g=Ag;
      b=2012.0;
      c=NULL;
      d="Hello world";
      //e=NULL;
    }
    
    inline bool operator <(const foo &other) const
    {
      return a>other.a;
    }
  } ;//__attribute__((aligned(64)));
  
int main ()
{
  TInternalPool pool;
  TBufferedStdSetAllocator<foo> MyAlloc(pool);
  std::set<foo, std::less<foo>, TBufferedStdSetAllocator<foo> > v_foo(std::less<foo>(), MyAlloc);
  //
  MyAlloc.reserve_set_items_cnt(10, foo(1, 2));
  for (int i=0;i<10;i++)
    v_foo.insert(foo(i, i*i));
  return 0;
}


Project is non-commercial just for my self-education, but i want to do it right.
I think it would be a good idea to look at some of the existing allocators, especially pre-c++11 allocators, before writing your own, such as the ones in boost, TBB, and many other portable libraries.

Or, if they seem too complicated, at least look at some of the educational examples that people post on forums and blogs, like a few of the dozens of aligned allocators:: http://stackoverflow.com/a/13713083/273767 http://gist.github.com/1471329
etc

Or, if you want to look at C++11, here's a canonical stack allocator: http://home.roadrunner.com/~hinnant/short_alloc.h
Topic archived. No new replies allowed.