ipv6: drop unused "dev" arg of icmpv6_send()
[linux-2.6/next.git] / tools / perf / util / wrapper.c
blobbf44ca85d23be29334d69ef6718d9b613f06a9bb
1 /*
2 * Various trivial helper wrappers around standard functions
3 */
4 #include "cache.h"
6 /*
7 * There's no pack memory to release - but stay close to the Git
8 * version so wrap this away:
9 */
10 static inline void release_pack_memory(size_t size __used, int flag __used)
14 char *xstrdup(const char *str)
16 char *ret = strdup(str);
17 if (!ret) {
18 release_pack_memory(strlen(str) + 1, -1);
19 ret = strdup(str);
20 if (!ret)
21 die("Out of memory, strdup failed");
23 return ret;
26 void *xmalloc(size_t size)
28 void *ret = malloc(size);
29 if (!ret && !size)
30 ret = malloc(1);
31 if (!ret) {
32 release_pack_memory(size, -1);
33 ret = malloc(size);
34 if (!ret && !size)
35 ret = malloc(1);
36 if (!ret)
37 die("Out of memory, malloc failed");
39 #ifdef XMALLOC_POISON
40 memset(ret, 0xA5, size);
41 #endif
42 return ret;
46 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
47 * "data" to the allocated memory, zero terminates the allocated memory,
48 * and returns a pointer to the allocated memory. If the allocation fails,
49 * the program dies.
51 void *xmemdupz(const void *data, size_t len)
53 char *p = xmalloc(len + 1);
54 memcpy(p, data, len);
55 p[len] = '\0';
56 return p;
59 char *xstrndup(const char *str, size_t len)
61 char *p = memchr(str, '\0', len);
63 return xmemdupz(str, p ? (size_t)(p - str) : len);
66 void *xrealloc(void *ptr, size_t size)
68 void *ret = realloc(ptr, size);
69 if (!ret && !size)
70 ret = realloc(ptr, 1);
71 if (!ret) {
72 release_pack_memory(size, -1);
73 ret = realloc(ptr, size);
74 if (!ret && !size)
75 ret = realloc(ptr, 1);
76 if (!ret)
77 die("Out of memory, realloc failed");
79 return ret;
83 * xread() is the same a read(), but it automatically restarts read()
84 * operations with a recoverable error (EAGAIN and EINTR). xread()
85 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
87 static ssize_t xread(int fd, void *buf, size_t len)
89 ssize_t nr;
90 while (1) {
91 nr = read(fd, buf, len);
92 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
93 continue;
94 return nr;
99 * xwrite() is the same a write(), but it automatically restarts write()
100 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
101 * GUARANTEE that "len" bytes is written even if the operation is successful.
103 static ssize_t xwrite(int fd, const void *buf, size_t len)
105 ssize_t nr;
106 while (1) {
107 nr = write(fd, buf, len);
108 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
109 continue;
110 return nr;
114 ssize_t read_in_full(int fd, void *buf, size_t count)
116 char *p = buf;
117 ssize_t total = 0;
119 while (count > 0) {
120 ssize_t loaded = xread(fd, p, count);
121 if (loaded <= 0)
122 return total ? total : loaded;
123 count -= loaded;
124 p += loaded;
125 total += loaded;
128 return total;
131 ssize_t write_in_full(int fd, const void *buf, size_t count)
133 const char *p = buf;
134 ssize_t total = 0;
136 while (count > 0) {
137 ssize_t written = xwrite(fd, p, count);
138 if (written < 0)
139 return -1;
140 if (!written) {
141 errno = ENOSPC;
142 return -1;
144 count -= written;
145 p += written;
146 total += written;
149 return total;