[API] Simplify blob API, remove lock
diff --git a/src/hb-blob.cc b/src/hb-blob.cc
index b903acc..cdb7496 100644
--- a/src/hb-blob.cc
+++ b/src/hb-blob.cc
@@ -28,7 +28,6 @@
#include "hb-blob.h"
#include "hb-object-private.hh"
-#include "hb-mutex-private.hh"
#ifdef HAVE_SYS_MMAN_H
#ifdef HAVE_UNISTD_H
@@ -51,15 +50,11 @@
struct _hb_blob_t {
hb_object_header_t header;
- unsigned int length;
-
- hb_mutex_t lock;
- /* the rest are protected by lock */
-
- unsigned int lock_count;
- hb_memory_mode_t mode;
+ bool immutable;
const char *data;
+ unsigned int length;
+ hb_memory_mode_t mode;
void *user_data;
hb_destroy_func_t destroy;
@@ -68,19 +63,19 @@
static hb_blob_t _hb_blob_nil = {
HB_OBJECT_HEADER_STATIC,
+ TRUE, /* immutable */
+
0, /* length */
-
- HB_MUTEX_INIT, /* lock */
-
- 0, /* lock_count */
- HB_MEMORY_MODE_READONLY, /* mode */
-
NULL, /* data */
+ HB_MEMORY_MODE_READONLY, /* mode */
NULL, /* user_data */
NULL /* destroy */
};
+
+static bool _try_writable (hb_blob_t *blob);
+
static void
_hb_blob_destroy_user_data (hb_blob_t *blob)
{
@@ -91,13 +86,6 @@
}
}
-static void
-_hb_blob_unlock_and_destroy (hb_blob_t *blob)
-{
- hb_blob_unlock (blob);
- hb_blob_destroy (blob);
-}
-
hb_blob_t *
hb_blob_create (const char *data,
unsigned int length,
@@ -113,9 +101,6 @@
return &_hb_blob_nil;
}
- hb_mutex_init (&blob->lock);
- blob->lock_count = 0;
-
blob->data = data;
blob->length = length;
blob->mode = mode;
@@ -125,7 +110,7 @@
if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
blob->mode = HB_MEMORY_MODE_READONLY;
- if (!hb_blob_try_writable (blob)) {
+ if (!_try_writable (blob)) {
hb_blob_destroy (blob);
return &_hb_blob_nil;
}
@@ -140,20 +125,17 @@
unsigned int length)
{
hb_blob_t *blob;
- const char *pdata;
if (!length || offset >= parent->length || !(blob = hb_object_create<hb_blob_t> ()))
return &_hb_blob_nil;
- pdata = hb_blob_lock (parent);
+ hb_blob_make_immutable (parent);
- hb_mutex_lock (&parent->lock);
- blob = hb_blob_create (pdata + offset,
+ blob = hb_blob_create (parent->data + offset,
MIN (length, parent->length - offset),
parent->mode,
hb_blob_reference (parent),
- (hb_destroy_func_t) _hb_blob_unlock_and_destroy);
- hb_mutex_unlock (&parent->lock);
+ (hb_destroy_func_t) hb_blob_destroy);
return blob;
}
@@ -176,7 +158,6 @@
if (!hb_object_destroy (blob)) return;
_hb_blob_destroy_user_data (blob);
- hb_mutex_free (&blob->lock);
free (blob);
}
@@ -198,6 +179,22 @@
}
+void
+hb_blob_make_immutable (hb_blob_t *blob)
+{
+ if (hb_object_is_inert (blob))
+ return;
+
+ blob->immutable = TRUE;
+}
+
+hb_bool_t
+hb_blob_is_immutable (hb_blob_t *blob)
+{
+ return blob->immutable;
+}
+
+
unsigned int
hb_blob_get_length (hb_blob_t *blob)
{
@@ -205,62 +202,33 @@
}
const char *
-hb_blob_lock (hb_blob_t *blob)
+hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
{
- if (hb_object_is_inert (blob))
- return NULL;
-
- hb_mutex_lock (&blob->lock);
-
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
- blob->lock_count, blob->data));
-
- blob->lock_count++;
-
- hb_mutex_unlock (&blob->lock);
+ if (length)
+ *length = blob->length;
return blob->data;
}
-void
-hb_blob_unlock (hb_blob_t *blob)
+char *
+hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
{
- if (hb_object_is_inert (blob))
- return;
+ if (!_try_writable (blob)) {
+ if (length)
+ *length = 0;
- hb_mutex_lock (&blob->lock);
+ return NULL;
+ }
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
- blob->lock_count, blob->data));
+ if (length)
+ *length = blob->length;
- assert (blob->lock_count > 0);
- blob->lock_count--;
-
- hb_mutex_unlock (&blob->lock);
-}
-
-hb_bool_t
-hb_blob_is_writable (hb_blob_t *blob)
-{
- hb_memory_mode_t mode;
-
- if (hb_object_is_inert (blob))
- return FALSE;
-
- hb_mutex_lock (&blob->lock);
-
- mode = blob->mode;
-
- hb_mutex_unlock (&blob->lock);
-
- return mode == HB_MEMORY_MODE_WRITABLE;
+ return const_cast<char *> (blob->data);
}
static hb_bool_t
-_try_make_writable_inplace_unix_locked (hb_blob_t *blob)
+_try_make_writable_inplace_unix (hb_blob_t *blob)
{
#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
uintptr_t pagesize = -1, mask, length;
@@ -295,6 +263,8 @@
return FALSE;
}
+ blob->mode = HB_MEMORY_MODE_WRITABLE;
+
(void) (HB_DEBUG_BLOB &&
fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
blob, HB_FUNC,
@@ -305,67 +275,59 @@
#endif
}
-static void
-try_writable_inplace_locked (hb_blob_t *blob)
+static bool
+_try_writable_inplace (hb_blob_t *blob)
{
(void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s: making writable\n", blob, HB_FUNC));
+ fprintf (stderr, "%p %s: making writable inplace\n", blob, HB_FUNC));
- if (_try_make_writable_inplace_unix_locked (blob)) {
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, HB_FUNC));
- blob->mode = HB_MEMORY_MODE_WRITABLE;
- } else {
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, HB_FUNC));
- /* Failed to make writable inplace, mark that */
- blob->mode = HB_MEMORY_MODE_READONLY;
- }
+ if (_try_make_writable_inplace_unix (blob))
+ return TRUE;
+
+ (void) (HB_DEBUG_BLOB &&
+ fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, HB_FUNC));
+
+ /* Failed to make writable inplace, mark that */
+ blob->mode = HB_MEMORY_MODE_READONLY;
+ return FALSE;
}
-hb_bool_t
-hb_blob_try_writable (hb_blob_t *blob)
+static bool
+_try_writable (hb_blob_t *blob)
{
- hb_memory_mode_t mode;
-
- if (hb_object_is_inert (blob))
+ if (blob->immutable)
return FALSE;
- hb_mutex_lock (&blob->lock);
+ if (blob->mode == HB_MEMORY_MODE_WRITABLE)
+ return TRUE;
- if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
- try_writable_inplace_locked (blob);
+ if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
+ return TRUE;
- if (blob->mode == HB_MEMORY_MODE_READONLY)
- {
- char *new_data;
+ if (blob->mode == HB_MEMORY_MODE_WRITABLE)
+ return TRUE;
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
- blob->lock_count, blob->data));
- if (blob->lock_count)
- goto done;
+ (void) (HB_DEBUG_BLOB &&
+ fprintf (stderr, "%p %s -> %p\n", blob, HB_FUNC, blob->data));
- new_data = (char *) malloc (blob->length);
- if (new_data) {
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, HB_FUNC, blob->data));
- memcpy (new_data, blob->data, blob->length);
- _hb_blob_destroy_user_data (blob);
- blob->mode = HB_MEMORY_MODE_WRITABLE;
- blob->data = new_data;
- blob->user_data = new_data;
- blob->destroy = free;
- }
- }
+ char *new_data;
-done:
- mode = blob->mode;
+ new_data = (char *) malloc (blob->length);
+ if (unlikely (!new_data))
+ return FALSE;
- hb_mutex_unlock (&blob->lock);
+ (void) (HB_DEBUG_BLOB &&
+ fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, HB_FUNC, blob->data));
- return mode == HB_MEMORY_MODE_WRITABLE;
+ memcpy (new_data, blob->data, blob->length);
+ _hb_blob_destroy_user_data (blob);
+ blob->mode = HB_MEMORY_MODE_WRITABLE;
+ blob->data = new_data;
+ blob->user_data = new_data;
+ blob->destroy = free;
+
+ return TRUE;
}