fatbase

portable OpenBSD tools
git clone git://git.2f30.org/fatbase
Log | Files | Refs

xmalloc.c (2171B)


      1 /* $OpenBSD: xmalloc.c,v 1.4 2013/08/19 20:21:15 millert Exp $ */
      2 /*
      3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
      4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
      5  *                    All rights reserved
      6  * Versions of malloc and friends that check their results, and never return
      7  * failure (they call fatal if they encounter an error).
      8  *
      9  * As far as I am concerned, the code I have written for this software
     10  * can be used freely for any purpose.  Any derived versions of this
     11  * software must be clearly marked as such, and if the derived work is
     12  * incompatible with the protocol description in the RFC file, it must be
     13  * called by a name other than "ssh" or "Secure Shell".
     14  */
     15 
     16 #include <limits.h>
     17 #include <stdarg.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <string.h>
     21 #include <sys/types.h>
     22 
     23 #include "util.h"
     24 #include "xmalloc.h"
     25 
     26 void *
     27 xmalloc(size_t size)
     28 {
     29 	void *ptr;
     30 
     31 	if (size == 0)
     32 		errx(2, "xmalloc: zero size");
     33 	ptr = malloc(size);
     34 	if (ptr == NULL)
     35 		err(2, NULL);
     36 	return ptr;
     37 }
     38 
     39 void *
     40 xcalloc(size_t nmemb, size_t size)
     41 {
     42 	void *ptr;
     43 
     44 	if (size == 0 || nmemb == 0)
     45 		errx(2, "xcalloc: zero size");
     46 	if (SIZE_MAX / nmemb < size)
     47 		errx(2, "xcalloc: nmemb * size > SIZE_MAX");
     48 	ptr = calloc(nmemb, size);
     49 	if (ptr == NULL)
     50 		errx(2, "xcalloc: out of memory (allocating %lu bytes)",
     51 		    (u_long)(size * nmemb));
     52 	return ptr;
     53 }
     54 
     55 void *
     56 xrealloc(void *ptr, size_t nmemb, size_t size)
     57 {
     58 	void *new_ptr;
     59 	size_t new_size = nmemb * size;
     60 
     61 	if (new_size == 0)
     62 		errx(2, "xrealloc: zero size");
     63 	if (SIZE_MAX / nmemb < size)
     64 		errx(2, "xrealloc: nmemb * size > SIZE_MAX");
     65 	if (ptr == NULL)
     66 		new_ptr = malloc(new_size);
     67 	else
     68 		new_ptr = realloc(ptr, new_size);
     69 	if (new_ptr == NULL)
     70 		err(2, NULL);
     71 	return new_ptr;
     72 }
     73 
     74 void
     75 xfree(void *ptr)
     76 {
     77 	if (ptr == NULL)
     78 		err(2, NULL);
     79 	free(ptr);
     80 }
     81 
     82 char *
     83 xstrdup(const char *str)
     84 {
     85 	size_t len;
     86 	char *cp;
     87 
     88 	len = strlen(str) + 1;
     89 	cp = xmalloc(len);
     90 	strlcpy(cp, str, len);
     91 	return cp;
     92 }
     93 
     94 int
     95 xasprintf(char **ret, const char *fmt, ...)
     96 {
     97 	va_list ap;
     98 	int i;
     99 
    100 	va_start(ap, fmt);
    101 	i = vasprintf(ret, fmt, ap);
    102 	va_end(ap);
    103 
    104 	if (i < 0 || *ret == NULL)
    105 		err(2, NULL);
    106 
    107 	return (i);
    108 }