dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / nfmalloc.c
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * nfmalloc.c - non-freeing malloc, used for in-core database
4 *
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <compat.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <obstack.h>
27
28 #include <dpkg/i18n.h>
29 #include <dpkg/dpkg.h>
30 #include <dpkg/dpkg-db.h>
31
32 #define obstack_chunk_alloc m_malloc
33 #define obstack_chunk_free free
34
35 static struct obstack db_obs;
36 static bool dbobs_init = false;
37
38 /* We use lots of mem, so use a large chunk. */
39 #define CHUNK_SIZE 8192
40
41 #define OBSTACK_INIT if (!dbobs_init) { nfobstack_init(); }
42
43 static void nfobstack_init(void) {
44 obstack_init(&db_obs);
45 dbobs_init = true;
46 obstack_chunk_size(&db_obs) = CHUNK_SIZE;
47 }
48
49 void *
50 nfmalloc(size_t size)
51 {
52 OBSTACK_INIT;
53 return obstack_alloc(&db_obs, size);
54 }
55
56 char *nfstrsave(const char *string) {
57 OBSTACK_INIT;
58 return obstack_copy0 (&db_obs, string, strlen(string));
59 }
60
61 char *
62 nfstrnsave(const char *string, size_t size)
63 {
64 OBSTACK_INIT;
65 return obstack_copy0(&db_obs, string, size);
66 }
67
68 void nffreeall(void) {
69 if (dbobs_init) {
70 obstack_free(&db_obs, NULL);
71 dbobs_init = false;
72 }
73 }