| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#pragma once |
| 23 |
|
| 24 |
#include <stdatomic.h> |
| 25 |
|
| 26 |
#include "dex-compat-private.h" |
| 27 |
#include "dex-object.h" |
| 28 |
|
| 29 |
G_BEGIN_DECLS |
| 30 |
|
| 31 |
#define DEX_OBJECT_CLASS(klass) G_TYPE_CHECK_CLASS_CAST(klass, DEX_TYPE_OBJECT, DexObjectClass) |
| 32 |
#define DEX_OBJECT_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS(obj, DEX_TYPE_OBJECT, DexObjectClass) |
| 33 |
|
| 34 |
#define _DEX_DEFINE_TYPE(ClassName, class_name, PARENT_TYPE, Flags) \ |
| 35 |
static void G_PASTE(class_name, _class_init) (G_PASTE (ClassName, Class) *); \ |
| 36 |
static void G_PASTE(class_name, _init) (ClassName *); \ |
| 37 |
static gpointer G_PASTE(class_name, _parent_class); \ |
| 38 |
static GType G_PASTE(class_name, _type); \ |
| 39 |
\ |
| 40 |
static void \ |
| 41 |
G_PASTE(class_name, _class_intern_init) (gpointer klass) \ |
| 42 |
{ \ |
| 43 |
G_PASTE (class_name, _parent_class) = g_type_class_peek_parent (klass); \ |
| 44 |
G_PASTE (class_name, _class_init) ((G_PASTE (ClassName, Class) *)klass); \ |
| 45 |
} \ |
| 46 |
\ |
| 47 |
GType G_PASTE(class_name, _get_type) (void) \ |
| 48 |
{ \ |
| 49 |
if (g_once_init_enter (&G_PASTE (class_name, _type))) \ |
| 50 |
{ \ |
| 51 |
GType gtype = g_type_register_static_simple ( \ |
| 52 |
PARENT_TYPE, \ |
| 53 |
g_intern_static_string (G_STRINGIFY (ClassName)), \ |
| 54 |
sizeof (G_PASTE (ClassName, Class)), \ |
| 55 |
(GClassInitFunc) G_PASTE(class_name, _class_intern_init), \ |
| 56 |
sizeof (ClassName), \ |
| 57 |
(GInstanceInitFunc) G_PASTE(class_name, _init), \ |
| 58 |
Flags); \ |
| 59 |
g_once_init_leave (&G_PASTE (class_name, _type), gtype); \ |
| 60 |
} \ |
| 61 |
return G_PASTE(class_name, _type); \ |
| 62 |
} |
| 63 |
#define DEX_DEFINE_ABSTRACT_TYPE(ClassName, class_name, PARENT_TYPE) \ |
| 64 |
_DEX_DEFINE_TYPE(ClassName, class_name, PARENT_TYPE, G_TYPE_FLAG_ABSTRACT) |
| 65 |
#define DEX_DEFINE_DERIVABLE_TYPE(ClassName, class_name, PARENT_TYPE) \ |
| 66 |
_DEX_DEFINE_TYPE(ClassName, class_name, PARENT_TYPE, 0) |
| 67 |
#define DEX_DEFINE_FINAL_TYPE(ClassName, class_name, PARENT_TYPE) \ |
| 68 |
_DEX_DEFINE_TYPE(ClassName, class_name, PARENT_TYPE, G_TYPE_FLAG_FINAL) |
| 69 |
|
| 70 |
#if defined(_MSC_VER) |
| 71 |
# define DEX_ALIGNED_BEGIN(_N) __declspec(align (_N)) |
| 72 |
# define DEX_ALIGNED_END(_N) |
| 73 |
#else |
| 74 |
# define DEX_ALIGNED_BEGIN(_N) |
| 75 |
# define DEX_ALIGNED_END(_N) __attribute__ ((aligned (_N))) |
| 76 |
#endif |
| 77 |
|
| 78 |
typedef struct _DexWeakRef |
| 79 |
{ |
| 80 |
GMutex mutex; |
| 81 |
struct _DexWeakRef *next; |
| 82 |
struct _DexWeakRef *prev; |
| 83 |
gpointer mem_block; |
| 84 |
} DexWeakRef; |
| 85 |
|
| 86 |
void dex_weak_ref_clear (DexWeakRef *weak_ref); |
| 87 |
void dex_weak_ref_init (DexWeakRef *weak_ref, |
| 88 |
gpointer mem_block); |
| 89 |
gpointer dex_weak_ref_get (DexWeakRef *weak_ref); |
| 90 |
void dex_weak_ref_set (DexWeakRef *weak_ref, |
| 91 |
gpointer mem_block); |
| 92 |
|
| 93 |
DEX_ALIGNED_BEGIN (8) |
| 94 |
typedef struct _DexObject |
| 95 |
{ |
| 96 |
GTypeInstance parent_instance; |
| 97 |
GMutex mutex; |
| 98 |
DexWeakRef *weak_refs; |
| 99 |
_Atomic guint weak_refs_watermark; |
| 100 |
_Atomic int ref_count; |
| 101 |
#ifdef HAVE_SYSPROF |
| 102 |
gint64 ctime; |
| 103 |
#endif |
| 104 |
} DexObject |
| 105 |
DEX_ALIGNED_END (8); |
| 106 |
|
| 107 |
static inline void |
| 108 |
dex_object_lock (gpointer data) |
| 109 |
{ |
| 110 |
g_mutex_lock (&DEX_OBJECT (data)->mutex); |
| 111 |
} |
| 112 |
|
| 113 |
static inline void |
| 114 |
dex_object_unlock (gpointer data) |
| 115 |
{ |
| 116 |
g_mutex_unlock (&DEX_OBJECT (data)->mutex); |
| 117 |
} |
| 118 |
|
| 119 |
typedef struct _DexObjectClass |
| 120 |
{ |
| 121 |
GTypeClass parent_class; |
| 122 |
|
| 123 |
void (*finalize) (DexObject *object); |
| 124 |
} DexObjectClass; |
| 125 |
|
| 126 |
DexObject *dex_object_create_instance (GType instance_type); |
| 127 |
|
| 128 |
G_END_DECLS |