MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helpers.hpp
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 
20 /* mySTL helpers implements misc constructs for vector and list
21  *
22  */
23 
24 #ifndef mySTL_HELPERS_HPP
25 #define mySTL_HELPERS_HPP
26 
27 #include <stdlib.h>
28 #ifdef _MSC_VER
29  #include <new>
30 #endif
31 
32 /*
33  Workaround for the lack of operator new(size_t, void*)
34  in IBM VA C++ 6.0
35  Also used as a workaround to avoid including <new>
36 */
37  struct Dummy {};
38 
39  inline void* operator new(size_t size, Dummy* d)
40  {
41  return static_cast<void*>(d);
42  }
43 
44  // for compilers that want matching delete
45  inline void operator delete(void* ptr, Dummy* d)
46  {
47  }
48 
49  typedef Dummy* yassl_pointer;
50 
51 namespace mySTL {
52 
53 
54 template <typename T, typename T2>
55 inline void construct(T* p, const T2& value)
56 {
57  new (reinterpret_cast<yassl_pointer>(p)) T(value);
58 }
59 
60 
61 template <typename T>
62 inline void construct(T* p)
63 {
64  new (reinterpret_cast<yassl_pointer>(p)) T();
65 }
66 
67 
68 template <typename T>
69 inline void destroy(T* p)
70 {
71  p->~T();
72 }
73 
74 
75 template <typename Iter>
76 void destroy(Iter first, Iter last)
77 {
78  while (first != last) {
79  destroy(&*first);
80  ++first;
81  }
82 }
83 
84 
85 template <typename Iter, typename PlaceIter>
86 PlaceIter uninit_copy(Iter first, Iter last, PlaceIter place)
87 {
88  while (first != last) {
89  construct(&*place, *first);
90  ++first;
91  ++place;
92  }
93  return place;
94 }
95 
96 
97 template <typename PlaceIter, typename Size, typename T>
98 PlaceIter uninit_fill_n(PlaceIter place, Size n, const T& value)
99 {
100  while (n) {
101  construct(&*place, value);
102  --n;
103  ++place;
104  }
105  return place;
106 }
107 
108 
109 template <typename T>
110 T* GetArrayMemory(size_t items)
111 {
112  unsigned char* ret;
113 
114  #ifdef YASSL_LIB
115  ret = NEW_YS unsigned char[sizeof(T) * items];
116  #else
117  ret = NEW_TC unsigned char[sizeof(T) * items];
118  #endif
119 
120  return reinterpret_cast<T*>(ret);
121 }
122 
123 
124 template <typename T>
125 void FreeArrayMemory(T* ptr)
126 {
127  unsigned char* p = reinterpret_cast<unsigned char*>(ptr);
128 
129  #ifdef YASSL_LIB
130  yaSSL::ysArrayDelete(p);
131  #else
132  TaoCrypt::tcArrayDelete(p);
133  #endif
134 }
135 
136 
137 
138 inline void* GetMemory(size_t bytes)
139 {
140  return GetArrayMemory<unsigned char>(bytes);
141 }
142 
143 
144 inline void FreeMemory(void* ptr)
145 {
146  FreeArrayMemory(ptr);
147 }
148 
149 
150 
151 } // namespace mySTL
152 
153 #endif // mySTL_HELPERS_HPP