Bump version to 0.3.5next
[archipelago] / xseg / sys / util.h
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 #ifndef _XSEG_SYS_UTIL_H
36 #define _XSEG_SYS_UTIL_H
37
38 #include <_sysutil.h>
39 #include <sys/domain.h>
40
41 /* performance stuff */
42
43 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 303)
44
45 #ifndef LIKELY
46 #define LIKELY(x)               __builtin_expect(!!(x),1)
47 #endif
48 #ifndef UNLIKELY
49 #define UNLIKELY(x)     __builtin_expect(!!(x),0)
50 #endif
51
52 #else
53 #define LIKELY(x)       (x)
54 #define UNLIKELY(x)     (x)
55 #endif
56 /* log stuff */
57
58
59
60 #define FMTARG(fmt, arg, format, ...) fmt format "%s", arg, ## __VA_ARGS__
61 #define XSEGLOG(...) (xseg_snprintf(__xseg_errbuf, 4096, FMTARG("%s: ", __func__, ## __VA_ARGS__, "")), \
62                     __xseg_errbuf[4095] = 0, __xseg_log(__xseg_errbuf))
63
64 #define XSEGLOG2(__ctx, __level, ...)           \
65                 do {                            \
66                         if (__level <= ((__ctx)->log_level)) { \
67                                 __xseg_log2(__ctx, __level, FMTARG("%s: ", __func__, ## __VA_ARGS__ ,"")); \
68                         }       \
69                 } while(0)
70
71 /*
72 void log_request(struct log_context *lc, struct xseg *xseg,  struct xseg_request *req)
73 {
74         __xseg_log2(lc, I, "\n\t"
75         "Request %lx: target[%u](xptr: %llu): %s, data[%llu](xptr: %llu): %s \n\t"
76         "offset: %llu, size: %llu, serviced; %llu, op: %u, state: %u, flags: %u \n\t"
77         "src: %u, transit: %u, dst: %u, effective dst: %u",
78         (unsigned long) req, req->targetlen, (unsigned long long)req->target,
79         xseg_get_target(xseg, req),
80         (unsigned long long) req->datalen, (unsigned long long) req->data,
81         xseg_get_data(xseg, req),
82         (unsigned long long) req->offset, (unsigned long long) req->size,
83         (unsigned long long) req->serviced, req->op, req->state, req->flags,
84         (unsigned int) req->src_portno, (unsigned int) req->transit_portno,
85         (unsigned int) req->dst_portno, (unsigned int) req->effective_dst_portno);
86 }
87 */
88
89 /* general purpose xflags */
90 #define X_ALLOC    ((uint32_t) (1 << 0))
91 #define X_LOCAL    ((uint32_t) (1 << 1))
92 #define X_NONBLOCK ((uint32_t) (1 << 2))
93
94
95 typedef uint64_t xpointer;
96
97 /* type to be used as absolute pointer
98  * this should be the same as xqindex
99  * and must fit into a pointer type
100  */
101 typedef uint64_t xptr; 
102
103 #define Noneidx ((xqindex)-1)
104 #define Null ((xpointer)-1)
105
106 #define __align(x, shift) (((((x) -1) >> (shift)) +1) << (shift))
107
108 #define XPTR_TYPE(ptrtype)      \
109         union {                 \
110                 ptrtype *t;     \
111                 xpointer x;     \
112         }
113
114 #define XPTRI(xptraddr)         \
115         (       (xpointer)(unsigned long)(xptraddr) +   \
116                 (xptraddr)->x                           )
117
118 #define XPTRISET(xptraddr, ptrval)      \
119         ((xptraddr)->x  =       (xpointer)(ptrval) -                    \
120                                 (xpointer)(unsigned long)(xptraddr)     )
121
122 #define XPTR(xptraddr)          \
123         (       (typeof((xptraddr)->t))                         \
124                 (unsigned long)                                 \
125                 (       (xpointer)(unsigned long)(xptraddr) +   \
126                         (xptraddr)->x           )               )
127
128 #define XPTRSET(xptraddr, ptrval)       \
129         ((xptraddr)->x  =       (xpointer)(unsigned long)(ptrval) -     \
130                                 (xpointer)(unsigned long)(xptraddr)     )
131
132
133
134 #define XPTR_OFFSET(base, ptr) ((unsigned long)(ptr) - (unsigned long)(base))
135
136 #define XPTR_MAKE(ptrval, base) ((xptr) XPTR_OFFSET(base, ptrval))
137
138 #define XPTR_TAKE(xptrval, base)        \
139         ( (void *) ( (unsigned long) base + (unsigned long) xptrval))
140
141 #endif