[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / OS_Memory.h
blobef5746b7cbd7f29314ebf91243fa09885dca24f5
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file OS_Memory.h
7 * $Id: OS_Memory.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Doug Schmidt <schmidt@cs.wustl.edu>
10 * @author Jesper S. M|ller<stophph@diku.dk>
11 * @author and a cast of thousands...
13 //=============================================================================
15 #ifndef ACE_OS_MEMORY_H
16 #define ACE_OS_MEMORY_H
17 #include /**/ "ace/pre.h"
19 #include /**/ "ace/ACE_export.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 # pragma once
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #include "ace/OS_Errno.h"
26 #include "ace/Basic_Types.h"
27 #include "ace/os_include/os_stddef.h"
29 // Allow an installation to replace the lowest-level allocation
30 // functions without changing the source of ACE.
32 // To do this, simple #define ACE_*_FUNC macros in config.h to
33 // the names of the site-specific functions, e.g.,
35 // #define ACE_MALLOC_FUNC dlmalloc
36 // #define ACE_CALLOC_FUNC dlcalloc
37 // #define ACE_FREE_FUNC dlfree
38 // #define ACE_REALLOC_FUNC dlrealloc
40 // For completeness' sake, you should probably put
41 // #define ACE_HAS_STRDUP_EMULATION
42 // #define ACE_HAS_WCSDUP_EMULATION
43 // too, so that you guarantee that strdup() and wcsdup() call your
44 // desired mallocator and not the system mallocator.
46 #if !defined (ACE_MALLOC_FUNC)
47 # define ACE_MALLOC_FUNC ::malloc
48 #endif
49 #if !defined (ACE_CALLOC_FUNC)
50 # define ACE_CALLOC_FUNC ::calloc
51 #endif
52 #if !defined (ACE_FREE_FUNC)
53 # define ACE_FREE_FUNC ::free
54 #endif
55 #if !defined (ACE_REALLOC_FUNC)
56 # define ACE_REALLOC_FUNC ::realloc
57 #endif
59 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
61 #if defined (ACE_HAS_OLD_MALLOC)
62 typedef char * ACE_MALLOC_T;
63 #else
64 typedef void * ACE_MALLOC_T;
65 #endif /* ACE_HAS_OLD_MALLOC */
67 ACE_END_VERSIONED_NAMESPACE_DECL
69 // ============================================================================
70 // ACE_NEW macros
72 // A useful abstraction for expressions involving operator new since
73 // we can change memory allocation error handling policies (e.g.,
74 // depending on whether ANSI/ISO exception handling semantics are
75 // being used).
76 // ============================================================================
78 // If new(std::nothrow) is defined then, by definition, new throws exceptions.
79 #if defined (ACE_HAS_NEW_NOTHROW)
80 # if !defined (ACE_NEW_THROWS_EXCEPTIONS)
81 # define ACE_NEW_THROWS_EXCEPTIONS
82 # endif
83 #endif
85 // The Windows MFC exception mechanism requires that a caught CException
86 // (including the CMemoryException in use here) be freed using its Delete()
87 // method. Thus, when MFC is in use and we're catching exceptions as a result
88 // of new(), the exception's Delete() method has to be called. No other
89 // platform imposes this sort of restriction/requirement. The Windows
90 // config stuff (at least for MSVC/MFC) defines a ACE_del_bad_alloc macro
91 // that works with its ACE_bad_alloc macro to implement this cleanup
92 // requirement. Since no other platform requires this, define it as
93 // empty here.
94 #if !defined (ACE_del_bad_alloc)
95 # define ACE_del_bad_alloc
96 #endif
98 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
100 // Since new() throws exceptions, we need a way to avoid passing
101 // exceptions past the call to new because ACE counts on having a 0
102 // return value for a failed allocation. Some compilers offer the
103 // new (nothrow) version, which does exactly what we want. Others
104 // do not. For those that do not, this sets up what exception is thrown,
105 // and then below we'll do a try/catch around the new to catch it and
106 // return a 0 pointer instead.
108 # if defined (__HP_aCC)
109 // I know this works for HP aC++... if <stdexcept> is used, it
110 // introduces other stuff that breaks things, like <memory>, which
111 // screws up auto_ptr.
112 # include /**/ <new>
113 // _HP_aCC was first defined at aC++ 03.13 on HP-UX 11. Prior to that
114 // (03.10 and before) a failed new threw bad_alloc. After that (03.13
115 // and above) the exception thrown is dependent on the below settings.
116 # if (HPUX_VERS >= 1100)
117 # if ((__HP_aCC < 32500 && !defined (RWSTD_NO_NAMESPACE)) || \
118 defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB))
119 # define ACE_bad_alloc ::std::bad_alloc
120 # define ACE_nothrow ::std::nothrow
121 # define ACE_nothrow_t ::std::nothrow_t
122 # else
123 # define ACE_bad_alloc bad_alloc
124 # define ACE_nothrow nothrow
125 # define ACE_nothrow_t nothrow_t
126 # endif /* __HP_aCC */
127 # elif ((__HP_aCC < 12500 && !defined (RWSTD_NO_NAMESPACE)) || \
128 defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB))
129 # define ACE_bad_alloc ::std::bad_alloc
130 # define ACE_nothrow ::std::nothrow
131 # define ACE_nothrow_t ::std::nothrow_t
132 # else
133 # define ACE_bad_alloc bad_alloc
134 # define ACE_nothrow nothrow
135 # define ACE_nothrow_t nothrow_t
136 # endif /* HPUX_VERS < 1100 */
137 # define ACE_throw_bad_alloc throw ACE_bad_alloc ()
138 # elif defined (__SUNPRO_CC)
139 # if (__SUNPRO_CC < 0x500) || (__SUNPRO_CC_COMPAT == 4)
140 # include /**/ <exception.h>
141 // Note: we catch ::xalloc rather than just xalloc because of
142 // a name clash with unsafe_ios::xalloc()
143 # define ACE_bad_alloc ::xalloc
144 # define ACE_throw_bad_alloc throw ACE_bad_alloc ("no more memory")
145 # else
146 # include /**/ <new>
147 # define ACE_bad_alloc ::std::bad_alloc
148 # if defined (ACE_HAS_NEW_NOTHROW)
149 # if defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)
150 # define ACE_nothrow ::std::nothrow
151 # define ACE_nothrow_t ::std::nothrow_t
152 # else
153 # define ACE_nothrow nothrow
154 # define ACE_nothrow_t nothrow_t
155 # endif /* ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB */
156 # endif /* ACE_HAS_NEW_NOTHROW */
157 # define ACE_throw_bad_alloc throw ACE_bad_alloc ()
158 # endif /* __SUNPRO_CC < 0x500 */
159 # elif defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)
160 # include /**/ <new>
161 # if !defined (ACE_bad_alloc)
162 # define ACE_bad_alloc ::std::bad_alloc
163 # endif
164 # define ACE_nothrow ::std::nothrow
165 # define ACE_nothrow_t ::std::nothrow_t
166 // MFC changes the behavior of operator new at all MSVC versions from 6 up.
167 # if defined (ACE_HAS_MFC) && (ACE_HAS_MFC == 1)
168 # define ACE_throw_bad_alloc AfxThrowMemoryException ()
169 # else
170 # define ACE_throw_bad_alloc throw ACE_bad_alloc ()
171 # endif
172 # else
173 # include /**/ <new>
174 # if !defined (ACE_bad_alloc)
175 # define ACE_bad_alloc bad_alloc
176 # endif
177 # define ACE_nothrow nothrow
178 # define ACE_nothrow_t nothrow_t
179 // MFC changes the behavior of operator new at all MSVC versions from 6 up.
180 # if defined (ACE_HAS_MFC) && (ACE_HAS_MFC == 1)
181 # define ACE_throw_bad_alloc AfxThrowMemoryException ()
182 # else
183 # define ACE_throw_bad_alloc throw ACE_bad_alloc ()
184 # endif
185 # endif /* __HP_aCC */
187 # if defined (ACE_HAS_NEW_NOTHROW)
188 # define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
189 do { POINTER = new (ACE_nothrow) CONSTRUCTOR; \
190 if (POINTER == 0) { errno = ENOMEM; return RET_VAL; } \
191 } while (0)
192 # define ACE_NEW(POINTER,CONSTRUCTOR) \
193 do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
194 if (POINTER == 0) { errno = ENOMEM; return; } \
195 } while (0)
196 # define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
197 do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
198 if (POINTER == 0) { errno = ENOMEM; } \
199 } while (0)
201 # else
203 # define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
204 do { try { POINTER = new CONSTRUCTOR; } \
205 catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; return RET_VAL; } \
206 } while (0)
208 # define ACE_NEW(POINTER,CONSTRUCTOR) \
209 do { try { POINTER = new CONSTRUCTOR; } \
210 catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; return; } \
211 } while (0)
213 # define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
214 do { try { POINTER = new CONSTRUCTOR; } \
215 catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; } \
216 } while (0)
217 # endif /* ACE_HAS_NEW_NOTHROW */
219 #else /* ACE_NEW_THROWS_EXCEPTIONS */
221 # define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
222 do { POINTER = new CONSTRUCTOR; \
223 if (POINTER == 0) { errno = ENOMEM; return RET_VAL; } \
224 } while (0)
225 # define ACE_NEW(POINTER,CONSTRUCTOR) \
226 do { POINTER = new CONSTRUCTOR; \
227 if (POINTER == 0) { errno = ENOMEM; return; } \
228 } while (0)
229 # define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
230 do { POINTER = new CONSTRUCTOR; \
231 if (POINTER == 0) { errno = ENOMEM; } \
232 } while (0)
234 # define ACE_throw_bad_alloc \
235 void* gcc_will_complain_if_literal_0_is_returned = 0; \
236 return gcc_will_complain_if_literal_0_is_returned
238 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
240 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
241 //@{
243 * @name Efficiently compute aligned pointers to powers of 2 boundaries.
247 * Efficiently align "value" up to "alignment", knowing that all such
248 * boundaries are binary powers and that we're using two's complement
249 * arithmetic.
251 * Since the alignment is a power of two its binary representation is:
253 * alignment = 0...010...0
255 * hence
257 * alignment - 1 = 0...001...1 = T1
259 * so the complement is:
261 * ~(alignment - 1) = 1...110...0 = T2
263 * Notice that there is a multiple of <alignment> in the range
264 * [<value>,<value> + T1], also notice that if
266 * X = ( <value> + T1 ) & T2
268 * then
270 * <value> <= X <= <value> + T1
272 * because the & operator only changes the last bits, and since X is a
273 * multiple of <alignment> (its last bits are zero) we have found the
274 * multiple we wanted.
276 /// Return the next integer aligned to a required boundary
278 * @param ptr the base pointer
279 * @param alignment the required alignment
281 #if defined (ACE_OPENVMS) && (!defined (__INITIAL_POINTER_SIZE) || (__INITIAL_POINTER_SIZE < 64))
282 inline unsigned int
283 ACE_align_binary (unsigned int ptr, unsigned int alignment)
285 unsigned int const tmp = alignment - 1;
286 return (ptr + tmp) & (~tmp);
288 #else
289 inline uintptr_t
290 ACE_align_binary (uintptr_t ptr, uintptr_t alignment)
292 uintptr_t const tmp = alignment - 1;
293 return (ptr + tmp) & (~tmp);
295 #endif
297 #if defined (ACE_OPENVMS) && (!defined (__INITIAL_POINTER_SIZE) || (__INITIAL_POINTER_SIZE < 64))
298 /// Return the next address aligned to a required boundary
299 inline char *
300 ACE_ptr_align_binary (char const * ptr, unsigned int alignment)
302 return
303 reinterpret_cast<char *> (
304 ACE_align_binary (reinterpret_cast<unsigned int> (ptr), alignment));
307 /// Return the next address aligned to a required boundary
308 inline char *
309 ACE_ptr_align_binary (unsigned char const * ptr, unsigned int alignment)
311 return
312 ACE_ptr_align_binary (reinterpret_cast<char const *> (ptr), alignment);
314 #else
315 /// Return the next address aligned to a required boundary
316 inline char *
317 ACE_ptr_align_binary (char const * ptr, uintptr_t alignment)
319 return
320 reinterpret_cast<char *> (
321 ACE_align_binary (reinterpret_cast<uintptr_t> (ptr), alignment));
324 /// Return the next address aligned to a required boundary
325 inline char *
326 ACE_ptr_align_binary (unsigned char const * ptr, uintptr_t alignment)
328 return
329 ACE_ptr_align_binary (reinterpret_cast<char const *> (ptr), alignment);
331 #endif /* ACE_OPENVMS && __INITIAL_POINTER_SIZE < 64 */
332 //@}
333 ACE_END_VERSIONED_NAMESPACE_DECL
335 #include "ace/OS_NS_stdlib.h"
337 #include /**/ "ace/post.h"
338 #endif /* ACE_OS_MEMORY_H */