X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Flibpspp%2Fpool.c;h=c39852dcc7a2977becee7d8c0b970c11d52471d4;hb=d08af71f38b751323cc9506631bf9ce71070a4ae;hp=0e6c9358fc828cd10f027c32d9f5750c7ffd5c23;hpb=e7bafb24d29d2370665c02e070f90fa180ad8056;p=pspp-builds.git diff --git a/src/libpspp/pool.c b/src/libpspp/pool.c index 0e6c9358..c39852dc 100644 --- a/src/libpspp/pool.c +++ b/src/libpspp/pool.c @@ -1,6 +1,5 @@ /* PSPP - computes sample statistics. Copyright (C) 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -21,6 +20,7 @@ #include "pool.h" #include #include "alloc.h" +#include #include "message.h" #include "size_max.h" #include "str.h" @@ -100,8 +100,8 @@ union align /* DISCRETE_BLOCKS may be declared as nonzero to prevent suballocation of blocks. This is useful under memory - debuggers like Checker or valgrind because it allows the - source location of bugs to be more accurately pinpointed. + debuggers like valgrind because it allows the source location + of bugs to be more accurately pinpointed. On the other hand, if we're testing the library, then we want to test the library's real functionality, not its crippled, slow, @@ -472,6 +472,34 @@ pool_nmalloc (struct pool *pool, size_t n, size_t s) return pool_malloc (pool, n * s); } +/* Allocates AMT bytes using malloc(), to be managed by POOL, + zeros the block, and returns a pointer to the beginning of the + block. + If POOL is a null pointer, then allocates a normal memory block + with xmalloc(). */ +void * +pool_zalloc (struct pool *pool, size_t amt) +{ + void *p = pool_malloc (pool, amt); + memset (p, 0, amt); + return p; +} + +/* Allocates and returns N elements of S bytes each, to be + managed by POOL, and zeros the block. + If POOL is a null pointer, then allocates a normal memory block + with malloc(). + N must be nonnegative, S must be positive. + Terminates the program if the memory cannot be obtained, + including the case where N * S overflows the range of size_t. */ +void * +pool_calloc (struct pool *pool, size_t n, size_t s) +{ + void *p = pool_nmalloc (pool, n, s); + memset (p, 0, n * s); + return p; +} + /* Changes the allocation size of the specified memory block P managed by POOL to AMT bytes and returns a pointer to the beginning of the block. @@ -699,8 +727,8 @@ pool_fopen (struct pool *pool, const char *file_name, const char *mode) assert (pool && file_name && mode); f = fopen (file_name, mode); - if (f == NULL) - return NULL; + if (f != NULL) + pool_attach_file (pool, f); return f; } @@ -776,8 +804,8 @@ pool_register (struct pool *pool, void (*free) (void *), void *p) } /* Unregisters previously registered P from POOL. - Returns nonzero only if P was found to be registered in POOL. */ -int + Returns true only if P was found to be registered in POOL. */ +bool pool_unregister (struct pool *pool, void *p) { assert (pool && p); @@ -789,11 +817,11 @@ pool_unregister (struct pool *pool, void *p) if (g->type == POOL_GIZMO_REGISTERED && g->p.registered.p == p) { delete_gizmo (pool, g); - return 1; + return true; } } - return 0; + return false; } /* Partial freeing. */ @@ -915,7 +943,7 @@ free_gizmo (struct pool_gizmo *gizmo) gizmo->p.registered.free (gizmo->p.registered.p); break; default: - assert (0); + NOT_REACHED (); } }