pool.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <string.h>
  24. #include <stdio.h>
  25. typedef struct fz_pool_node
  26. {
  27. struct fz_pool_node *next;
  28. char mem[FZ_FLEXIBLE_ARRAY];
  29. } fz_pool_node;
  30. #define POOL_SIZE (4<<10) /* default size of pool blocks */
  31. #define POOL_SELF (1<<10) /* size where allocs are put into their own blocks */
  32. struct fz_pool
  33. {
  34. size_t size;
  35. fz_pool_node *head, *tail;
  36. char *pos, *end;
  37. };
  38. fz_pool *fz_new_pool(fz_context *ctx)
  39. {
  40. fz_pool *pool;
  41. fz_pool_node *node = NULL;
  42. pool = fz_malloc_struct(ctx, fz_pool);
  43. fz_try(ctx)
  44. {
  45. node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block");
  46. pool->head = pool->tail = node;
  47. pool->pos = node->mem;
  48. pool->end = node->mem + POOL_SIZE;
  49. }
  50. fz_catch(ctx)
  51. {
  52. fz_free(ctx, pool);
  53. fz_rethrow(ctx);
  54. }
  55. return pool;
  56. }
  57. static void *fz_pool_alloc_oversize(fz_context *ctx, fz_pool *pool, size_t size)
  58. {
  59. fz_pool_node *node;
  60. /* link in memory at the head of the list */
  61. node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + size, 1), "fz_pool_oversize");
  62. node->next = pool->head;
  63. pool->head = node;
  64. pool->size += offsetof(fz_pool_node, mem) + size;
  65. return node->mem;
  66. }
  67. void *fz_pool_alloc(fz_context *ctx, fz_pool *pool, size_t size)
  68. {
  69. char *ptr;
  70. if (size >= POOL_SELF)
  71. return fz_pool_alloc_oversize(ctx, pool, size);
  72. /* round size to pointer alignment (we don't expect to use doubles) */
  73. size = (size + FZ_POINTER_ALIGN_MOD - 1) & ~(FZ_POINTER_ALIGN_MOD-1);
  74. if (pool->pos + size > pool->end)
  75. {
  76. fz_pool_node *node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block");
  77. pool->tail = pool->tail->next = node;
  78. pool->pos = node->mem;
  79. pool->end = node->mem + POOL_SIZE;
  80. pool->size += offsetof(fz_pool_node, mem) + POOL_SIZE;
  81. }
  82. ptr = pool->pos;
  83. pool->pos += size;
  84. return ptr;
  85. }
  86. char *fz_pool_strdup(fz_context *ctx, fz_pool *pool, const char *s)
  87. {
  88. size_t n = strlen(s) + 1;
  89. char *p = fz_pool_alloc(ctx, pool, n);
  90. memcpy(p, s, n);
  91. return p;
  92. }
  93. size_t fz_pool_size(fz_context *ctx, fz_pool *pool)
  94. {
  95. return pool ? pool->size : 0;
  96. }
  97. void fz_drop_pool(fz_context *ctx, fz_pool *pool)
  98. {
  99. fz_pool_node *node;
  100. if (!pool)
  101. return;
  102. node = pool->head;
  103. while (node)
  104. {
  105. fz_pool_node *next = node->next;
  106. fz_free(ctx, node);
  107. node = next;
  108. }
  109. fz_free(ctx, pool);
  110. }