/* Copyright (C) 2008 Emmanuel Varoquaux This file is part of XOS. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef _UTIL_H #define _UTIL_H #include <stddef.h> #define downcast(type, member, ptr) ({ \ typeof (ptr) _ptr = (ptr); \ ((type *)(_ptr ? (void *)_ptr - offsetof(type, member) : 0)); \ }) #define min(a, b) ({ \ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a < _b ? _a : _b; \ }) #define max(a, b) ({ \ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; \ }) #define set_if_lower(var, val) ({ \ typeof (var) *_pvar = &(var); \ typeof (val) _val = (val); \ if (*_pvar < _val) \ *_pvar = _val; \ }) #define set_if_upper(var, val) ({ \ typeof (var) *_pvar = &(var); \ typeof (val) _val = (val); \ if (*_pvar > _val) \ *_pvar = _val; \ }) #endif