blob: 926481dbda406d44cfa1b2424732fe6249de1fc1 [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 Esfahbodacdba3f2010-07-23 15:11:18 -040038#include <stdio.h>
39#include <errno.h>
40
41HB_BEGIN_DECLS
42
43
Behdad Esfahbod95e20242009-08-28 16:31:20 -040044#ifndef HB_DEBUG_BLOB
Behdad Esfahbod444fffb2010-04-28 13:16:38 -040045#define HB_DEBUG_BLOB HB_DEBUG+0
Behdad Esfahbod95e20242009-08-28 16:31:20 -040046#endif
47
Behdad Esfahbod1cebfbb2010-04-23 20:49:18 -040048hb_blob_t _hb_blob_nil = {
Behdad Esfahbod35a73832009-08-01 19:30:31 -040049 HB_REFERENCE_COUNT_INVALID, /* ref_count */
Behdad Esfahbode97a95f2009-08-01 19:05:44 -040050
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -040051 0, /* length */
52
53 HB_MUTEX_INIT, /* lock */
54
55 0, /* lock_count */
Behdad Esfahbod388ad032009-08-19 16:45:41 -040056 HB_MEMORY_MODE_READONLY, /* mode */
Behdad Esfahboda2644242009-08-03 17:53:29 -040057
Behdad Esfahbod35a73832009-08-01 19:30:31 -040058 NULL, /* data */
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040059
Behdad Esfahbod35a73832009-08-01 19:30:31 -040060 NULL, /* destroy */
61 NULL /* user_data */
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040062};
63
64static void
65_hb_blob_destroy_user_data (hb_blob_t *blob)
66{
67 if (blob->destroy) {
68 blob->destroy (blob->user_data);
69 blob->destroy = NULL;
70 blob->user_data = NULL;
71 }
72}
73
Behdad Esfahboda2644242009-08-03 17:53:29 -040074static void
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040075_hb_blob_unlock_and_destroy (hb_blob_t *blob)
Behdad Esfahboda2644242009-08-03 17:53:29 -040076{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040077 hb_blob_unlock (blob);
78 hb_blob_destroy (blob);
Behdad Esfahboda2644242009-08-03 17:53:29 -040079}
80
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040081hb_blob_t *
82hb_blob_create (const char *data,
Behdad Esfahboda2644242009-08-03 17:53:29 -040083 unsigned int length,
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040084 hb_memory_mode_t mode,
85 hb_destroy_func_t destroy,
86 void *user_data)
87{
88 hb_blob_t *blob;
89
Behdad Esfahbod5fc22e62009-08-03 22:43:02 -040090 if (!length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob)) {
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040091 if (destroy)
92 destroy (user_data);
93 return &_hb_blob_nil;
94 }
95
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -040096 hb_mutex_init (blob->lock);
97 blob->lock_count = 0;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -040098
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040099 blob->data = data;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400100 blob->length = length;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400101 blob->mode = mode;
102
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400103 blob->destroy = destroy;
104 blob->user_data = user_data;
105
106 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
107 blob->mode = HB_MEMORY_MODE_READONLY;
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400108 if (!hb_blob_try_writable (blob)) {
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400109 hb_blob_destroy (blob);
110 return &_hb_blob_nil;
111 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400112 }
113
114 return blob;
115}
116
117hb_blob_t *
Behdad Esfahboda2644242009-08-03 17:53:29 -0400118hb_blob_create_sub_blob (hb_blob_t *parent,
119 unsigned int offset,
120 unsigned int length)
121{
122 hb_blob_t *blob;
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400123 const char *pdata;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400124
Behdad Esfahbod5fc22e62009-08-03 22:43:02 -0400125 if (!length || offset >= parent->length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob))
Behdad Esfahboda2644242009-08-03 17:53:29 -0400126 return &_hb_blob_nil;
127
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400128 pdata = hb_blob_lock (parent);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400129
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400130 blob->data = pdata + offset;
131 blob->length = MIN (length, parent->length - offset);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400132
133 hb_mutex_lock (parent->lock);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400134 blob->mode = parent->mode;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400135 hb_mutex_unlock (parent->lock);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400136
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400137 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400138 blob->user_data = hb_blob_reference (parent);
139
Behdad Esfahboda2644242009-08-03 17:53:29 -0400140 return blob;
141}
142
143hb_blob_t *
144hb_blob_create_empty (void)
145{
146 return &_hb_blob_nil;
147}
148
149hb_blob_t *
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400150hb_blob_reference (hb_blob_t *blob)
151{
Behdad Esfahbodc62b5032009-08-01 19:54:49 -0400152 HB_OBJECT_DO_REFERENCE (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400153}
154
Behdad Esfahboda12dd322009-08-01 21:36:15 -0400155unsigned int
156hb_blob_get_reference_count (hb_blob_t *blob)
157{
158 HB_OBJECT_DO_GET_REFERENCE_COUNT (blob);
159}
160
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400161void
162hb_blob_destroy (hb_blob_t *blob)
163{
Behdad Esfahbodc62b5032009-08-01 19:54:49 -0400164 HB_OBJECT_DO_DESTROY (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400165
166 _hb_blob_destroy_user_data (blob);
167
168 free (blob);
169}
170
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400171unsigned int
172hb_blob_get_length (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400173{
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400174 return blob->length;
175}
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400176
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400177const char *
178hb_blob_lock (hb_blob_t *blob)
179{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400180 if (HB_OBJECT_IS_INERT (blob))
181 return NULL;
Behdad Esfahboda2644242009-08-03 17:53:29 -0400182
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400183 hb_mutex_lock (blob->lock);
184
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400185 if (HB_DEBUG_BLOB)
186 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
187 blob->lock_count, blob->data);
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400188
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 Esfahbod444fffb2010-04-28 13:16:38 -0400204 if (HB_DEBUG_BLOB)
205 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
206 blob->lock_count, blob->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400207
Behdad Esfahbod5a11c872009-11-05 20:08:17 -0500208 assert (blob->lock_count > 0);
209 blob->lock_count--;
210
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400211 hb_mutex_unlock (blob->lock);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400212}
213
214hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400215hb_blob_is_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400216{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400217 hb_memory_mode_t mode;
218
219 if (HB_OBJECT_IS_INERT (blob))
220 return FALSE;
221
222 hb_mutex_lock (blob->lock);
223
224 mode = blob->mode;
225
226 hb_mutex_unlock (blob->lock);
227
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400228 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400229}
230
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400231
232static hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400233_try_make_writable_inplace_unix_locked (hb_blob_t *blob)
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400234{
235#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500236 uintptr_t pagesize = -1, mask, length;
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400237 const char *addr;
238
239#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500240 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400241#elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500242 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400243#elif defined(HAVE_GETPAGESIZE)
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500244 pagesize = (uintptr_t) getpagesize ();
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400245#endif
246
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500247 if ((uintptr_t) -1L == pagesize) {
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400248 if (HB_DEBUG_BLOB)
249 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno));
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400250 return FALSE;
251 }
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400252 if (HB_DEBUG_BLOB)
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700253 fprintf (stderr, "%p %s: pagesize is %lu\n", blob, __FUNCTION__, (unsigned long) pagesize);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400254
255 mask = ~(pagesize-1);
Behdad Esfahbod917c2272010-02-23 16:47:51 -0500256 addr = (const char *) (((uintptr_t) blob->data) & mask);
257 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400258 if (HB_DEBUG_BLOB)
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700259 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n",
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400260 blob, __FUNCTION__,
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700261 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400262 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400263 if (HB_DEBUG_BLOB)
264 fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400265 return FALSE;
266 }
267
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400268 if (HB_DEBUG_BLOB)
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700269 fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400270 blob, __FUNCTION__,
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700271 addr, addr+length, (unsigned long) length);
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400272 return TRUE;
273#else
274 return FALSE;
275#endif
276}
277
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400278static void
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400279try_writable_inplace_locked (hb_blob_t *blob)
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400280{
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400281 if (HB_DEBUG_BLOB)
282 fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__);
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400283
284 if (_try_make_writable_inplace_unix_locked (blob)) {
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400285 if (HB_DEBUG_BLOB)
286 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__);
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400287 blob->mode = HB_MEMORY_MODE_WRITABLE;
288 } else {
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400289 if (HB_DEBUG_BLOB)
290 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__);
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400291 /* Failed to make writable inplace, mark that */
292 blob->mode = HB_MEMORY_MODE_READONLY;
293 }
294}
Behdad Esfahbod4ff2a582009-08-18 15:49:23 -0400295
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400296hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400297hb_blob_try_writable_inplace (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400298{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400299 hb_memory_mode_t mode;
300
301 if (HB_OBJECT_IS_INERT (blob))
302 return FALSE;
303
304 hb_mutex_lock (blob->lock);
305
Behdad Esfahbod388ad032009-08-19 16:45:41 -0400306 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400307 try_writable_inplace_locked (blob);
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400308
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400309 mode = blob->mode;
310
311 hb_mutex_unlock (blob->lock);
312
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400313 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400314}
315
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400316hb_bool_t
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400317hb_blob_try_writable (hb_blob_t *blob)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400318{
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400319 hb_memory_mode_t mode;
320
321 if (HB_OBJECT_IS_INERT (blob))
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400322 return FALSE;
323
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400324 hb_mutex_lock (blob->lock);
325
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400326 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400327 try_writable_inplace_locked (blob);
Behdad Esfahbod71e735e2010-04-23 13:48:06 -0400328
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400329 if (blob->mode == HB_MEMORY_MODE_READONLY)
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400330 {
331 char *new_data;
332
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400333 if (HB_DEBUG_BLOB)
334 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
335 blob->lock_count, blob->data);
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400336
337 if (blob->lock_count)
338 goto done;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400339
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400340 new_data = malloc (blob->length);
341 if (new_data) {
Behdad Esfahbod444fffb2010-04-28 13:16:38 -0400342 if (HB_DEBUG_BLOB)
343 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400344 memcpy (new_data, blob->data, blob->length);
Behdad Esfahboda2644242009-08-03 17:53:29 -0400345 _hb_blob_destroy_user_data (blob);
Behdad Esfahbod12b27ed2010-03-27 17:00:19 -0400346 blob->mode = HB_MEMORY_MODE_WRITABLE;
347 blob->data = new_data;
348 blob->destroy = free;
349 blob->user_data = new_data;
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400350 }
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400351 }
Behdad Esfahbodfc6c9402009-08-03 21:27:08 -0400352
Behdad Esfahbod7f3d5c82009-08-06 13:33:51 -0400353done:
354 mode = blob->mode;
355
356 hb_mutex_unlock (blob->lock);
357
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400358 return mode == HB_MEMORY_MODE_WRITABLE;
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400359}
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400360
361
362HB_END_DECLS