summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorKlaus Kaempf <kkaempf@suse.de>2007-10-02 12:40:07 +0000
committerKlaus Kaempf <kkaempf@suse.de>2007-10-02 12:40:07 +0000
commit8f8a9ed5192a6737d63364029cb05d91f1d0e399 (patch)
tree287434efe18f662976f8dad334d489dd60ff6e88 /src/util.c
downloadlibsolv-8f8a9ed5192a6737d63364029cb05d91f1d0e399.tar.gz
libsolv-8f8a9ed5192a6737d63364029cb05d91f1d0e399.tar.bz2
libsolv-8f8a9ed5192a6737d63364029cb05d91f1d0e399.zip
current state of 'sat-solver'
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..4b95036
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "util.h"
+
+void *
+xmalloc(size_t len)
+{
+ void *r = malloc(len ? len : 1);
+ if (r)
+ return r;
+ fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
+ exit(1);
+}
+
+void *
+xmalloc2(size_t num, size_t len)
+{
+ if (len && (num * len) / len != num)
+ {
+ fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
+ exit(1);
+ }
+ return xmalloc(num * len);
+}
+
+void *
+xrealloc(void *old, size_t len)
+{
+ if (old == 0)
+ old = malloc(len ? len : 1);
+ else
+ old = realloc(old, len ? len : 1);
+ if (old)
+ return old;
+ fprintf(stderr, "Out of memory reallocating %zu bytes!\n", len);
+ exit(1);
+}
+
+void *
+xrealloc2(void *old, size_t num, size_t len)
+{
+ if (len && (num * len) / len != num)
+ {
+ fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
+ exit(1);
+ }
+ return xrealloc(old, num * len);
+}
+
+void *
+xcalloc(size_t num, size_t len)
+{
+ void *r;
+ if (num == 0 || len == 0)
+ r = malloc(1);
+ else
+ r = calloc(num, len);
+ if (r)
+ return r;
+ fprintf(stderr, "Out of memory allocating %zu bytes!\n", num * len);
+ exit(1);
+}
+
+void *
+xfree(void *mem)
+{
+ if (mem)
+ free(mem);
+ return 0;
+}
+