1 /* PSPP - computes sample statistics.
2 Copyright (C) 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
28 /* Maximum size of a suballocated block. Larger blocks are allocated
29 directly with malloc() to avoid memory wastage at the end of a
30 suballocation block. */
32 #define MAX_SUBALLOC 64
36 /* Records the state of a pool for later restoration. */
39 /* Current block and offset into it. */
40 struct pool_block *block;
43 /* Current serial number to allow freeing of gizmos. */
47 /* General routines. */
48 struct pool *pool_create (void);
49 void pool_destroy (struct pool *);
50 void pool_clear (struct pool *);
52 /* Creates a pool, allocates an instance of the given STRUCT
53 within it, sets the struct's MEMBER to the pool's address, and
54 returns the allocated structure. */
55 #define pool_create_container(STRUCT, MEMBER) \
56 ((STRUCT *) pool_create_at_offset (sizeof (STRUCT), \
57 offsetof (STRUCT, MEMBER)))
58 void *pool_create_at_offset (size_t struct_size, size_t pool_member_offset);
60 /* Suballocation routines. */
61 void *pool_alloc (struct pool *, size_t) MALLOC_LIKE;
62 void *pool_nalloc (struct pool *, size_t n, size_t s) MALLOC_LIKE;
63 void *pool_clone (struct pool *, const void *, size_t) MALLOC_LIKE;
65 void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE;
66 void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE;
67 char *pool_strdup (struct pool *, const char *) MALLOC_LIKE;
68 char *pool_vasprintf (struct pool *, const char *, va_list)
69 MALLOC_LIKE PRINTF_FORMAT (2, 0);
70 char *pool_asprintf (struct pool *, const char *, ...)
71 MALLOC_LIKE PRINTF_FORMAT (2, 3);
73 /* Standard allocation routines. */
74 void *pool_malloc (struct pool *, size_t) MALLOC_LIKE;
75 void *pool_nmalloc (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 *);
82 struct pool *pool_create_subpool (struct pool *);
83 void pool_add_subpool (struct pool *, struct pool *subpool);
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 *);
92 /* Custom allocations. */
93 void pool_register (struct pool *, void (*free) (void *), void *p);
94 bool pool_unregister (struct pool *, void *);
96 /* Partial freeing. */
97 void pool_mark (struct pool *, struct pool_mark *);
98 void pool_release (struct pool *, const struct pool_mark *);
101 void pool_dump (const struct pool *, const char *title);