1
0
mirror of https://gitlab.xiph.org/xiph/ezstream.git synced 2024-11-03 04:17:18 -05:00

Add new utility functions, for memory management. These do The Right Thing and

never return NULL.


git-svn-id: https://svn.xiph.org/trunk/ezstream@12524 0101bb08-14d6-0310-b084-bc0e0c8e3800
This commit is contained in:
moritz 2007-02-24 20:49:25 +00:00
parent a90b52665d
commit 5d932e4191
2 changed files with 166 additions and 0 deletions

140
src/util.c Normal file
View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2007 Moritz Grimm <gtgbr@gmx.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#ifndef SIZE_T_MAX
# define SIZE_T_MAX UINT_MAX
#endif
extern int errno;
extern char *__progname;
void *
xmalloc(size_t size)
{
void *ret;
if (size == 0) {
printf("%s: xmalloc(): Internal error: zero size\n",
__progname);
abort();
}
if ((ret = malloc(size)) == NULL) {
printf("%s: xmalloc(): Allocating %lu bytes: %s\n",
__progname, (unsigned long)(size), strerror(errno));
exit(1);
}
return (ret);
}
void *
xcalloc(size_t nmemb, size_t size)
{
void *ret;
if (nmemb == 0 || size == 0) {
printf("%s: xcalloc(): Internal error: zero size\n",
__progname);
abort();
}
if (SIZE_T_MAX / nmemb < size) {
printf("%s: xcalloc(): Integer overflow, nmemb * size > SIZE_T_MAX\n",
__progname);
exit(1);
}
if ((ret = calloc(nmemb, size)) == NULL) {
printf("%s: xcalloc(): Allocating %lu bytes: %s\n",
__progname, (unsigned long)(nmemb * size), strerror(errno));
exit(1);
}
return (ret);
}
void *
xrealloc(void *ptr, size_t nmemb, size_t size)
{
void *ret;
size_t nsiz = nmemb * size;
if (nmemb == 0 || size == 0) {
printf("%s: xrealloc(): Internal error: zero size\n",
__progname);
abort();
}
if (SIZE_T_MAX / nmemb < size) {
printf("%s: xrealloc(): Integer overflow, nmemb * size > SIZE_T_MAX\n",
__progname);
exit(1);
}
if (ptr == NULL)
ret = malloc(nsiz);
else
ret = realloc(ptr, nsiz);
if (ret == NULL) {
printf("%s: xrealloc(): (Re)allocating %lu bytes: %s\n",
__progname, (unsigned long)(nmemb * size), strerror(errno));
exit(1);
}
return (ret);
}
void
xfree(void *ptr)
{
if (ptr == NULL) {
printf("%s: xfree(): Internal error: NULL argument\n",
__progname);
abort();
}
free(ptr);
ptr = NULL;
}
char *
xstrdup(const char *str)
{
size_t len;
char *nstr;
len = strlen(str) + 1;
nstr = xcalloc(1, len);
memcpy(nstr, str, len);
return (nstr);
}

26
src/util.h Normal file
View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2007 Moritz Grimm <gtgbr@gmx.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __UTIL_H__
#define __UTIL_H__
void * xmalloc(size_t /* size */);
void * xcalloc(size_t /* nmemb */, size_t /* size */);
void * xrealloc(void *, size_t /* nmemb */, size_t /* size */);
void xfree(void *);
char * xstrdup(const char *);
#endif /* __UTIL_H__ */