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