xref: /freebsd/libexec/rtld-elf/xmalloc.c (revision 3124c3e09305ad80d1b67c11e4962dc5f6e7fb01)
13124c3e0SJohn Polstra /*-
23124c3e0SJohn Polstra  * Copyright 1996-1998 John D. Polstra.
33124c3e0SJohn Polstra  * All rights reserved.
43124c3e0SJohn Polstra  *
53124c3e0SJohn Polstra  * Redistribution and use in source and binary forms, with or without
63124c3e0SJohn Polstra  * modification, are permitted provided that the following conditions
73124c3e0SJohn Polstra  * are met:
83124c3e0SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
93124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
103124c3e0SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
113124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
123124c3e0SJohn Polstra  *    documentation and/or other materials provided with the distribution.
133124c3e0SJohn Polstra  *
143124c3e0SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153124c3e0SJohn Polstra  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163124c3e0SJohn Polstra  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173124c3e0SJohn Polstra  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183124c3e0SJohn Polstra  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193124c3e0SJohn Polstra  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203124c3e0SJohn Polstra  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213124c3e0SJohn Polstra  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223124c3e0SJohn Polstra  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233124c3e0SJohn Polstra  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243124c3e0SJohn Polstra  *
253124c3e0SJohn Polstra  *      $Id: xmalloc.c,v 1.1 1998/03/05 21:05:54 jdp Exp $
263124c3e0SJohn Polstra  */
273124c3e0SJohn Polstra 
283124c3e0SJohn Polstra #include <err.h>
293124c3e0SJohn Polstra #include <stddef.h>
303124c3e0SJohn Polstra #include <stdlib.h>
313124c3e0SJohn Polstra #include <string.h>
323124c3e0SJohn Polstra 
333124c3e0SJohn Polstra void *xmalloc(size_t);
343124c3e0SJohn Polstra 
353124c3e0SJohn Polstra void *
363124c3e0SJohn Polstra xcalloc(size_t size)
373124c3e0SJohn Polstra {
383124c3e0SJohn Polstra     return memset(xmalloc(size), 0, size);
393124c3e0SJohn Polstra }
403124c3e0SJohn Polstra 
413124c3e0SJohn Polstra void *
423124c3e0SJohn Polstra xmalloc(size_t size)
433124c3e0SJohn Polstra {
443124c3e0SJohn Polstra     void *p = malloc(size);
453124c3e0SJohn Polstra     if (p == NULL)
463124c3e0SJohn Polstra 	err(1, "Out of memory");
473124c3e0SJohn Polstra     return p;
483124c3e0SJohn Polstra }
493124c3e0SJohn Polstra 
503124c3e0SJohn Polstra char *
513124c3e0SJohn Polstra xstrdup(const char *s)
523124c3e0SJohn Polstra {
533124c3e0SJohn Polstra     char *p = strdup(s);
543124c3e0SJohn Polstra     if (p == NULL)
553124c3e0SJohn Polstra 	err(1, "Out of memory");
563124c3e0SJohn Polstra     return p;
573124c3e0SJohn Polstra }
58