Path: blob/master/thirdparty/graphite/src/inc/Main.h
10279 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2010, SIL International, All rights reserved.23#pragma once45#include <cstdlib>6#include "graphite2/Types.h"78#ifdef GRAPHITE2_CUSTOM_HEADER9#include GRAPHITE2_CUSTOM_HEADER10#endif1112namespace graphite2 {1314typedef gr_uint8 uint8;15typedef gr_uint8 byte;16typedef gr_uint16 uint16;17typedef gr_uint32 uint32;18typedef gr_int8 int8;19typedef gr_int16 int16;20typedef gr_int32 int32;21typedef size_t uintptr;2223#ifdef GRAPHITE2_TELEMETRY24struct telemetry25{26class category;2728static size_t * _category;29static void set_category(size_t & t) throw() { _category = &t; }30static void stop() throw() { _category = 0; }31static void count_bytes(size_t n) throw() { if (_category) *_category += n; }3233size_t misc,34silf,35glyph,36code,37states,38starts,39transitions;4041telemetry() : misc(0), silf(0), glyph(0), code(0), states(0), starts(0), transitions(0) {}42};4344class telemetry::category45{46size_t * _prev;47public:48category(size_t & t) : _prev(_category) { _category = &t; }49~category() { _category = _prev; }50};5152#else53struct telemetry {};54#endif5556// Checked multiplaction to catch overflow or underflow when allocating memory57#if defined(__has_builtin)58#if __has_builtin(__builtin_mul_overflow)59#define HAVE_BUILTIN_OVERFLOW60#endif61#elif defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__INTEL_COMPILER)62#define HAVE_BUILTIN_OVERFLOW63#endif64#if defined(__has_include)65#if __has_include(<intsafe.h>) && !defined(__CYGWIN__)66#define HAVE_INTSAFE_H67#endif68#elif defined(_WIN32)69#define HAVE_INTSAFE_H70#endif7172// Need to import intsafe into the top level namespace73#if defined(HAVE_INTSAFE_H)74} // namespace graphite27576#include <intsafe.h>7778namespace graphite2 {79#endif8081#if defined(HAVE_BUILTIN_OVERFLOW)82inline83bool checked_mul(const size_t a, const size_t b, size_t & t) {84return __builtin_mul_overflow(a, b, &t);85}86#elif defined(HAVE_INTSAFE_H)87inline88bool checked_mul(const size_t a, const size_t b, size_t & t) {89return SizeTMult(a, b, &t) == INTSAFE_E_ARITHMETIC_OVERFLOW;90}91#else92inline93bool checked_mul(const size_t a, const size_t b, size_t & t) {94t = a*b;95return (((a | b) & (~size_t(0) << (sizeof(size_t) << 2))) && (t / a != b));96}97#endif9899// typesafe wrapper around malloc for simple types100// use free(pointer) to deallocate101102template <typename T> T * gralloc(size_t n)103{104size_t total;105if (checked_mul(n, sizeof(T), total))106return 0;107#ifdef GRAPHITE2_TELEMETRY108telemetry::count_bytes(total);109#endif110return static_cast<T*>(malloc(total));111}112113template <typename T> T * grzeroalloc(size_t n)114{115#ifdef GRAPHITE2_TELEMETRY116telemetry::count_bytes(sizeof(T) * n);117#endif118return static_cast<T*>(calloc(n, sizeof(T)));119}120121template <typename T>122inline T min(const T a, const T b)123{124return a < b ? a : b;125}126127template <typename T>128inline T max(const T a, const T b)129{130return a > b ? a : b;131}132133} // namespace graphite2134135#define CLASS_NEW_DELETE \136void * operator new (size_t size){ return gralloc<byte>(size);} \137void * operator new (size_t, void * p) throw() { return p; } \138void * operator new[] (size_t size) {return gralloc<byte>(size);} \139void * operator new[] (size_t, void * p) throw() { return p; } \140void operator delete (void * p) throw() { free(p);} \141void operator delete (void *, void *) throw() {} \142void operator delete[] (void * p)throw() { free(p); } \143void operator delete[] (void *, void *) throw() {}144145#if defined(__GNUC__) || defined(__clang__)146#define GR_MAYBE_UNUSED __attribute__((unused))147#else148#define GR_MAYBE_UNUSED149#endif150151#ifndef __has_cpp_attribute152# define __has_cpp_attribute(x) 0153#endif154155#if __has_cpp_attribute(clang::fallthrough)156# define GR_FALLTHROUGH [[clang::fallthrough]]157#elif __has_cpp_attribute(gnu::fallthrough)158# define GR_FALLTHROUGH [[gnu::fallthrough]]159#elif defined(_MSC_VER)160/*161* MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):162* https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx163*/164#include <sal.h>165#define GR_FALLTHROUGH __fallthrough166#elif __GNUC__ >= 7167#define GR_FALLTHROUGH __attribute__ ((fallthrough))168#else169#define GR_FALLTHROUGH /* fallthrough */170#endif171172#ifdef _MSC_VER173#pragma warning(disable: 4800)174#pragma warning(disable: 4355)175#endif176177178