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"
29 #include "hb-blob-private.hh"
31 #ifdef HAVE_SYS_MMAN_H
34 #endif /* HAVE_UNISTD_H */
36 #endif /* HAVE_SYS_MMAN_H */
45 #define HB_DEBUG_BLOB (HB_DEBUG+0)
48 hb_blob_t _hb_blob_nil = {
49 HB_OBJECT_HEADER_STATIC,
53 HB_MUTEX_INIT, /* lock */
56 HB_MEMORY_MODE_READONLY, /* mode */
65 _hb_blob_destroy_user_data (hb_blob_t *blob)
68 blob->destroy (blob->user_data);
69 blob->user_data = NULL;
75 _hb_blob_unlock_and_destroy (hb_blob_t *blob)
77 hb_blob_unlock (blob);
78 hb_blob_destroy (blob);
82 hb_blob_create (const char *data,
84 hb_memory_mode_t mode,
86 hb_destroy_func_t destroy)
90 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
96 hb_mutex_init (blob->lock);
100 blob->length = length;
103 blob->user_data = user_data;
104 blob->destroy = destroy;
106 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
107 blob->mode = HB_MEMORY_MODE_READONLY;
108 if (!hb_blob_try_writable (blob)) {
109 hb_blob_destroy (blob);
110 return &_hb_blob_nil;
118 hb_blob_create_sub_blob (hb_blob_t *parent,
125 if (!length || offset >= parent->length || !(blob = hb_object_create<hb_blob_t> ()))
126 return &_hb_blob_nil;
128 pdata = hb_blob_lock (parent);
130 blob->data = pdata + offset;
131 blob->length = MIN (length, parent->length - offset);
133 hb_mutex_lock (parent->lock);
134 blob->mode = parent->mode;
135 hb_mutex_unlock (parent->lock);
137 blob->user_data = hb_blob_reference (parent);
138 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
144 hb_blob_create_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_get_length (hb_blob_t *blob)
189 hb_blob_lock (hb_blob_t *blob)
191 if (hb_object_is_inert (blob))
194 hb_mutex_lock (blob->lock);
196 (void) (HB_DEBUG_BLOB &&
197 fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
198 blob->lock_count, blob->data));
202 hb_mutex_unlock (blob->lock);
208 hb_blob_unlock (hb_blob_t *blob)
210 if (hb_object_is_inert (blob))
213 hb_mutex_lock (blob->lock);
215 (void) (HB_DEBUG_BLOB &&
216 fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
217 blob->lock_count, blob->data));
219 assert (blob->lock_count > 0);
222 hb_mutex_unlock (blob->lock);
226 hb_blob_is_writable (hb_blob_t *blob)
228 hb_memory_mode_t mode;
230 if (hb_object_is_inert (blob))
233 hb_mutex_lock (blob->lock);
237 hb_mutex_unlock (blob->lock);
239 return mode == HB_MEMORY_MODE_WRITABLE;
244 _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
246 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
247 uintptr_t pagesize = -1, mask, length;
250 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
251 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
252 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
253 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
254 #elif defined(HAVE_GETPAGESIZE)
255 pagesize = (uintptr_t) getpagesize ();
258 if ((uintptr_t) -1L == pagesize) {
259 (void) (HB_DEBUG_BLOB &&
260 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, HB_FUNC, strerror (errno)));
263 (void) (HB_DEBUG_BLOB &&
264 fprintf (stderr, "%p %s: pagesize is %lu\n", blob, HB_FUNC, (unsigned long) pagesize));
266 mask = ~(pagesize-1);
267 addr = (const char *) (((uintptr_t) blob->data) & mask);
268 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
269 (void) (HB_DEBUG_BLOB &&
270 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n",
272 addr, addr+length, (unsigned long) length));
273 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
274 (void) (HB_DEBUG_BLOB &&
275 fprintf (stderr, "%p %s: %s\n", blob, HB_FUNC, strerror (errno)));
279 (void) (HB_DEBUG_BLOB &&
280 fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
282 addr, addr+length, (unsigned long) length));
290 try_writable_inplace_locked (hb_blob_t *blob)
292 (void) (HB_DEBUG_BLOB &&
293 fprintf (stderr, "%p %s: making writable\n", blob, HB_FUNC));
295 if (_try_make_writable_inplace_unix_locked (blob)) {
296 (void) (HB_DEBUG_BLOB &&
297 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, HB_FUNC));
298 blob->mode = HB_MEMORY_MODE_WRITABLE;
300 (void) (HB_DEBUG_BLOB &&
301 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, HB_FUNC));
302 /* Failed to make writable inplace, mark that */
303 blob->mode = HB_MEMORY_MODE_READONLY;
308 hb_blob_try_writable_inplace (hb_blob_t *blob)
310 hb_memory_mode_t mode;
312 if (hb_object_is_inert (blob))
315 hb_mutex_lock (blob->lock);
317 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
318 try_writable_inplace_locked (blob);
322 hb_mutex_unlock (blob->lock);
324 return mode == HB_MEMORY_MODE_WRITABLE;
328 hb_blob_try_writable (hb_blob_t *blob)
330 hb_memory_mode_t mode;
332 if (hb_object_is_inert (blob))
335 hb_mutex_lock (blob->lock);
337 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
338 try_writable_inplace_locked (blob);
340 if (blob->mode == HB_MEMORY_MODE_READONLY)
344 (void) (HB_DEBUG_BLOB &&
345 fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
346 blob->lock_count, blob->data));
348 if (blob->lock_count)
351 new_data = (char *) malloc (blob->length);
353 (void) (HB_DEBUG_BLOB &&
354 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, HB_FUNC, blob->data));
355 memcpy (new_data, blob->data, blob->length);
356 _hb_blob_destroy_user_data (blob);
357 blob->mode = HB_MEMORY_MODE_WRITABLE;
358 blob->data = new_data;
359 blob->user_data = new_data;
360 blob->destroy = free;
367 hb_mutex_unlock (blob->lock);
369 return mode == HB_MEMORY_MODE_WRITABLE;