gfx/graphite2/src/inc/Main.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/inc/Main.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2010, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street, 
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +#pragma once
    1.31 +
    1.32 +#include <cstdlib>
    1.33 +#include "graphite2/Types.h"
    1.34 +
    1.35 +#ifdef GRAPHITE2_CUSTOM_HEADER
    1.36 +#include GRAPHITE2_CUSTOM_HEADER
    1.37 +#endif
    1.38 +
    1.39 +namespace graphite2 {
    1.40 +
    1.41 +typedef gr_uint8        uint8;
    1.42 +typedef gr_uint8        byte;
    1.43 +typedef gr_uint16       uint16;
    1.44 +typedef gr_uint32       uint32;
    1.45 +typedef gr_int8         int8;
    1.46 +typedef gr_int16        int16;
    1.47 +typedef gr_int32        int32;
    1.48 +typedef size_t          uintptr;
    1.49 +
    1.50 +#ifdef GRAPHITE2_TELEMETRY
    1.51 +struct telemetry
    1.52 +{
    1.53 +    class category;
    1.54 +
    1.55 +    static size_t   * _category;
    1.56 +    static void set_category(size_t & t) throw()    { _category = &t; }
    1.57 +    static void stop() throw()                      { _category = 0; }
    1.58 +    static void count_bytes(size_t n) throw()       { if (_category) *_category += n; }
    1.59 +
    1.60 +    size_t  misc,
    1.61 +            silf,
    1.62 +            glyph,
    1.63 +            code,
    1.64 +            states,
    1.65 +            starts,
    1.66 +            transitions;
    1.67 +
    1.68 +    telemetry() : misc(0), silf(0), glyph(0), code(0), states(0), starts(0), transitions(0) {}
    1.69 +};
    1.70 +
    1.71 +class telemetry::category
    1.72 +{
    1.73 +    size_t * _prev;
    1.74 +public:
    1.75 +    category(size_t & t) : _prev(_category) { _category = &t; }
    1.76 +    ~category() { _category = _prev; }
    1.77 +};
    1.78 +
    1.79 +#else
    1.80 +struct telemetry  {};
    1.81 +#endif
    1.82 +
    1.83 +// typesafe wrapper around malloc for simple types
    1.84 +// use free(pointer) to deallocate
    1.85 +
    1.86 +template <typename T> T * gralloc(size_t n)
    1.87 +{
    1.88 +#ifdef GRAPHITE2_TELEMETRY
    1.89 +    telemetry::count_bytes(sizeof(T) * n);
    1.90 +#endif
    1.91 +    return reinterpret_cast<T*>(malloc(sizeof(T) * n));
    1.92 +}
    1.93 +
    1.94 +template <typename T> T * grzeroalloc(size_t n)
    1.95 +{
    1.96 +#ifdef GRAPHITE2_TELEMETRY
    1.97 +    telemetry::count_bytes(sizeof(T) * n);
    1.98 +#endif
    1.99 +    return reinterpret_cast<T*>(calloc(n, sizeof(T)));
   1.100 +}
   1.101 +
   1.102 +template <typename T>
   1.103 +inline T min(const T a, const T b)
   1.104 +{
   1.105 +    return a < b ? a : b;
   1.106 +}
   1.107 +
   1.108 +template <typename T>
   1.109 +inline T max(const T a, const T b)
   1.110 +{
   1.111 +    return a > b ? a : b;
   1.112 +}
   1.113 +
   1.114 +} // namespace graphite2
   1.115 +
   1.116 +#define CLASS_NEW_DELETE \
   1.117 +    void * operator new   (size_t size){ return gralloc<byte>(size);} \
   1.118 +    void * operator new   (size_t, void * p) throw() { return p; } \
   1.119 +    void * operator new[] (size_t size) {return gralloc<byte>(size);} \
   1.120 +    void * operator new[] (size_t, void * p) throw() { return p; } \
   1.121 +    void operator delete   (void * p) throw() { free(p);} \
   1.122 +    void operator delete   (void *, void *) throw() {} \
   1.123 +    void operator delete[] (void * p)throw() { free(p); } \
   1.124 +    void operator delete[] (void *, void *) throw() {}
   1.125 +
   1.126 +#ifdef __GNUC__
   1.127 +#define GR_MAYBE_UNUSED __attribute__((unused))
   1.128 +#else
   1.129 +#define GR_MAYBE_UNUSED
   1.130 +#endif

mercurial