replaced printf by a locking function
[platform/upstream/libsolv.git] / src / util.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "util.h"
14 #include "sat_debug.h"
15
16 void *
17 xmalloc(size_t len)
18 {
19   void *r = malloc(len ? len : 1);
20   if (r)
21     return r;
22   sat_debug (ERROR, "Out of memory allocating %zu bytes!\n", len);
23   exit(1);
24 }
25
26 void *
27 xmalloc2(size_t num, size_t len)
28 {
29   if (len && (num * len) / len != num)
30     {
31        sat_debug (ERROR, "Out of memory allocating %zu*%zu bytes!\n", num, len);
32       exit(1);
33     }
34   return xmalloc(num * len);
35 }
36
37 void *
38 xrealloc(void *old, size_t len)
39 {
40   if (old == 0)
41     old = malloc(len ? len : 1);
42   else
43     old = realloc(old, len ? len : 1);
44   if (old)
45     return old;
46   sat_debug (ERROR, "Out of memory reallocating %zu bytes!\n", len);
47   exit(1);
48 }
49
50 void *
51 xrealloc2(void *old, size_t num, size_t len)
52 {
53   if (len && (num * len) / len != num)
54     {
55        sat_debug (ERROR, "Out of memory allocating %zu*%zu bytes!\n", num, len);
56       exit(1);
57     }
58   return xrealloc(old, num * len);
59 }
60
61 void *
62 xcalloc(size_t num, size_t len)
63 {
64   void *r;
65   if (num == 0 || len == 0)
66     r = malloc(1);
67   else
68     r = calloc(num, len);
69   if (r)
70     return r;
71   sat_debug (ERROR, "Out of memory allocating %zu bytes!\n", num * len);
72   exit(1);
73 }
74
75 void *
76 xfree(void *mem)
77 {
78   if (mem)
79     free(mem);
80   return 0;
81 }
82