Categorical value cache added
[pspp-builds.git] / src / 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
25 /* Records the state of a pool for later restoration. */
26 struct pool_mark 
27   {
28     /* Current block and offset into it. */
29     struct pool_block *block;
30     size_t ofs;
31
32     /* Current serial number to allow freeing of gizmos. */
33     long serial;
34   };
35
36 /* General routines. */
37 struct pool *pool_create (void);
38 void pool_destroy (struct pool *);
39 void pool_clear (struct pool *);
40
41 /* Creates a pool, allocates an instance of the given STRUCT
42    within it, sets the struct's MEMBER to the pool's address, and
43    returns the allocated structure. */
44 #define pool_create_container(STRUCT, MEMBER)                           \
45         ((STRUCT *) pool_create_at_offset (sizeof (STRUCT),             \
46                                            offsetof (STRUCT, MEMBER)))
47 void *pool_create_at_offset (size_t struct_size, size_t pool_member_offset);
48
49 /* Suballocation routines. */
50 void *pool_alloc (struct pool *, size_t) MALLOC_LIKE;
51 void *pool_nalloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
52 void *pool_clone (struct pool *, const void *, size_t) MALLOC_LIKE;
53
54 void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE;
55 void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE;
56 char *pool_strdup (struct pool *, const char *) MALLOC_LIKE;
57 char *pool_strcat (struct pool *, const char *, ...) MALLOC_LIKE;
58
59 /* Standard allocation routines. */
60 void *pool_malloc (struct pool *, size_t) MALLOC_LIKE;
61 void *pool_nmalloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
62 void *pool_realloc (struct pool *, void *, size_t);
63 void *pool_nrealloc (struct pool *, void *, size_t n, size_t s);
64 void *pool_2nrealloc (struct pool *, void *, size_t *pn, size_t s);
65 void pool_free (struct pool *, void *);
66
67 /* Gizmo allocations. */
68 struct pool *pool_create_subpool (struct pool *);
69 void pool_add_subpool (struct pool *, struct pool *subpool);
70 FILE *pool_fopen (struct pool *, const char *, const char *);
71 int pool_fclose (struct pool *, FILE *);
72
73 /* Custom allocations. */
74 void pool_register (struct pool *, void (*free) (void *), void *p);
75 int pool_unregister (struct pool *, void *);
76
77 /* Partial freeing. */
78 void pool_mark (struct pool *, struct pool_mark *);
79 void pool_release (struct pool *, const struct pool_mark *);
80
81 #if GLOBAL_DEBUGGING
82 void pool_dump (const struct pool *, const char *title);
83 #endif
84
85 #endif /* pool.h */