MonkOS  v0.1
A simple 64-bit operating system (x86_64)
core.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file core.h
3 /// @brief Core include file.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license that can
7 // be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 // Standard headers included by all code in the project.
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <stdatomic.h>
17 
18 //----------------------------------------------------------------------------
19 // Macros
20 //----------------------------------------------------------------------------
21 
22 /// Force a function to be inline, even in debug mode.
23 #define __forceinline inline __attribute__((always_inline))
24 
25 /// Return the number of elements in the C array.
26 #define arrsize(x) ((int)(sizeof(x) / sizeof(x[0])))
27 
28 /// Generic min/max routines
29 #define min(a, b) ((a) < (b) ? (a) : (b))
30 #define max(a, b) ((a) > (b) ? (a) : (b))
31 
32 // Division macros, rounding (a/b) up or down
33 #define div_dn(a, b) ((a) / (b))
34 #define div_up(a, b) (((a) + (b) - 1) / (b))
35 
36 // Alignment macros, align a to the nearest multiple of b.
37 #define align_dn(a, b) (div_dn(a, b) * (b))
38 #define align_up(a, b) (div_up(a, b) * (b))
39 
40 // Typed pointer arithmetic. Pointer p +/- offset o, cast to t*.
41 #define ptr_add(t, p, o) ((t *)((uint8_t *)(p) + (o)))
42 #define ptr_sub(t, p, o) ((t *)((uint8_t *)(p) - (o)))
43 
44 /// Compile-time static assertion
45 #define STATIC_ASSERT(a, b) _Static_assert(a, b)
46 
47 /// Forced structure packing (use only when absolutely necessary)
48 #define PACKSTRUCT __attribute__((packed, aligned(1)))