2 * Copyright © 2009 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
27 /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
28 #ifndef _POSIX_C_SOURCE
29 #define _POSIX_C_SOURCE 199309L
32 #include "hb-private.hh"
34 #include "hb-object-private.hh"
36 #ifdef HAVE_SYS_MMAN_H
39 #endif /* HAVE_UNISTD_H */
41 #endif /* HAVE_SYS_MMAN_H */
49 #define HB_DEBUG_BLOB (HB_DEBUG+0)
54 hb_object_header_t header;
61 hb_memory_mode_t mode;
64 hb_destroy_func_t destroy;
68 static bool _try_writable (hb_blob_t *blob);
71 _hb_blob_destroy_user_data (hb_blob_t *blob)
74 blob->destroy (blob->user_data);
75 blob->user_data = NULL;
81 * hb_blob_create: (Xconstructor)
82 * @data: (array length=length) (closure user_data) (destroy destroy) (scope notified) (transfer none): Pointer to blob data.
83 * @length: Length of @data in bytes.
84 * @mode: Memory mode for @data.
85 * @user_data: Data parameter to pass to @destroy.
86 * @destroy: Callback to call when @data is not needed anymore.
88 * Creates a new "blob" object wrapping @data. The @mode parameter is used
89 * to negotiate ownership and lifecycle of @data.
91 * Return value: New blob, or the empty blob if something failed or if @length is
92 * zero. Destroy with hb_blob_destroy().
97 hb_blob_create (const char *data,
99 hb_memory_mode_t mode,
101 hb_destroy_func_t destroy)
105 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
108 return hb_blob_get_empty ();
112 blob->length = length;
115 blob->user_data = user_data;
116 blob->destroy = destroy;
118 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
119 blob->mode = HB_MEMORY_MODE_READONLY;
120 if (!_try_writable (blob)) {
121 hb_blob_destroy (blob);
122 return hb_blob_get_empty ();
130 * hb_blob_create_sub_blob:
131 * @parent: Parent blob.
132 * @offset: Start offset of sub-blob within @parent, in bytes.
133 * @length: Length of sub-blob.
135 * Returns a blob that represents a range of bytes in @parent. The new
136 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
137 * will never modify data in the parent blob. The parent data is not
138 * expected to be modified, and will result in undefined behavior if it
141 * Makes @parent immutable.
143 * Return value: New blob, or the empty blob if something failed or if
144 * @length is zero or @offset is beyond the end of @parent's data. Destroy
145 * with hb_blob_destroy().
150 hb_blob_create_sub_blob (hb_blob_t *parent,
156 if (!length || offset >= parent->length)
157 return hb_blob_get_empty ();
159 hb_blob_make_immutable (parent);
161 blob = hb_blob_create (parent->data + offset,
162 MIN (length, parent->length - offset),
163 HB_MEMORY_MODE_READONLY,
164 hb_blob_reference (parent),
165 (hb_destroy_func_t) hb_blob_destroy);
173 * Returns the singleton empty blob.
175 * See TODO:link object types for more information.
177 * Return value: (transfer full): the empty blob.
182 hb_blob_get_empty (void)
184 static const hb_blob_t _hb_blob_nil = {
185 HB_OBJECT_HEADER_STATIC,
187 true, /* immutable */
191 HB_MEMORY_MODE_READONLY, /* mode */
193 NULL, /* user_data */
197 return const_cast<hb_blob_t *> (&_hb_blob_nil);
201 * hb_blob_reference: (skip)
204 * Increases the reference count on @blob.
206 * See TODO:link object types for more information.
208 * Return value: @blob.
213 hb_blob_reference (hb_blob_t *blob)
215 return hb_object_reference (blob);
219 * hb_blob_destroy: (skip)
222 * Descreases the reference count on @blob, and if it reaches zero, destroys
223 * @blob, freeing all memory, possibly calling the destroy-callback the blob
224 * was created for if it has not been called already.
226 * See TODO:link object types for more information.
231 hb_blob_destroy (hb_blob_t *blob)
233 if (!hb_object_destroy (blob)) return;
235 _hb_blob_destroy_user_data (blob);
241 * hb_blob_set_user_data: (skip)
243 * @key: key for data to set.
244 * @data: data to set.
245 * @destroy: callback to call when @data is not needed anymore.
246 * @replace: whether to replace an existing data with the same key.
253 hb_blob_set_user_data (hb_blob_t *blob,
254 hb_user_data_key_t *key,
256 hb_destroy_func_t destroy,
259 return hb_object_set_user_data (blob, key, data, destroy, replace);
263 * hb_blob_get_user_data: (skip)
265 * @key: key for data to get.
269 * Return value: (transfer none):
274 hb_blob_get_user_data (hb_blob_t *blob,
275 hb_user_data_key_t *key)
277 return hb_object_get_user_data (blob, key);
282 * hb_blob_make_immutable:
290 hb_blob_make_immutable (hb_blob_t *blob)
292 if (hb_object_is_inert (blob))
295 blob->immutable = true;
299 * hb_blob_is_immutable:
309 hb_blob_is_immutable (hb_blob_t *blob)
311 return blob->immutable;
316 * hb_blob_get_length:
321 * Return value: the length of blob data in bytes.
326 hb_blob_get_length (hb_blob_t *blob)
338 * Returns: (transfer none) (array length=length):
343 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
346 *length = blob->length;
352 * hb_blob_get_data_writable:
354 * @length: (out): output length of the writable data.
356 * Tries to make blob data writable (possibly copying it) and
357 * return pointer to data.
359 * Fails if blob has been made immutable, or if memory allocation
362 * Returns: (transfer none) (array length=length): Writable blob data,
363 * or %NULL if failed.
368 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
370 if (!_try_writable (blob)) {
378 *length = blob->length;
380 return const_cast<char *> (blob->data);
385 _try_make_writable_inplace_unix (hb_blob_t *blob)
387 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
388 uintptr_t pagesize = -1, mask, length;
391 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
392 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
393 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
394 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
395 #elif defined(HAVE_GETPAGESIZE)
396 pagesize = (uintptr_t) getpagesize ();
399 if ((uintptr_t) -1L == pagesize) {
400 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
403 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
405 mask = ~(pagesize-1);
406 addr = (const char *) (((uintptr_t) blob->data) & mask);
407 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
408 DEBUG_MSG_FUNC (BLOB, blob,
409 "calling mprotect on [%p..%p] (%lu bytes)",
410 addr, addr+length, (unsigned long) length);
411 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
412 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
416 blob->mode = HB_MEMORY_MODE_WRITABLE;
418 DEBUG_MSG_FUNC (BLOB, blob,
419 "successfully made [%p..%p] (%lu bytes) writable\n",
420 addr, addr+length, (unsigned long) length);
428 _try_writable_inplace (hb_blob_t *blob)
430 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
432 if (_try_make_writable_inplace_unix (blob))
435 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
437 /* Failed to make writable inplace, mark that */
438 blob->mode = HB_MEMORY_MODE_READONLY;
443 _try_writable (hb_blob_t *blob)
448 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
451 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
454 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
458 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
462 new_data = (char *) malloc (blob->length);
463 if (unlikely (!new_data))
466 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
468 memcpy (new_data, blob->data, blob->length);
469 _hb_blob_destroy_user_data (blob);
470 blob->mode = HB_MEMORY_MODE_WRITABLE;
471 blob->data = new_data;
472 blob->user_data = new_data;
473 blob->destroy = free;