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: (skip)
82 * @data: 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
)
106 length
>= 1u << 31 ||
107 data
+ length
< data
/* overflows */ ||
108 !(blob
= hb_object_create
<hb_blob_t
> ())) {
111 return hb_blob_get_empty ();
115 blob
->length
= length
;
118 blob
->user_data
= user_data
;
119 blob
->destroy
= destroy
;
121 if (blob
->mode
== HB_MEMORY_MODE_DUPLICATE
) {
122 blob
->mode
= HB_MEMORY_MODE_READONLY
;
123 if (!_try_writable (blob
)) {
124 hb_blob_destroy (blob
);
125 return hb_blob_get_empty ();
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
),
168 (hb_destroy_func_t
) hb_blob_destroy
);
176 * Returns the singleton empty blob.
178 * See TODO:link object types for more information.
180 * Return value: (transfer full): the empty blob.
185 hb_blob_get_empty (void)
187 static const hb_blob_t _hb_blob_nil
= {
188 HB_OBJECT_HEADER_STATIC
,
190 true, /* immutable */
194 HB_MEMORY_MODE_READONLY
, /* mode */
196 NULL
, /* user_data */
200 return const_cast<hb_blob_t
*> (&_hb_blob_nil
);
204 * hb_blob_reference: (skip)
207 * Increases the reference count on @blob.
209 * See TODO:link object types for more information.
211 * Return value: @blob.
216 hb_blob_reference (hb_blob_t
*blob
)
218 return hb_object_reference (blob
);
222 * hb_blob_destroy: (skip)
225 * Descreases the reference count on @blob, and if it reaches zero, destroys
226 * @blob, freeing all memory, possibly calling the destroy-callback the blob
227 * was created for if it has not been called already.
229 * See TODO:link object types for more information.
234 hb_blob_destroy (hb_blob_t
*blob
)
236 if (!hb_object_destroy (blob
)) return;
238 _hb_blob_destroy_user_data (blob
);
244 * hb_blob_set_user_data: (skip)
246 * @key: key for data to set.
247 * @data: data to set.
248 * @destroy: callback to call when @data is not needed anymore.
249 * @replace: whether to replace an existing data with the same key.
256 hb_blob_set_user_data (hb_blob_t
*blob
,
257 hb_user_data_key_t
*key
,
259 hb_destroy_func_t destroy
,
262 return hb_object_set_user_data (blob
, key
, data
, destroy
, replace
);
266 * hb_blob_get_user_data: (skip)
268 * @key: key for data to get.
272 * Return value: (transfer none):
277 hb_blob_get_user_data (hb_blob_t
*blob
,
278 hb_user_data_key_t
*key
)
280 return hb_object_get_user_data (blob
, key
);
285 * hb_blob_make_immutable:
293 hb_blob_make_immutable (hb_blob_t
*blob
)
295 if (hb_object_is_inert (blob
))
298 blob
->immutable
= true;
302 * hb_blob_is_immutable:
312 hb_blob_is_immutable (hb_blob_t
*blob
)
314 return blob
->immutable
;
319 * hb_blob_get_length:
324 * Return value: the length of blob data in bytes.
329 hb_blob_get_length (hb_blob_t
*blob
)
341 * Returns: (transfer none) (array length=length):
346 hb_blob_get_data (hb_blob_t
*blob
, unsigned int *length
)
349 *length
= blob
->length
;
355 * hb_blob_get_data_writable:
357 * @length: (out): output length of the writable data.
359 * Tries to make blob data writable (possibly copying it) and
360 * return pointer to data.
362 * Fails if blob has been made immutable, or if memory allocation
365 * Returns: (transfer none) (array length=length): Writable blob data,
366 * or %NULL if failed.
371 hb_blob_get_data_writable (hb_blob_t
*blob
, unsigned int *length
)
373 if (!_try_writable (blob
)) {
381 *length
= blob
->length
;
383 return const_cast<char *> (blob
->data
);
388 _try_make_writable_inplace_unix (hb_blob_t
*blob
)
390 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
391 uintptr_t pagesize
= -1, mask
, length
;
394 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
395 pagesize
= (uintptr_t) sysconf (_SC_PAGE_SIZE
);
396 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
397 pagesize
= (uintptr_t) sysconf (_SC_PAGESIZE
);
398 #elif defined(HAVE_GETPAGESIZE)
399 pagesize
= (uintptr_t) getpagesize ();
402 if ((uintptr_t) -1L == pagesize
) {
403 DEBUG_MSG_FUNC (BLOB
, blob
, "failed to get pagesize: %s", strerror (errno
));
406 DEBUG_MSG_FUNC (BLOB
, blob
, "pagesize is %lu", (unsigned long) pagesize
);
408 mask
= ~(pagesize
-1);
409 addr
= (const char *) (((uintptr_t) blob
->data
) & mask
);
410 length
= (const char *) (((uintptr_t) blob
->data
+ blob
->length
+ pagesize
-1) & mask
) - addr
;
411 DEBUG_MSG_FUNC (BLOB
, blob
,
412 "calling mprotect on [%p..%p] (%lu bytes)",
413 addr
, addr
+length
, (unsigned long) length
);
414 if (-1 == mprotect ((void *) addr
, length
, PROT_READ
| PROT_WRITE
)) {
415 DEBUG_MSG_FUNC (BLOB
, blob
, "mprotect failed: %s", strerror (errno
));
419 blob
->mode
= HB_MEMORY_MODE_WRITABLE
;
421 DEBUG_MSG_FUNC (BLOB
, blob
,
422 "successfully made [%p..%p] (%lu bytes) writable\n",
423 addr
, addr
+length
, (unsigned long) length
);
431 _try_writable_inplace (hb_blob_t
*blob
)
433 DEBUG_MSG_FUNC (BLOB
, blob
, "making writable inplace\n");
435 if (_try_make_writable_inplace_unix (blob
))
438 DEBUG_MSG_FUNC (BLOB
, blob
, "making writable -> FAILED\n");
440 /* Failed to make writable inplace, mark that */
441 blob
->mode
= HB_MEMORY_MODE_READONLY
;
446 _try_writable (hb_blob_t
*blob
)
451 if (blob
->mode
== HB_MEMORY_MODE_WRITABLE
)
454 if (blob
->mode
== HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE
&& _try_writable_inplace (blob
))
457 if (blob
->mode
== HB_MEMORY_MODE_WRITABLE
)
461 DEBUG_MSG_FUNC (BLOB
, blob
, "current data is -> %p\n", blob
->data
);
465 new_data
= (char *) malloc (blob
->length
);
466 if (unlikely (!new_data
))
469 DEBUG_MSG_FUNC (BLOB
, blob
, "dupped successfully -> %p\n", blob
->data
);
471 memcpy (new_data
, blob
->data
, blob
->length
);
472 _hb_blob_destroy_user_data (blob
);
473 blob
->mode
= HB_MEMORY_MODE_WRITABLE
;
474 blob
->data
= new_data
;
475 blob
->user_data
= new_data
;
476 blob
->destroy
= free
;