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 200809L
32 #include "hb-private.hh"
33 #include "hb-debug.hh"
35 #include "hb-object-private.hh"
37 #ifdef HAVE_SYS_MMAN_H
40 #endif /* HAVE_UNISTD_H */
42 #endif /* HAVE_SYS_MMAN_H */
49 hb_object_header_t header;
56 hb_memory_mode_t mode;
59 hb_destroy_func_t destroy;
63 static bool _try_writable (hb_blob_t *blob);
66 _hb_blob_destroy_user_data (hb_blob_t *blob)
69 blob->destroy (blob->user_data);
70 blob->user_data = nullptr;
71 blob->destroy = nullptr;
76 * hb_blob_create: (skip)
77 * @data: Pointer to blob data.
78 * @length: Length of @data in bytes.
79 * @mode: Memory mode for @data.
80 * @user_data: Data parameter to pass to @destroy.
81 * @destroy: Callback to call when @data is not needed anymore.
83 * Creates a new "blob" object wrapping @data. The @mode parameter is used
84 * to negotiate ownership and lifecycle of @data.
86 * Return value: New blob, or the empty blob if something failed or if @length is
87 * zero. Destroy with hb_blob_destroy().
92 hb_blob_create (const char *data,
94 hb_memory_mode_t mode,
96 hb_destroy_func_t destroy)
101 length >= 1u << 31 ||
102 !(blob = hb_object_create<hb_blob_t> ())) {
105 return hb_blob_get_empty ();
109 blob->length = length;
112 blob->user_data = user_data;
113 blob->destroy = destroy;
115 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
116 blob->mode = HB_MEMORY_MODE_READONLY;
117 if (!_try_writable (blob)) {
118 hb_blob_destroy (blob);
119 return hb_blob_get_empty ();
127 _hb_blob_destroy (void *data)
129 hb_blob_destroy ((hb_blob_t *) data);
133 * hb_blob_create_sub_blob:
134 * @parent: Parent blob.
135 * @offset: Start offset of sub-blob within @parent, in bytes.
136 * @length: Length of sub-blob.
138 * Returns a blob that represents a range of bytes in @parent. The new
139 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
140 * will never modify data in the parent blob. The parent data is not
141 * expected to be modified, and will result in undefined behavior if it
144 * Makes @parent immutable.
146 * Return value: New blob, or the empty blob if something failed or if
147 * @length is zero or @offset is beyond the end of @parent's data. Destroy
148 * with hb_blob_destroy().
153 hb_blob_create_sub_blob (hb_blob_t *parent,
159 if (!length || offset >= parent->length)
160 return hb_blob_get_empty ();
162 hb_blob_make_immutable (parent);
164 blob = hb_blob_create (parent->data + offset,
165 MIN (length, parent->length - offset),
166 HB_MEMORY_MODE_READONLY,
167 hb_blob_reference (parent),
174 * hb_blob_copy_writable_or_fail:
177 * Makes a writable copy of @blob.
179 * Return value: New blob, or nullptr if allocation failed.
184 hb_blob_copy_writable_or_fail (hb_blob_t *blob)
186 blob = hb_blob_create (blob->data,
188 HB_MEMORY_MODE_DUPLICATE,
192 if (unlikely (blob == hb_blob_get_empty ()))
201 * Returns the singleton empty blob.
203 * See TODO:link object types for more information.
205 * Return value: (transfer full): the empty blob.
210 hb_blob_get_empty (void)
212 static const hb_blob_t _hb_blob_nil = {
213 HB_OBJECT_HEADER_STATIC,
215 true, /* immutable */
219 HB_MEMORY_MODE_READONLY, /* mode */
221 nullptr, /* user_data */
222 nullptr /* destroy */
225 return const_cast<hb_blob_t *> (&_hb_blob_nil);
229 * hb_blob_reference: (skip)
232 * Increases the reference count on @blob.
234 * See TODO:link object types for more information.
236 * Return value: @blob.
241 hb_blob_reference (hb_blob_t *blob)
243 return hb_object_reference (blob);
247 * hb_blob_destroy: (skip)
250 * Decreases the reference count on @blob, and if it reaches zero, destroys
251 * @blob, freeing all memory, possibly calling the destroy-callback the blob
252 * was created for if it has not been called already.
254 * See TODO:link object types for more information.
259 hb_blob_destroy (hb_blob_t *blob)
261 if (!hb_object_destroy (blob)) return;
263 _hb_blob_destroy_user_data (blob);
269 * hb_blob_set_user_data: (skip)
271 * @key: key for data to set.
272 * @data: data to set.
273 * @destroy: callback to call when @data is not needed anymore.
274 * @replace: whether to replace an existing data with the same key.
281 hb_blob_set_user_data (hb_blob_t *blob,
282 hb_user_data_key_t *key,
284 hb_destroy_func_t destroy,
287 return hb_object_set_user_data (blob, key, data, destroy, replace);
291 * hb_blob_get_user_data: (skip)
293 * @key: key for data to get.
297 * Return value: (transfer none):
302 hb_blob_get_user_data (hb_blob_t *blob,
303 hb_user_data_key_t *key)
305 return hb_object_get_user_data (blob, key);
310 * hb_blob_make_immutable:
318 hb_blob_make_immutable (hb_blob_t *blob)
320 if (hb_object_is_inert (blob))
323 blob->immutable = true;
327 * hb_blob_is_immutable:
337 hb_blob_is_immutable (hb_blob_t *blob)
339 return blob->immutable;
344 * hb_blob_get_length:
349 * Return value: the length of blob data in bytes.
354 hb_blob_get_length (hb_blob_t *blob)
366 * Returns: (transfer none) (array length=length):
371 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
374 *length = blob->length;
380 * hb_blob_get_data_writable:
382 * @length: (out): output length of the writable data.
384 * Tries to make blob data writable (possibly copying it) and
385 * return pointer to data.
387 * Fails if blob has been made immutable, or if memory allocation
390 * Returns: (transfer none) (array length=length): Writable blob data,
391 * or %NULL if failed.
396 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
398 if (!_try_writable (blob)) {
406 *length = blob->length;
408 return const_cast<char *> (blob->data);
413 _try_make_writable_inplace_unix (hb_blob_t *blob)
415 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
416 uintptr_t pagesize = -1, mask, length;
419 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
420 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
421 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
422 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
423 #elif defined(HAVE_GETPAGESIZE)
424 pagesize = (uintptr_t) getpagesize ();
427 if ((uintptr_t) -1L == pagesize) {
428 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
431 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
433 mask = ~(pagesize-1);
434 addr = (const char *) (((uintptr_t) blob->data) & mask);
435 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
436 DEBUG_MSG_FUNC (BLOB, blob,
437 "calling mprotect on [%p..%p] (%lu bytes)",
438 addr, addr+length, (unsigned long) length);
439 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
440 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
444 blob->mode = HB_MEMORY_MODE_WRITABLE;
446 DEBUG_MSG_FUNC (BLOB, blob,
447 "successfully made [%p..%p] (%lu bytes) writable\n",
448 addr, addr+length, (unsigned long) length);
456 _try_writable_inplace (hb_blob_t *blob)
458 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
460 if (_try_make_writable_inplace_unix (blob))
463 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
465 /* Failed to make writable inplace, mark that */
466 blob->mode = HB_MEMORY_MODE_READONLY;
471 _try_writable (hb_blob_t *blob)
476 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
479 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
482 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
486 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
490 new_data = (char *) malloc (blob->length);
491 if (unlikely (!new_data))
494 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
496 memcpy (new_data, blob->data, blob->length);
497 _hb_blob_destroy_user_data (blob);
498 blob->mode = HB_MEMORY_MODE_WRITABLE;
499 blob->data = new_data;
500 blob->user_data = new_data;
501 blob->destroy = free;