OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 Red Hat, Inc. |
| 3 * |
| 4 * This is part of HarfBuzz, a text shaping library. |
| 5 * |
| 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 |
| 29 #include "hb-blob-private.h" |
| 30 |
| 31 #ifdef HAVE_SYS_MMAN_H |
| 32 #ifdef HAVE_UNISTD_H |
| 33 #include <unistd.h> |
| 34 #endif /* HAVE_UNISTD_H */ |
| 35 #include <sys/mman.h> |
| 36 #endif /* HAVE_SYS_MMAN_H */ |
| 37 |
| 38 #include <stdio.h> |
| 39 #include <errno.h> |
| 40 |
| 41 HB_BEGIN_DECLS |
| 42 |
| 43 |
| 44 #ifndef HB_DEBUG_BLOB |
| 45 #define HB_DEBUG_BLOB (HB_DEBUG+0) |
| 46 #endif |
| 47 |
| 48 hb_blob_t _hb_blob_nil = { |
| 49 HB_REFERENCE_COUNT_INVALID, /* ref_count */ |
| 50 |
| 51 0, /* length */ |
| 52 |
| 53 HB_MUTEX_INIT, /* lock */ |
| 54 |
| 55 0, /* lock_count */ |
| 56 HB_MEMORY_MODE_READONLY, /* mode */ |
| 57 |
| 58 NULL, /* data */ |
| 59 |
| 60 NULL, /* destroy */ |
| 61 NULL /* user_data */ |
| 62 }; |
| 63 |
| 64 static 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 |
| 74 static void |
| 75 _hb_blob_unlock_and_destroy (hb_blob_t *blob) |
| 76 { |
| 77 hb_blob_unlock (blob); |
| 78 hb_blob_destroy (blob); |
| 79 } |
| 80 |
| 81 hb_blob_t * |
| 82 hb_blob_create (const char *data, |
| 83 unsigned int length, |
| 84 hb_memory_mode_t mode, |
| 85 hb_destroy_func_t destroy, |
| 86 void *user_data) |
| 87 { |
| 88 hb_blob_t *blob; |
| 89 |
| 90 if (!length || !HB_OBJECT_DO_CREATE (hb_blob_t, blob)) { |
| 91 if (destroy) |
| 92 destroy (user_data); |
| 93 return &_hb_blob_nil; |
| 94 } |
| 95 |
| 96 hb_mutex_init (blob->lock); |
| 97 blob->lock_count = 0; |
| 98 |
| 99 blob->data = data; |
| 100 blob->length = length; |
| 101 blob->mode = mode; |
| 102 |
| 103 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; |
| 108 if (!hb_blob_try_writable (blob)) { |
| 109 hb_blob_destroy (blob); |
| 110 return &_hb_blob_nil; |
| 111 } |
| 112 } |
| 113 |
| 114 return blob; |
| 115 } |
| 116 |
| 117 hb_blob_t * |
| 118 hb_blob_create_sub_blob (hb_blob_t *parent, |
| 119 unsigned int offset, |
| 120 unsigned int length) |
| 121 { |
| 122 hb_blob_t *blob; |
| 123 const char *pdata; |
| 124 |
| 125 if (!length || offset >= parent->length || !HB_OBJECT_DO_CREATE (hb_blob_t, bl
ob)) |
| 126 return &_hb_blob_nil; |
| 127 |
| 128 pdata = hb_blob_lock (parent); |
| 129 |
| 130 blob->data = pdata + offset; |
| 131 blob->length = MIN (length, parent->length - offset); |
| 132 |
| 133 hb_mutex_lock (parent->lock); |
| 134 blob->mode = parent->mode; |
| 135 hb_mutex_unlock (parent->lock); |
| 136 |
| 137 blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy; |
| 138 blob->user_data = hb_blob_reference (parent); |
| 139 |
| 140 return blob; |
| 141 } |
| 142 |
| 143 hb_blob_t * |
| 144 hb_blob_create_empty (void) |
| 145 { |
| 146 return &_hb_blob_nil; |
| 147 } |
| 148 |
| 149 hb_blob_t * |
| 150 hb_blob_reference (hb_blob_t *blob) |
| 151 { |
| 152 HB_OBJECT_DO_REFERENCE (blob); |
| 153 } |
| 154 |
| 155 unsigned int |
| 156 hb_blob_get_reference_count (hb_blob_t *blob) |
| 157 { |
| 158 HB_OBJECT_DO_GET_REFERENCE_COUNT (blob); |
| 159 } |
| 160 |
| 161 void |
| 162 hb_blob_destroy (hb_blob_t *blob) |
| 163 { |
| 164 HB_OBJECT_DO_DESTROY (blob); |
| 165 |
| 166 _hb_blob_destroy_user_data (blob); |
| 167 |
| 168 free (blob); |
| 169 } |
| 170 |
| 171 unsigned int |
| 172 hb_blob_get_length (hb_blob_t *blob) |
| 173 { |
| 174 return blob->length; |
| 175 } |
| 176 |
| 177 const char * |
| 178 hb_blob_lock (hb_blob_t *blob) |
| 179 { |
| 180 if (HB_OBJECT_IS_INERT (blob)) |
| 181 return NULL; |
| 182 |
| 183 hb_mutex_lock (blob->lock); |
| 184 |
| 185 (void) (HB_DEBUG_BLOB && |
| 186 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, |
| 187 blob->lock_count, blob->data)); |
| 188 |
| 189 blob->lock_count++; |
| 190 |
| 191 hb_mutex_unlock (blob->lock); |
| 192 |
| 193 return blob->data; |
| 194 } |
| 195 |
| 196 void |
| 197 hb_blob_unlock (hb_blob_t *blob) |
| 198 { |
| 199 if (HB_OBJECT_IS_INERT (blob)) |
| 200 return; |
| 201 |
| 202 hb_mutex_lock (blob->lock); |
| 203 |
| 204 (void) (HB_DEBUG_BLOB && |
| 205 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, |
| 206 blob->lock_count, blob->data)); |
| 207 |
| 208 assert (blob->lock_count > 0); |
| 209 blob->lock_count--; |
| 210 |
| 211 hb_mutex_unlock (blob->lock); |
| 212 } |
| 213 |
| 214 hb_bool_t |
| 215 hb_blob_is_writable (hb_blob_t *blob) |
| 216 { |
| 217 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 |
| 228 return mode == HB_MEMORY_MODE_WRITABLE; |
| 229 } |
| 230 |
| 231 |
| 232 static hb_bool_t |
| 233 _try_make_writable_inplace_unix_locked (hb_blob_t *blob) |
| 234 { |
| 235 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT) |
| 236 uintptr_t pagesize = -1, mask, length; |
| 237 const char *addr; |
| 238 |
| 239 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE) |
| 240 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE); |
| 241 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 242 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE); |
| 243 #elif defined(HAVE_GETPAGESIZE) |
| 244 pagesize = (uintptr_t) getpagesize (); |
| 245 #endif |
| 246 |
| 247 if ((uintptr_t) -1L == pagesize) { |
| 248 (void) (HB_DEBUG_BLOB && |
| 249 fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__
, strerror (errno))); |
| 250 return FALSE; |
| 251 } |
| 252 (void) (HB_DEBUG_BLOB && |
| 253 fprintf (stderr, "%p %s: pagesize is %lu\n", blob, __FUNCTION__, (unsigned l
ong) pagesize)); |
| 254 |
| 255 mask = ~(pagesize-1); |
| 256 addr = (const char *) (((uintptr_t) blob->data) & mask); |
| 257 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1)
& mask) - addr; |
| 258 (void) (HB_DEBUG_BLOB && |
| 259 fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n", |
| 260 blob, __FUNCTION__, |
| 261 addr, addr+length, (unsigned long) length)); |
| 262 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) { |
| 263 (void) (HB_DEBUG_BLOB && |
| 264 fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno))); |
| 265 return FALSE; |
| 266 } |
| 267 |
| 268 (void) (HB_DEBUG_BLOB && |
| 269 fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n", |
| 270 blob, __FUNCTION__, |
| 271 addr, addr+length, (unsigned long) length)); |
| 272 return TRUE; |
| 273 #else |
| 274 return FALSE; |
| 275 #endif |
| 276 } |
| 277 |
| 278 static void |
| 279 try_writable_inplace_locked (hb_blob_t *blob) |
| 280 { |
| 281 (void) (HB_DEBUG_BLOB && |
| 282 fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__)); |
| 283 |
| 284 if (_try_make_writable_inplace_unix_locked (blob)) { |
| 285 (void) (HB_DEBUG_BLOB && |
| 286 fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION
__)); |
| 287 blob->mode = HB_MEMORY_MODE_WRITABLE; |
| 288 } else { |
| 289 (void) (HB_DEBUG_BLOB && |
| 290 fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__)
); |
| 291 /* Failed to make writable inplace, mark that */ |
| 292 blob->mode = HB_MEMORY_MODE_READONLY; |
| 293 } |
| 294 } |
| 295 |
| 296 hb_bool_t |
| 297 hb_blob_try_writable_inplace (hb_blob_t *blob) |
| 298 { |
| 299 hb_memory_mode_t mode; |
| 300 |
| 301 if (HB_OBJECT_IS_INERT (blob)) |
| 302 return FALSE; |
| 303 |
| 304 hb_mutex_lock (blob->lock); |
| 305 |
| 306 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE) |
| 307 try_writable_inplace_locked (blob); |
| 308 |
| 309 mode = blob->mode; |
| 310 |
| 311 hb_mutex_unlock (blob->lock); |
| 312 |
| 313 return mode == HB_MEMORY_MODE_WRITABLE; |
| 314 } |
| 315 |
| 316 hb_bool_t |
| 317 hb_blob_try_writable (hb_blob_t *blob) |
| 318 { |
| 319 hb_memory_mode_t mode; |
| 320 |
| 321 if (HB_OBJECT_IS_INERT (blob)) |
| 322 return FALSE; |
| 323 |
| 324 hb_mutex_lock (blob->lock); |
| 325 |
| 326 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE) |
| 327 try_writable_inplace_locked (blob); |
| 328 |
| 329 if (blob->mode == HB_MEMORY_MODE_READONLY) |
| 330 { |
| 331 char *new_data; |
| 332 |
| 333 (void) (HB_DEBUG_BLOB && |
| 334 fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, |
| 335 blob->lock_count, blob->data)); |
| 336 |
| 337 if (blob->lock_count) |
| 338 goto done; |
| 339 |
| 340 new_data = malloc (blob->length); |
| 341 if (new_data) { |
| 342 (void) (HB_DEBUG_BLOB && |
| 343 fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION_
_, blob->data)); |
| 344 memcpy (new_data, blob->data, blob->length); |
| 345 _hb_blob_destroy_user_data (blob); |
| 346 blob->mode = HB_MEMORY_MODE_WRITABLE; |
| 347 blob->data = new_data; |
| 348 blob->destroy = free; |
| 349 blob->user_data = new_data; |
| 350 } |
| 351 } |
| 352 |
| 353 done: |
| 354 mode = blob->mode; |
| 355 |
| 356 hb_mutex_unlock (blob->lock); |
| 357 |
| 358 return mode == HB_MEMORY_MODE_WRITABLE; |
| 359 } |
| 360 |
| 361 |
| 362 HB_END_DECLS |
OLD | NEW |