[HB] Simplify refcounting functions

This commit is contained in:
Behdad Esfahbod 2009-08-01 19:30:31 -04:00
parent 23af767a44
commit 35a7383c61
4 changed files with 44 additions and 23 deletions

View File

@ -40,14 +40,14 @@ struct _hb_blob_t {
void *user_data;
};
static hb_blob_t _hb_blob_nil = {
HB_REFERENCE_COUNT_INVALID,
HB_REFERENCE_COUNT_INVALID, /* ref_count */
NULL,
0,
HB_MEMORY_MODE_READONLY,
NULL, /* data */
0, /* len */
HB_MEMORY_MODE_READONLY, /* mode */
NULL,
NULL
NULL, /* destroy */
NULL /* user_data */
};
static void
@ -76,11 +76,12 @@ hb_blob_create (const char *data,
return &_hb_blob_nil;
}
HB_REFERENCE_COUNT_DO_CREATE (blob);
blob->data = data;
blob->len = len;
blob->mode = mode;
HB_REFERENCE_COUNT_INIT (blob->ref_count, 1);
blob->destroy = destroy;
blob->user_data = user_data;
@ -95,26 +96,13 @@ hb_blob_create (const char *data,
hb_blob_t *
hb_blob_reference (hb_blob_t *blob)
{
if (blob == NULL || HB_REFERENCE_COUNT_IS_INVALID (blob->ref_count))
return blob;
assert (HB_REFERENCE_COUNT_HAS_REFERENCE (blob->ref_count));
_hb_reference_count_inc (blob->ref_count);
return blob;
HB_REFERENCE_COUNT_DO_REFERENCE (blob);
}
void
hb_blob_destroy (hb_blob_t *blob)
{
if (blob == NULL || HB_REFERENCE_COUNT_IS_INVALID (blob->ref_count))
return;
assert (HB_REFERENCE_COUNT_HAS_REFERENCE (blob->ref_count));
if (!_hb_reference_count_dec_and_test (blob->ref_count))
return;
HB_REFERENCE_COUNT_DO_DESTROY (blob);
_hb_blob_destroy_user_data (blob);

View File

@ -59,4 +59,6 @@ typedef struct _hb_unicode_callbacks_t hb_unicode_callbacks_t;
typedef struct _hb_face_t hb_face_t;
typedef struct _hb_font_t hb_font_t;
typedef hb_blob_t * (*hb_get_table_func_t) (hb_tag_t tag, void *user_data);
#endif /* HB_COMMON_H */

View File

@ -73,6 +73,8 @@
# define TRUE 1
#endif
#define HB_STMT_START do
#define HB_STMT_END while (0)
#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))

View File

@ -1,5 +1,6 @@
/*
* Copyright © 2007 Chris Wilson
* Copyright (C) 2007 Chris Wilson
* Copyright (C) 2009 Red Hat, Inc.
*
* This is part of HarfBuzz, an OpenType Layout engine library.
*
@ -23,6 +24,7 @@
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
* Red Hat Author(s): Behdad Esfahbod
*/
#ifndef HB_REFCOUNT_PRIVATE_H
@ -51,4 +53,31 @@ typedef struct {
#define HB_REFERENCE_COUNT_HAS_REFERENCE(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) > 0)
/* Helper macros */
#define HB_REFERENCE_COUNT_DO_CREATE(obj) \
HB_STMT_START { \
HB_REFERENCE_COUNT_INIT (obj->ref_count, 1); \
} HB_STMT_END
#define HB_REFERENCE_COUNT_DO_REFERENCE(obj) \
HB_STMT_START { \
if (obj == NULL || HB_REFERENCE_COUNT_IS_INVALID (obj->ref_count)) \
return obj; \
assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
_hb_reference_count_inc (obj->ref_count); \
return obj; \
} HB_STMT_END
#define HB_REFERENCE_COUNT_DO_DESTROY(obj) \
HB_STMT_START { \
if (obj == NULL || HB_REFERENCE_COUNT_IS_INVALID (obj->ref_count)) \
return; \
assert (HB_REFERENCE_COUNT_HAS_REFERENCE (obj->ref_count)); \
if (!_hb_reference_count_dec_and_test (obj->ref_count)) \
return; \
} HB_STMT_END
#endif /* HB_REFCOUNT_PRIVATE_H */