libfoedus-core
FOEDUS Core Library
assorted_func.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_ASSORTED_FUNC_HPP_
19 #define FOEDUS_ASSORTED_ASSORTED_FUNC_HPP_
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <string>
24 #include <typeinfo>
25 
26 #include "foedus/assert_nd.hpp"
27 #include "foedus/cxx11.hpp"
29 
30 namespace foedus {
31 namespace assorted {
32 
43 template <typename T, uint64_t ALIGNMENT>
44 inline T align(T value) {
45  uint64_t left = (value + ALIGNMENT - 1);
46  uint64_t right = -ALIGNMENT;
47  uint64_t result = left & right;
48  ASSERT_ND(result >= static_cast<uint64_t>(value));
49  ASSERT_ND(result % ALIGNMENT == 0);
50  ASSERT_ND(result < static_cast<uint64_t>(value) + ALIGNMENT);
51  return static_cast<T>(result);
52 }
53 
58 template <typename T> inline T align8(T value) { return align<T, 8>(value); }
59 
64 template <typename T> inline T align16(T value) { return align<T, 16>(value); }
65 
70 template <typename T> inline T align64(T value) { return align<T, 64>(value); }
71 
72 
77 int64_t int_div_ceil(int64_t dividee, int64_t dividor);
78 
84 std::string replace_all(const std::string& target, const std::string& search,
85  const std::string& replacement);
90 std::string replace_all(const std::string& target, const std::string& search,
91  int replacement);
92 
97 std::string os_error();
98 
103 std::string os_error(int error_number);
104 
111 std::string get_current_executable_path();
112 
124 struct Hex {
125  template<typename T>
126  Hex(T val, int fix_digits = -1) : val_(static_cast<uint64_t>(val)), fix_digits_(fix_digits) {}
127 
128  uint64_t val_;
130  friend std::ostream& operator<<(std::ostream& o, const Hex& v);
131 };
132 
143 struct HexString {
144  HexString(const std::string& str, uint32_t max_bytes = 64U)
145  : str_(str), max_bytes_(max_bytes) {}
146 
147  std::string str_;
148  uint32_t max_bytes_;
149  friend std::ostream& operator<<(std::ostream& o, const HexString& v);
150 };
151 
162 struct Top {
163  Top(const char* data, uint32_t data_len, uint32_t max_bytes = 8)
164  : data_(data), data_len_(data_len), max_bytes_(max_bytes) {}
165 
166  const char* data_;
167  uint32_t data_len_;
168  uint32_t max_bytes_;
169  friend std::ostream& operator<<(std::ostream& o, const Top& v);
170 };
171 
172 
183 void spinlock_yield();
184 
194 template<uint64_t SIZE1, uint64_t SIZE2>
195 inline int static_size_check() {
196  CXX11_STATIC_ASSERT(SIZE1 == SIZE2,
197  "Static Size Check failed. Look for a message like this to see the value of Size1 and "
198  "Size2: 'In instantiation of int foedus::assorted::static_size_check() [with long unsigned"
199  " int SIZE1 = <size1>ul; long unsigned int SIZE2 = <size2>ul]'");
200  return 0;
201 }
202 
207 std::string demangle_type_name(const char* mangled_name);
208 
214 template <typename T>
215 std::string get_pretty_type_name() {
216  return demangle_type_name(typeid(T).name());
217 }
218 
230 uint64_t generate_almost_prime_below(uint64_t threshold);
231 
233 struct SpinlockStat {
234  inline SpinlockStat() : spins_(0) {}
235  void yield_backoff();
236 
237  uint32_t spins_;
239 };
240 
241 } // namespace assorted
242 } // namespace foedus
243 
256 #define SPINLOCK_WHILE(x) \
257  for (foedus::assorted::SpinlockStat __spins; (x); __spins.yield_backoff())
258 
259 // Use __COUNTER__ to generate a unique method name
260 #define STATIC_SIZE_CHECK_CONCAT_DETAIL(x, y) x##y
261 #define STATIC_SIZE_CHECK_CONCAT(x, y) STATIC_SIZE_CHECK_CONCAT_DETAIL(x, y)
262 #define STATIC_SIZE_CHECK_METHOD_NAME \
263  STATIC_SIZE_CHECK_CONCAT(_dummy_static_size_check, __COUNTER__)
264 #define STATIC_SIZE_CHECK(desired, actual) \
265  inline void STATIC_SIZE_CHECK_METHOD_NAME() { \
266  foedus::assorted::static_size_check< desired, actual >();\
267  }
268 
313 #define INSTANTIATE_ALL_INTEGER_TYPES(M) M(int64_t); \
314  M(int32_t); M(int16_t); M(int8_t); M(uint64_t); \
315  M(uint32_t); M(uint16_t); M(uint8_t);
317 #define INSTANTIATE_ALL_INTEGER_PLUS_BOOL_TYPES(M) INSTANTIATE_ALL_INTEGER_TYPES(M);\
318  M(bool);
320 #define INSTANTIATE_ALL_NUMERIC_TYPES(M) INSTANTIATE_ALL_INTEGER_TYPES(M);\
321  M(bool); M(float); M(double);
323 #define INSTANTIATE_ALL_TYPES(M) INSTANTIATE_ALL_NUMERIC_TYPES(M);\
324  M(std::string);
327 #endif // FOEDUS_ASSORTED_ASSORTED_FUNC_HPP_
T align16(T value)
16-alignment.
T align8(T value)
8-alignment.
T align64(T value)
64-alignment.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
friend std::ostream & operator<<(std::ostream &o, const Hex &v)
int static_size_check()
Alternative for static_assert(sizeof(foo) == sizeof(bar), "oh crap") to display sizeof(foo).
std::string get_current_executable_path()
Returns the full path of current executable.
std::string replace_all(const std::string &target, const std::string &search, const std::string &replacement)
target.replaceAll(search, replacement).
Top(const char *data, uint32_t data_len, uint32_t max_bytes=8)
Hex(T val, int fix_digits=-1)
Equivalent to std::hex in case the stream doesn't support it.
#define CXX11_STATIC_ASSERT(expr, message)
Used in public headers in place of "static_assert" of C++11.
Definition: cxx11.hpp:135
void spinlock_yield()
Invoke _mm_pause(), x86 PAUSE instruction, or something equivalent in the env.
Helper for SPINLOCK_WHILE.
Write only first few bytes to stream.
std::string os_error()
Thread-safe strerror(errno).
T align(T value)
Returns the smallest multiply of ALIGNMENT that is equal or larger than the given number...
A very simple and deterministic random generator that is more aligned with standard benchmark such as...
uint64_t generate_almost_prime_below(uint64_t threshold)
Generate a prime or some number that is almost prime less than the given number.
std::string demangle_type_name(const char *mangled_name)
Demangle the given C++ type name if possible (otherwise the original string).
Convenient way of writing hex integers to stream.
int64_t int_div_ceil(int64_t dividee, int64_t dividor)
Efficient ceil(dividee/dividor) for integer.
std::string get_pretty_type_name()
Returns the name of the C++ type as readable as possible.
HexString(const std::string &str, uint32_t max_bytes=64U)
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
friend std::ostream & operator<<(std::ostream &o, const Top &v)
friend std::ostream & operator<<(std::ostream &o, const HexString &v)