2 * Copyright (C) 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.h"
31 #ifdef HAVE_SYS_MMAN_H
34 #endif /* HAVE_UNISTD_H */
36 #endif /* HAVE_SYS_MMAN_H */
39 #define HB_DEBUG_BLOB HB_DEBUG
48 hb_reference_count_t ref_count;
53 /* the rest are protected by lock */
55 unsigned int lock_count;
56 hb_memory_mode_t mode;
60 hb_destroy_func_t destroy;
63 static hb_blob_t _hb_blob_nil = {
64 HB_REFERENCE_COUNT_INVALID, /* ref_count */
68 HB_MUTEX_INIT, /* lock */
71 HB_MEMORY_MODE_READONLY, /* mode */
80 _hb_blob_destroy_user_data (hb_blob_t *blob)
83 blob->destroy (blob->user_data);
85 blob->user_data = NULL;
90 _hb_blob_unlock_and_destroy (hb_blob_t *blob)
92 hb_blob_unlock (blob);
93 hb_blob_destroy (blob);
97 hb_blob_create (const char *data,
99 hb_memory_mode_t mode,
100 hb_destroy_func_t destroy,
105 if (!length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob)) {
108 return &_hb_blob_nil;
111 hb_mutex_init (blob->lock);
112 blob->lock_count = 0;
115 blob->length = length;
118 blob->destroy = destroy;
119 blob->user_data = user_data;
121 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
122 blob->mode = HB_MEMORY_MODE_READONLY;
123 if (!hb_blob_try_writable (blob)) {
124 hb_blob_destroy (blob);
125 return &_hb_blob_nil;
133 hb_blob_create_sub_blob (hb_blob_t *parent,
140 if (!length || offset >= parent->length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob))
141 return &_hb_blob_nil;
143 pdata = hb_blob_lock (parent);
145 blob->data = pdata + offset;
146 blob->length = MIN (length, parent->length - offset);
148 hb_mutex_lock (parent->lock);
149 blob->mode = parent->mode;
150 hb_mutex_unlock (parent->lock);
152 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
153 blob->user_data = hb_blob_reference (parent);
159 hb_blob_create_empty (void)
161 return &_hb_blob_nil;
165 hb_blob_reference (hb_blob_t *blob)
167 HB_OBJECT_DO_REFERENCE (blob);
171 hb_blob_get_reference_count (hb_blob_t *blob)
173 HB_OBJECT_DO_GET_REFERENCE_COUNT (blob);
177 hb_blob_destroy (hb_blob_t *blob)
179 HB_OBJECT_DO_DESTROY (blob);
181 _hb_blob_destroy_user_data (blob);
187 hb_blob_get_length (hb_blob_t *blob)
193 hb_blob_lock (hb_blob_t *blob)
195 if (HB_OBJECT_IS_INERT (blob))
198 hb_mutex_lock (blob->lock);
201 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
202 blob->lock_count, blob->data);
207 hb_mutex_unlock (blob->lock);
213 hb_blob_unlock (hb_blob_t *blob)
215 if (HB_OBJECT_IS_INERT (blob))
218 hb_mutex_lock (blob->lock);
221 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
222 blob->lock_count, blob->data);
225 assert (blob->lock_count > 0);
228 hb_mutex_unlock (blob->lock);
232 hb_blob_is_writable (hb_blob_t *blob)
234 hb_memory_mode_t mode;
236 if (HB_OBJECT_IS_INERT (blob))
239 hb_mutex_lock (blob->lock);
243 hb_mutex_unlock (blob->lock);
245 return mode == HB_MEMORY_MODE_WRITABLE;
250 _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
252 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
253 uintptr_t pagesize = -1, mask, length;
256 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
257 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
258 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
259 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
260 #elif defined(HAVE_GETPAGESIZE)
261 pagesize = (uintptr_t) getpagesize ();
264 if ((uintptr_t) -1L == pagesize) {
266 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno));
271 fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
274 mask = ~(pagesize-1);
275 addr = (const char *) (((uintptr_t) blob->data) & mask);
276 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
278 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n",
280 addr, addr+length, length);
282 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
284 fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
290 fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writable\n",
292 addr, addr+length, length);
301 _try_writable_inplace_locked (hb_blob_t *blob)
304 fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__);
307 if (_try_make_writable_inplace_unix_locked (blob)) {
309 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__);
311 blob->mode = HB_MEMORY_MODE_WRITABLE;
314 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__);
316 /* Failed to make writable inplace, mark that */
317 blob->mode = HB_MEMORY_MODE_READONLY;
322 hb_blob_try_writable_inplace (hb_blob_t *blob)
324 hb_memory_mode_t mode;
326 if (HB_OBJECT_IS_INERT (blob))
329 hb_mutex_lock (blob->lock);
331 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
332 _try_writable_inplace_locked (blob);
336 hb_mutex_unlock (blob->lock);
338 return mode == HB_MEMORY_MODE_WRITABLE;
342 hb_blob_try_writable (hb_blob_t *blob)
344 hb_memory_mode_t mode;
346 if (HB_OBJECT_IS_INERT (blob))
349 hb_mutex_lock (blob->lock);
351 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
352 _try_writable_inplace_locked (blob);
354 if (blob->mode == HB_MEMORY_MODE_READONLY)
359 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
360 blob->lock_count, blob->data);
363 if (blob->lock_count)
366 new_data = malloc (blob->length);
369 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
371 memcpy (new_data, blob->data, blob->length);
372 _hb_blob_destroy_user_data (blob);
373 blob->mode = HB_MEMORY_MODE_WRITABLE;
374 blob->data = new_data;
375 blob->destroy = free;
376 blob->user_data = new_data;
383 hb_mutex_unlock (blob->lock);
385 return mode == HB_MEMORY_MODE_WRITABLE;