Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
aligned_allocator.h
1 #pragma once
2 
3 #include "memory.h"
4 #include "../macros.h"
5 
6 namespace smlt {
7 
8 template <class T, int N>
10 public:
11  typedef T value_type;
12  typedef T& reference;
13  typedef const T& const_reference;
14  typedef T* pointer;
15  typedef const T* const_pointer;
16  typedef size_t size_type;
17  typedef ptrdiff_t difference_type;
18 
19  template <class U>
20  struct rebind {
22  };
23 
24  inline aligned_allocator() throw() {}
25  inline aligned_allocator(const aligned_allocator&) throw() {}
26 
27  template <class U>
28  inline aligned_allocator(const aligned_allocator<U,N>&) throw() {}
29  inline ~aligned_allocator() throw() {}
30 
31  inline pointer address(reference r) { return &r; }
32  inline const_pointer address(const_reference r) const { return &r; }
33 
34  pointer allocate(size_type n, typename std::allocator<void>::const_pointer hint = 0);
35  inline void deallocate(pointer p, size_type);
36 
37  inline void construct(pointer p, const_reference value) { new (p) value_type(value); }
38  inline void destroy(pointer p) { p->~value_type(); }
39 
40  inline size_type max_size() const throw() { return size_type(-1) / sizeof(T); }
41 
42  inline bool operator==(const aligned_allocator&) { return true; }
43  inline bool operator!=(const aligned_allocator& rhs) { return !operator==(rhs); }
44 };
45 
46 template <class T, int N>
47 typename aligned_allocator<T, N>::pointer
48 aligned_allocator<T, N>::allocate(size_type n, typename std::allocator<void>::const_pointer hint) {
49  _S_UNUSED(hint);
50 
51  pointer res = reinterpret_cast<pointer>(aligned_alloc(N, sizeof(T) * n));
52  if(res == 0)
53  throw std::bad_alloc();
54  return res;
55 }
56 
57 template <class T, int N>
58 void aligned_allocator<T, N>::deallocate(pointer p, size_type) {
59  free(p);
60 }
61 
62 }
smlt
Definition: animation.cpp:25
smlt::aligned_allocator
Definition: aligned_allocator.h:9
smlt::aligned_allocator::rebind
Definition: aligned_allocator.h:20