From 131758eae0078c12a3492678d89393dead2a4df7 Mon Sep 17 00:00:00 2001 From: Sebastian Sumpf Date: Tue, 19 May 2015 17:45:14 +0200 Subject: [PATCH] dde_linux: lx utility library Issue #1565 --- repos/dde_linux/include/lx/extern_c_begin.h | 26 +++++ repos/dde_linux/include/lx/extern_c_end.h | 20 ++++ repos/dde_linux/include/lx/list.h | 102 ++++++++++++++++++++ repos/dde_linux/include/lx/lx.h | 83 ++++++++++++++++ repos/dde_linux/lib/mk/lx.mk | 5 + repos/dde_linux/src/lib/lx/lx.cc | 17 ++++ 6 files changed, 253 insertions(+) create mode 100644 repos/dde_linux/include/lx/extern_c_begin.h create mode 100644 repos/dde_linux/include/lx/extern_c_end.h create mode 100644 repos/dde_linux/include/lx/list.h create mode 100644 repos/dde_linux/include/lx/lx.h create mode 100644 repos/dde_linux/lib/mk/lx.mk create mode 100644 repos/dde_linux/src/lib/lx/lx.cc diff --git a/repos/dde_linux/include/lx/extern_c_begin.h b/repos/dde_linux/include/lx/extern_c_begin.h new file mode 100644 index 000000000..8fadabf14 --- /dev/null +++ b/repos/dde_linux/include/lx/extern_c_begin.h @@ -0,0 +1,26 @@ +/* + * \brief Include before including Linux headers in C++ + * \author Christian Helmuth + * \date 2014-08-21 + */ + +/* + * Copyright (C) 2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#define extern_c_begin + +extern "C" { + +/* some warnings should only be switched of for Linux headers */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpointer-arith" +#pragma GCC diagnostic ignored "-Wsign-compare" + +/* deal with C++ keywords used for identifiers etc. */ +#define private private_ +#define class class_ +#define new new_ diff --git a/repos/dde_linux/include/lx/extern_c_end.h b/repos/dde_linux/include/lx/extern_c_end.h new file mode 100644 index 000000000..ac6b23f8d --- /dev/null +++ b/repos/dde_linux/include/lx/extern_c_end.h @@ -0,0 +1,20 @@ +/* + * \brief Include after including Linux headers in C++ + * \author Christian Helmuth + * \date 2014-08-21 + */ + +/* + * Copyright (C) 2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#undef new +#undef class +#undef private + +#pragma GCC diagnostic pop + +} /* extern "C" */ diff --git a/repos/dde_linux/include/lx/list.h b/repos/dde_linux/include/lx/list.h new file mode 100644 index 000000000..c12fdaf1a --- /dev/null +++ b/repos/dde_linux/include/lx/list.h @@ -0,0 +1,102 @@ +/* + * \brief Slightly improved list + * \author Christian Helmuth + * \date 2014-09-25 + */ + +/* + * Copyright (C) 2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#ifndef _LIST_H_ +#define _LIST_H_ + +#include + + +namespace Lx { + template class List; + template class List_element; +} + +template +class Lx::List : private Genode::List +{ + private: + + typedef Genode::List Base; + + public: + + using Base::Element; + + void append(LT const *le) + { + LT *at = nullptr; + + for (LT *l = first(); l; l = l->next()) + at = l; + + Base::insert(le, at); + } + + void prepend(LT const *le) + { + Base::insert(le); + } + + void insert_before(LT const *le, LT const *at) + { + if (at == first()) { + prepend(le); + return; + } else if (!at) { + append(le); + return; + } + + for (LT *l = first(); l; l = l->next()) + if (l->next() == at) + at = l; + + Base::insert(le, at); + } + + + /**************************** + ** Genode::List interface ** + ****************************/ + + LT *first() { return Base::first(); } + LT const *first() const { return Base::first(); } + + void insert(LT const *le, LT const *at = 0) + { + Base::insert(le, at); + } + + void remove(LT const *le) + { + Base::remove(le); + } +}; + + +template +class Lx::List_element : public Lx::List >::Element +{ + private: + + T *_object; + + public: + + List_element(T *object) : _object(object) { } + + T *object() const { return _object; } +}; + +#endif /* _LIST_H_ */ diff --git a/repos/dde_linux/include/lx/lx.h b/repos/dde_linux/include/lx/lx.h new file mode 100644 index 000000000..74072fd0c --- /dev/null +++ b/repos/dde_linux/include/lx/lx.h @@ -0,0 +1,83 @@ +#ifndef _INCLUDE__LX__LX_H_ +#define _INCLUDE__LX__LX_H_ + +#include +#include + +typedef genode_int8_t int8_t; +typedef genode_int16_t int16_t; +typedef genode_int32_t int32_t; +typedef genode_uint32_t uint32_t; +typedef genode_int64_t int64_t; +typedef genode_uint8_t uint8_t; +typedef genode_uint16_t uint16_t; +typedef genode_uint64_t uint64_t; +typedef __SIZE_TYPE__ size_t; + +void lx_printf(char const *, ...) __attribute__((format(printf, 1, 2))); +void lx_vprintf(char const *, va_list); + +#define lx_log(doit, msg...) \ + do { \ + if (doit) { \ + lx_printf("%s(): ", __func__); \ + lx_printf(msg); \ + lx_printf("\n"); \ + } \ + } while(0) + + +/********************** + ** linux/compiler.h ** + **********************/ + +#define __printf(a, b) __attribute__((format(printf, a, b))) + + +/************************** + ** linux/compiler-gcc.h ** + **************************/ + +#define __noreturn __attribute__((noreturn)) + +/*************** + ** asm/bug.h ** + ***************/ + +#define WARN_ON(condition) ({ \ + int ret = !!(condition); \ + if (ret) lx_printf("[%s] WARN_ON(" #condition ") ", __func__); \ + ret; }) + +#define WARN(condition, fmt, arg...) ({ \ + int ret = !!(condition); \ + if (ret) lx_printf("[%s] *WARN* " fmt , __func__ , ##arg); \ + ret; }) + +#define BUG() do { \ + lx_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ + while (1); \ +} while (0) + +#define WARN_ON_ONCE WARN_ON +#define WARN_ONCE WARN + +#define BUG_ON(condition) do { if (condition) BUG(); } while(0) + + +/******************** + ** linux/kernel.h ** + ********************/ + +static inline __printf(1, 2) void panic(const char *fmt, ...) __noreturn; +static inline void panic(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + lx_vprintf(fmt, args); + va_end(args); + lx_printf("panic()"); + while (1) ; +} + +#endif /* _INCLUDE__LX__LX_H_ */ diff --git a/repos/dde_linux/lib/mk/lx.mk b/repos/dde_linux/lib/mk/lx.mk new file mode 100644 index 000000000..c87e369da --- /dev/null +++ b/repos/dde_linux/lib/mk/lx.mk @@ -0,0 +1,5 @@ +SRC_CC = lx.cc + +LIB_DIR = $(REP_DIR)/src/lib/lx + +vpath %.cc $(LIB_DIR) diff --git a/repos/dde_linux/src/lib/lx/lx.cc b/repos/dde_linux/src/lib/lx/lx.cc new file mode 100644 index 000000000..da661e80e --- /dev/null +++ b/repos/dde_linux/src/lib/lx/lx.cc @@ -0,0 +1,17 @@ +#include +#include +#include + +#include + +void lx_printf(char const *fmt, ...) +{ + va_list va; + va_start(va, fmt); + Genode::vprintf(fmt, va); + va_end(va); +} + + +void lx_vprintf(char const *fmt, va_list va) { + Genode::vprintf(fmt, va); }