current state of 'sat-solver'
[platform/upstream/libsolv.git] / src / util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "util.h"
7
8 void *
9 xmalloc(size_t len)
10 {
11   void *r = malloc(len ? len : 1);
12   if (r)
13     return r;
14   fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
15   exit(1);
16 }
17
18 void *
19 xmalloc2(size_t num, size_t len)
20 {
21   if (len && (num * len) / len != num)
22     {
23       fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
24       exit(1);
25     }
26   return xmalloc(num * len);
27 }
28
29 void *
30 xrealloc(void *old, size_t len)
31 {
32   if (old == 0)
33     old = malloc(len ? len : 1);
34   else
35     old = realloc(old, len ? len : 1);
36   if (old)
37     return old;
38   fprintf(stderr, "Out of memory reallocating %zu bytes!\n", len);
39   exit(1);
40 }
41
42 void *
43 xrealloc2(void *old, size_t num, size_t len)
44 {
45   if (len && (num * len) / len != num)
46     {
47       fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
48       exit(1);
49     }
50   return xrealloc(old, num * len);
51 }
52
53 void *
54 xcalloc(size_t num, size_t len)
55 {
56   void *r;
57   if (num == 0 || len == 0)
58     r = malloc(1);
59   else
60     r = calloc(num, len);
61   if (r)
62     return r;
63   fprintf(stderr, "Out of memory allocating %zu bytes!\n", num * len);
64   exit(1);
65 }
66
67 void *
68 xfree(void *mem)
69 {
70   if (mem)
71     free(mem);
72   return 0;
73 }
74