dde_linux: lx utility library

Issue #1565
This commit is contained in:
Sebastian Sumpf 2015-05-19 17:45:14 +02:00 committed by Christian Helmuth
parent 1c38d4903b
commit 131758eae0
6 changed files with 253 additions and 0 deletions

View File

@ -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_

View File

@ -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" */

View File

@ -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 <util/list.h>
namespace Lx {
template <typename> class List;
template <typename> class List_element;
}
template <typename LT>
class Lx::List : private Genode::List<LT>
{
private:
typedef Genode::List<LT> 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 <typename T>
class Lx::List_element : public Lx::List<List_element<T> >::Element
{
private:
T *_object;
public:
List_element(T *object) : _object(object) { }
T *object() const { return _object; }
};
#endif /* _LIST_H_ */

View File

@ -0,0 +1,83 @@
#ifndef _INCLUDE__LX__LX_H_
#define _INCLUDE__LX__LX_H_
#include <stdarg.h>
#include <base/fixed_stdint.h>
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_ */

View File

@ -0,0 +1,5 @@
SRC_CC = lx.cc
LIB_DIR = $(REP_DIR)/src/lib/lx
vpath %.cc $(LIB_DIR)

View File

@ -0,0 +1,17 @@
#include <lx/extern_c_begin.h>
#include <lx/lx.h>
#include <lx/extern_c_end.h>
#include <base/printf.h>
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); }