source: trunk/packages/xen-3.1/xen-3.1/tools/xenstore/utils.h @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 2.1 KB
Line 
1#ifndef _UTILS_H
2#define _UTILS_H
3#include <stdbool.h>
4#include <string.h>
5#include <stdint.h>
6
7/* Is A == B ? */
8#define streq(a,b) (strcmp((a),(b)) == 0)
9
10/* Does A start with B ? */
11#define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
12
13/* Does A end in B ? */
14static inline bool strends(const char *a, const char *b)
15{
16        if (strlen(a) < strlen(b))
17                return false;
18
19        return streq(a + strlen(a) - strlen(b), b);
20}
21
22#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
23
24#define ___stringify(x) #x
25#define __stringify(x)          ___stringify(x)
26
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)))
31
32void *malloc_nofail(size_t size);
33void *realloc_nofail(void *ptr, size_t size);
34void *_realloc_array(void *ptr, size_t size, size_t num);
35
36void barf(const char *fmt, ...) __attribute__((noreturn));
37void barf_perror(const char *fmt, ...) __attribute__((noreturn));
38
39/* This version adds one byte (for nul term) */
40void *grab_file(const char *filename, unsigned long *size);
41void release_file(void *data, unsigned long size);
42
43/* Signal handling: returns fd to listen on. */
44int signal_to_fd(int signal);
45void close_signal(int fd);
46
47void xprintf(const char *fmt, ...);
48
49#define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
50#define iprintf(_fmt, _args...) xprintf("[INF] %s" _fmt, __FUNCTION__, ##_args)
51
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
57
58/*
59 * Mux errno values onto returned pointers.
60 */
61
62static inline void *ERR_PTR(long error)
63{
64        return (void *)error;
65}
66
67static inline long PTR_ERR(const void *ptr)
68{
69        return (long)ptr;
70}
71
72static inline long IS_ERR(const void *ptr)
73{
74        return ((unsigned long)ptr > (unsigned long)-1000L);
75}
76
77
78#endif /* _UTILS_H */
79
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 */
Note: See TracBrowser for help on using the repository browser.