X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=include%2Fust%2Fcore.h;h=c6057ea7b0a93e13de955d1c903a4f4cd2f8b3a4;hb=518d7abb8e3720433c611499f704c3bd9d554102;hp=27c7e6d4802717215792fe68cd4c3234ebaa0ad0;hpb=051d2184d15f5c4a69a4d2e5b5306e69ab03889b;p=ust.git diff --git a/include/ust/core.h b/include/ust/core.h index 27c7e6d..c6057ea 100644 --- a/include/ust/core.h +++ b/include/ust/core.h @@ -1,6 +1,28 @@ +/* Copyright (C) 2010 Pierre-Marc Fournier + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef UST_CORE_H #define UST_CORE_H +#include + +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + #if defined(CONFIG_LTT) && defined(CONFIG_LTT_ALIGNMENT) /* @@ -36,4 +58,101 @@ static inline int ltt_get_alignment(void) } #endif /* defined(CONFIG_LTT) && defined(CONFIG_LTT_ALIGNMENT) */ + +/* ARRAYS */ + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + + +/* ALIGNMENT SHORTCUTS */ + +#include + +#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) +#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) +#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) +#define PAGE_SIZE sysconf(_SC_PAGE_SIZE) +#define PAGE_MASK (~(PAGE_SIZE-1)) + +/* ERROR OPS */ +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) + +static inline void *ERR_PTR(long error) +{ + return (void *) error; +} + +static inline long PTR_ERR(const void *ptr) +{ + return (long) ptr; +} + +static inline long IS_ERR(const void *ptr) +{ + return IS_ERR_VALUE((unsigned long)ptr); +} + + +/* Min / Max */ + +#define min_t(type, x, y) ({ \ + type __min1 = (x); \ + type __min2 = (y); \ + __min1 < __min2 ? __min1: __min2; }) + +#define max_t(type, x, y) ({ \ + type __max1 = (x); \ + type __max2 = (y); \ + __max1 > __max2 ? __max1: __max2; }) + + +/* MUTEXES */ + +#include + +#define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; +#define DECLARE_MUTEX(m) extern pthread_mutex_t (m); + +#define mutex_lock(m) pthread_mutex_lock(m) + +#define mutex_unlock(m) pthread_mutex_unlock(m) + + +/* MALLOCATION */ + +#define zmalloc(s) calloc(1, s) + +/* ATTRIBUTES */ + +/* FIXME: define this */ +#define ____cacheline_aligned + +/* MATH */ + +#include +static inline unsigned int hweight32(unsigned int w) +{ + unsigned int res = w - ((w >> 1) & 0x55555555); + res = (res & 0x33333333) + ((res >> 2) & 0x33333333); + res = (res + (res >> 4)) & 0x0F0F0F0F; + res = res + (res >> 8); + return (res + (res >> 16)) & 0x000000FF; +} + +static __inline__ int get_count_order(unsigned int count) +{ + int order; + + order = fls(count) - 1; + if (count & (count - 1)) + order++; + return order; +} + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + #endif /* UST_CORE_H */