2 * Copyright (C) 2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine 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
43 hb_reference_count_t ref_count;
48 /* the rest are protected by lock */
50 unsigned int lock_count;
51 hb_memory_mode_t mode;
55 hb_destroy_func_t destroy;
58 static hb_blob_t _hb_blob_nil = {
59 HB_REFERENCE_COUNT_INVALID, /* ref_count */
63 HB_MUTEX_INIT, /* lock */
66 HB_MEMORY_MODE_READONLY, /* mode */
75 _hb_blob_destroy_user_data (hb_blob_t *blob)
78 blob->destroy (blob->user_data);
80 blob->user_data = NULL;
85 _hb_blob_unlock_and_destroy (hb_blob_t *blob)
87 hb_blob_unlock (blob);
88 hb_blob_destroy (blob);
92 hb_blob_create (const char *data,
94 hb_memory_mode_t mode,
95 hb_destroy_func_t destroy,
100 if (!length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob)) {
103 return &_hb_blob_nil;
106 hb_mutex_init (blob->lock);
107 blob->lock_count = 0;
110 blob->length = length;
113 blob->destroy = destroy;
114 blob->user_data = user_data;
116 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
117 blob->mode = HB_MEMORY_MODE_READONLY;
118 if (!hb_blob_try_writable (blob)) {
119 hb_blob_destroy (blob);
120 return &_hb_blob_nil;
128 hb_blob_create_sub_blob (hb_blob_t *parent,
135 if (!length || offset >= parent->length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob))
136 return &_hb_blob_nil;
138 pdata = hb_blob_lock (parent);
140 blob->data = pdata + offset;
141 blob->length = MIN (length, parent->length - offset);
143 hb_mutex_lock (parent->lock);
144 blob->mode = parent->mode;
145 hb_mutex_unlock (parent->lock);
147 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
148 blob->user_data = hb_blob_reference (parent);
154 hb_blob_create_empty (void)
156 return &_hb_blob_nil;
160 hb_blob_reference (hb_blob_t *blob)
162 HB_OBJECT_DO_REFERENCE (blob);
166 hb_blob_get_reference_count (hb_blob_t *blob)
168 HB_OBJECT_DO_GET_REFERENCE_COUNT (blob);
172 hb_blob_destroy (hb_blob_t *blob)
174 HB_OBJECT_DO_DESTROY (blob);
176 _hb_blob_destroy_user_data (blob);
182 hb_blob_get_length (hb_blob_t *blob)
188 hb_blob_lock (hb_blob_t *blob)
190 if (HB_OBJECT_IS_INERT (blob))
193 hb_mutex_lock (blob->lock);
197 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
198 blob->lock_count, blob->data);
201 hb_mutex_unlock (blob->lock);
207 hb_blob_unlock (hb_blob_t *blob)
209 if (HB_OBJECT_IS_INERT (blob))
212 hb_mutex_lock (blob->lock);
214 assert (blob->lock_count > 0);
217 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
218 hb_atomic_int_get (blob->lock_count), blob->data);
221 hb_mutex_unlock (blob->lock);
225 hb_blob_is_writable (hb_blob_t *blob)
227 hb_memory_mode_t mode;
229 if (HB_OBJECT_IS_INERT (blob))
232 hb_mutex_lock (blob->lock);
236 hb_mutex_unlock (blob->lock);
238 return mode == HB_MEMORY_MODE_WRITABLE;
243 _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
245 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
246 unsigned int pagesize = -1, mask, length;
249 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
250 pagesize = (unsigned int) sysconf (_SC_PAGE_SIZE);
251 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
252 pagesize = (unsigned int) sysconf (_SC_PAGESIZE);
253 #elif defined(HAVE_GETPAGESIZE)
254 pagesize = (unsigned int) getpagesize ();
257 if ((unsigned int) -1 == pagesize) {
259 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno));
264 fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
267 mask = ~(pagesize-1);
268 addr = (const char *) (((size_t) blob->data) & mask);
269 length = (const char *) (((size_t) blob->data + blob->length + pagesize-1) & mask) - addr;
271 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n",
273 addr, addr+length, length);
275 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
277 fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
283 fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writable\n",
285 addr, addr+length, length);
294 _try_writable_inplace_locked (hb_blob_t *blob)
297 fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__);
300 if (_try_make_writable_inplace_unix_locked (blob)) {
302 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__);
304 blob->mode = HB_MEMORY_MODE_WRITABLE;
307 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__);
309 /* Failed to make writable inplace, mark that */
310 blob->mode = HB_MEMORY_MODE_READONLY;
315 hb_blob_try_writable_inplace (hb_blob_t *blob)
317 hb_memory_mode_t mode;
319 if (HB_OBJECT_IS_INERT (blob))
322 hb_mutex_lock (blob->lock);
324 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
325 _try_writable_inplace_locked (blob);
329 hb_mutex_unlock (blob->lock);
331 return mode == HB_MEMORY_MODE_WRITABLE;
335 hb_blob_try_writable (hb_blob_t *blob)
337 hb_memory_mode_t mode;
339 if (HB_OBJECT_IS_INERT (blob))
342 hb_mutex_lock (blob->lock);
344 if (blob->mode == HB_MEMORY_MODE_READONLY)
349 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
350 blob->lock_count, blob->data);
353 if (blob->lock_count)
356 new_data = malloc (blob->length);
359 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
361 memcpy (new_data, blob->data, blob->length);
362 blob->data = new_data;
363 blob->mode = HB_MEMORY_MODE_WRITABLE;
364 _hb_blob_destroy_user_data (blob);
367 else if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
368 _try_writable_inplace_locked (blob);
373 hb_mutex_unlock (blob->lock);
375 return mode == HB_MEMORY_MODE_WRITABLE;