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