Debian Codegraph @

common/dynamips_common.h

raw
1 /** @file
2 * @brief Common includes, types, defines and platform specific stuff.
3 *
4 * This header should be included before other headers.
5 * This header should not contain code.
6 */
7
8 /*
9 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
10 * Copyright (c) 2014 Flávio J. Saraiva <[email protected]>
11 */
12
13 #pragma once
14 #ifndef __DYNAMIPS_COMMON_H__
15 #define __DYNAMIPS_COMMON_H__
16
17 /* Config file - not used at the moment */
18 #if HAVE_DYNAMIPS_CONFIG_H
19 #include "dynamips_config.h"
20 #endif
21
22 /* By default, Cygwin supports only 64 FDs with select()! */
23 #if defined(__CYGWIN__) && !defined(FD_SETSIZE)
24 #define FD_SETSIZE 1024
25 #endif
26
27 #define _GNU_SOURCE
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <errno.h>
37
38 #include <pthread.h>
39
40 /* True/False definitions */
41 #ifndef FALSE
42 #define FALSE 0
43 #endif
44
45 #ifndef TRUE
46 #define TRUE 1
47 #endif
48
49 /* Endianness */
50 #define ARCH_BIG_ENDIAN 0x4321
51 #define ARCH_LITTLE_ENDIAN 0x1234
52
53 #if defined(PPC) || defined(__powerpc__) || defined(__ppc__)
54 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
55 #elif defined(__sparc) || defined(__sparc__)
56 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
57 #elif defined(__alpha) || defined(__alpha__)
58 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
59 #elif defined(__i386) || defined(__i386__) || defined(i386)
60 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
61 #define ARCH_REGPARM_SUPPORTED 1
62 #elif defined(__x86_64__)
63 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
64 #elif defined(__ia64__)
65 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
66 #elif defined(__arm__) || defined (__aarch64__)
67 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
68 #elif defined(__ppc64__)
69 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
70 #elif defined(__s390x__)
71 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
72 #elif defined(__riscv) || defined(__riscv__)
73 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
74 #endif
75
76 #ifndef ARCH_BYTE_ORDER
77 #error Please define your architecture!
78 #endif
79
80 /* Useful attributes for functions */
81 #ifdef ARCH_REGPARM_SUPPORTED
82 #define asmlinkage __attribute__((regparm(0)))
83 #define fastcall __attribute__((regparm(3)))
84 #else
85 #define asmlinkage
86 #define fastcall
87 #endif
88
89 #ifndef _Unused
90 /* Function that is never used */
91 #define _Unused __attribute__((unused))
92 #endif
93
94 #ifndef _maybe_used
95 /* Function that is referenced from excluded code (commented out or depends on preprocessor) */
96 #define _maybe_used __attribute__((unused))
97 #endif
98
99 #ifndef UNUSED
100 /* Variable that is never used (name is changed to get an error on use) */
101 #define UNUSED(x) UNUSED_ ## x __attribute__((unused))
102 #endif
103
104 #if __GNUC__ > 2
105 #define forced_inline inline __attribute__((always_inline))
106 #define no_inline __attribute__ ((noinline))
107 #else
108 #define forced_inline inline
109 #define no_inline
110 #endif
111
112 #if __GNUC__ > 2
113 /* http://kerneltrap.org/node/4705 */
114 #define likely(x) __builtin_expect(!!(x),1)
115 #define unlikely(x) __builtin_expect((x),0)
116 #else
117 #define likely(x) (x)
118 #define unlikely(x) (x)
119 #endif
120
121 #ifndef _not_aligned
122 #define _not_aligned __attribute__ ((aligned (1)))
123 #endif
124
125 /* Common types */
126 typedef unsigned char m_uint8_t;
127 typedef signed char m_int8_t;
128
129 typedef unsigned short m_uint16_t;
130 typedef signed short m_int16_t;
131
132 typedef unsigned int m_uint32_t;
133 typedef signed int m_int32_t;
134
135 typedef unsigned long long m_uint64_t;
136 typedef signed long long m_int64_t;
137
138 typedef unsigned long m_iptr_t;
139 typedef m_uint64_t m_tmcnt_t;
140
141 /* Max and min macro */
142 #define m_max(a,b) (((a) > (b)) ? (a) : (b))
143 #define m_min(a,b) (((a) < (b)) ? (a) : (b))
144
145 /* A simple macro for adjusting pointers */
146 #define PTR_ADJUST(type,ptr,size) (type)((char *)(ptr) + (size))
147
148 /* Size of a field in a structure */
149 #define SIZEOF(st,field) (sizeof(((st *)NULL)->field))
150
151 /* Compute offset of a field in a structure */
152 #define OFFSET(st,f) ((long)&((st *)(NULL))->f)
153
154 /* Stringify a constant */
155 #define XSTRINGIFY(val) #val
156 #define STRINGIFY(val) XSTRINGIFY(val)
157
158 #endif