hb-object-private.hh 3.4 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2 3 4
 * Copyright © 2007  Chris Wilson
 * Copyright © 2009,2010  Red Hat, Inc.
 * Copyright © 2011  Google, Inc.
B
Behdad Esfahbod 已提交
5
 *
B
Behdad Esfahbod 已提交
6
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Contributor(s):
 *	Chris Wilson <chris@chris-wilson.co.uk>
28
 * Red Hat Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
29
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
30 31
 */

32 33
#ifndef HB_OBJECT_PRIVATE_HH
#define HB_OBJECT_PRIVATE_HH
B
Behdad Esfahbod 已提交
34

35
#include "hb-private.hh"
36

B
Behdad Esfahbod 已提交
37
HB_BEGIN_DECLS
38 39


40 41 42
/* Debug */

#ifndef HB_DEBUG_OBJECT
43
#define HB_DEBUG_OBJECT (HB_DEBUG+0)
44 45 46
#endif


47
typedef struct _hb_object_header_t hb_object_header_t;
48

49 50
struct _hb_object_header_t {
  hb_reference_count_t ref_count;
51

52
#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
53

54 55
  static inline void *create (unsigned int size) {
    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
56

57 58
    if (likely (obj))
      obj->init ();
59

60 61
    return obj;
  }
B
Behdad Esfahbod 已提交
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  inline void init (void) {
    ref_count.init (1);
  }

  inline bool is_inert (void) const { return unlikely (ref_count.is_invalid ()); }

  inline void reference (void) {
    if (unlikely (!this || this->is_inert ()))
      return;
    ref_count.inc ();
  }

  inline bool destroy (void) {
    if (unlikely (!this || this->is_inert ()))
      return false;
    return ref_count.dec () == 1;
  }

  inline void trace (const char *function) const {
    (void) (HB_DEBUG_OBJECT &&
	    fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
		     this,
		     this ? ref_count.get () : 0,
		     function));
  }

};


92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
HB_END_DECLS


template <typename Type>
static inline Type *hb_object_create () { return (Type *) hb_object_header_t::create (sizeof (Type)); }

template <typename Type>
static inline bool hb_object_is_inert (const Type *obj) { return unlikely (obj->header.is_inert()); }

template <typename Type>
static inline Type *hb_object_reference (Type *obj) { obj->header.reference (); return obj; }

template <typename Type>
static inline bool hb_object_destroy (Type *obj) { return obj->header.destroy (); }

template <typename Type>
static inline void hb_object_trace (const Type *obj) { obj->header.trace (__FUNCTION__); }


HB_BEGIN_DECLS


114 115
/* Object allocation and lifecycle manamgement macros */

116 117 118 119 120
#define HB_TRACE_OBJECT(obj) hb_object_trace (obj)
#define HB_OBJECT_DO_CREATE(Type, obj) likely (obj = hb_object_create<Type> ())
#define HB_OBJECT_IS_INERT(obj) hb_object_is_inert (obj)
#define HB_OBJECT_DO_REFERENCE(obj) return hb_object_reference (obj)
#define HB_OBJECT_DO_DESTROY(obj) if (!hb_object_destroy (obj)) return
121 122


B
Behdad Esfahbod 已提交
123
HB_END_DECLS
124

125
#endif /* HB_OBJECT_PRIVATE_HH */