Fixes for Android GN build input/outputs
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-blob.cc
blob8759a252a57cc6088aced8762867bcd1a93d84a7
1 /*
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
16 * DAMAGE.
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
30 #endif
32 #include "hb-private.hh"
34 #include "hb-object-private.hh"
36 #ifdef HAVE_SYS_MMAN_H
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif /* HAVE_UNISTD_H */
40 #include <sys/mman.h>
41 #endif /* HAVE_SYS_MMAN_H */
43 #include <stdio.h>
44 #include <errno.h>
48 #ifndef HB_DEBUG_BLOB
49 #define HB_DEBUG_BLOB (HB_DEBUG+0)
50 #endif
53 struct hb_blob_t {
54 hb_object_header_t header;
55 ASSERT_POD ();
57 bool immutable;
59 const char *data;
60 unsigned int length;
61 hb_memory_mode_t mode;
63 void *user_data;
64 hb_destroy_func_t destroy;
68 static bool _try_writable (hb_blob_t *blob);
70 static void
71 _hb_blob_destroy_user_data (hb_blob_t *blob)
73 if (blob->destroy) {
74 blob->destroy (blob->user_data);
75 blob->user_data = NULL;
76 blob->destroy = NULL;
80 /**
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().
94 * Since: 1.0
95 **/
96 hb_blob_t *
97 hb_blob_create (const char *data,
98 unsigned int length,
99 hb_memory_mode_t mode,
100 void *user_data,
101 hb_destroy_func_t destroy)
103 hb_blob_t *blob;
105 if (!length ||
106 length >= 1u << 31 ||
107 data + length < data /* overflows */ ||
108 !(blob = hb_object_create<hb_blob_t> ())) {
109 if (destroy)
110 destroy (user_data);
111 return hb_blob_get_empty ();
114 blob->data = data;
115 blob->length = length;
116 blob->mode = mode;
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 ();
129 return blob;
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
142 * is.
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().
150 * Since: 1.0
152 hb_blob_t *
153 hb_blob_create_sub_blob (hb_blob_t *parent,
154 unsigned int offset,
155 unsigned int length)
157 hb_blob_t *blob;
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);
170 return blob;
174 * hb_blob_get_empty:
176 * Returns the singleton empty blob.
178 * See TODO:link object types for more information.
180 * Return value: (transfer full): the empty blob.
182 * Since: 1.0
184 hb_blob_t *
185 hb_blob_get_empty (void)
187 static const hb_blob_t _hb_blob_nil = {
188 HB_OBJECT_HEADER_STATIC,
190 true, /* immutable */
192 NULL, /* data */
193 0, /* length */
194 HB_MEMORY_MODE_READONLY, /* mode */
196 NULL, /* user_data */
197 NULL /* destroy */
200 return const_cast<hb_blob_t *> (&_hb_blob_nil);
204 * hb_blob_reference: (skip)
205 * @blob: a blob.
207 * Increases the reference count on @blob.
209 * See TODO:link object types for more information.
211 * Return value: @blob.
213 * Since: 1.0
215 hb_blob_t *
216 hb_blob_reference (hb_blob_t *blob)
218 return hb_object_reference (blob);
222 * hb_blob_destroy: (skip)
223 * @blob: a blob.
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.
231 * Since: 1.0
233 void
234 hb_blob_destroy (hb_blob_t *blob)
236 if (!hb_object_destroy (blob)) return;
238 _hb_blob_destroy_user_data (blob);
240 free (blob);
244 * hb_blob_set_user_data: (skip)
245 * @blob: a blob.
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.
251 * Return value:
253 * Since: 1.0
255 hb_bool_t
256 hb_blob_set_user_data (hb_blob_t *blob,
257 hb_user_data_key_t *key,
258 void * data,
259 hb_destroy_func_t destroy,
260 hb_bool_t replace)
262 return hb_object_set_user_data (blob, key, data, destroy, replace);
266 * hb_blob_get_user_data: (skip)
267 * @blob: a blob.
268 * @key: key for data to get.
272 * Return value: (transfer none):
274 * Since: 1.0
276 void *
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:
286 * @blob: a blob.
290 * Since: 1.0
292 void
293 hb_blob_make_immutable (hb_blob_t *blob)
295 if (hb_object_is_inert (blob))
296 return;
298 blob->immutable = true;
302 * hb_blob_is_immutable:
303 * @blob: a blob.
307 * Return value: TODO
309 * Since: 1.0
311 hb_bool_t
312 hb_blob_is_immutable (hb_blob_t *blob)
314 return blob->immutable;
319 * hb_blob_get_length:
320 * @blob: a blob.
324 * Return value: the length of blob data in bytes.
326 * Since: 1.0
328 unsigned int
329 hb_blob_get_length (hb_blob_t *blob)
331 return blob->length;
335 * hb_blob_get_data:
336 * @blob: a blob.
337 * @length: (out):
341 * Returns: (transfer none) (array length=length):
343 * Since: 1.0
345 const char *
346 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
348 if (length)
349 *length = blob->length;
351 return blob->data;
355 * hb_blob_get_data_writable:
356 * @blob: a blob.
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
363 * fails.
365 * Returns: (transfer none) (array length=length): Writable blob data,
366 * or %NULL if failed.
368 * Since: 1.0
370 char *
371 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
373 if (!_try_writable (blob)) {
374 if (length)
375 *length = 0;
377 return NULL;
380 if (length)
381 *length = blob->length;
383 return const_cast<char *> (blob->data);
387 static hb_bool_t
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;
392 const char *addr;
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 ();
400 #endif
402 if ((uintptr_t) -1L == pagesize) {
403 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
404 return false;
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));
416 return false;
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);
424 return true;
425 #else
426 return false;
427 #endif
430 static bool
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))
436 return true;
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;
442 return false;
445 static bool
446 _try_writable (hb_blob_t *blob)
448 if (blob->immutable)
449 return false;
451 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
452 return true;
454 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
455 return true;
457 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
458 return true;
461 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
463 char *new_data;
465 new_data = (char *) malloc (blob->length);
466 if (unlikely (!new_data))
467 return false;
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;
478 return true;