ia64/xen-unstable

view tools/xenstore/utils.h @ 8740:3d7ea7972b39

Update patches for linux 2.6.15.

Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Thu Feb 02 17:16:00 2006 +0000 (2006-02-02)
parents 015f8ae81276
children eabac09d9e4e
line source
1 #ifndef _UTILS_H
2 #define _UTILS_H
3 #include <stdbool.h>
4 #include <string.h>
5 #include <stdint.h>
7 /* Is A == B ? */
8 #define streq(a,b) (strcmp((a),(b)) == 0)
10 /* Does A start with B ? */
11 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
13 /* Does A end in B ? */
14 static inline bool strends(const char *a, const char *b)
15 {
16 if (strlen(a) < strlen(b))
17 return false;
19 return streq(a + strlen(a) - strlen(b), b);
20 }
22 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
24 #define ___stringify(x) #x
25 #define __stringify(x) ___stringify(x)
27 /* Convenient wrappers for malloc and realloc. Use them. */
28 #define new(type) ((type *)malloc_nofail(sizeof(type)))
29 #define new_array(type, num) realloc_array((type *)0, (num))
30 #define realloc_array(ptr, num) ((__typeof__(ptr))_realloc_array((ptr), sizeof((*ptr)), (num)))
32 void *malloc_nofail(size_t size);
33 void *realloc_nofail(void *ptr, size_t size);
34 void *_realloc_array(void *ptr, size_t size, size_t num);
36 void barf(const char *fmt, ...) __attribute__((noreturn));
37 void barf_perror(const char *fmt, ...) __attribute__((noreturn));
39 /* This version adds one byte (for nul term) */
40 void *grab_file(const char *filename, unsigned long *size);
41 void release_file(void *data, unsigned long size);
43 /* Signal handling: returns fd to listen on. */
44 int signal_to_fd(int signal);
45 void close_signal(int fd);
47 void xprintf(const char *fmt, ...);
49 #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
50 #define iprintf(_fmt, _args...) xprintf("[INF] %s" _fmt, __FUNCTION__, ##_args)
52 #ifdef DEBUG
53 #define dprintf(_fmt, _args...) xprintf("[DBG] %s" _fmt, __FUNCTION__, ##_args)
54 #else
55 #define dprintf(_fmt, _args...) ((void)0)
56 #endif
58 /*
59 * Mux errno values onto returned pointers.
60 */
62 static inline void *ERR_PTR(long error)
63 {
64 return (void *)error;
65 }
67 static inline long PTR_ERR(const void *ptr)
68 {
69 return (long)ptr;
70 }
72 static inline long IS_ERR(const void *ptr)
73 {
74 return ((unsigned long)ptr > (unsigned long)-1000L);
75 }
78 #endif /* _UTILS_H */
80 /*
81 * Local variables:
82 * c-file-style: "linux"
83 * indent-tabs-mode: t
84 * c-indent-level: 8
85 * c-basic-offset: 8
86 * tab-width: 8
87 * End:
88 */