InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
K2Allocator.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Mat Marcus
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __K2Allocator__
25 #define __K2Allocator__
26 
27 #include "K2STLUtilities.h"
28 #include "K2Assert.h"
29 
30 
36 template <class T>
37 class K2Allocator {
38 public:
39  typedef uint32 size_type;
40  typedef std::ptrdiff_t difference_type;
41  typedef T* pointer;
42  typedef const T* const_pointer;
43  typedef T& reference;
44  typedef const T& const_reference;
45  typedef T value_type;
46 
47  template <class U> struct rebind { typedef K2Allocator<U> other; };
48 
53 
54  template <class U>
56  {
57  }
58 
63  pointer address(reference x) const {return &x;}
64  const_pointer address(const_reference x) const {return &x;}
65 
70  pointer allocate(size_type n, const void* hint = nil);
71 
77  void deallocate(pointer p, size_type) {operator delete(p);}
78 
82  size_type max_size() const {return kMaxUInt32 / sizeof(T);}
83 
88  void construct(pointer p, const T& val) {new(p) T(val);}
89 
94  void destroy(pointer p) {p->~T();}
95 };
96 
97 
98 // ===================================================================================
99 // K2Allocator<void>
100 // specialization for void
101 // ===================================================================================
102 template <>
103 class K2Allocator<void> {
104 public:
105  typedef std::size_t size_type;
106  typedef std::ptrdiff_t difference_type;
107  typedef void* pointer;
108  typedef const void* const_pointer;
109  typedef void value_type;
110 };
111 
112 
113 // ===================================================================================
114 // Inlines
115 // ===================================================================================
116 template <class T>
117 inline typename K2Allocator<T>::pointer
118 K2Allocator<T>::allocate(size_type n, const void*)
119 {
120  pointer p = static_cast<pointer>(operator new(n * sizeof(T)));
121  ASSERT_MSG(p != nil, "Memory allocation failure");
122  return p;
123 }
124 
125 template <class T, class U>
126 inline bool16
127 operator==(const K2Allocator<T>&, const K2Allocator<U>&)
128 {
129  return true;
130 }
131 
132 template <class T, class U>
133 inline bool16
134 operator!=(const K2Allocator<T>&, const K2Allocator<U>&)
135 {
136  return false;
137 }
138 
139 
140 #endif