gfx/harfbuzz/src/hb-blob.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright © 2009 Red Hat, Inc.
michael@0 3 *
michael@0 4 * This is part of HarfBuzz, a text shaping library.
michael@0 5 *
michael@0 6 * Permission is hereby granted, without written agreement and without
michael@0 7 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 8 * software and its documentation for any purpose, provided that the
michael@0 9 * above copyright notice and the following two paragraphs appear in
michael@0 10 * all copies of this software.
michael@0 11 *
michael@0 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 16 * DAMAGE.
michael@0 17 *
michael@0 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 23 *
michael@0 24 * Red Hat Author(s): Behdad Esfahbod
michael@0 25 */
michael@0 26
michael@0 27 /* http://www.oracle.com/technetwork/articles/servers-storage-dev/standardheaderfiles-453865.html */
michael@0 28 #define _POSIX_C_SOURCE 199309L
michael@0 29
michael@0 30 #include "hb-private.hh"
michael@0 31
michael@0 32 #include "hb-object-private.hh"
michael@0 33
michael@0 34 #ifdef HAVE_SYS_MMAN_H
michael@0 35 #ifdef HAVE_UNISTD_H
michael@0 36 #include <unistd.h>
michael@0 37 #endif /* HAVE_UNISTD_H */
michael@0 38 #include <sys/mman.h>
michael@0 39 #endif /* HAVE_SYS_MMAN_H */
michael@0 40
michael@0 41 #include <stdio.h>
michael@0 42 #include <errno.h>
michael@0 43
michael@0 44
michael@0 45
michael@0 46 #ifndef HB_DEBUG_BLOB
michael@0 47 #define HB_DEBUG_BLOB (HB_DEBUG+0)
michael@0 48 #endif
michael@0 49
michael@0 50
michael@0 51 struct hb_blob_t {
michael@0 52 hb_object_header_t header;
michael@0 53 ASSERT_POD ();
michael@0 54
michael@0 55 bool immutable;
michael@0 56
michael@0 57 const char *data;
michael@0 58 unsigned int length;
michael@0 59 hb_memory_mode_t mode;
michael@0 60
michael@0 61 void *user_data;
michael@0 62 hb_destroy_func_t destroy;
michael@0 63 };
michael@0 64
michael@0 65
michael@0 66 static bool _try_writable (hb_blob_t *blob);
michael@0 67
michael@0 68 static void
michael@0 69 _hb_blob_destroy_user_data (hb_blob_t *blob)
michael@0 70 {
michael@0 71 if (blob->destroy) {
michael@0 72 blob->destroy (blob->user_data);
michael@0 73 blob->user_data = NULL;
michael@0 74 blob->destroy = NULL;
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 /**
michael@0 79 * hb_blob_create: (Xconstructor)
michael@0 80 * @data: (array length=length) (closure user_data) (destroy destroy) (scope notified) (transfer none): Pointer to blob data.
michael@0 81 * @length: Length of @data in bytes.
michael@0 82 * @mode: Memory mode for @data.
michael@0 83 * @user_data: Data parameter to pass to @destroy.
michael@0 84 * @destroy: Callback to call when @data is not needed anymore.
michael@0 85 *
michael@0 86 * Creates a new "blob" object wrapping @data. The @mode parameter is used
michael@0 87 * to negotiate ownership and lifecycle of @data.
michael@0 88 *
michael@0 89 * Return value: New blob, or the empty blob if something failed or if @length is
michael@0 90 * zero. Destroy with hb_blob_destroy().
michael@0 91 *
michael@0 92 * Since: 1.0
michael@0 93 **/
michael@0 94 hb_blob_t *
michael@0 95 hb_blob_create (const char *data,
michael@0 96 unsigned int length,
michael@0 97 hb_memory_mode_t mode,
michael@0 98 void *user_data,
michael@0 99 hb_destroy_func_t destroy)
michael@0 100 {
michael@0 101 hb_blob_t *blob;
michael@0 102
michael@0 103 if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
michael@0 104 if (destroy)
michael@0 105 destroy (user_data);
michael@0 106 return hb_blob_get_empty ();
michael@0 107 }
michael@0 108
michael@0 109 blob->data = data;
michael@0 110 blob->length = length;
michael@0 111 blob->mode = mode;
michael@0 112
michael@0 113 blob->user_data = user_data;
michael@0 114 blob->destroy = destroy;
michael@0 115
michael@0 116 if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
michael@0 117 blob->mode = HB_MEMORY_MODE_READONLY;
michael@0 118 if (!_try_writable (blob)) {
michael@0 119 hb_blob_destroy (blob);
michael@0 120 return hb_blob_get_empty ();
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 return blob;
michael@0 125 }
michael@0 126
michael@0 127 /**
michael@0 128 * hb_blob_create_sub_blob:
michael@0 129 * @parent: Parent blob.
michael@0 130 * @offset: Start offset of sub-blob within @parent, in bytes.
michael@0 131 * @length: Length of sub-blob.
michael@0 132 *
michael@0 133 * Returns a blob that represents a range of bytes in @parent. The new
michael@0 134 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it
michael@0 135 * will never modify data in the parent blob. The parent data is not
michael@0 136 * expected to be modified, and will result in undefined behavior if it
michael@0 137 * is.
michael@0 138 *
michael@0 139 * Makes @parent immutable.
michael@0 140 *
michael@0 141 * Return value: New blob, or the empty blob if something failed or if
michael@0 142 * @length is zero or @offset is beyond the end of @parent's data. Destroy
michael@0 143 * with hb_blob_destroy().
michael@0 144 *
michael@0 145 * Since: 1.0
michael@0 146 **/
michael@0 147 hb_blob_t *
michael@0 148 hb_blob_create_sub_blob (hb_blob_t *parent,
michael@0 149 unsigned int offset,
michael@0 150 unsigned int length)
michael@0 151 {
michael@0 152 hb_blob_t *blob;
michael@0 153
michael@0 154 if (!length || offset >= parent->length)
michael@0 155 return hb_blob_get_empty ();
michael@0 156
michael@0 157 hb_blob_make_immutable (parent);
michael@0 158
michael@0 159 blob = hb_blob_create (parent->data + offset,
michael@0 160 MIN (length, parent->length - offset),
michael@0 161 HB_MEMORY_MODE_READONLY,
michael@0 162 hb_blob_reference (parent),
michael@0 163 (hb_destroy_func_t) hb_blob_destroy);
michael@0 164
michael@0 165 return blob;
michael@0 166 }
michael@0 167
michael@0 168 /**
michael@0 169 * hb_blob_get_empty:
michael@0 170 *
michael@0 171 * Returns the singleton empty blob.
michael@0 172 *
michael@0 173 * See TODO:link object types for more information.
michael@0 174 *
michael@0 175 * Return value: (transfer full): the empty blob.
michael@0 176 *
michael@0 177 * Since: 1.0
michael@0 178 **/
michael@0 179 hb_blob_t *
michael@0 180 hb_blob_get_empty (void)
michael@0 181 {
michael@0 182 static const hb_blob_t _hb_blob_nil = {
michael@0 183 HB_OBJECT_HEADER_STATIC,
michael@0 184
michael@0 185 true, /* immutable */
michael@0 186
michael@0 187 NULL, /* data */
michael@0 188 0, /* length */
michael@0 189 HB_MEMORY_MODE_READONLY, /* mode */
michael@0 190
michael@0 191 NULL, /* user_data */
michael@0 192 NULL /* destroy */
michael@0 193 };
michael@0 194
michael@0 195 return const_cast<hb_blob_t *> (&_hb_blob_nil);
michael@0 196 }
michael@0 197
michael@0 198 /**
michael@0 199 * hb_blob_reference: (skip)
michael@0 200 * @blob: a blob.
michael@0 201 *
michael@0 202 * Increases the reference count on @blob.
michael@0 203 *
michael@0 204 * See TODO:link object types for more information.
michael@0 205 *
michael@0 206 * Return value: @blob.
michael@0 207 *
michael@0 208 * Since: 1.0
michael@0 209 **/
michael@0 210 hb_blob_t *
michael@0 211 hb_blob_reference (hb_blob_t *blob)
michael@0 212 {
michael@0 213 return hb_object_reference (blob);
michael@0 214 }
michael@0 215
michael@0 216 /**
michael@0 217 * hb_blob_destroy: (skip)
michael@0 218 * @blob: a blob.
michael@0 219 *
michael@0 220 * Descreases the reference count on @blob, and if it reaches zero, destroys
michael@0 221 * @blob, freeing all memory, possibly calling the destroy-callback the blob
michael@0 222 * was created for if it has not been called already.
michael@0 223 *
michael@0 224 * See TODO:link object types for more information.
michael@0 225 *
michael@0 226 * Since: 1.0
michael@0 227 **/
michael@0 228 void
michael@0 229 hb_blob_destroy (hb_blob_t *blob)
michael@0 230 {
michael@0 231 if (!hb_object_destroy (blob)) return;
michael@0 232
michael@0 233 _hb_blob_destroy_user_data (blob);
michael@0 234
michael@0 235 free (blob);
michael@0 236 }
michael@0 237
michael@0 238 /**
michael@0 239 * hb_blob_set_user_data: (skip)
michael@0 240 * @blob: a blob.
michael@0 241 * @key: key for data to set.
michael@0 242 * @data: data to set.
michael@0 243 * @destroy: callback to call when @data is not needed anymore.
michael@0 244 * @replace: whether to replace an existing data with the same key.
michael@0 245 *
michael@0 246 * Return value:
michael@0 247 *
michael@0 248 * Since: 1.0
michael@0 249 **/
michael@0 250 hb_bool_t
michael@0 251 hb_blob_set_user_data (hb_blob_t *blob,
michael@0 252 hb_user_data_key_t *key,
michael@0 253 void * data,
michael@0 254 hb_destroy_func_t destroy,
michael@0 255 hb_bool_t replace)
michael@0 256 {
michael@0 257 return hb_object_set_user_data (blob, key, data, destroy, replace);
michael@0 258 }
michael@0 259
michael@0 260 /**
michael@0 261 * hb_blob_get_user_data: (skip)
michael@0 262 * @blob: a blob.
michael@0 263 * @key: key for data to get.
michael@0 264 *
michael@0 265 *
michael@0 266 *
michael@0 267 * Return value: (transfer none):
michael@0 268 *
michael@0 269 * Since: 1.0
michael@0 270 **/
michael@0 271 void *
michael@0 272 hb_blob_get_user_data (hb_blob_t *blob,
michael@0 273 hb_user_data_key_t *key)
michael@0 274 {
michael@0 275 return hb_object_get_user_data (blob, key);
michael@0 276 }
michael@0 277
michael@0 278
michael@0 279 /**
michael@0 280 * hb_blob_make_immutable:
michael@0 281 * @blob: a blob.
michael@0 282 *
michael@0 283 *
michael@0 284 *
michael@0 285 * Since: 1.0
michael@0 286 **/
michael@0 287 void
michael@0 288 hb_blob_make_immutable (hb_blob_t *blob)
michael@0 289 {
michael@0 290 if (hb_object_is_inert (blob))
michael@0 291 return;
michael@0 292
michael@0 293 blob->immutable = true;
michael@0 294 }
michael@0 295
michael@0 296 /**
michael@0 297 * hb_blob_is_immutable:
michael@0 298 * @blob: a blob.
michael@0 299 *
michael@0 300 *
michael@0 301 *
michael@0 302 * Return value: TODO
michael@0 303 *
michael@0 304 * Since: 1.0
michael@0 305 **/
michael@0 306 hb_bool_t
michael@0 307 hb_blob_is_immutable (hb_blob_t *blob)
michael@0 308 {
michael@0 309 return blob->immutable;
michael@0 310 }
michael@0 311
michael@0 312
michael@0 313 /**
michael@0 314 * hb_blob_get_length:
michael@0 315 * @blob: a blob.
michael@0 316 *
michael@0 317 *
michael@0 318 *
michael@0 319 * Return value: the length of blob data in bytes.
michael@0 320 *
michael@0 321 * Since: 1.0
michael@0 322 **/
michael@0 323 unsigned int
michael@0 324 hb_blob_get_length (hb_blob_t *blob)
michael@0 325 {
michael@0 326 return blob->length;
michael@0 327 }
michael@0 328
michael@0 329 /**
michael@0 330 * hb_blob_get_data:
michael@0 331 * @blob: a blob.
michael@0 332 * @length: (out):
michael@0 333 *
michael@0 334 *
michael@0 335 *
michael@0 336 * Returns: (transfer none) (array length=length):
michael@0 337 *
michael@0 338 * Since: 1.0
michael@0 339 **/
michael@0 340 const char *
michael@0 341 hb_blob_get_data (hb_blob_t *blob, unsigned int *length)
michael@0 342 {
michael@0 343 if (length)
michael@0 344 *length = blob->length;
michael@0 345
michael@0 346 return blob->data;
michael@0 347 }
michael@0 348
michael@0 349 /**
michael@0 350 * hb_blob_get_data_writable:
michael@0 351 * @blob: a blob.
michael@0 352 * @length: (out): output length of the writable data.
michael@0 353 *
michael@0 354 * Tries to make blob data writable (possibly copying it) and
michael@0 355 * return pointer to data.
michael@0 356 *
michael@0 357 * Fails if blob has been made immutable, or if memory allocation
michael@0 358 * fails.
michael@0 359 *
michael@0 360 * Returns: (transfer none) (array length=length): Writable blob data,
michael@0 361 * or %NULL if failed.
michael@0 362 *
michael@0 363 * Since: 1.0
michael@0 364 **/
michael@0 365 char *
michael@0 366 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length)
michael@0 367 {
michael@0 368 if (!_try_writable (blob)) {
michael@0 369 if (length)
michael@0 370 *length = 0;
michael@0 371
michael@0 372 return NULL;
michael@0 373 }
michael@0 374
michael@0 375 if (length)
michael@0 376 *length = blob->length;
michael@0 377
michael@0 378 return const_cast<char *> (blob->data);
michael@0 379 }
michael@0 380
michael@0 381
michael@0 382 static hb_bool_t
michael@0 383 _try_make_writable_inplace_unix (hb_blob_t *blob)
michael@0 384 {
michael@0 385 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
michael@0 386 uintptr_t pagesize = -1, mask, length;
michael@0 387 const char *addr;
michael@0 388
michael@0 389 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
michael@0 390 pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
michael@0 391 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
michael@0 392 pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
michael@0 393 #elif defined(HAVE_GETPAGESIZE)
michael@0 394 pagesize = (uintptr_t) getpagesize ();
michael@0 395 #endif
michael@0 396
michael@0 397 if ((uintptr_t) -1L == pagesize) {
michael@0 398 DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno));
michael@0 399 return false;
michael@0 400 }
michael@0 401 DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize);
michael@0 402
michael@0 403 mask = ~(pagesize-1);
michael@0 404 addr = (const char *) (((uintptr_t) blob->data) & mask);
michael@0 405 length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
michael@0 406 DEBUG_MSG_FUNC (BLOB, blob,
michael@0 407 "calling mprotect on [%p..%p] (%lu bytes)",
michael@0 408 addr, addr+length, (unsigned long) length);
michael@0 409 if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
michael@0 410 DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno));
michael@0 411 return false;
michael@0 412 }
michael@0 413
michael@0 414 blob->mode = HB_MEMORY_MODE_WRITABLE;
michael@0 415
michael@0 416 DEBUG_MSG_FUNC (BLOB, blob,
michael@0 417 "successfully made [%p..%p] (%lu bytes) writable\n",
michael@0 418 addr, addr+length, (unsigned long) length);
michael@0 419 return true;
michael@0 420 #else
michael@0 421 return false;
michael@0 422 #endif
michael@0 423 }
michael@0 424
michael@0 425 static bool
michael@0 426 _try_writable_inplace (hb_blob_t *blob)
michael@0 427 {
michael@0 428 DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n");
michael@0 429
michael@0 430 if (_try_make_writable_inplace_unix (blob))
michael@0 431 return true;
michael@0 432
michael@0 433 DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n");
michael@0 434
michael@0 435 /* Failed to make writable inplace, mark that */
michael@0 436 blob->mode = HB_MEMORY_MODE_READONLY;
michael@0 437 return false;
michael@0 438 }
michael@0 439
michael@0 440 static bool
michael@0 441 _try_writable (hb_blob_t *blob)
michael@0 442 {
michael@0 443 if (blob->immutable)
michael@0 444 return false;
michael@0 445
michael@0 446 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
michael@0 447 return true;
michael@0 448
michael@0 449 if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob))
michael@0 450 return true;
michael@0 451
michael@0 452 if (blob->mode == HB_MEMORY_MODE_WRITABLE)
michael@0 453 return true;
michael@0 454
michael@0 455
michael@0 456 DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data);
michael@0 457
michael@0 458 char *new_data;
michael@0 459
michael@0 460 new_data = (char *) malloc (blob->length);
michael@0 461 if (unlikely (!new_data))
michael@0 462 return false;
michael@0 463
michael@0 464 DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data);
michael@0 465
michael@0 466 memcpy (new_data, blob->data, blob->length);
michael@0 467 _hb_blob_destroy_user_data (blob);
michael@0 468 blob->mode = HB_MEMORY_MODE_WRITABLE;
michael@0 469 blob->data = new_data;
michael@0 470 blob->user_data = new_data;
michael@0 471 blob->destroy = free;
michael@0 472
michael@0 473 return true;
michael@0 474 }

mercurial