pool: New function pool_strdup0.
[pspp-builds.git] / src / libpspp / pool.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #if !pool_h
18 #define pool_h 1
19
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include "compiler.h"
24
25 /* Maximum size of a suballocated block.  Larger blocks are allocated
26    directly with malloc() to avoid memory wastage at the end of a
27    suballocation block. */
28 #ifndef MAX_SUBALLOC
29 #define MAX_SUBALLOC 64
30 #endif
31
32
33 /* Records the state of a pool for later restoration. */
34 struct pool_mark
35   {
36     /* Current block and offset into it. */
37     struct pool_block *block;
38     size_t ofs;
39
40     /* Current serial number to allow freeing of gizmos. */
41     long serial;
42   };
43
44 /* General routines. */
45 struct pool *pool_create (void);
46 void pool_destroy (struct pool *);
47 void pool_clear (struct pool *);
48
49 /* Creates a pool, allocates an instance of the given STRUCT
50    within it, sets the struct's MEMBER to the pool's address, and
51    returns the allocated structure. */
52 #define pool_create_container(STRUCT, MEMBER)                           \
53         ((STRUCT *) pool_create_at_offset (sizeof (STRUCT),             \
54                                            offsetof (STRUCT, MEMBER)))
55 void *pool_create_at_offset (size_t struct_size, size_t pool_member_offset);
56
57 /* Suballocation routines. */
58 void *pool_alloc (struct pool *, size_t) MALLOC_LIKE;
59 void *pool_nalloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
60 void *pool_clone (struct pool *, const void *, size_t) MALLOC_LIKE;
61
62 void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE;
63 void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE;
64 char *pool_strdup (struct pool *, const char *) MALLOC_LIKE;
65 char *pool_strdup0 (struct pool *, const char *, size_t) MALLOC_LIKE;
66 char *pool_vasprintf (struct pool *, const char *, va_list)
67      MALLOC_LIKE PRINTF_FORMAT (2, 0);
68 char *pool_asprintf (struct pool *, const char *, ...)
69      MALLOC_LIKE PRINTF_FORMAT (2, 3);
70
71 /* Standard allocation routines. */
72 void *pool_malloc (struct pool *, size_t) MALLOC_LIKE;
73 void *pool_nmalloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
74 void *pool_zalloc (struct pool *, size_t) MALLOC_LIKE;
75 void *pool_calloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
76 void *pool_realloc (struct pool *, void *, size_t);
77 void *pool_nrealloc (struct pool *, void *, size_t n, size_t s);
78 void *pool_2nrealloc (struct pool *, void *, size_t *pn, size_t s);
79 void pool_free (struct pool *, void *);
80
81 /* Subpools. */
82 struct pool *pool_create_subpool (struct pool *);
83 void pool_add_subpool (struct pool *, struct pool *subpool);
84
85 /* Files. */
86 FILE *pool_fopen (struct pool *, const char *, const char *);
87 int pool_fclose (struct pool *, FILE *) WARN_UNUSED_RESULT;
88 FILE *pool_tmpfile (struct pool *);
89 void pool_attach_file (struct pool *, FILE *);
90 void pool_detach_file (struct pool *, FILE *);
91
92 /* Custom allocations. */
93 void pool_register (struct pool *, void (*free) (void *), void *p);
94 bool pool_unregister (struct pool *, void *);
95
96 /* Partial freeing. */
97 void pool_mark (struct pool *, struct pool_mark *);
98 void pool_release (struct pool *, const struct pool_mark *);
99
100 #if DEBUGGING
101 void pool_dump (const struct pool *, const char *title);
102 #endif
103
104 #endif /* pool.h */