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 #include "hb-private.hh"
30 #include "hb-object-private.hh"
32 #ifdef HAVE_SYS_MMAN_H
35 #endif /* HAVE_UNISTD_H */
37 #endif /* HAVE_SYS_MMAN_H */
46 #define HB_DEBUG_BLOB (HB_DEBUG+0)
51 hb_object_header_t header;
57 hb_memory_mode_t mode;
60 hb_destroy_func_t destroy;
63 static hb_blob_t _hb_blob_nil = {
64 HB_OBJECT_HEADER_STATIC,
70 HB_MEMORY_MODE_READONLY, /* mode */
77 static bool _try_writable (hb_blob_t *blob);
80 _hb_blob_destroy_user_data (hb_blob_t *blob)
83 blob->destroy (blob->user_data);
84 blob->user_data = NULL;
90 hb_blob_create (const char *data,
92 hb_memory_mode_t mode,
94 hb_destroy_func_t destroy)
98 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
101 return &_hb_blob_nil;
105 blob->length = length;
108 blob->user_data = user_data;
109 blob->destroy = destroy;
111 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
112 blob->mode = HB_MEMORY_MODE_READONLY;
113 if (!_try_writable (blob)) {
114 hb_blob_destroy (blob);
115 return &_hb_blob_nil;
123 hb_blob_create_sub_blob (hb_blob_t *parent,
129 if (!length || offset >= parent->length || !(blob = hb_object_create<hb_blob_t> ()))
130 return &_hb_blob_nil;
132 hb_blob_make_immutable (parent);
134 blob = hb_blob_create (parent->data + offset,
135 MIN (length, parent->length - offset),
137 hb_blob_reference (parent),
138 (hb_destroy_func_t) hb_blob_destroy);
144 hb_blob_get_empty (void)
146 return &_hb_blob_nil;
150 hb_blob_reference (hb_blob_t *blob)
152 return hb_object_reference (blob);
156 hb_blob_destroy (hb_blob_t *blob)
158 if (!hb_object_destroy (blob)) return;
160 _hb_blob_destroy_user_data (blob);
166 hb_blob_set_user_data (hb_blob_t *blob,
167 hb_user_data_key_t *key,
169 hb_destroy_func_t destroy)
171 return hb_object_set_user_data (blob, key, data, destroy);
175 hb_blob_get_user_data (hb_blob_t *blob,
176 hb_user_data_key_t *key)
178 return hb_object_get_user_data (blob, key);
183 hb_blob_make_immutable (hb_blob_t *blob)
185 if (hb_object_is_inert (blob))
188 blob->immutable = TRUE;
192 hb_blob_is_immutable (hb_blob_t *blob)
194 return blob->immutable;
199 hb_blob_get_length (hb_blob_t *blob)
205 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
208 *length = blob->length;
214 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
216 if (!_try_writable (blob)) {
224 *length = blob->length;
226 return const_cast<char *> (blob->data);
231 _try_make_writable_inplace_unix (hb_blob_t *blob)
233 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
234 uintptr_t pagesize = -1, mask, length;
237 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
238 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
239 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
240 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
241 #elif defined(HAVE_GETPAGESIZE)
242 pagesize = (uintptr_t) getpagesize ();
245 if ((uintptr_t) -1L == pagesize) {
246 (void) (HB_DEBUG_BLOB &&
247 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, HB_FUNC, strerror (errno)));
250 (void) (HB_DEBUG_BLOB &&
251 fprintf (stderr, "%p %s: pagesize is %lu\n", blob, HB_FUNC, (unsigned long) pagesize));
253 mask = ~(pagesize-1);
254 addr = (const char *) (((uintptr_t) blob->data) & mask);
255 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
256 (void) (HB_DEBUG_BLOB &&
257 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n",
259 addr, addr+length, (unsigned long) length));
260 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
261 (void) (HB_DEBUG_BLOB &&
262 fprintf (stderr, "%p %s: %s\n", blob, HB_FUNC, strerror (errno)));
266 blob->mode = HB_MEMORY_MODE_WRITABLE;
268 (void) (HB_DEBUG_BLOB &&
269 fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
271 addr, addr+length, (unsigned long) length));
279 _try_writable_inplace (hb_blob_t *blob)
281 (void) (HB_DEBUG_BLOB &&
282 fprintf (stderr, "%p %s: making writable inplace\n", blob, HB_FUNC));
284 if (_try_make_writable_inplace_unix (blob))
287 (void) (HB_DEBUG_BLOB &&
288 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, HB_FUNC));
290 /* Failed to make writable inplace, mark that */
291 blob->mode = HB_MEMORY_MODE_READONLY;
296 _try_writable (hb_blob_t *blob)
301 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
304 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
307 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
311 (void) (HB_DEBUG_BLOB &&
312 fprintf (stderr, "%p %s -> %p\n", blob, HB_FUNC, blob->data));
316 new_data = (char *) malloc (blob->length);
317 if (unlikely (!new_data))
320 (void) (HB_DEBUG_BLOB &&
321 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, HB_FUNC, blob->data));
323 memcpy (new_data, blob->data, blob->length);
324 _hb_blob_destroy_user_data (blob);
325 blob->mode = HB_MEMORY_MODE_WRITABLE;
326 blob->data = new_data;
327 blob->user_data = new_data;
328 blob->destroy = free;