michael@0: /* michael@0: * Copyright © 2009 Red Hat, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */ michael@0: #define _POSIX_C_SOURCE 199309L michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-object-private.hh" michael@0: michael@0: #ifdef HAVE_SYS_MMAN_H michael@0: #ifdef HAVE_UNISTD_H michael@0: #include michael@0: #endif /* HAVE_UNISTD_H */ michael@0: #include michael@0: #endif /* HAVE_SYS_MMAN_H */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: michael@0: michael@0: #ifndef HB_DEBUG_BLOB michael@0: #define HB_DEBUG_BLOB (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: struct hb_blob_t { michael@0: hb_object_header_t header; michael@0: ASSERT_POD (); michael@0: michael@0: bool immutable; michael@0: michael@0: const char *data; michael@0: unsigned int length; michael@0: hb_memory_mode_t mode; michael@0: michael@0: void *user_data; michael@0: hb_destroy_func_t destroy; michael@0: }; michael@0: michael@0: michael@0: static bool _try_writable (hb_blob_t *blob); michael@0: michael@0: static void michael@0: _hb_blob_destroy_user_data (hb_blob_t *blob) michael@0: { michael@0: if (blob->destroy) { michael@0: blob->destroy (blob->user_data); michael@0: blob->user_data = NULL; michael@0: blob->destroy = NULL; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_create: (Xconstructor) michael@0: * @data: (array length=length) (closure user_data) (destroy destroy) (scope notified) (transfer none): Pointer to blob data. michael@0: * @length: Length of @data in bytes. michael@0: * @mode: Memory mode for @data. michael@0: * @user_data: Data parameter to pass to @destroy. michael@0: * @destroy: Callback to call when @data is not needed anymore. michael@0: * michael@0: * Creates a new "blob" object wrapping @data. The @mode parameter is used michael@0: * to negotiate ownership and lifecycle of @data. michael@0: * michael@0: * Return value: New blob, or the empty blob if something failed or if @length is michael@0: * zero. Destroy with hb_blob_destroy(). michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_blob_create (const char *data, michael@0: unsigned int length, michael@0: hb_memory_mode_t mode, michael@0: void *user_data, michael@0: hb_destroy_func_t destroy) michael@0: { michael@0: hb_blob_t *blob; michael@0: michael@0: if (!length || !(blob = hb_object_create ())) { michael@0: if (destroy) michael@0: destroy (user_data); michael@0: return hb_blob_get_empty (); michael@0: } michael@0: michael@0: blob->data = data; michael@0: blob->length = length; michael@0: blob->mode = mode; michael@0: michael@0: blob->user_data = user_data; michael@0: blob->destroy = destroy; michael@0: michael@0: if (blob->mode == HB_MEMORY_MODE_DUPLICATE) { michael@0: blob->mode = HB_MEMORY_MODE_READONLY; michael@0: if (!_try_writable (blob)) { michael@0: hb_blob_destroy (blob); michael@0: return hb_blob_get_empty (); michael@0: } michael@0: } michael@0: michael@0: return blob; michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_create_sub_blob: michael@0: * @parent: Parent blob. michael@0: * @offset: Start offset of sub-blob within @parent, in bytes. michael@0: * @length: Length of sub-blob. michael@0: * michael@0: * Returns a blob that represents a range of bytes in @parent. The new michael@0: * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it michael@0: * will never modify data in the parent blob. The parent data is not michael@0: * expected to be modified, and will result in undefined behavior if it michael@0: * is. michael@0: * michael@0: * Makes @parent immutable. michael@0: * michael@0: * Return value: New blob, or the empty blob if something failed or if michael@0: * @length is zero or @offset is beyond the end of @parent's data. Destroy michael@0: * with hb_blob_destroy(). michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_blob_create_sub_blob (hb_blob_t *parent, michael@0: unsigned int offset, michael@0: unsigned int length) michael@0: { michael@0: hb_blob_t *blob; michael@0: michael@0: if (!length || offset >= parent->length) michael@0: return hb_blob_get_empty (); michael@0: michael@0: hb_blob_make_immutable (parent); michael@0: michael@0: blob = hb_blob_create (parent->data + offset, michael@0: MIN (length, parent->length - offset), michael@0: HB_MEMORY_MODE_READONLY, michael@0: hb_blob_reference (parent), michael@0: (hb_destroy_func_t) hb_blob_destroy); michael@0: michael@0: return blob; michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_get_empty: michael@0: * michael@0: * Returns the singleton empty blob. michael@0: * michael@0: * See TODO:link object types for more information. michael@0: * michael@0: * Return value: (transfer full): the empty blob. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_blob_get_empty (void) michael@0: { michael@0: static const hb_blob_t _hb_blob_nil = { michael@0: HB_OBJECT_HEADER_STATIC, michael@0: michael@0: true, /* immutable */ michael@0: michael@0: NULL, /* data */ michael@0: 0, /* length */ michael@0: HB_MEMORY_MODE_READONLY, /* mode */ michael@0: michael@0: NULL, /* user_data */ michael@0: NULL /* destroy */ michael@0: }; michael@0: michael@0: return const_cast (&_hb_blob_nil); michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_reference: (skip) michael@0: * @blob: a blob. michael@0: * michael@0: * Increases the reference count on @blob. michael@0: * michael@0: * See TODO:link object types for more information. michael@0: * michael@0: * Return value: @blob. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_blob_reference (hb_blob_t *blob) michael@0: { michael@0: return hb_object_reference (blob); michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_destroy: (skip) michael@0: * @blob: a blob. michael@0: * michael@0: * Descreases the reference count on @blob, and if it reaches zero, destroys michael@0: * @blob, freeing all memory, possibly calling the destroy-callback the blob michael@0: * was created for if it has not been called already. michael@0: * michael@0: * See TODO:link object types for more information. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_blob_destroy (hb_blob_t *blob) michael@0: { michael@0: if (!hb_object_destroy (blob)) return; michael@0: michael@0: _hb_blob_destroy_user_data (blob); michael@0: michael@0: free (blob); michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_set_user_data: (skip) michael@0: * @blob: a blob. michael@0: * @key: key for data to set. michael@0: * @data: data to set. michael@0: * @destroy: callback to call when @data is not needed anymore. michael@0: * @replace: whether to replace an existing data with the same key. michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_blob_set_user_data (hb_blob_t *blob, michael@0: hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace) michael@0: { michael@0: return hb_object_set_user_data (blob, key, data, destroy, replace); michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_get_user_data: (skip) michael@0: * @blob: a blob. michael@0: * @key: key for data to get. michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void * michael@0: hb_blob_get_user_data (hb_blob_t *blob, michael@0: hb_user_data_key_t *key) michael@0: { michael@0: return hb_object_get_user_data (blob, key); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_blob_make_immutable: michael@0: * @blob: a blob. michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_blob_make_immutable (hb_blob_t *blob) michael@0: { michael@0: if (hb_object_is_inert (blob)) michael@0: return; michael@0: michael@0: blob->immutable = true; michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_is_immutable: michael@0: * @blob: a blob. michael@0: * michael@0: * michael@0: * michael@0: * Return value: TODO michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_blob_is_immutable (hb_blob_t *blob) michael@0: { michael@0: return blob->immutable; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_blob_get_length: michael@0: * @blob: a blob. michael@0: * michael@0: * michael@0: * michael@0: * Return value: the length of blob data in bytes. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: unsigned int michael@0: hb_blob_get_length (hb_blob_t *blob) michael@0: { michael@0: return blob->length; michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_get_data: michael@0: * @blob: a blob. michael@0: * @length: (out): michael@0: * michael@0: * michael@0: * michael@0: * Returns: (transfer none) (array length=length): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_blob_get_data (hb_blob_t *blob, unsigned int *length) michael@0: { michael@0: if (length) michael@0: *length = blob->length; michael@0: michael@0: return blob->data; michael@0: } michael@0: michael@0: /** michael@0: * hb_blob_get_data_writable: michael@0: * @blob: a blob. michael@0: * @length: (out): output length of the writable data. michael@0: * michael@0: * Tries to make blob data writable (possibly copying it) and michael@0: * return pointer to data. michael@0: * michael@0: * Fails if blob has been made immutable, or if memory allocation michael@0: * fails. michael@0: * michael@0: * Returns: (transfer none) (array length=length): Writable blob data, michael@0: * or %NULL if failed. michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: char * michael@0: hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length) michael@0: { michael@0: if (!_try_writable (blob)) { michael@0: if (length) michael@0: *length = 0; michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: if (length) michael@0: *length = blob->length; michael@0: michael@0: return const_cast (blob->data); michael@0: } michael@0: michael@0: michael@0: static hb_bool_t michael@0: _try_make_writable_inplace_unix (hb_blob_t *blob) michael@0: { michael@0: #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT) michael@0: uintptr_t pagesize = -1, mask, length; michael@0: const char *addr; michael@0: michael@0: #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE) michael@0: pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE); michael@0: #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) michael@0: pagesize = (uintptr_t) sysconf (_SC_PAGESIZE); michael@0: #elif defined(HAVE_GETPAGESIZE) michael@0: pagesize = (uintptr_t) getpagesize (); michael@0: #endif michael@0: michael@0: if ((uintptr_t) -1L == pagesize) { michael@0: DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno)); michael@0: return false; michael@0: } michael@0: DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize); michael@0: michael@0: mask = ~(pagesize-1); michael@0: addr = (const char *) (((uintptr_t) blob->data) & mask); michael@0: length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr; michael@0: DEBUG_MSG_FUNC (BLOB, blob, michael@0: "calling mprotect on [%p..%p] (%lu bytes)", michael@0: addr, addr+length, (unsigned long) length); michael@0: if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) { michael@0: DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno)); michael@0: return false; michael@0: } michael@0: michael@0: blob->mode = HB_MEMORY_MODE_WRITABLE; michael@0: michael@0: DEBUG_MSG_FUNC (BLOB, blob, michael@0: "successfully made [%p..%p] (%lu bytes) writable\n", michael@0: addr, addr+length, (unsigned long) length); michael@0: return true; michael@0: #else michael@0: return false; michael@0: #endif michael@0: } michael@0: michael@0: static bool michael@0: _try_writable_inplace (hb_blob_t *blob) michael@0: { michael@0: DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n"); michael@0: michael@0: if (_try_make_writable_inplace_unix (blob)) michael@0: return true; michael@0: michael@0: DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n"); michael@0: michael@0: /* Failed to make writable inplace, mark that */ michael@0: blob->mode = HB_MEMORY_MODE_READONLY; michael@0: return false; michael@0: } michael@0: michael@0: static bool michael@0: _try_writable (hb_blob_t *blob) michael@0: { michael@0: if (blob->immutable) michael@0: return false; michael@0: michael@0: if (blob->mode == HB_MEMORY_MODE_WRITABLE) michael@0: return true; michael@0: michael@0: if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob)) michael@0: return true; michael@0: michael@0: if (blob->mode == HB_MEMORY_MODE_WRITABLE) michael@0: return true; michael@0: michael@0: michael@0: DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data); michael@0: michael@0: char *new_data; michael@0: michael@0: new_data = (char *) malloc (blob->length); michael@0: if (unlikely (!new_data)) michael@0: return false; michael@0: michael@0: DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data); michael@0: michael@0: memcpy (new_data, blob->data, blob->length); michael@0: _hb_blob_destroy_user_data (blob); michael@0: blob->mode = HB_MEMORY_MODE_WRITABLE; michael@0: blob->data = new_data; michael@0: blob->user_data = new_data; michael@0: blob->destroy = free; michael@0: michael@0: return true; michael@0: }