[xiph-commits] r12524 - trunk/ezstream/src

moritz at svn.xiph.org moritz at svn.xiph.org
Sat Feb 24 12:49:27 PST 2007


Author: moritz
Date: 2007-02-24 12:49:25 -0800 (Sat, 24 Feb 2007)
New Revision: 12524

Added:
   trunk/ezstream/src/util.c
   trunk/ezstream/src/util.h
Log:
Add new utility functions, for memory management. These do The Right Thing and
never return NULL.


Added: trunk/ezstream/src/util.c
===================================================================
--- trunk/ezstream/src/util.c	2007-02-24 20:26:01 UTC (rev 12523)
+++ trunk/ezstream/src/util.c	2007-02-24 20:49:25 UTC (rev 12524)
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2007 Moritz Grimm <gtgbr at 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);
+}

Added: trunk/ezstream/src/util.h
===================================================================
--- trunk/ezstream/src/util.h	2007-02-24 20:26:01 UTC (rev 12523)
+++ trunk/ezstream/src/util.h	2007-02-24 20:49:25 UTC (rev 12524)
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2007 Moritz Grimm <gtgbr at 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__ */



More information about the commits mailing list