LAPIC memory mapping
[akaros.git] / inc / types.h
1 #ifndef JOS_INC_TYPES_H
2 #define JOS_INC_TYPES_H
3
4 #ifndef NULL
5 #define NULL ((void*) 0)
6 #endif
7
8 // Represents true-or-false values
9 typedef int bool;
10
11 // Explicitly-sized versions of integer types
12 typedef __signed char int8_t;
13 typedef unsigned char uint8_t;
14 typedef short int16_t;
15 typedef unsigned short uint16_t;
16 typedef int int32_t;
17 typedef unsigned int uint32_t;
18 typedef long long int64_t;
19 typedef unsigned long long uint64_t;
20
21 // Pointers and addresses are 32 bits long.
22 // We use pointer types to represent virtual addresses,
23 // uintptr_t to represent the numerical values of virtual addresses,
24 // and physaddr_t to represent physical addresses.
25 typedef int32_t intptr_t;
26 typedef uint32_t uintptr_t;
27 typedef uint32_t physaddr_t;
28
29 // Page numbers are 32 bits long.
30 typedef uint32_t ppn_t;
31
32 // size_t is used for memory object sizes.
33 typedef uint32_t size_t;
34 // ssize_t is a signed version of ssize_t, used in case there might be an
35 // error return.
36 typedef int32_t ssize_t;
37
38 // off_t is used for file offsets and lengths.
39 typedef int32_t off_t;
40
41 // Efficient min and max operations
42 #define MIN(_a, _b)                                             \
43 ({                                                              \
44         typeof(_a) __a = (_a);                                  \
45         typeof(_b) __b = (_b);                                  \
46         __a <= __b ? __a : __b;                                 \
47 })
48 #define MAX(_a, _b)                                             \
49 ({                                                              \
50         typeof(_a) __a = (_a);                                  \
51         typeof(_b) __b = (_b);                                  \
52         __a >= __b ? __a : __b;                                 \
53 })
54
55 // Rounding operations (efficient when n is a power of 2)
56 // Round down to the nearest multiple of n
57 #define ROUNDDOWN(a, n)                                         \
58 ({                                                              \
59         uint32_t __a = (uint32_t) (a);                          \
60         (typeof(a)) (__a - __a % (n));                          \
61 })
62 // Round up to the nearest multiple of n
63 #define ROUNDUP(a, n)                                           \
64 ({                                                              \
65         uint32_t __n = (uint32_t) (n);                          \
66         (typeof(a)) (ROUNDDOWN((uint32_t) (a) + __n - 1, __n)); \
67 })
68
69 // Return the offset of 'member' relative to the beginning of a struct type
70 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
71
72 #endif /* !JOS_INC_TYPES_H */