aboutsummaryrefslogtreecommitdiff
path: root/include/std/misc.h
blob: 03d7972c20cde0f2a34a391b797a225648a51481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef AMALGAM_MISC_H
#define AMALGAM_MISC_H

#include "types.h"
#include <sys/types.h>

#if defined(__BYTE_ORDER)
    #if __BYTE_ORDER == __LITTLE_ENDIAN
        #define AMAL_LITTLE_ENDIAN
    #elif __BYTE_ORDER == __BIG_ENDIAN
        #define AMAL_BIG_ENDIAN
    #endif
#elif defined(__BYTE_ORDER__)
    #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
        #define AMAL_LITTLE_ENDIAN
    #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
        #define AMAL_BIG_ENDIAN
    #endif
#endif

#if !defined(AMAL_LITTLE_ENDIAN) && !defined(AMAL_BIG_ENDIAN)
#error Unsupported endian, neither little or big endian
#endif

u16 byteswap16(u16 value);
u32 byteswap32(u32 value);
u64 byteswap64(u64 value);

#ifndef AMAL_PEDANTIC
#include "log.h"
#endif

#ifdef AMAL_PEDANTIC
    #define throw_debug_msg do {} while(0)
#else
    #define throw_debug_msg do { amal_log_error("Throwing from %s:%d", __FILE__, __LINE__); } while(0)
#endif

#ifdef AMAL_PEDANTIC
    #define return_if_debug_msg do {} while(0)
    #define cleanup_if_debug_msg do {} while(0)
#else
    #define return_if_debug_msg do { amal_log_error("Return from %s:%d", __FILE__, __LINE__); } while(0)
    #define cleanup_if_debug_msg do { amal_log_error("cleanup from %s:%d", __FILE__, __LINE__); } while(0)
#endif

#define return_if_error(result) \
    do { \
        int return_if_result = (result); \
        if(return_if_result != 0) { \
            return_if_debug_msg; \
            return return_if_result; \
        } \
    } while(0)

#define cleanup_if_error(result) do { if((result) != 0) { cleanup_if_debug_msg; goto cleanup; } } while(0)

typedef struct {
    int result;
    void *data;
} ResultMem;

#if defined(__GNUC__) && __GNUC__ >= 4
#define CHECK_RESULT __attribute__ ((warn_unused_result))
#elif defined(_MSC_VER) && _MSC_VER >= 1700
#define CHECK_RESULT _Check_return_
#else
#define CHECK_RESULT
#endif

#define ignore_result_int(expr) (void)((expr)+1)

#endif