134 lines
3.6 KiB
C
134 lines
3.6 KiB
C
#ifndef _UTILS_H_
|
|
#define _UTILS_H_
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
#define PAGE_SHIFT 12
|
|
#define PAGE_SIZE (1 << PAGE_SHIFT)
|
|
#define PAGE_MASK (~(PAGE_SIZE-1))
|
|
|
|
|
|
#define ___PASTE(a,b) a##b
|
|
|
|
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
|
|
|
|
#define __min(t1, t2, min1, min2, x, y) ({ \
|
|
t1 min1 = (x); \
|
|
t2 min2 = (y); \
|
|
(void) (&min1 == &min2); \
|
|
min1 < min2 ? min1 : min2; })
|
|
|
|
#define min(x, y) \
|
|
__min(typeof(x), typeof(y), \
|
|
__UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
|
|
x, y)
|
|
|
|
#define __max(t1, t2, max1, max2, x, y) ({ \
|
|
t1 max1 = (x); \
|
|
t2 max2 = (y); \
|
|
(void) (&max1 == &max2); \
|
|
max1 > max2 ? max1 : max2; })
|
|
|
|
#define max(x, y) \
|
|
__max(typeof(x), typeof(y), \
|
|
__UNIQUE_ID(max1_), __UNIQUE_ID(max2_), \
|
|
x, y)
|
|
|
|
#define min3(x, y, z) min((typeof(x))min(x, y), z)
|
|
#define max3(x, y, z) max((typeof(x))max(x, y), z)
|
|
|
|
/**
|
|
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
|
|
* @x: value1
|
|
* @y: value2
|
|
*/
|
|
#define min_not_zero(x, y) ({ \
|
|
typeof(x) __x = (x); \
|
|
typeof(y) __y = (y); \
|
|
__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
|
|
|
|
/**
|
|
* clamp - return a value clamped to a given range with strict typechecking
|
|
* @val: current value
|
|
* @lo: lowest allowable value
|
|
* @hi: highest allowable value
|
|
*
|
|
* This macro does strict typechecking of lo/hi to make sure they are of the
|
|
* same type as val. See the unnecessary pointer comparisons.
|
|
*/
|
|
#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
|
|
|
|
/*
|
|
* ..and if you can't take the strict
|
|
* types, you can specify one yourself.
|
|
*
|
|
* Or not use min/max/clamp at all, of course.
|
|
*/
|
|
#define min_t(type, x, y) \
|
|
__min(type, type, \
|
|
__UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
|
|
x, y)
|
|
|
|
#define max_t(type, x, y) \
|
|
__max(type, type, \
|
|
__UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
|
|
x, y)
|
|
|
|
/**
|
|
* clamp_t - return a value clamped to a given range using a given type
|
|
* @type: the type of variable to use
|
|
* @val: current value
|
|
* @lo: minimum allowable value
|
|
* @hi: maximum allowable value
|
|
*
|
|
* This macro does no typechecking and uses temporary variables of type
|
|
* 'type' to make all the comparisons.
|
|
*/
|
|
#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
|
|
|
|
/**
|
|
* clamp_val - return a value clamped to a given range using val's type
|
|
* @val: current value
|
|
* @lo: minimum allowable value
|
|
* @hi: maximum allowable value
|
|
*
|
|
* This macro does no typechecking and uses temporary variables of whatever
|
|
* type the input argument 'val' is. This is useful when val is an unsigned
|
|
* type and min and max are literals that will otherwise be assigned a signed
|
|
* integer type.
|
|
*/
|
|
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
|
|
|
|
|
|
/*
|
|
* swap - swap value of @a and @b
|
|
*/
|
|
#define swap(a, b) \
|
|
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
|
|
|
|
/**
|
|
* container_of - cast a member of a structure out to the containing structure
|
|
* @ptr: the pointer to the member.
|
|
* @type: the type of the container struct this is embedded in.
|
|
* @member: the name of the member within the struct.
|
|
*
|
|
*/
|
|
//#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
|
|
|
|
|
|
|
|
#define container_of(ptr, type, member) ({ \
|
|
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
|
(type *)( (char *)__mptr - offsetof(type,member) );})
|
|
|
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
|
|
|
//typedef long __kernel_long_t;
|
|
//typedef unsigned long __kernel_ulong_t;
|
|
#define BITS_PER_LONG 32
|
|
#define UINT_MAX (~0U)
|
|
|