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 */
45 #define HB_DEBUG_BLOB (HB_DEBUG+0)
50 hb_object_header_t header;
57 hb_memory_mode_t mode;
60 hb_destroy_func_t destroy;
64 static bool _try_writable (hb_blob_t *blob);
67 _hb_blob_destroy_user_data (hb_blob_t *blob)
70 blob->destroy (blob->user_data);
71 blob->user_data = NULL;
77 hb_blob_create (const char *data,
79 hb_memory_mode_t mode,
81 hb_destroy_func_t destroy)
85 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
88 return hb_blob_get_empty ();
92 blob->length = length;
95 blob->user_data = user_data;
96 blob->destroy = destroy;
98 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
99 blob->mode = HB_MEMORY_MODE_READONLY;
100 if (!_try_writable (blob)) {
101 hb_blob_destroy (blob);
102 return hb_blob_get_empty ();
110 hb_blob_create_sub_blob (hb_blob_t *parent,
116 if (!length || offset >= parent->length)
117 return hb_blob_get_empty ();
119 hb_blob_make_immutable (parent);
121 blob = hb_blob_create (parent->data + offset,
122 MIN (length, parent->length - offset),
124 hb_blob_reference (parent),
125 (hb_destroy_func_t) hb_blob_destroy);
131 hb_blob_get_empty (void)
133 static const hb_blob_t _hb_blob_nil = {
134 HB_OBJECT_HEADER_STATIC,
136 true, /* immutable */
140 HB_MEMORY_MODE_READONLY, /* mode */
142 NULL, /* user_data */
146 return const_cast<hb_blob_t *> (&_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,
172 return hb_object_set_user_data (blob, key, data, destroy, replace);
176 hb_blob_get_user_data (hb_blob_t *blob,
177 hb_user_data_key_t *key)
179 return hb_object_get_user_data (blob, key);
184 hb_blob_make_immutable (hb_blob_t *blob)
186 if (hb_object_is_inert (blob))
189 blob->immutable = true;
193 hb_blob_is_immutable (hb_blob_t *blob)
195 return blob->immutable;
200 hb_blob_get_length (hb_blob_t *blob)
206 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
209 *length = blob->length;
215 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
217 if (!_try_writable (blob)) {
225 *length = blob->length;
227 return const_cast<char *> (blob->data);
232 _try_make_writable_inplace_unix (hb_blob_t *blob)
234 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
235 uintptr_t pagesize = -1, mask, length;
238 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
239 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
240 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
241 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
242 #elif defined(HAVE_GETPAGESIZE)
243 pagesize = (uintptr_t) getpagesize ();
246 if ((uintptr_t) -1L == pagesize) {
247 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
250 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
252 mask = ~(pagesize-1);
253 addr = (const char *) (((uintptr_t) blob->data) & mask);
254 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
255 DEBUG_MSG_FUNC (BLOB, blob,
256 "calling mprotect on [%p..%p] (%lu bytes)",
257 addr, addr+length, (unsigned long) length);
258 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
259 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
263 blob->mode = HB_MEMORY_MODE_WRITABLE;
265 DEBUG_MSG_FUNC (BLOB, blob,
266 "successfully made [%p..%p] (%lu bytes) writable\n",
267 addr, addr+length, (unsigned long) length);
275 _try_writable_inplace (hb_blob_t *blob)
277 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
279 if (_try_make_writable_inplace_unix (blob))
282 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
284 /* Failed to make writable inplace, mark that */
285 blob->mode = HB_MEMORY_MODE_READONLY;
290 _try_writable (hb_blob_t *blob)
295 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
298 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
301 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
305 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
309 new_data = (char *) malloc (blob->length);
310 if (unlikely (!new_data))
313 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
315 memcpy (new_data, blob->data, blob->length);
316 _hb_blob_destroy_user_data (blob);
317 blob->mode = HB_MEMORY_MODE_WRITABLE;
318 blob->data = new_data;
319 blob->user_data = new_data;
320 blob->destroy = free;