2009-07-30 21:33:57 +02:00
|
|
|
/*
|
2011-04-21 23:14:28 +02:00
|
|
|
* Copyright © 2007 Chris Wilson
|
|
|
|
* Copyright © 2009,2010 Red Hat, Inc.
|
|
|
|
* Copyright © 2011 Google, Inc.
|
2009-07-30 21:33:57 +02:00
|
|
|
*
|
2010-04-22 06:11:43 +02:00
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
2009-07-30 21:33:57 +02:00
|
|
|
*
|
|
|
|
* 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>
|
2009-08-02 01:30:31 +02:00
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
2011-04-21 23:14:28 +02:00
|
|
|
* Google Author(s): Behdad Esfahbod
|
2009-07-30 21:33:57 +02:00
|
|
|
*/
|
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#ifndef HB_OBJECT_PRIVATE_HH
|
|
|
|
#define HB_OBJECT_PRIVATE_HH
|
2009-07-30 21:33:57 +02:00
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#include "hb-private.hh"
|
2009-08-04 03:40:20 +02:00
|
|
|
|
2010-07-23 21:11:18 +02:00
|
|
|
HB_BEGIN_DECLS
|
2009-11-06 01:37:58 +01:00
|
|
|
|
|
|
|
|
2010-04-28 19:29:55 +02:00
|
|
|
/* Debug */
|
|
|
|
|
|
|
|
#ifndef HB_DEBUG_OBJECT
|
2010-11-03 20:11:04 +01:00
|
|
|
#define HB_DEBUG_OBJECT (HB_DEBUG+0)
|
2010-04-28 19:29:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
typedef struct _hb_object_header_t hb_object_header_t;
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
struct _hb_object_header_t {
|
|
|
|
hb_reference_count_t ref_count;
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
static inline void *create (unsigned int size) {
|
|
|
|
hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
|
2009-08-02 01:30:31 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
if (likely (obj))
|
|
|
|
obj->init ();
|
2009-08-02 01:54:49 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
return obj;
|
|
|
|
}
|
2009-08-02 02:29:22 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Object allocation and lifecycle manamgement macros */
|
|
|
|
|
|
|
|
#define TRACE_OBJECT(obj) \
|
|
|
|
obj->header.trace (__FUNCTION__)
|
|
|
|
|
|
|
|
#define HB_OBJECT_IS_INERT(obj) \
|
|
|
|
(unlikely ((obj)->header.is_inert ()))
|
2009-08-02 01:30:31 +02:00
|
|
|
|
2009-08-04 04:43:02 +02:00
|
|
|
#define HB_OBJECT_DO_CREATE(Type, obj) \
|
2010-05-04 04:51:19 +02:00
|
|
|
likely (( \
|
2011-04-22 00:24:02 +02:00
|
|
|
((obj) = (Type *) hb_object_header_t::create (sizeof (Type))), \
|
|
|
|
TRACE_OBJECT (obj), \
|
|
|
|
(obj) \
|
|
|
|
))
|
2009-08-02 02:29:22 +02:00
|
|
|
|
2009-08-02 01:54:49 +02:00
|
|
|
#define HB_OBJECT_DO_REFERENCE(obj) \
|
2009-08-02 01:30:31 +02:00
|
|
|
HB_STMT_START { \
|
2010-05-24 19:01:09 +02:00
|
|
|
TRACE_OBJECT (obj); \
|
2011-04-22 00:24:02 +02:00
|
|
|
obj->header.reference (); \
|
2009-08-02 01:30:31 +02:00
|
|
|
return obj; \
|
|
|
|
} HB_STMT_END
|
|
|
|
|
2009-08-02 01:54:49 +02:00
|
|
|
#define HB_OBJECT_DO_DESTROY(obj) \
|
2009-08-02 01:30:31 +02:00
|
|
|
HB_STMT_START { \
|
2010-05-24 19:01:09 +02:00
|
|
|
TRACE_OBJECT (obj); \
|
2011-04-22 00:24:02 +02:00
|
|
|
if (!obj->header.destroy ()) \
|
2009-08-02 01:30:31 +02:00
|
|
|
return; \
|
|
|
|
} HB_STMT_END
|
|
|
|
|
|
|
|
|
2010-07-23 21:11:18 +02:00
|
|
|
HB_END_DECLS
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#endif /* HB_OBJECT_PRIVATE_HH */
|