coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
helpers.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef COMMONLIB_HELPERS_H
4 #define COMMONLIB_HELPERS_H
5 
6 /* This file is for helpers for both coreboot firmware and its utilities. Most
7  of this has moved into <commonlib/bsd/helpers.h> now, this wrapper is just
8  for the stuff that nobody bothered to confirm BSD-licensability of yet. */
9 
10 #include <commonlib/bsd/helpers.h>
11 
12 /*
13  * Divide positive or negative dividend by positive divisor and round
14  * to closest integer. Result is undefined for negative divisors and
15  * for negative dividends if the divisor variable type is unsigned.
16  */
17 #define DIV_ROUND_CLOSEST(x, divisor)({ \
18  __typeof__(x) _div_local_x = (x); \
19  __typeof__(divisor) _div_local_d = (divisor); \
20  (((__typeof__(x))-1) > 0 || \
21  ((__typeof__(divisor))-1) > 0 || (_div_local_x) > 0) ? \
22  ((_div_local_x + (_div_local_d / 2)) / _div_local_d) : \
23  ((_div_local_x - (_div_local_d / 2)) / _div_local_d); \
24 })
25 
26 /**
27  * container_of - cast a member of a structure out to the containing structure
28  * @param ptr: the pointer to the member.
29  * @param type: the type of the container struct this is embedded in.
30  * @param member: the name of the member within the struct.
31  *
32  */
33 #define container_of(ptr, type, member) ({ \
34  const __typeof__(((type *)0)->member) *__mptr = (ptr); \
35  (type *)((char *)__mptr - offsetof(type, member)); })
36 
37 #ifndef __unused
38 #define __unused __attribute__((unused))
39 #endif
40 
41 #ifndef alloca
42 #define alloca(x) __builtin_alloca(x)
43 #endif
44 
45 #endif /* COMMONLIB_HELPERS_H */