blob: 8013de1ff1e2af5f9c7e20e0929df43f023ba277 [file] [log] [blame]
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04001/*
2 * Copyright (C) 2009 Red Hat, Inc.
3 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04004 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbodf0954d12009-07-30 15:33:57 -04005 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
27#include "hb-private.h"
28
Behdad Esfahbod1cebfbb2010-04-23 20:49:18 -040029#include "hb-blob-private.h"
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040030
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040031#ifdef HAVE_SYS_MMAN_H
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040032#ifdef HAVE_UNISTD_H
Behdad Esfahboda2644242009-08-03 17:53:29 -040033#include <unistd.h>
Behdad Esfahbodc486ea92009-08-12 19:36:29 -040034#endif /* HAVE_UNISTD_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040035#include <sys/mman.h>
Behdad Esfahbodec90ee22009-08-13 05:25:23 -040036#endif /* HAVE_SYS_MMAN_H */
Behdad Esfahboda2644242009-08-03 17:53:29 -040037
Behdad Esfahbod95e20242009-08-28 16:31:20 -040038#ifndef HB_DEBUG_BLOB
39#define HB_DEBUG_BLOB HB_DEBUG
40#endif
41
Behdad Esfahbodd0351312009-11-05 16:16:06 -050042#if HB_DEBUG_BLOB
43#include <stdio.h>
Behdad Esfahbodbcc04062009-11-05 19:54:23 -050044#include <errno.h>
Behdad Esfahbodd0351312009-11-05 16:16:06 -050045#endif
46
Behdad Esfahbod1cebfbb2010-04-23 20:49:18 -040047hb_blob_t _hb_blob_nil = {
Behdad Esfahbod35a73832009-08-01 19:30:31 -040048 HB_REFERENCE_COUNT_INVALID, /* ref_count */
Behdad Esfahbode97a95f2009-08-01 19:05:44 -040049
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -040050 0, /* length */
51
52 HB_MUTEX_INIT, /* lock */
53
54 0, /* lock_count */
Behdad Esfahbod388ad032009-08-19 16:45:41 -040055 HB_MEMORY_MODE_READONLY, /* mode */
Behdad Esfahboda2644242009-08-03 17:53:29 -040056
Behdad Esfahbod35a73832009-08-01 19:30:31 -040057 NULL, /* data */
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040058
Behdad Esfahbod35a73832009-08-01 19:30:31 -040059 NULL, /* destroy */
60 NULL /* user_data */
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040061};
62
63static void
64_hb_blob_destroy_user_data (hb_blob_t *blob)
65{
66 if (blob->destroy) {
67 blob->destroy (blob->user_data);
68 blob->destroy = NULL;
69 blob->user_data = NULL;
70 }
71}
72
Behdad Esfahboda2644242009-08-03 17:53:29 -040073static void
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040074_hb_blob_unlock_and_destroy (hb_blob_t *blob)
Behdad Esfahboda2644242009-08-03 17:53:29 -040075{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040076 hb_blob_unlock (blob);
77 hb_blob_destroy (blob);
Behdad Esfahboda2644242009-08-03 17:53:29 -040078}
79
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040080hb_blob_t *
81hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040082 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040083 hb_memory_mode_t mode,
84 hb_destroy_func_t destroy,
85 void *user_data)
86{
87 hb_blob_t *blob;
88
Behdad Esfahbod5fc22e62009-08-03 22:43:02 -040089 if (!length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob)) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040090 if (destroy)
91 destroy (user_data);
92 return &_hb_blob_nil;
93 }
94
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -040095 hb_mutex_init (blob->lock);
96 blob->lock_count = 0;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040097
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040098 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -040099 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400100 blob->mode = mode;
101
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400102 blob->destroy = destroy;
103 blob->user_data = user_data;
104
105 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
106 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400107 if (!hb_blob_try_writable (blob)) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400108 hb_blob_destroy (blob);
109 return &_hb_blob_nil;
110 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400111 }
112
113 return blob;
114}
115
116hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400117hb_blob_create_sub_blob (hb_blob_t *parent,
118 unsigned int offset,
119 unsigned int length)
120{
121 hb_blob_t *blob;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400122 const char *pdata;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400123
Behdad Esfahbod5fc22e62009-08-03 22:43:02 -0400124 if (!length || offset >= parent->length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob))
Behdad Esfahboda2644242009-08-03 17:53:29 -0400125 return &_hb_blob_nil;
126
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400127 pdata = hb_blob_lock (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400128
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400129 blob->data = pdata + offset;
130 blob->length = MIN (length, parent->length - offset);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400131
132 hb_mutex_lock (parent->lock);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400133 blob->mode = parent->mode;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400134 hb_mutex_unlock (parent->lock);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400135
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400136 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400137 blob->user_data = hb_blob_reference (parent);
138
Behdad Esfahboda2644242009-08-03 17:53:29 -0400139 return blob;
140}
141
142hb_blob_t *
143hb_blob_create_empty (void)
144{
145 return &_hb_blob_nil;
146}
147
148hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400149hb_blob_reference (hb_blob_t *blob)
150{
Behdad Esfahbodc62b5032009-08-01 19:54:49 -0400151 HB_OBJECT_DO_REFERENCE (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400152}
153
Behdad Esfahboda12dd322009-08-01 21:36:15 -0400154unsigned int
155hb_blob_get_reference_count (hb_blob_t *blob)
156{
157 HB_OBJECT_DO_GET_REFERENCE_COUNT (blob);
158}
159
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400160void
161hb_blob_destroy (hb_blob_t *blob)
162{
Behdad Esfahbodc62b5032009-08-01 19:54:49 -0400163 HB_OBJECT_DO_DESTROY (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400164
165 _hb_blob_destroy_user_data (blob);
166
167 free (blob);
168}
169
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400170unsigned int
171hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400172{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400173 return blob->length;
174}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400175
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400176const char *
177hb_blob_lock (hb_blob_t *blob)
178{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400179 if (HB_OBJECT_IS_INERT (blob))
180 return NULL;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400181
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400182 hb_mutex_lock (blob->lock);
183
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400184#if HB_DEBUG_BLOB
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400185 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
186 blob->lock_count, blob->data);
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400187#endif
188
Behdad Esfahbod5a11c872009-11-05 20:08:17 -0500189 blob->lock_count++;
190
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400191 hb_mutex_unlock (blob->lock);
192
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400193 return blob->data;
194}
195
196void
197hb_blob_unlock (hb_blob_t *blob)
198{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400199 if (HB_OBJECT_IS_INERT (blob))
200 return;
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400201
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400202 hb_mutex_lock (blob->lock);
203
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400204#if HB_DEBUG_BLOB
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400205 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
Behdad Esfahbod5a11c872009-11-05 20:08:17 -0500206 blob->lock_count, blob->data);
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400207#endif
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400208
Behdad Esfahbod5a11c872009-11-05 20:08:17 -0500209 assert (blob->lock_count > 0);
210 blob->lock_count--;
211
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400212 hb_mutex_unlock (blob->lock);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400213}
214
215hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400216hb_blob_is_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400217{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400218 hb_memory_mode_t mode;
219
220 if (HB_OBJECT_IS_INERT (blob))
221 return FALSE;
222
223 hb_mutex_lock (blob->lock);
224
225 mode = blob->mode;
226
227 hb_mutex_unlock (blob->lock);
228
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400229 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400230}
231
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400232
233static hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400234_try_make_writable_inplace_unix_locked (hb_blob_t *blob)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400235{
236#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500237 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400238 const char *addr;
239
240#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500241 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400242#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500243 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400244#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500245 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400246#endif
247
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500248 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400249#if HB_DEBUG_BLOB
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400250 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno));
251#endif
252 return FALSE;
253 }
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400254#if HB_DEBUG_BLOB
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400255 fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
256#endif
257
258 mask = ~(pagesize-1);
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500259 addr = (const char *) (((uintptr_t) blob->data) & mask);
260 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400261#if HB_DEBUG_BLOB
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400262 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n",
263 blob, __FUNCTION__,
264 addr, addr+length, length);
265#endif
266 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400267#if HB_DEBUG_BLOB
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400268 fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
269#endif
270 return FALSE;
271 }
272
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400273#if HB_DEBUG_BLOB
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400274 fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writable\n",
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400275 blob, __FUNCTION__,
276 addr, addr+length, length);
277#endif
278 return TRUE;
279#else
280 return FALSE;
281#endif
282}
283
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400284static void
285_try_writable_inplace_locked (hb_blob_t *blob)
286{
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400287#if HB_DEBUG_BLOB
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400288 fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__);
289#endif
290
291 if (_try_make_writable_inplace_unix_locked (blob)) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400292#if HB_DEBUG_BLOB
Behdad Esfahbod81a5c4d2009-08-27 00:21:04 -0400293 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__);
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400294#endif
295 blob->mode = HB_MEMORY_MODE_WRITABLE;
296 } else {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400297#if HB_DEBUG_BLOB
Behdad Esfahbod81a5c4d2009-08-27 00:21:04 -0400298 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__);
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400299#endif
300 /* Failed to make writable inplace, mark that */
301 blob->mode = HB_MEMORY_MODE_READONLY;
302 }
303}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400304
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400305hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400306hb_blob_try_writable_inplace (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400307{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400308 hb_memory_mode_t mode;
309
310 if (HB_OBJECT_IS_INERT (blob))
311 return FALSE;
312
313 hb_mutex_lock (blob->lock);
314
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400315 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
316 _try_writable_inplace_locked (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400317
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400318 mode = blob->mode;
319
320 hb_mutex_unlock (blob->lock);
321
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400322 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400323}
324
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400325hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400326hb_blob_try_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400327{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400328 hb_memory_mode_t mode;
329
330 if (HB_OBJECT_IS_INERT (blob))
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400331 return FALSE;
332
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400333 hb_mutex_lock (blob->lock);
334
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400335 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
336 _try_writable_inplace_locked (blob);
337
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400338 if (blob->mode == HB_MEMORY_MODE_READONLY)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400339 {
340 char *new_data;
341
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400342#if HB_DEBUG_BLOB
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400343 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
344 blob->lock_count, blob->data);
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400345#endif
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400346
347 if (blob->lock_count)
348 goto done;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400349
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400350 new_data = malloc (blob->length);
351 if (new_data) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400352#if HB_DEBUG_BLOB
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400353 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
354#endif
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400355 memcpy (new_data, blob->data, blob->length);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400356 _hb_blob_destroy_user_data (blob);
Behdad Esfahbod12b27ed2010-03-27 17:00:19 -0400357 blob->mode = HB_MEMORY_MODE_WRITABLE;
358 blob->data = new_data;
359 blob->destroy = free;
360 blob->user_data = new_data;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400361 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400362 }
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400363
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400364done:
365 mode = blob->mode;
366
367 hb_mutex_unlock (blob->lock);
368
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400369 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400370}