memory.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #include "mupdf/fitz.h"
  23. #include <limits.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. /* Enable FITZ_DEBUG_LOCKING_TIMES below if you want to check the times
  29. * for which locks are held too. */
  30. #ifdef FITZ_DEBUG_LOCKING
  31. #undef FITZ_DEBUG_LOCKING_TIMES
  32. #endif
  33. /*
  34. * The malloc family of functions will always try scavenging when they run out of memory.
  35. * They will only fail when scavenging cannot free up memory from caches in the fz_context.
  36. * All the functions will throw an exception when no memory can be allocated,
  37. * except the _no_throw family which instead silently returns NULL.
  38. */
  39. static void *
  40. do_scavenging_malloc(fz_context *ctx, size_t size)
  41. {
  42. void *p;
  43. int phase = 0;
  44. fz_lock(ctx, FZ_LOCK_ALLOC);
  45. do {
  46. p = ctx->alloc.malloc(ctx->alloc.user, size);
  47. if (p != NULL)
  48. {
  49. fz_unlock(ctx, FZ_LOCK_ALLOC);
  50. return p;
  51. }
  52. } while (fz_store_scavenge(ctx, size, &phase));
  53. fz_unlock(ctx, FZ_LOCK_ALLOC);
  54. return NULL;
  55. }
  56. static void *
  57. do_scavenging_realloc(fz_context *ctx, void *p, size_t size)
  58. {
  59. void *q;
  60. int phase = 0;
  61. fz_lock(ctx, FZ_LOCK_ALLOC);
  62. do {
  63. q = ctx->alloc.realloc(ctx->alloc.user, p, size);
  64. if (q != NULL)
  65. {
  66. fz_unlock(ctx, FZ_LOCK_ALLOC);
  67. return q;
  68. }
  69. } while (fz_store_scavenge(ctx, size, &phase));
  70. fz_unlock(ctx, FZ_LOCK_ALLOC);
  71. return NULL;
  72. }
  73. void *
  74. fz_malloc(fz_context *ctx, size_t size)
  75. {
  76. void *p;
  77. if (size == 0)
  78. return NULL;
  79. p = do_scavenging_malloc(ctx, size);
  80. if (!p)
  81. {
  82. errno = ENOMEM;
  83. fz_throw(ctx, FZ_ERROR_SYSTEM, "malloc (%zu bytes) failed", size);
  84. }
  85. return p;
  86. }
  87. void *
  88. fz_malloc_no_throw(fz_context *ctx, size_t size)
  89. {
  90. if (size == 0)
  91. return NULL;
  92. return do_scavenging_malloc(ctx, size);
  93. }
  94. void *
  95. fz_calloc(fz_context *ctx, size_t count, size_t size)
  96. {
  97. void *p;
  98. if (count == 0 || size == 0)
  99. return NULL;
  100. if (count > SIZE_MAX / size)
  101. fz_throw(ctx, FZ_ERROR_LIMIT, "calloc (%zu x %zu bytes) failed (size_t overflow)", count, size);
  102. p = do_scavenging_malloc(ctx, count * size);
  103. if (!p)
  104. {
  105. errno = ENOMEM;
  106. fz_throw(ctx, FZ_ERROR_SYSTEM, "calloc (%zu x %zu bytes) failed", count, size);
  107. }
  108. memset(p, 0, count*size);
  109. return p;
  110. }
  111. void *
  112. fz_calloc_no_throw(fz_context *ctx, size_t count, size_t size)
  113. {
  114. void *p;
  115. if (count == 0 || size == 0)
  116. return NULL;
  117. if (count > SIZE_MAX / size)
  118. return NULL;
  119. p = do_scavenging_malloc(ctx, count * size);
  120. if (p)
  121. memset(p, 0, count * size);
  122. return p;
  123. }
  124. void *
  125. fz_realloc(fz_context *ctx, void *p, size_t size)
  126. {
  127. if (size == 0)
  128. {
  129. fz_free(ctx, p);
  130. return NULL;
  131. }
  132. p = do_scavenging_realloc(ctx, p, size);
  133. if (!p)
  134. {
  135. errno = ENOMEM;
  136. fz_throw(ctx, FZ_ERROR_SYSTEM, "realloc (%zu bytes) failed", size);
  137. }
  138. return p;
  139. }
  140. void *
  141. fz_realloc_no_throw(fz_context *ctx, void *p, size_t size)
  142. {
  143. if (size == 0)
  144. {
  145. fz_free(ctx, p);
  146. return NULL;
  147. }
  148. return do_scavenging_realloc(ctx, p, size);
  149. }
  150. void
  151. fz_free(fz_context *ctx, void *p)
  152. {
  153. if (p)
  154. {
  155. fz_lock(ctx, FZ_LOCK_ALLOC);
  156. ctx->alloc.free(ctx->alloc.user, p);
  157. fz_unlock(ctx, FZ_LOCK_ALLOC);
  158. }
  159. }
  160. /* align is assumed to be a power of 2. */
  161. void *fz_malloc_aligned(fz_context *ctx, size_t size, int align)
  162. {
  163. uint8_t *block;
  164. uint8_t *aligned;
  165. if (size == 0)
  166. return NULL;
  167. if (align >= 256)
  168. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Alignment too large");
  169. if ((align & (align-1)) != 0)
  170. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Alignment must be a power of 2");
  171. block = fz_malloc(ctx, size + align);
  172. aligned = (void *)((intptr_t)(block + align-1) & ~(align-1));
  173. if (aligned == block)
  174. aligned = block + align;
  175. memset(block, aligned-block, aligned-block);
  176. return aligned;
  177. }
  178. void fz_free_aligned(fz_context *ctx, void *ptr)
  179. {
  180. uint8_t *block = ptr;
  181. if (ptr == NULL)
  182. return;
  183. block -= block[-1];
  184. fz_free(ctx, block);
  185. }
  186. char *
  187. fz_strdup(fz_context *ctx, const char *s)
  188. {
  189. size_t len = strlen(s) + 1;
  190. char *ns = fz_malloc(ctx, len);
  191. memcpy(ns, s, len);
  192. return ns;
  193. }
  194. fz_string *
  195. fz_new_string(fz_context *ctx, const char *s)
  196. {
  197. fz_string *str = fz_malloc_flexible(ctx, fz_string, str, strlen(s) + 1);
  198. str->refs = 1;
  199. strcpy(str->str, s);
  200. return str;
  201. }
  202. fz_string *fz_keep_string(fz_context *ctx, fz_string *str)
  203. {
  204. return fz_keep_imp(ctx, str, &str->refs);
  205. }
  206. void fz_drop_string(fz_context *ctx, fz_string *str)
  207. {
  208. if (fz_drop_imp(ctx, str, &str->refs))
  209. fz_free(ctx, str);
  210. }
  211. static void *
  212. fz_malloc_default(void *opaque, size_t size)
  213. {
  214. return malloc(size);
  215. }
  216. static void *
  217. fz_realloc_default(void *opaque, void *old, size_t size)
  218. {
  219. return realloc(old, size);
  220. }
  221. static void
  222. fz_free_default(void *opaque, void *ptr)
  223. {
  224. free(ptr);
  225. }
  226. fz_alloc_context fz_alloc_default =
  227. {
  228. NULL,
  229. fz_malloc_default,
  230. fz_realloc_default,
  231. fz_free_default
  232. };
  233. static void
  234. fz_lock_default(void *user, int lock)
  235. {
  236. }
  237. static void
  238. fz_unlock_default(void *user, int lock)
  239. {
  240. }
  241. fz_locks_context fz_locks_default =
  242. {
  243. NULL,
  244. fz_lock_default,
  245. fz_unlock_default
  246. };
  247. #ifdef FITZ_DEBUG_LOCKING
  248. enum
  249. {
  250. FZ_LOCK_DEBUG_CONTEXT_MAX = 100
  251. };
  252. fz_context *fz_lock_debug_contexts[FZ_LOCK_DEBUG_CONTEXT_MAX];
  253. int fz_locks_debug[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX];
  254. #ifdef FITZ_DEBUG_LOCKING_TIMES
  255. int fz_debug_locking_inited = 0;
  256. int fz_lock_program_start;
  257. int fz_lock_time[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } };
  258. int fz_lock_taken[FZ_LOCK_DEBUG_CONTEXT_MAX][FZ_LOCK_MAX] = { { 0 } };
  259. /* We implement our own millisecond clock, as clock() cannot be trusted
  260. * when threads are involved. */
  261. static int ms_clock(void)
  262. {
  263. #ifdef _WIN32
  264. return (int)GetTickCount();
  265. #else
  266. struct timeval tp;
  267. gettimeofday(&tp, NULL);
  268. return (tp.tv_sec*1000) + (tp.tv_usec/1000);
  269. #endif
  270. }
  271. static void dump_lock_times(void)
  272. {
  273. int i, j;
  274. int prog_time = ms_clock() - fz_lock_program_start;
  275. for (j = 0; j < FZ_LOCK_MAX; j++)
  276. {
  277. int total = 0;
  278. for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
  279. {
  280. total += fz_lock_time[i][j];
  281. }
  282. printf("Lock %d held for %g seconds (%g%%)\n", j, total / 1000.0f, 100.0f*total/prog_time);
  283. }
  284. printf("Total program time %g seconds\n", prog_time / 1000.0f);
  285. }
  286. #endif
  287. static int find_context(fz_context *ctx)
  288. {
  289. int i;
  290. for (i = 0; i < FZ_LOCK_DEBUG_CONTEXT_MAX; i++)
  291. {
  292. if (fz_lock_debug_contexts[i] == ctx)
  293. return i;
  294. if (fz_lock_debug_contexts[i] == NULL)
  295. {
  296. int gottit = 0;
  297. /* We've not locked on this context before, so use
  298. * this one for this new context. We might have other
  299. * threads trying here too though so, so claim it
  300. * atomically. No one has locked on this context
  301. * before, so we are safe to take the ALLOC lock. */
  302. ctx->locks.lock(ctx->locks.user, FZ_LOCK_ALLOC);
  303. /* If it's still free, then claim it as ours,
  304. * otherwise we'll keep hunting. */
  305. if (fz_lock_debug_contexts[i] == NULL)
  306. {
  307. gottit = 1;
  308. fz_lock_debug_contexts[i] = ctx;
  309. #ifdef FITZ_DEBUG_LOCKING_TIMES
  310. if (fz_debug_locking_inited == 0)
  311. {
  312. fz_debug_locking_inited = 1;
  313. fz_lock_program_start = ms_clock();
  314. atexit(dump_lock_times);
  315. }
  316. #endif
  317. }
  318. ctx->locks.unlock(ctx->locks.user, FZ_LOCK_ALLOC);
  319. if (gottit)
  320. return i;
  321. }
  322. }
  323. return -1;
  324. }
  325. void
  326. fz_assert_lock_held(fz_context *ctx, int lock)
  327. {
  328. int idx;
  329. if (ctx->locks.lock != fz_lock_default)
  330. return;
  331. idx = find_context(ctx);
  332. if (idx < 0)
  333. return;
  334. if (fz_locks_debug[idx][lock] == 0)
  335. fprintf(stderr, "Lock %d not held when expected\n", lock);
  336. }
  337. void
  338. fz_assert_lock_not_held(fz_context *ctx, int lock)
  339. {
  340. int idx;
  341. if (ctx->locks.lock != fz_lock_default)
  342. return;
  343. idx = find_context(ctx);
  344. if (idx < 0)
  345. return;
  346. if (fz_locks_debug[idx][lock] != 0)
  347. fprintf(stderr, "Lock %d held when not expected\n", lock);
  348. }
  349. void fz_lock_debug_lock(fz_context *ctx, int lock)
  350. {
  351. int i, idx;
  352. if (ctx->locks.lock != fz_lock_default)
  353. return;
  354. idx = find_context(ctx);
  355. if (idx < 0)
  356. return;
  357. if (fz_locks_debug[idx][lock] != 0)
  358. {
  359. fprintf(stderr, "Attempt to take lock %d when held already!\n", lock);
  360. }
  361. for (i = lock-1; i >= 0; i--)
  362. {
  363. if (fz_locks_debug[idx][i] != 0)
  364. {
  365. fprintf(stderr, "Lock ordering violation: Attempt to take lock %d when %d held already!\n", lock, i);
  366. }
  367. }
  368. fz_locks_debug[idx][lock] = 1;
  369. #ifdef FITZ_DEBUG_LOCKING_TIMES
  370. fz_lock_taken[idx][lock] = ms_clock();
  371. #endif
  372. }
  373. void fz_lock_debug_unlock(fz_context *ctx, int lock)
  374. {
  375. int idx;
  376. if (ctx->locks.lock != fz_lock_default)
  377. return;
  378. idx = find_context(ctx);
  379. if (idx < 0)
  380. return;
  381. if (fz_locks_debug[idx][lock] == 0)
  382. {
  383. fprintf(stderr, "Attempt to release lock %d when not held!\n", lock);
  384. }
  385. fz_locks_debug[idx][lock] = 0;
  386. #ifdef FITZ_DEBUG_LOCKING_TIMES
  387. fz_lock_time[idx][lock] += ms_clock() - fz_lock_taken[idx][lock];
  388. #endif
  389. }
  390. #else
  391. void
  392. (fz_assert_lock_held)(fz_context *ctx, int lock)
  393. {
  394. }
  395. void
  396. (fz_assert_lock_not_held)(fz_context *ctx, int lock)
  397. {
  398. }
  399. void (fz_lock_debug_lock)(fz_context *ctx, int lock)
  400. {
  401. }
  402. void (fz_lock_debug_unlock)(fz_context *ctx, int lock)
  403. {
  404. }
  405. #endif