Main.h (5644B)
1 /* GRAPHITE2 LICENSING 2 3 Copyright 2010, SIL International 4 All rights reserved. 5 6 This library is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published 8 by the Free Software Foundation; either version 2.1 of License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should also have received a copy of the GNU Lesser General Public 17 License along with this library in the file named "LICENSE". 18 If not, write to the Free Software Foundation, 51 Franklin Street, 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 20 internet at http://www.fsf.org/licenses/lgpl.html. 21 22 Alternatively, the contents of this file may be used under the terms of the 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 24 License, as published by the Free Software Foundation, either version 2 25 of the License or (at your option) any later version. 26 */ 27 #pragma once 28 29 #include <cstdlib> 30 #include "graphite2/Types.h" 31 32 #ifdef GRAPHITE2_CUSTOM_HEADER 33 #include GRAPHITE2_CUSTOM_HEADER 34 #endif 35 36 namespace graphite2 { 37 38 typedef gr_uint8 uint8; 39 typedef gr_uint8 byte; 40 typedef gr_uint16 uint16; 41 typedef gr_uint32 uint32; 42 typedef gr_int8 int8; 43 typedef gr_int16 int16; 44 typedef gr_int32 int32; 45 typedef size_t uintptr; 46 47 #ifdef GRAPHITE2_TELEMETRY 48 struct telemetry 49 { 50 class category; 51 52 static size_t * _category; 53 static void set_category(size_t & t) throw() { _category = &t; } 54 static void stop() throw() { _category = 0; } 55 static void count_bytes(size_t n) throw() { if (_category) *_category += n; } 56 57 size_t misc, 58 silf, 59 glyph, 60 code, 61 states, 62 starts, 63 transitions; 64 65 telemetry() : misc(0), silf(0), glyph(0), code(0), states(0), starts(0), transitions(0) {} 66 }; 67 68 class telemetry::category 69 { 70 size_t * _prev; 71 public: 72 category(size_t & t) : _prev(_category) { _category = &t; } 73 ~category() { _category = _prev; } 74 }; 75 76 #else 77 struct telemetry {}; 78 #endif 79 80 // Checked multiplaction to catch overflow or underflow when allocating memory 81 #if defined(__has_builtin) 82 #if __has_builtin(__builtin_mul_overflow) 83 #define HAVE_BUILTIN_OVERFLOW 84 #endif 85 #elif defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__INTEL_COMPILER) 86 #define HAVE_BUILTIN_OVERFLOW 87 #endif 88 #if defined(__has_include) 89 #if __has_include(<intsafe.h>) && !defined(__CYGWIN__) 90 #define HAVE_INTSAFE_H 91 #endif 92 #elif defined(_WIN32) 93 #define HAVE_INTSAFE_H 94 #endif 95 96 // Need to import intsafe into the top level namespace 97 #if defined(HAVE_INTSAFE_H) 98 } // namespace graphite2 99 100 #include <intsafe.h> 101 102 namespace graphite2 { 103 #endif 104 105 #if defined(HAVE_BUILTIN_OVERFLOW) 106 inline 107 bool checked_mul(const size_t a, const size_t b, size_t & t) { 108 return __builtin_mul_overflow(a, b, &t); 109 } 110 #elif defined(HAVE_INTSAFE_H) 111 inline 112 bool checked_mul(const size_t a, const size_t b, size_t & t) { 113 return SizeTMult(a, b, &t) == INTSAFE_E_ARITHMETIC_OVERFLOW; 114 } 115 #else 116 inline 117 bool checked_mul(const size_t a, const size_t b, size_t & t) { 118 t = a*b; 119 return (((a | b) & (~size_t(0) << (sizeof(size_t) << 2))) && (t / a != b)); 120 } 121 #endif 122 123 // typesafe wrapper around malloc for simple types 124 // use free(pointer) to deallocate 125 126 template <typename T> T * gralloc(size_t n) 127 { 128 size_t total; 129 if (checked_mul(n, sizeof(T), total)) 130 return 0; 131 #ifdef GRAPHITE2_TELEMETRY 132 telemetry::count_bytes(total); 133 #endif 134 return static_cast<T*>(malloc(total)); 135 } 136 137 template <typename T> T * grzeroalloc(size_t n) 138 { 139 #ifdef GRAPHITE2_TELEMETRY 140 telemetry::count_bytes(sizeof(T) * n); 141 #endif 142 return static_cast<T*>(calloc(n, sizeof(T))); 143 } 144 145 template <typename T> 146 inline T min(const T a, const T b) 147 { 148 return a < b ? a : b; 149 } 150 151 template <typename T> 152 inline T max(const T a, const T b) 153 { 154 return a > b ? a : b; 155 } 156 157 } // namespace graphite2 158 159 #define CLASS_NEW_DELETE \ 160 void * operator new (size_t size){ return gralloc<byte>(size);} \ 161 void * operator new (size_t, void * p) throw() { return p; } \ 162 void * operator new[] (size_t size) {return gralloc<byte>(size);} \ 163 void * operator new[] (size_t, void * p) throw() { return p; } \ 164 void operator delete (void * p) throw() { free(p);} \ 165 void operator delete (void *, void *) throw() {} \ 166 void operator delete[] (void * p)throw() { free(p); } \ 167 void operator delete[] (void *, void *) throw() {} 168 169 #if defined(__GNUC__) || defined(__clang__) 170 #define GR_MAYBE_UNUSED __attribute__((unused)) 171 #else 172 #define GR_MAYBE_UNUSED 173 #endif 174 175 #ifndef __has_cpp_attribute 176 # define __has_cpp_attribute(x) 0 177 #endif 178 179 #if __has_cpp_attribute(clang::fallthrough) 180 # define GR_FALLTHROUGH [[clang::fallthrough]] 181 #elif __has_cpp_attribute(gnu::fallthrough) 182 # define GR_FALLTHROUGH [[gnu::fallthrough]] 183 #elif defined(_MSC_VER) 184 /* 185 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis): 186 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx 187 */ 188 #include <sal.h> 189 #define GR_FALLTHROUGH __fallthrough 190 #elif __GNUC__ >= 7 191 #define GR_FALLTHROUGH __attribute__ ((fallthrough)) 192 #else 193 #define GR_FALLTHROUGH /* fallthrough */ 194 #endif 195 196 #ifdef _MSC_VER 197 #pragma warning(disable: 4800) 198 #pragma warning(disable: 4355) 199 #endif