This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon_i.h
95 lines (82 loc) · 3.17 KB
/
common_i.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
USBI3C - Library to talk to I3C devices via USB.
-------------------
copyright : (C) 2022 Intel Corporation
SPDX-License-Identifier: LGPL-2.1-only
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License *
* version 2.1 as published by the Free Software Foundation. *
* *
***************************************************************************/
#ifndef __COMMON_I_H__
#define __COMMON_I_H__
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#ifdef DEBUG
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s(): " fmt, __func__, ##__VA_ARGS__) // ignore_linebreak
#else
#define DEBUG_PRINT(...) (void)0 // ignore_linebreak
#endif
/**
* @brief Macro that takes three arguments - a pointer, type of the container, and the
* name of the member the pointer refers to. The macro will then expand to a new address
* pointing to the container which accommodates the respective member.
*/
#define container_of(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
/**
* @brief Macro to abort program execution if the value of the variable is NULL.
* This macro is useful to be used in situations where unrecoverable errors have
* occurred, for example running out of memory.
*/
#define ON_NULL_ABORT(var) \
if (!var) { \
abort(); \
}
/**
* @brief wrapper for the free function that always set the pointer to NULL after freeing.
* This macro should be used instead of free to prevent invalid memory access.
*/
#define FREE(ptr) \
do { \
free(ptr); \
ptr = NULL; \
} while (0)
/**
* @brief Allocates memory of the specified size and initializes it.
*
* This is a wrapper function to be used instead of calling malloc directly.
* It uses calloc to make sure the memory gets initialized with zero,this
* way we avoid getting segmentation faults.
* Abort on out of memory errors, continuing the program at this point
* would cause unexpected behaviors.
*/
static inline void *malloc_or_die(size_t size)
{
void *ptr;
ptr = calloc(1, (size_t)size); // ignore_function
ON_NULL_ABORT(ptr);
return ptr;
}
/**
* @brief Reallocates memory of the specified size and initializes it.
*
* This is a wrapper function to be used instead of calling realloc directly.
* It uses realloc to reallocate memory, this way we avoid having errors if
* the memory is not allocated.
* Abort on out of memory errors, continuing the program at this point
* would cause unexpected behaviors.
*/
static inline void *realloc_or_die(void *ptr, size_t size)
{
void *new_ptr;
new_ptr = realloc(ptr, (size_t)size); // ignore_function
ON_NULL_ABORT(new_ptr);
return new_ptr;
}
#endif /* end of include guard: __COMMON_I_H__ */