1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2000, 2010, 2011, 2012 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "libpspp/pool.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/message.h"
26 #include "libpspp/temp-file.h"
27 #include "libpspp/str.h"
29 #include "gl/xalloc.h"
31 /* Fast, low-overhead memory block suballocator. */
34 struct pool *parent; /* Pool of which this pool is a subpool. */
35 struct pool_block *blocks; /* Blocks owned by the pool. */
36 struct pool_gizmo *gizmos; /* Other stuff owned by the pool. */
42 struct pool_block *prev;
43 struct pool_block *next;
54 POOL_GIZMO_REGISTERED,
57 /* Pool routines can maintain objects (`gizmos') as well as doing
59 This structure is used to keep track of them. */
63 struct pool_gizmo *prev;
64 struct pool_gizmo *next;
66 long serial; /* Serial number. */
67 int type; /* Type of this gizmo. */
69 /* Type-dependent info. */
72 FILE *file; /* POOL_GIZMO_FILE, POOL_GIZMO_TEMP_FILE. */
73 struct pool *subpool; /* POOL_GIZMO_SUBPOOL. */
75 /* POOL_GIZMO_REGISTERED. */
78 void (*free) (void *p);
86 /* Rounds X up to the next multiple of Y. */
88 #define ROUND_UP(X, Y) \
89 (((X) + ((Y) - 1)) / (Y) * (Y))
92 /* Types that provide typically useful alignment sizes. */
100 /* glibc jmp_buf on ia64 requires 16-byte alignment. This ensures it. */
104 /* This should be the alignment size used by malloc(). The size of
105 the union above is correct, if not optimal, in all known cases.
107 This is normally 8 bytes for 32-bit architectures and 16 bytes for 64-bit
109 #define ALIGN_SIZE sizeof (union align)
111 /* DISCRETE_BLOCKS may be declared as nonzero to prevent
112 suballocation of blocks. This is useful under memory
113 debuggers like valgrind because it allows the source location
114 of bugs to be more accurately pinpointed.
116 On the other hand, if we're testing the library, then we want to
117 test the library's real functionality, not its crippled, slow,
118 simplified functionality. */
119 /*#define DISCRETE_BLOCKS 1*/
121 /* Size of each block allocated in the pool, in bytes.
122 Should be at least 1k. */
124 #define BLOCK_SIZE 1024
128 /* Sizes of some structures with alignment padding included. */
129 #define POOL_BLOCK_SIZE ROUND_UP (sizeof (struct pool_block), ALIGN_SIZE)
130 #define POOL_GIZMO_SIZE ROUND_UP (sizeof (struct pool_gizmo), ALIGN_SIZE)
131 #define POOL_SIZE ROUND_UP (sizeof (struct pool), ALIGN_SIZE)
133 /* Serial number used to keep track of gizmos for mark/release. */
134 static long serial = 0;
137 static void add_gizmo (struct pool *, struct pool_gizmo *);
138 static void free_gizmo (struct pool_gizmo *);
139 static void free_all_gizmos (struct pool *pool);
140 static void delete_gizmo (struct pool *, struct pool_gizmo *);
141 static void check_gizmo (struct pool *, struct pool_gizmo *);
143 /* General routines. */
145 /* Creates and returns a new memory pool, which allows malloc()'d
146 blocks to be suballocated in a time- and space-efficient manner.
147 The entire contents of the memory pool are freed at once.
149 In addition, other objects can be associated with a memory pool.
150 These are released when the pool is destroyed. */
154 struct pool_block *block;
157 block = xmalloc (BLOCK_SIZE);
158 block->prev = block->next = block;
159 block->ofs = POOL_BLOCK_SIZE + POOL_SIZE;
161 pool = (struct pool *) (((char *) block) + POOL_BLOCK_SIZE);
163 pool->blocks = block;
169 /* Creates a pool, allocates a block STRUCT_SIZE bytes in
170 length from it, stores the pool's address at offset
171 POOL_MEMBER_OFFSET within the block, and returns the allocated
174 Meant for use indirectly via pool_create_container(). */
176 pool_create_at_offset (size_t struct_size, size_t pool_member_offset)
181 assert (struct_size >= sizeof pool);
182 assert (pool_member_offset <= struct_size - sizeof pool);
184 pool = pool_create ();
185 struct_ = pool_alloc (pool, struct_size);
186 *(struct pool **) (struct_ + pool_member_offset) = pool;
190 /* Destroy the specified pool, including all subpools. */
192 pool_destroy (struct pool *pool)
197 /* Remove this pool from its parent's list of gizmos. */
199 delete_gizmo (pool->parent, (void *) (((char *) pool) + POOL_SIZE));
201 free_all_gizmos (pool);
203 /* Free all the memory. */
205 struct pool_block *cur, *next;
207 pool->blocks->prev->next = NULL;
208 for (cur = pool->blocks; cur; cur = next)
216 /* Release all the memory and gizmos in POOL.
217 Blocks are not given back with free() but kept for later
218 allocations. To give back memory, use a subpool instead. */
220 pool_clear (struct pool *pool)
222 free_all_gizmos (pool);
224 /* Zero out block sizes. */
226 struct pool_block *cur;
231 cur->ofs = POOL_BLOCK_SIZE;
232 if ((char *) cur + POOL_BLOCK_SIZE == (char *) pool)
234 cur->ofs += POOL_SIZE;
235 if (pool->parent != NULL)
236 cur->ofs += POOL_GIZMO_SIZE;
240 while (cur != pool->blocks);
244 /* Suballocation routines. */
246 /* Allocates a memory region AMT bytes in size from POOL and returns a
247 pointer to the region's start.
248 The region is properly aligned for storing any object. */
250 pool_alloc (struct pool *pool, size_t amt)
252 assert (pool != NULL);
257 #ifndef DISCRETE_BLOCKS
258 if (amt <= MAX_SUBALLOC)
260 /* If there is space in this block, take it. */
261 struct pool_block *b = pool->blocks;
262 b->ofs = ROUND_UP (b->ofs, ALIGN_SIZE);
263 if (b->ofs + amt <= BLOCK_SIZE)
265 void *const p = ((char *) b) + b->ofs;
270 /* No space in this block, so we must make other
272 if (b->next->ofs == 0)
274 /* The next block is empty. Use it. */
276 b->ofs = POOL_BLOCK_SIZE;
277 if ((char *) b + POOL_BLOCK_SIZE == (char *) pool)
282 /* Create a new block at the start of the list. */
283 b = xmalloc (BLOCK_SIZE);
284 b->next = pool->blocks;
285 b->prev = pool->blocks->prev;
286 b->ofs = POOL_BLOCK_SIZE;
287 pool->blocks->prev->next = b;
288 pool->blocks->prev = b;
292 /* Allocate space from B. */
294 return ((char *) b) + b->ofs - amt;
298 return pool_malloc (pool, amt);
301 /* Allocates a memory region AMT bytes in size from POOL and
302 returns a pointer to the region's start. The region is not
303 necessarily aligned, so it is most suitable for storing
306 pool_alloc_unaligned (struct pool *pool, size_t amt)
309 return xmalloc (amt);
311 #ifndef DISCRETE_BLOCKS
312 /* Strings need not be aligned on any boundary, but some
313 operations may be more efficient when they are. However,
314 that's only going to help with reasonably long strings. */
315 if (amt < ALIGN_SIZE)
321 struct pool_block *const b = pool->blocks;
323 if (b->ofs + amt <= BLOCK_SIZE)
325 void *p = ((char *) b) + b->ofs;
333 return pool_alloc (pool, amt);
336 /* Allocates a memory region N * S bytes in size from POOL and
337 returns a pointer to the region's start.
338 N must be nonnegative, S must be positive.
339 Terminates the program if the memory cannot be obtained,
340 including the case where N * S overflows the range of size_t. */
342 pool_nalloc (struct pool *pool, size_t n, size_t s)
344 if (xalloc_oversized (n, s))
346 return pool_alloc (pool, n * s);
349 /* Allocates SIZE bytes in POOL, copies BUFFER into it, and
350 returns the new copy. */
352 pool_clone (struct pool *pool, const void *buffer, size_t size)
354 void *block = pool_alloc (pool, size);
355 memcpy (block, buffer, size);
359 /* Allocates SIZE bytes of unaligned data in POOL, copies BUFFER
360 into it, and returns the new copy. */
362 pool_clone_unaligned (struct pool *pool, const void *buffer, size_t size)
364 void *block = pool_alloc_unaligned (pool, size);
365 memcpy (block, buffer, size);
369 /* Duplicates null-terminated STRING, within POOL, and returns a
370 pointer to the duplicate. For use only with strings, because
371 the returned pointere may not be aligned properly for other
374 pool_strdup (struct pool *pool, const char *string)
376 return pool_clone_unaligned (pool, string, strlen (string) + 1);
379 /* Duplicates the SIZE bytes of STRING, plus a trailing 0 byte,
380 and returns a pointer to the duplicate. For use only with
381 strings, because the returned pointere may not be aligned
382 properly for other types. */
384 pool_strdup0 (struct pool *pool, const char *string, size_t size)
386 char *new_string = pool_alloc_unaligned (pool, size + 1);
387 memcpy (new_string, string, size);
388 new_string[size] = '\0';
392 /* Formats FORMAT with the given ARGS in memory allocated from
393 POOL and returns the formatted string. */
395 pool_vasprintf (struct pool *pool, const char *format, va_list args_)
397 struct pool_block *b;
402 va_copy (args, args_);
404 avail = BLOCK_SIZE - b->ofs;
405 s = ((char *) b) + b->ofs;
406 needed = vsnprintf (s, avail, format, args);
413 /* Success. Reserve the space that was actually used. */
414 b->ofs += needed + 1;
418 /* Failure, but now we know how much space is needed.
419 Allocate that much and reformat. */
420 s = pool_alloc (pool, needed + 1);
422 va_copy (args, args_);
423 vsprintf (s, format, args);
429 /* Some old libc's returned -1 when the destination string
430 was too short. This should be uncommon these days and
431 it's a rare case anyhow. Use the easiest solution: punt
432 to dynamic allocation. */
433 va_copy (args, args_);
434 s = xvasprintf (format, args);
437 pool_register (pool, free, s);
443 /* Formats FORMAT in memory allocated from POOL
444 and returns the formatted string. */
446 pool_asprintf (struct pool *pool, const char *format, ...)
451 va_start (args, format);
452 string = pool_vasprintf (pool, format, args);
458 /* Standard allocation routines. */
460 /* Allocates AMT bytes using malloc(), to be managed by POOL, and
461 returns a pointer to the beginning of the block.
462 If POOL is a null pointer, then allocates a normal memory block
465 pool_malloc (struct pool *pool, size_t amt)
471 struct pool_gizmo *g = xmalloc (amt + POOL_GIZMO_SIZE);
472 g->type = POOL_GIZMO_MALLOC;
475 return ((char *) g) + POOL_GIZMO_SIZE;
481 return xmalloc (amt);
484 /* Allocates and returns N elements of S bytes each, to be
486 If POOL is a null pointer, then allocates a normal memory block
488 N must be nonnegative, S must be positive.
489 Terminates the program if the memory cannot be obtained,
490 including the case where N * S overflows the range of size_t. */
492 pool_nmalloc (struct pool *pool, size_t n, size_t s)
494 if (xalloc_oversized (n, s))
496 return pool_malloc (pool, n * s);
499 /* Allocates AMT bytes using malloc(), to be managed by POOL,
500 zeros the block, and returns a pointer to the beginning of the
502 If POOL is a null pointer, then allocates a normal memory block
505 pool_zalloc (struct pool *pool, size_t amt)
507 void *p = pool_malloc (pool, amt);
512 /* Allocates and returns N elements of S bytes each, to be
513 managed by POOL, and zeros the block.
514 If POOL is a null pointer, then allocates a normal memory block
516 N must be nonnegative, S must be positive.
517 Terminates the program if the memory cannot be obtained,
518 including the case where N * S overflows the range of size_t. */
520 pool_calloc (struct pool *pool, size_t n, size_t s)
522 void *p = pool_nmalloc (pool, n, s);
523 memset (p, 0, n * s);
527 /* Changes the allocation size of the specified memory block P managed
528 by POOL to AMT bytes and returns a pointer to the beginning of the
530 If POOL is a null pointer, then the block is reallocated in the
531 usual way with realloc(). */
533 pool_realloc (struct pool *pool, void *p, size_t amt)
541 struct pool_gizmo *g = (void *) (((char *) p) - POOL_GIZMO_SIZE);
542 check_gizmo (pool, g);
544 g = xrealloc (g, amt + POOL_GIZMO_SIZE);
551 check_gizmo (pool, g);
553 return ((char *) g) + POOL_GIZMO_SIZE;
562 return pool_malloc (pool, amt);
565 return xrealloc (p, amt);
568 /* Changes the allocation size of the specified memory block P
569 managed by POOL to N * S bytes and returns a pointer to the
570 beginning of the block.
571 N must be nonnegative, S must be positive.
572 If POOL is a null pointer, then the block is reallocated in
573 the usual way with xrealloc().
574 Terminates the program if the memory cannot be obtained,
575 including the case where N * S overflows the range of size_t. */
577 pool_nrealloc (struct pool *pool, void *p, size_t n, size_t s)
579 if (xalloc_oversized (n, s))
581 return pool_realloc (pool, p, n * s);
584 /* If P is null, allocate a block of at least *PN such objects;
585 otherwise, reallocate P so that it contains more than *PN
586 objects each of S bytes. *PN must be nonzero unless P is
587 null, and S must be nonzero. Set *PN to the new number of
588 objects, and return the pointer to the new block. *PN is
589 never set to zero, and the returned pointer is never null.
591 The block returned is managed by POOL. If POOL is a null
592 pointer, then the block is reallocated in the usual way with
595 Terminates the program if the memory cannot be obtained,
596 including the case where the memory required overflows the
599 Repeated reallocations are guaranteed to make progress, either by
600 allocating an initial block with a nonzero size, or by allocating a
603 In the following implementation, nonzero sizes are doubled so that
604 repeated reallocations have O(N log N) overall cost rather than
605 O(N**2) cost, but the specification for this function does not
606 guarantee that sizes are doubled.
608 Here is an example of use:
613 size_t allocated = 0;
616 append_int (int value)
618 if (used == allocated)
619 p = pool_2nrealloc (pool, p, &allocated, sizeof *p);
623 This causes x2nrealloc to allocate a block of some nonzero size the
624 first time it is called.
626 To have finer-grained control over the initial size, set *PN to a
627 nonzero value before calling this function with P == NULL. For
633 size_t allocated = 0;
634 size_t allocated1 = 1000;
637 append_int (int value)
639 if (used == allocated)
641 p = pool_2nrealloc (pool, p, &allocated1, sizeof *p);
642 allocated = allocated1;
647 This function implementation is from gnulib. */
649 pool_2nrealloc (struct pool *pool, void *p, size_t *pn, size_t s)
657 /* The approximate size to use for initial small allocation
658 requests, when the invoking code specifies an old size of
659 zero. 64 bytes is the largest "small" request for the
660 GNU C library malloc. */
661 enum { DEFAULT_MXFAST = 64 };
663 n = DEFAULT_MXFAST / s;
669 if (SIZE_MAX / 2 / s < n)
675 return pool_realloc (pool, p, n * s);
678 /* Frees block P managed by POOL.
679 If POOL is a null pointer, then the block is freed as usual with
682 pool_free (struct pool *pool, void *p)
684 if (pool != NULL && p != NULL)
686 struct pool_gizmo *g = (void *) (((char *) p) - POOL_GIZMO_SIZE);
687 check_gizmo (pool, g);
688 delete_gizmo (pool, g);
695 /* Gizmo allocations. */
697 /* Creates and returns a pool as a subpool of POOL.
698 The subpool will be destroyed automatically when POOL is destroyed.
699 It may also be destroyed explicitly in advance. */
701 pool_create_subpool (struct pool *pool)
703 struct pool *subpool;
704 struct pool_gizmo *g;
706 assert (pool != NULL);
707 subpool = pool_create ();
708 subpool->parent = pool;
710 g = (void *) (((char *) subpool->blocks) + subpool->blocks->ofs);
711 subpool->blocks->ofs += POOL_GIZMO_SIZE;
713 g->type = POOL_GIZMO_SUBPOOL;
714 g->p.subpool = subpool;
721 /* Makes SUBPOOL a subpool of POOL.
722 SUBPOOL must not already have a parent pool.
723 The subpool will be destroyed automatically when POOL is destroyed.
724 It may also be destroyed explicitly in advance. */
726 pool_add_subpool (struct pool *pool, struct pool *subpool)
728 struct pool_gizmo *g;
730 assert (pool != NULL);
731 assert (subpool != NULL);
732 assert (subpool->parent == NULL);
734 g = pool_alloc (subpool, sizeof *g);
735 g->type = POOL_GIZMO_SUBPOOL;
736 g->p.subpool = subpool;
739 subpool->parent = pool;
742 /* Opens file FILE_NAME with mode MODE and returns a handle to it
743 if successful or a null pointer if not.
744 The file will be closed automatically when POOL is destroyed, or it
745 may be closed explicitly in advance using pool_fclose(), or
746 detached from the pool with pool_detach_file(). */
748 pool_fopen (struct pool *pool, const char *file_name, const char *mode)
752 assert (pool && file_name && mode);
753 f = fopen (file_name, mode);
755 pool_attach_file (pool, f);
760 /* Closes file FILE managed by POOL.
761 Returns 0 if successful, EOF if an I/O error occurred. */
763 pool_fclose (struct pool *pool, FILE *file)
765 assert (pool && file);
766 pool_detach_file (pool, file);
767 return fclose (file);
770 /* Attaches FILE to POOL.
771 The file will be closed automatically when POOL is destroyed, or it
772 may be closed explicitly in advance using pool_fclose(), or
773 detached from the pool with pool_detach_file(). */
775 pool_attach_file (struct pool *pool, FILE *file)
777 struct pool_gizmo *g = pool_alloc (pool, sizeof *g);
778 g->type = POOL_GIZMO_FILE;
783 /* Detaches FILE from POOL. */
785 pool_detach_file (struct pool *pool, FILE *file)
787 struct pool_gizmo *g;
789 for (g = pool->gizmos; g; g = g->next)
790 if (g->type == POOL_GIZMO_FILE && g->p.file == file)
792 delete_gizmo (pool, g);
797 /* Creates a temporary file with create_temp_file() and returns a handle to it
798 if successful or a null pointer if not.
799 The file will be closed automatically when POOL is destroyed, or it
800 may be closed explicitly in advance using pool_fclose_temp_file(), or
801 detached from the pool with pool_detach_temp_file(). */
803 pool_create_temp_file (struct pool *pool)
805 FILE *file = create_temp_file ();
807 pool_attach_temp_file (pool, file);
811 /* Closes file FILE managed by POOL.
812 FILE must have been opened with create_temp_file(). */
814 pool_fclose_temp_file (struct pool *pool, FILE *file)
816 assert (pool && file);
817 pool_detach_temp_file (pool, file);
818 close_temp_file (file);
821 /* Attaches FILE, which must have been opened with create_temp_file(), to POOL.
822 The file will be closed automatically when POOL is destroyed, or it
823 may be closed explicitly in advance using pool_fclose_temp_file(), or
824 detached from the pool with pool_detach_temp_file(). */
826 pool_attach_temp_file (struct pool *pool, FILE *file)
828 struct pool_gizmo *g = pool_alloc (pool, sizeof *g);
829 g->type = POOL_GIZMO_TEMP_FILE;
834 /* Detaches FILE that was opened with create_temp_file() from POOL. */
836 pool_detach_temp_file (struct pool *pool, FILE *file)
838 struct pool_gizmo *g;
840 for (g = pool->gizmos; g; g = g->next)
841 if (g->type == POOL_GIZMO_TEMP_FILE && g->p.file == file)
843 delete_gizmo (pool, g);
848 /* Registers FREE to be called with argument P.
849 P should be unique among those registered in POOL so that it can be
850 uniquely identified by pool_unregister().
851 If not unregistered, FREE will be called with argument P when POOL
854 pool_register (struct pool *pool, void (*free) (void *), void *p)
856 assert (pool && free && p);
859 struct pool_gizmo *g = pool_alloc (pool, sizeof *g);
860 g->type = POOL_GIZMO_REGISTERED;
861 g->p.registered.free = free;
862 g->p.registered.p = p;
867 /* Unregisters previously registered P from POOL.
868 Returns true only if P was found to be registered in POOL. */
870 pool_unregister (struct pool *pool, void *p)
875 struct pool_gizmo *g;
877 for (g = pool->gizmos; g; g = g->next)
878 if (g->type == POOL_GIZMO_REGISTERED && g->p.registered.p == p)
880 delete_gizmo (pool, g);
888 /* Partial freeing. */
890 /* Notes the state of POOL into MARK so that it may be restored
891 by a call to pool_release(). */
893 pool_mark (struct pool *pool, struct pool_mark *mark)
895 assert (pool && mark);
897 mark->block = pool->blocks;
898 mark->ofs = pool->blocks->ofs;
900 mark->serial = serial;
903 /* Restores to POOL the state recorded in MARK.
904 Emptied blocks are not given back with free() but kept for
905 later allocations. To get that behavior, use a subpool
908 pool_release (struct pool *pool, const struct pool_mark *mark)
910 assert (pool && mark);
913 struct pool_gizmo *cur, *next;
915 for (cur = pool->gizmos; cur && cur->serial >= mark->serial; cur = next)
931 struct pool_block *cur;
933 for (cur = pool->blocks; cur != mark->block; cur = cur->next)
935 cur->ofs = POOL_BLOCK_SIZE;
936 if ((char *) cur + POOL_BLOCK_SIZE == (char *) pool)
938 cur->ofs += POOL_SIZE;
939 if (pool->parent != NULL)
940 cur->ofs += POOL_GIZMO_SIZE;
943 pool->blocks = mark->block;
944 pool->blocks->ofs = mark->ofs;
948 /* Private functions. */
950 /* Adds GIZMO at the beginning of POOL's gizmo list. */
952 add_gizmo (struct pool *pool, struct pool_gizmo *gizmo)
954 assert (pool && gizmo);
957 gizmo->next = pool->gizmos;
960 pool->gizmos->prev = gizmo;
961 pool->gizmos = gizmo;
963 gizmo->serial = serial++;
965 check_gizmo (pool, gizmo);
968 /* Removes GIZMO from POOL's gizmo list. */
970 delete_gizmo (struct pool *pool, struct pool_gizmo *gizmo)
972 assert (pool && gizmo);
974 check_gizmo (pool, gizmo);
977 gizmo->prev->next = gizmo->next;
979 pool->gizmos = gizmo->next;
981 gizmo->next->prev = gizmo->prev;
984 /* Frees any of GIZMO's internal state.
985 GIZMO's data must not be referenced after calling this function. */
987 free_gizmo (struct pool_gizmo *gizmo)
989 assert (gizmo != NULL);
993 case POOL_GIZMO_MALLOC:
996 case POOL_GIZMO_FILE:
997 fclose (gizmo->p.file); /* Ignore errors. */
999 case POOL_GIZMO_TEMP_FILE:
1000 close_temp_file (gizmo->p.file); /* Ignore errors. */
1002 case POOL_GIZMO_SUBPOOL:
1003 gizmo->p.subpool->parent = NULL;
1004 pool_destroy (gizmo->p.subpool);
1006 case POOL_GIZMO_REGISTERED:
1007 gizmo->p.registered.free (gizmo->p.registered.p);
1014 /* Free all the gizmos in POOL. */
1016 free_all_gizmos (struct pool *pool)
1018 struct pool_gizmo *cur, *next;
1020 for (cur = pool->gizmos; cur; cur = next)
1025 pool->gizmos = NULL;
1029 check_gizmo (struct pool *p, struct pool_gizmo *g)
1031 assert (g->pool == p);
1032 assert (g->next == NULL || g->next->prev == g);
1033 assert ((g->prev != NULL && g->prev->next == g)
1034 || (g->prev == NULL && p->gizmos == g));