Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/fitz/memory.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 // Copyright (C) 2004-2025 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 #include "mupdf/fitz.h" | |
| 24 | |
| 25 #include <limits.h> | |
| 26 #include <string.h> | |
| 27 #include <stdlib.h> | |
| 28 #include <stdio.h> | |
| 29 #include <errno.h> | |
| 30 | |
| 31 /* Enable FITZ_DEBUG_LOCKING_TIMES below if you want to check the times | |
| 32 * for which locks are held too. */ | |
| 33 #ifdef FITZ_DEBUG_LOCKING | |
| 34 #undef FITZ_DEBUG_LOCKING_TIMES | |
| 35 #endif | |
| 36 | |
| 37 /* | |
| 38 * The malloc family of functions will always try scavenging when they run out of memory. | |
| 39 * They will only fail when scavenging cannot free up memory from caches in the fz_context. | |
| 40 * All the functions will throw an exception when no memory can be allocated, | |
| 41 * except the _no_throw family which instead silently returns NULL. | |
| 42 */ | |
| 43 | |
| 44 static void * | |
| 45 do_scavenging_malloc(fz_context *ctx, size_t size) | |
| 46 { | |
| 47 void *p; | |
| 48 int phase = 0; | |
| 49 | |
| 50 fz_lock(ctx, FZ_LOCK_ALLOC); | |
| 51 do { | |
| 52 p = ctx->alloc.malloc(ctx->alloc.user, size); | |
| 53 if (p != NULL) | |
| 54 { | |
| 55 fz_unlock(ctx, FZ_LOCK_ALLOC); | |
| 56 return p; | |
| 57 } | |
| 58 } while (fz_store_scavenge(ctx, size, &phase)); | |
| 59 fz_unlock(ctx, FZ_LOCK_ALLOC); | |
| 60 | |
| 61 return NULL; | |
| 62 } | |
| 63 | |
| 64 static void * | |
| 65 do_scavenging_realloc(fz_context *ctx, void *p, size_t size) | |
| 66 { | |
| 67 void *q; | |
| 68 int phase = 0; | |
| 69 | |
| 70 fz_lock(ctx, FZ_LOCK_ALLOC); | |
| 71 do { | |
| 72 q = ctx->alloc.realloc(ctx->alloc.user, p, size); | |
| 73 if (q != NULL) | |
| 74 { | |
| 75 fz_unlock(ctx, FZ_LOCK_ALLOC); | |
| 76 return q; | |
| 77 } | |
| 78 } while (fz_store_scavenge(ctx, size, &phase)); | |
| 79 fz_unlock(ctx, FZ_LOCK_ALLOC); | |
| 80 | |
| 81 return NULL; | |
| 82 } | |
| 83 | |
| 84 void * | |
| 85 fz_malloc(fz_context *ctx, size_t size) | |
| 86 { | |
| 87 void *p; | |
| 88 if (size == 0) | |
| 89 return NULL; | |
| 90 p = do_scavenging_malloc(ctx, size); | |
| 91 if (!p) | |
| 92 { | |
| 93 errno = ENOMEM; | |
| 94 fz_throw(ctx, FZ_ERROR_SYSTEM, "malloc (%zu bytes) failed", size); | |
| 95 } | |
| 96 return p; | |
| 97 } | |
| 98 | |
| 99 void * | |
| 100 fz_malloc_no_throw(fz_context *ctx, size_t size) | |
| 101 { | |
| 102 if (size == 0) | |
| 103 return NULL; | |
| 104 return do_scavenging_malloc(ctx, size); | |
| 105 } | |
| 106 | |
| 107 void * | |
| 108 fz_calloc(fz_context *ctx, size_t count, size_t size) | |
| 109 { | |
| 110 void *p; | |
| 111 if (count == 0 || size == 0) | |
| 112 return NULL; | |
| 113 if (count > SIZE_MAX / size) | |
| 114 fz_throw(ctx, FZ_ERROR_LIMIT, "calloc (%zu x %zu bytes) failed (size_t overflow)", count, size); | |
| 115 p = do_scavenging_malloc(ctx, count * size); | |
| 116 if (!p) | |
| 117 { | |
| 118 errno = ENOMEM; | |
| 119 fz_throw(ctx, FZ_ERROR_SYSTEM, "calloc (%zu x %zu bytes) failed", count, size); | |
| 120 } | |
| 121 memset(p, 0, count*size); | |
| 122 return p; | |
| 123 } | |
| 124 | |
| 125 void * | |
| 126 fz_calloc_no_throw(fz_context *ctx, size_t count, size_t size) | |
| 127 { | |
| 128 void *p; | |
| 129 if (count == 0 || size == 0) | |
| 130 return NULL; | |
| 131 if (count > SIZE_MAX / size) | |
| 132 return NULL; | |
| 133 p = do_scavenging_malloc(ctx, count * size); | |
| 134 if (p) | |
| 135 memset(p, 0, count * size); | |
| 136 return p; | |
| 137 } | |
| 138 | |
| 139 void * | |
| 140 fz_realloc(fz_context *ctx, void *p, size_t size) | |
| 141 { | |
| 142 if (size == 0) | |
| 143 { | |
| 144 fz_free(ctx, p); | |
| 145 return NULL; | |
| 146 } | |
| 147 p = do_scavenging_realloc(ctx, p, size); | |
| 148 if (!p) | |
| 149 { | |
| 150 errno = ENOMEM; | |
| 151 fz_throw(ctx, FZ_ERROR_SYSTEM, "realloc (%zu bytes) failed", size); | |
| 152 } | |
| 153 return p; | |
| 154 } | |
| 155 | |
| 156 void * | |
| 157 fz_realloc_no_throw(fz_context *ctx, void *p, size_t size) | |
| 158 { | |
| 159 if (size == 0) | |
| 160 { | |
| 161 fz_free(ctx, p); | |
| 162 return NULL; | |
| 163 } | |
| 164 return do_scavenging_realloc(ctx, p, size); | |
| 165 } | |
| 166 | |
| 167 void | |
| 168 fz_free(fz_context *ctx, void *p) | |
| 169 { | |
| 170 if (p) | |
| 171 { | |
| 172 fz_lock(ctx, FZ_LOCK_ALLOC); | |
| 173 ctx->alloc.free(ctx->alloc.user, p); | |
| 174 fz_unlock(ctx, FZ_LOCK_ALLOC); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 /* align is assumed to be a power of 2. */ | |
| 179 void *fz_malloc_aligned(fz_context *ctx, size_t size, int align) | |
| 180 { | |
| 181 uint8_t *block; | |
| 182 uint8_t *aligned; | |
| 183 | |
| 184 if (size == 0) | |
| 185 return NULL; | |
| 186 | |
| 187 if (align >= 256) | |
| 188 fz_throw(ctx, FZ_ERROR_ARGUMENT, "Alignment too large"); | |
| 189 if ((align & (align-1)) != 0) | |
| 190 fz_throw(ctx, FZ_ERROR_ARGUMENT, "Alignment must be a power of 2"); | |
| 191 | |
| 192 block = fz_malloc(ctx, size + align); | |
| 193 | |
| 194 aligned = (void *)((intptr_t)(block + align-1) & ~(align-1)); | |
| 195 if (aligned == block) | |
| 196 aligned = block + align; | |
| 197 memset(block, aligned-block, aligned-block); | |
| 198 | |
| 199 return aligned; | |
| 200 } | |
| 201 | |
| 202 void fz_free_aligned(fz_context *ctx, void *ptr) | |
| 203 { | |
| 204 uint8_t *block = ptr; | |
| 205 | |
| 206 if (ptr == NULL) | |
| 207 return; | |
| 208 | |
| 209 block -= block[-1]; | |
| 210 | |
| 211 fz_free(ctx, block); | |
| 212 } | |
| 213 | |
| 214 char * | |
| 215 fz_strdup(fz_context *ctx, const char *s) | |
| 216 { | |
| 217 size_t len = strlen(s) + 1; | |
| 218 char *ns = fz_malloc(ctx, len); | |
| 219 memcpy(ns, s, len); | |
| 220 return ns; | |
| 221 } | |
| 222 | |
| 223 fz_string * | |
| 224 fz_new_string(fz_context *ctx, const char *s) | |
| 225 { | |
| 226 fz_string *str = fz_malloc_flexible(ctx, fz_string, str, strlen(s) + 1); | |
| 227 str->refs = 1; | |
| 228 strcpy(str->str, s); | |
| 229 return str; | |
| 230 } | |
| 231 | |
| 232 fz_string *fz_keep_string(fz_context *ctx, fz_string *str) | |
| 233 { | |
| 234 return fz_keep_imp(ctx, str, &str->refs); | |
| 235 } | |
| 236 | |
| 237 void fz_drop_string(fz_context *ctx, fz_string *str) | |
| 238 { | |
| 239 if (fz_drop_imp(ctx, str, &str->refs)) | |
| 240 fz_free(ctx, str); | |
| 241 } | |
| 242 | |
| 243 | |
| 244 static void * | |
| 245 fz_malloc_default(void *opaque, size_t size) | |
| 246 { | |
| 247 return malloc(size); | |
| 248 } | |
| 249 | |
| 250 static void * | |
| 251 fz_realloc_default(void *opaque, void *old, size_t size) | |
| 252 { | |
| 253 return realloc(old, size); | |
| 254 } | |
| 255 | |
| 256 static void | |
| 257 fz_free_default(void *opaque, void *ptr) | |
| 258 { | |
| 259 free(ptr); | |
| 260 } | |
| 261 | |
| 262 fz_alloc_context fz_alloc_default = | |
| 263 { | |
| 264 NULL, | |
| 265 fz_malloc_default, | |
| 266 fz_realloc_default, | |
| 267 fz_free_default | |
| 268 }; | |
| 269 | |
| 270 static void | |
| 271 fz_lock_default(void *user, int lock) | |
| 272 { | |
| 273 } | |
| 274 | |
| 275 static void | |
| 276 fz_unlock_default(void *user, int lock) | |
| 277 { | |
| 278 } | |
| 279 | |
| 280 fz_locks_context fz_locks_default = | |
| 281 { | |
| 282 NULL, | |
| 283 fz_lock_default, | |
| 284 fz_unlock_default | |
| 285 }; | |
| 286 | |
| 287 #ifdef FITZ_DEBUG_LOCKING | |
| 288 | |
| 289 enum | |
| 290 { | |
| 291 FZ_LOCK_DEBUG_CONTEXT_MAX = 100 | |
| 292 }; | |
| 293 | |
| 294 fz_context *fz_lock_debug_contexts[FZ_LOCK_DEBUG_CONTEXT_MAX]; | |
| 295 int fz_locks_debug[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX]; | |
| 296 | |
| 297 #ifdef FITZ_DEBUG_LOCKING_TIMES | |
| 298 | |
| 299 int fz_debug_locking_inited = 0; | |
| 300 int fz_lock_program_start; | |
| 301 int fz_lock_time[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } }; | |
| 302 int fz_lock_taken[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } }; | |
| 303 | |
| 304 /* We implement our own millisecond clock, as clock() cannot be trusted | |
| 305 * when threads are involved. */ | |
| 306 static int ms_clock(void) | |
| 307 { | |
| 308 #ifdef _WIN32 | |
| 309 return (int)GetTickCount(); | |
| 310 #else | |
| 311 struct timeval tp; | |
| 312 gettimeofday(&tp, NULL); | |
| 313 return (tp.tv_sec*1000) + (tp.tv_usec/1000); | |
| 314 #endif | |
| 315 } | |
| 316 | |
| 317 static void dump_lock_times(void) | |
| 318 { | |
| 319 int i, j; | |
| 320 int prog_time = ms_clock() - fz_lock_program_start; | |
| 321 | |
| 322 for (j = 0; j < FZ_LOCK_MAX; j++) | |
| 323 { | |
| 324 int total = 0; | |
| 325 for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++) | |
| 326 { | |
| 327 total += fz_lock_time[i][j]; | |
| 328 } | |
| 329 printf("Lock %d held for %g seconds (%g%%)\n", j, total / 1000.0f, 100.0f*total/prog_time); | |
| 330 } | |
| 331 printf("Total program time %g seconds\n", prog_time / 1000.0f); | |
| 332 } | |
| 333 | |
| 334 #endif | |
| 335 | |
| 336 static int find_context(fz_context *ctx) | |
| 337 { | |
| 338 int i; | |
| 339 | |
| 340 for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++) | |
| 341 { | |
| 342 if (fz_lock_debug_contexts[i] == ctx) | |
| 343 return i; | |
| 344 if (fz_lock_debug_contexts[i] == NULL) | |
| 345 { | |
| 346 int gottit = 0; | |
| 347 /* We've not locked on this context before, so use | |
| 348 * this one for this new context. We might have other | |
| 349 * threads trying here too though so, so claim it | |
| 350 * atomically. No one has locked on this context | |
| 351 * before, so we are safe to take the ALLOC lock. */ | |
| 352 ctx->locks.lock(ctx->locks.user, FZ_LOCK_ALLOC); | |
| 353 /* If it's still free, then claim it as ours, | |
| 354 * otherwise we'll keep hunting. */ | |
| 355 if (fz_lock_debug_contexts[i] == NULL) | |
| 356 { | |
| 357 gottit = 1; | |
| 358 fz_lock_debug_contexts[i] = ctx; | |
| 359 #ifdef FITZ_DEBUG_LOCKING_TIMES | |
| 360 if (fz_debug_locking_inited == 0) | |
| 361 { | |
| 362 fz_debug_locking_inited = 1; | |
| 363 fz_lock_program_start = ms_clock(); | |
| 364 atexit(dump_lock_times); | |
| 365 } | |
| 366 #endif | |
| 367 } | |
| 368 ctx->locks.unlock(ctx->locks.user, FZ_LOCK_ALLOC); | |
| 369 if (gottit) | |
| 370 return i; | |
| 371 } | |
| 372 } | |
| 373 return -1; | |
| 374 } | |
| 375 | |
| 376 void | |
| 377 fz_assert_lock_held(fz_context *ctx, int lock) | |
| 378 { | |
| 379 int idx; | |
| 380 | |
| 381 if (ctx->locks.lock != fz_lock_default) | |
| 382 return; | |
| 383 | |
| 384 idx = find_context(ctx); | |
| 385 if (idx < 0) | |
| 386 return; | |
| 387 | |
| 388 if (fz_locks_debug[idx][lock] == 0) | |
| 389 fprintf(stderr, "Lock %d not held when expected\n", lock); | |
| 390 } | |
| 391 | |
| 392 void | |
| 393 fz_assert_lock_not_held(fz_context *ctx, int lock) | |
| 394 { | |
| 395 int idx; | |
| 396 | |
| 397 if (ctx->locks.lock != fz_lock_default) | |
| 398 return; | |
| 399 | |
| 400 idx = find_context(ctx); | |
| 401 if (idx < 0) | |
| 402 return; | |
| 403 | |
| 404 if (fz_locks_debug[idx][lock] != 0) | |
| 405 fprintf(stderr, "Lock %d held when not expected\n", lock); | |
| 406 } | |
| 407 | |
| 408 void fz_lock_debug_lock(fz_context *ctx, int lock) | |
| 409 { | |
| 410 int i, idx; | |
| 411 | |
| 412 if (ctx->locks.lock != fz_lock_default) | |
| 413 return; | |
| 414 | |
| 415 idx = find_context(ctx); | |
| 416 if (idx < 0) | |
| 417 return; | |
| 418 | |
| 419 if (fz_locks_debug[idx][lock] != 0) | |
| 420 { | |
| 421 fprintf(stderr, "Attempt to take lock %d when held already!\n", lock); | |
| 422 } | |
| 423 for (i = lock-1; i >= 0; i--) | |
| 424 { | |
| 425 if (fz_locks_debug[idx][i] != 0) | |
| 426 { | |
| 427 fprintf(stderr, "Lock ordering violation: Attempt to take lock %d when %d held already!\n", lock, i); | |
| 428 } | |
| 429 } | |
| 430 fz_locks_debug[idx][lock] = 1; | |
| 431 #ifdef FITZ_DEBUG_LOCKING_TIMES | |
| 432 fz_lock_taken[idx][lock] = ms_clock(); | |
| 433 #endif | |
| 434 } | |
| 435 | |
| 436 void fz_lock_debug_unlock(fz_context *ctx, int lock) | |
| 437 { | |
| 438 int idx; | |
| 439 | |
| 440 if (ctx->locks.lock != fz_lock_default) | |
| 441 return; | |
| 442 | |
| 443 idx = find_context(ctx); | |
| 444 if (idx < 0) | |
| 445 return; | |
| 446 | |
| 447 if (fz_locks_debug[idx][lock] == 0) | |
| 448 { | |
| 449 fprintf(stderr, "Attempt to release lock %d when not held!\n", lock); | |
| 450 } | |
| 451 fz_locks_debug[idx][lock] = 0; | |
| 452 #ifdef FITZ_DEBUG_LOCKING_TIMES | |
| 453 fz_lock_time[idx][lock] += ms_clock() - fz_lock_taken[idx][lock]; | |
| 454 #endif | |
| 455 } | |
| 456 | |
| 457 #else | |
| 458 | |
| 459 void | |
| 460 (fz_assert_lock_held)(fz_context *ctx, int lock) | |
| 461 { | |
| 462 } | |
| 463 | |
| 464 void | |
| 465 (fz_assert_lock_not_held)(fz_context *ctx, int lock) | |
| 466 { | |
| 467 } | |
| 468 | |
| 469 void (fz_lock_debug_lock)(fz_context *ctx, int lock) | |
| 470 { | |
| 471 } | |
| 472 | |
| 473 void (fz_lock_debug_unlock)(fz_context *ctx, int lock) | |
| 474 { | |
| 475 } | |
| 476 | |
| 477 #endif |
