#include "hb-blob.h"
#include "hb-object-private.hh"
-#include "hb-mutex-private.hh"
#ifdef HAVE_SYS_MMAN_H
#ifdef HAVE_UNISTD_H
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;
static hb_blob_t _hb_blob_nil = {
HB_OBJECT_HEADER_STATIC,
- 0, /* length */
-
- HB_MUTEX_INIT, /* lock */
-
- 0, /* lock_count */
- HB_MEMORY_MODE_READONLY, /* mode */
+ TRUE, /* immutable */
+ 0, /* length */
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)
{
}
}
-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,
return &_hb_blob_nil;
}
- hb_mutex_init (&blob->lock);
- blob->lock_count = 0;
-
blob->data = data;
blob->length = length;
blob->mode = mode;
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;
}
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;
}
if (!hb_object_destroy (blob)) return;
_hb_blob_destroy_user_data (blob);
- hb_mutex_free (&blob->lock);
free (blob);
}
}
-unsigned int
-hb_blob_get_length (hb_blob_t *blob)
-{
- return blob->length;
-}
-
-const char *
-hb_blob_lock (hb_blob_t *blob)
-{
- 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);
-
- return blob->data;
-}
-
void
-hb_blob_unlock (hb_blob_t *blob)
+hb_blob_make_immutable (hb_blob_t *blob)
{
if (hb_object_is_inert (blob))
return;
- hb_mutex_lock (&blob->lock);
+ blob->immutable = TRUE;
+}
- (void) (HB_DEBUG_BLOB &&
- fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
- blob->lock_count, blob->data));
+hb_bool_t
+hb_blob_is_immutable (hb_blob_t *blob)
+{
+ return blob->immutable;
+}
- assert (blob->lock_count > 0);
- blob->lock_count--;
- hb_mutex_unlock (&blob->lock);
+unsigned int
+hb_blob_get_length (hb_blob_t *blob)
+{
+ return blob->length;
}
-hb_bool_t
-hb_blob_is_writable (hb_blob_t *blob)
+const char *
+hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
{
- hb_memory_mode_t mode;
+ if (length)
+ *length = blob->length;
- if (hb_object_is_inert (blob))
- return FALSE;
+ return blob->data;
+}
- hb_mutex_lock (&blob->lock);
+char *
+hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
+{
+ if (!_try_writable (blob)) {
+ if (length)
+ *length = 0;
- mode = blob->mode;
+ return NULL;
+ }
- hb_mutex_unlock (&blob->lock);
+ if (length)
+ *length = blob->length;
- 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;
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,
#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;
-
- 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;
- }
- }
-done:
- mode = blob->mode;
+ (void) (HB_DEBUG_BLOB &&
+ fprintf (stderr, "%p %s -> %p\n", blob, HB_FUNC, blob->data));
+
+ char *new_data;
+
+ 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;
}
inline void init (hb_blob_t *blob)
{
this->blob = hb_blob_reference (blob);
- this->start = hb_blob_lock (blob);
+ this->writable = false;
+ }
+
+ inline void setup (void)
+ {
+ this->start = hb_blob_get_data (blob, NULL);
this->end = this->start + hb_blob_get_length (blob);
- this->writable = hb_blob_is_writable (blob);
this->edit_count = 0;
this->debug_depth = 0;
fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
this->blob, this->start, this->end, this->edit_count));
- hb_blob_unlock (this->blob);
hb_blob_destroy (this->blob);
this->blob = NULL;
this->start = this->end = NULL;
/* TODO is_sane() stuff */
+ c->init (blob);
+
retry:
(void) (HB_DEBUG_SANITIZE &&
fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC));
- c->init (blob);
+ c->setup ();
if (unlikely (!c->start)) {
c->finish ();
} else {
unsigned int edit_count = c->edit_count;
c->finish ();
- if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
- /* ok, we made it writable by relocating. try again */
- (void) (HB_DEBUG_SANITIZE &&
- fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC));
- goto retry;
+ if (edit_count && !c->writable) {
+ c->start = hb_blob_get_data_writable (blob, NULL);
+ c->end = c->start + hb_blob_get_length (blob);
+
+ if (c->start) {
+ c->writable = true;
+ /* ok, we made it writable by relocating. try again */
+ (void) (HB_DEBUG_SANITIZE &&
+ fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC));
+ goto retry;
+ }
}
}
}
static const Type* lock_instance (hb_blob_t *blob) {
- const char *base = hb_blob_lock (blob);
+ hb_blob_make_immutable (blob);
+ const char *base = hb_blob_get_data (blob, NULL);
return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
}
};