/src/mupdf/source/fitz/pool.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (C) 2004-2021 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 <string.h> |
26 | | #include <stdio.h> |
27 | | |
28 | | typedef struct fz_pool_node |
29 | | { |
30 | | struct fz_pool_node *next; |
31 | | char mem[1]; |
32 | | } fz_pool_node; |
33 | | |
34 | 3.17k | #define POOL_SIZE (4<<10) /* default size of pool blocks */ |
35 | 185k | #define POOL_SELF (1<<10) /* size where allocs are put into their own blocks */ |
36 | | |
37 | | struct fz_pool |
38 | | { |
39 | | size_t size; |
40 | | fz_pool_node *head, *tail; |
41 | | char *pos, *end; |
42 | | }; |
43 | | |
44 | | |
45 | | fz_pool *fz_new_pool(fz_context *ctx) |
46 | 1.39k | { |
47 | 1.39k | fz_pool *pool; |
48 | 1.39k | fz_pool_node *node = NULL; |
49 | | |
50 | 1.39k | pool = fz_malloc_struct(ctx, fz_pool); |
51 | 2.79k | fz_try(ctx) |
52 | 2.79k | { |
53 | 1.39k | node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block"); |
54 | 1.39k | pool->head = pool->tail = node; |
55 | 1.39k | pool->pos = node->mem; |
56 | 1.39k | pool->end = node->mem + POOL_SIZE; |
57 | 1.39k | } |
58 | 2.79k | fz_catch(ctx) |
59 | 0 | { |
60 | 0 | fz_free(ctx, pool); |
61 | 0 | fz_rethrow(ctx); |
62 | 0 | } |
63 | | |
64 | 1.39k | return pool; |
65 | 1.39k | } |
66 | | |
67 | | static void *fz_pool_alloc_oversize(fz_context *ctx, fz_pool *pool, size_t size) |
68 | 9 | { |
69 | 9 | fz_pool_node *node; |
70 | | |
71 | | /* link in memory at the head of the list */ |
72 | 9 | node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + size, 1), "fz_pool_oversize"); |
73 | 9 | node->next = pool->head; |
74 | 9 | pool->head = node; |
75 | 9 | pool->size += offsetof(fz_pool_node, mem) + size; |
76 | | |
77 | 9 | return node->mem; |
78 | 9 | } |
79 | | |
80 | | void *fz_pool_alloc(fz_context *ctx, fz_pool *pool, size_t size) |
81 | 185k | { |
82 | 185k | char *ptr; |
83 | | |
84 | 185k | if (size >= POOL_SELF) |
85 | 9 | return fz_pool_alloc_oversize(ctx, pool, size); |
86 | | |
87 | | /* round size to pointer alignment (we don't expect to use doubles) */ |
88 | 185k | size = (size + FZ_POINTER_ALIGN_MOD - 1) & ~(FZ_POINTER_ALIGN_MOD-1); |
89 | | |
90 | 185k | if (pool->pos + size > pool->end) |
91 | 888 | { |
92 | 888 | fz_pool_node *node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block"); |
93 | 888 | pool->tail = pool->tail->next = node; |
94 | 888 | pool->pos = node->mem; |
95 | 888 | pool->end = node->mem + POOL_SIZE; |
96 | 888 | pool->size += offsetof(fz_pool_node, mem) + POOL_SIZE; |
97 | 888 | } |
98 | 185k | ptr = pool->pos; |
99 | 185k | pool->pos += size; |
100 | 185k | return ptr; |
101 | 185k | } |
102 | | |
103 | | char *fz_pool_strdup(fz_context *ctx, fz_pool *pool, const char *s) |
104 | 50.7k | { |
105 | 50.7k | size_t n = strlen(s) + 1; |
106 | 50.7k | char *p = fz_pool_alloc(ctx, pool, n); |
107 | 50.7k | memcpy(p, s, n); |
108 | 50.7k | return p; |
109 | 50.7k | } |
110 | | |
111 | | size_t fz_pool_size(fz_context *ctx, fz_pool *pool) |
112 | 0 | { |
113 | 0 | return pool ? pool->size : 0; |
114 | 0 | } |
115 | | |
116 | | void fz_drop_pool(fz_context *ctx, fz_pool *pool) |
117 | 1.39k | { |
118 | 1.39k | fz_pool_node *node; |
119 | | |
120 | 1.39k | if (!pool) |
121 | 0 | return; |
122 | | |
123 | 1.39k | node = pool->head; |
124 | 3.68k | while (node) |
125 | 2.29k | { |
126 | 2.29k | fz_pool_node *next = node->next; |
127 | 2.29k | fz_free(ctx, node); |
128 | 2.29k | node = next; |
129 | 2.29k | } |
130 | 1.39k | fz_free(ctx, pool); |
131 | 1.39k | } |