/* * alloc.c * * Trivial veneers for allocating memory * * © 1998 Straylight/Edgeware */ /*----- Licensing note ----------------------------------------------------* * * This file is part of Straylight's core utilities (coreutils). * * Coreutils is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Coreutils is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with coreutils. If not, write to the Free Software Foundation, * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*----- Header files ------------------------------------------------------*/ #include #include #include #include "alloc.h" /*----- Main code ---------------------------------------------------------*/ /* --- @xmalloc@ --- * * * Arguments: @size_t sz@ = size of block to allocate * * Returns: Pointer to newly allocated block. * * Use: Returns a block of the requested size, or not at all. */ void *xmalloc(size_t sz) { void *p = malloc(sz); if (!p) { fprintf(stderr, "inst: not enough memory\n"); exit(1); } return (p); } /* --- @xrealloc@ --- * * * Arguments: @void *p@ = pointer to a block of memory * @size_t sz@ = size we want it to be * * Returns: Pointer to resized block * * Use: Resizes a block. Returns the resized block, or not at all. */ void *xrealloc(void *p, size_t sz) { p = realloc(p, sz); if (!p) { fprintf(stderr, "inst: not enough memory\n"); exit(1); } return (p); } /* --- @xstrdup@ --- * * * Arguments: @const char *p@ = pointer to string to copy * * Returns: Pointer to a copy of the string. * * Use: Returns a copy of a string, or not at all. */ char *xstrdup(const char *p) { size_t sz = strlen(p) + 1; char *q = xmalloc(sz); memcpy(q, p, sz); return (q); } /*----- That's all, folks -------------------------------------------------*/