14 #ifndef STXXL_UTILS_HEADER
15 #define STXXL_UTILS_HEADER
30 #include <sys/types.h>
33 #ifdef STXXL_BOOST_CONFIG
34 #include <boost/config.hpp>
37 #ifdef STXXL_BOOST_FILESYSTEM
38 #include <boost/filesystem/operations.hpp>
41 #include <stxxl/bits/namespace.h>
42 #include <stxxl/bits/common/log.h>
43 #include <stxxl/bits/common/exceptions.h>
44 #include <stxxl/bits/common/types.h>
45 #include <stxxl/bits/common/timer.h>
46 #include <stxxl/bits/common/is_sorted.h>
49 __STXXL_BEGIN_NAMESPACE
52 inline void UNUSED(
const U &)
56 #define __STXXL_DEPRECATED(x) __declspec(deprecated) x
58 #define __STXXL_DEPRECATED(x) x __attribute__ ((__deprecated__))
63 #define __STXXL_STRING(x) # x
66 #define _STXXL_PRINT(label, outstream, log_stream, message) \
67 { std::ostringstream str_; \
68 str_ << "[" label "] " << message << std::endl; \
69 outstream << str_.str() << std::flush; \
70 stxxl::logger::get_instance()->log_stream() << str_.str() << std::flush; \
73 #define STXXL_MSG(x) _STXXL_PRINT("STXXL-MSG", std::cout, log_stream, x)
75 #define STXXL_ERRMSG(x) _STXXL_PRINT("STXXL-ERRMSG", std::cerr, errlog_stream, x)
78 #ifdef STXXL_FORCE_VERBOSE_LEVEL
79 #undef STXXL_VERBOSE_LEVEL
80 #define STXXL_VERBOSE_LEVEL STXXL_FORCE_VERBOSE_LEVEL
83 #ifdef STXXL_DEFAULT_VERBOSE_LEVEL
84 #ifndef STXXL_VERBOSE_LEVEL
85 #define STXXL_VERBOSE_LEVEL STXXL_DEFAULT_VERBOSE_LEVEL
89 #ifndef STXXL_VERBOSE_LEVEL
90 #define STXXL_VERBOSE_LEVEL -1
97 #if STXXL_VERBOSE_LEVEL > -1
98 #define STXXL_VERBOSE0(x) _STXXL_PRINT("STXXL-VERBOSE0", std::cout, log_stream, x)
100 #define STXXL_VERBOSE0(x)
103 #if STXXL_VERBOSE_LEVEL > 0
104 #define STXXL_VERBOSE1(x) _STXXL_PRINT("STXXL-VERBOSE1", std::cout, log_stream, x)
106 #define STXXL_VERBOSE1(x)
109 #define STXXL_VERBOSE(x) STXXL_VERBOSE1(x)
111 #if STXXL_VERBOSE_LEVEL > 1
112 #define STXXL_VERBOSE2(x) _STXXL_PRINT("STXXL-VERBOSE2", std::cout, log_stream, x)
114 #define STXXL_VERBOSE2(x)
117 #if STXXL_VERBOSE_LEVEL > 2
118 #define STXXL_VERBOSE3(x) _STXXL_PRINT("STXXL-VERBOSE3", std::cout, log_stream, x)
120 #define STXXL_VERBOSE3(x)
126 #define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
128 #define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
131 #define STXXL_FORMAT_ERROR_MSG(str_, errmsg_) \
132 std::ostringstream str_; str_ << "Error in " << errmsg_
134 #define STXXL_THROW(exception_type, location, error_message) \
136 std::ostringstream msg_; \
137 msg_ << "Error in " << location << ": " << error_message; \
138 throw exception_type(msg_.str()); \
141 #define STXXL_THROW2(exception_type, error_message) \
142 STXXL_THROW(exception_type, "function " << STXXL_PRETTY_FUNCTION_NAME, \
143 "Info: " << error_message << " " << strerror(errno))
145 template <
typename E>
146 inline void stxxl_util_function_error(
const char * func_name,
const char * expr = 0,
const char * error = 0)
148 std::ostringstream str_;
149 str_ <<
"Error in function " << func_name <<
" " << (expr ? expr : strerror(errno));
151 str_ <<
" " << error;
155 #define stxxl_function_error(exception_type) \
156 stxxl::stxxl_util_function_error<exception_type>(STXXL_PRETTY_FUNCTION_NAME)
158 template <
typename E>
159 inline bool helper_check_success(
bool success,
const char * func_name,
const char * expr = 0,
const char * error = 0)
162 stxxl_util_function_error<E>(func_name, expr, error);
166 template <
typename E,
typename INT>
167 inline bool helper_check_eq_0(INT res,
const char * func_name,
const char * expr,
bool res_2_strerror =
false)
169 return helper_check_success<E>(res == 0, func_name, expr, res_2_strerror ? strerror(res) : 0);
172 #define check_pthread_call(expr) \
173 stxxl::helper_check_eq_0<stxxl::resource_error>(expr, STXXL_PRETTY_FUNCTION_NAME, __STXXL_STRING(expr), true)
175 template <
typename E,
typename INT>
176 inline bool helper_check_ge_0(INT res,
const char * func_name)
178 return helper_check_success<E>(res >= 0, func_name);
181 #define stxxl_check_ge_0(expr, exception_type) \
182 stxxl::helper_check_ge_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
184 template <
typename E,
typename INT>
185 inline bool helper_check_ne_0(INT res,
const char * func_name)
187 return helper_check_success<E>(res != 0, func_name);
190 #define stxxl_check_ne_0(expr, exception_type) \
191 stxxl::helper_check_ne_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
195 #define stxxl_win_lasterror_exit(errmsg, exception_type) \
199 DWORD dw = GetLastError(); \
201 FORMAT_MESSAGE_ALLOCATE_BUFFER | \
202 FORMAT_MESSAGE_FROM_SYSTEM, \
205 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
208 std::ostringstream str_; \
209 str_ << "Error in " << errmsg << ", error code " << dw << ": " << ((char *)lpMsgBuf); \
210 LocalFree(lpMsgBuf); \
211 throw exception_type(str_.str()); \
219 stxxl_tmpfilename(std::string dir, std::string prefix)
226 #ifndef STXXL_BOOST_FILESYSTEM
233 sprintf(buffer,
"%d", rnd);
234 result = dir + prefix + buffer;
236 #ifdef STXXL_BOOST_FILESYSTEM
237 while (boost::filesystem::exists(result));
241 while (!lstat(result.c_str(), &st));
244 stxxl_function_error(io_error);
252 inline std::vector<std::string>
253 split(
const std::string & str,
const std::string & sep)
255 std::vector<std::string> result;
259 std::string::size_type CurPos(0), LastPos(0);
262 CurPos = str.find(sep, LastPos);
263 if (CurPos == std::string::npos)
268 std::string::size_type(CurPos -
271 result.push_back(sub);
273 LastPos = CurPos + sep.size();
276 std::string sub = str.substr(LastPos);
278 result.push_back(sub);
285 #define str2int(str) atoi(str.c_str())
287 inline std::string int2str(
int i)
290 sprintf(buf,
"%d", i);
291 return std::string(buf);
294 inline stxxl::int64 atoint64(
const char * s)
305 #define STXXL_MIN(a, b) ((std::min)(a, b))
306 #define STXXL_MAX(a, b) ((std::max)(a, b))
308 #define STXXL_L2_SIZE (512 * 1024)
310 #define div_and_round_up(a, b) ((a) / (b) + !(!((a) % (b))))
312 #define log2(x) (log(x) / log(2.))
318 #ifdef HAVE_BUILTIN_EXPECT
319 #define LIKELY(c) __builtin_expect((c), 1)
324 #ifdef HAVE_BUILTIN_EXPECT
325 #define UNLIKELY(c) __builtin_expect((c), 0)
327 #define UNLIKELY(c) c
332 inline uint64 longhash1(uint64 key_)
334 key_ += ~(key_ << 32);
335 key_ ^= (key_ >> 22);
336 key_ += ~(key_ << 13);
339 key_ ^= (key_ >> 15);
340 key_ += ~(key_ << 27);
341 key_ ^= (key_ >> 31);
348 inline void swap_1D_arrays(T * a, T * b, unsigned_type size)
350 for (unsigned_type i = 0; i < size; ++i)
351 std::swap(a[i], b[i]);
359 template <
typename T,
typename U>
360 struct new_alloc_rebind;
362 template <
typename T>
363 struct new_alloc_rebind<T, T>{
364 typedef new_alloc<T> other;
367 template <
typename T,
typename U>
368 struct new_alloc_rebind {
369 typedef std::allocator<U> other;
378 typedef T value_type;
380 typedef const T * const_pointer;
381 typedef T & reference;
382 typedef const T & const_reference;
383 typedef std::size_t size_type;
384 typedef std::ptrdiff_t difference_type;
389 typedef typename new_alloc_rebind<T, U>::other other;
393 pointer address(reference value)
const
397 const_pointer address(const_reference value)
const
402 new_alloc() throw () { }
403 new_alloc(
const new_alloc &) throw () { }
405 new_alloc(
const new_alloc<U> &) throw () { }
406 ~new_alloc() throw () { }
409 operator std::allocator<U>()
411 static std::allocator<U> helper_allocator;
412 return helper_allocator;
416 size_type max_size()
const throw ()
418 return (std::numeric_limits<std::size_t>::max) () /
sizeof(T);
422 pointer allocate(size_type num,
const void * = 0)
424 pointer ret = (pointer)(T::operator
new (num *
sizeof(T)));
429 void construct(pointer p,
const T & value)
432 new ((
void *)p)T(value);
436 void destroy(pointer p)
443 void deallocate(pointer p, size_type )
445 T::operator
delete ((
void *)p);
450 template <
class T1,
class T2>
451 inline bool operator == (
const new_alloc<T1> &,
452 const new_alloc<T2> &) throw ()
457 template <
class T1,
class T2>
458 inline bool operator != (
const new_alloc<T1> &,
459 const new_alloc<T2> &) throw ()
464 __STXXL_END_NAMESPACE
466 #endif // !STXXL_UTILS_HEADER