X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Falloc.c;h=a7b20283f38588f7fd7f955f692c0d624e4eff29;hb=92fb12eb06716d14c05b781f5d9dcde956d77c30;hp=763dab3d720ecbf482e94757ca41dad6b621b7bd;hpb=4944c86a9318bc5b5578ab145a95c116ffd2c9fd;p=pspp diff --git a/src/alloc.c b/src/alloc.c index 763dab3d72..a7b20283f3 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -14,109 +14,19 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include -#include -#include +#include "alloc.h" #include -#include "str.h" -static void out_of_memory (void); - -/* Public functions. */ - -/* Allocates a block of SIZE bytes and returns it. - If SIZE is 0, returns a null pointer. - Aborts if unsuccessful. */ -void * -xmalloc (size_t size) -{ - void *vp; - if (size == 0) - return NULL; - - vp = malloc (size); - if (!vp) - out_of_memory (); - - return vp; -} - -/* Allocates a block of SIZE bytes, fill it with all-bits-0, and - returns it. - If SIZE is 0, returns a null pointer. - Aborts if unsuccessful. */ +/* Allocates and returns N elements of S bytes each. + N must be nonnegative, S must be positive. + Returns a null pointer if the memory cannot be obtained, + including the case where N * S overflows the range of size_t. */ void * -xcalloc (size_t size) -{ - void *vp = xmalloc (size); - memset (vp, 0, size); - return vp; -} - -/* If SIZE is 0, then block PTR is freed and a null pointer is - returned. - Otherwise, if PTR is a null pointer, then a new block is allocated - and returned. - Otherwise, block PTR is reallocated to be SIZE bytes in size and - the new location of the block is returned. - Aborts if unsuccessful. */ -void * -xrealloc (void *ptr, size_t size) -{ - void *vp; - if (!size) - { - if (ptr) - free (ptr); - - return NULL; - } - - if (ptr) - vp = realloc (ptr, size); - else - vp = malloc (size); - - if (!vp) - out_of_memory (); - - return vp; -} - -/* Makes a copy of string S in malloc()'d memory and returns the copy. - S must not be a null pointer. */ -char * -xstrdup (const char *s) -{ - size_t size; - char *t; - - assert (s != NULL); - - size = strlen (s) + 1; - - t = malloc (size); - if (!t) - out_of_memory (); - - memcpy (t, s, size); - return t; -} - -/* Private functions. */ - -/* Report an out-of-memory condition and abort execution. */ -static void -out_of_memory (void) +nmalloc (size_t n, size_t s) { -#if __CHECKER__ - fprintf (stderr, "Out of memory: inducing segfault\n"); - *((int *) 0) = 0; -#else - fprintf (stderr, "virtual memory exhausted\n"); - exit (EXIT_FAILURE); -#endif + return !xalloc_oversized (n, s) ? malloc (n * s) : NULL; }