L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
error_helper
Go to the documentation of this file.
1// vi:set ft=cpp: -*- Mode: C++ -*-
6/*
7 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8 * Alexander Warg <warg@os.inf.tu-dresden.de>,
9 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
10 * economic rights: Technische Universität Dresden (Germany)
11 *
12 * License: see LICENSE.spdx (in this directory or the directories above)
13 */
14#pragma once
15
16#include <l4/sys/types.h>
17#include <l4/cxx/exceptions>
18#include <l4/cxx/type_traits>
19#include <l4/sys/err.h>
20
21#include <stdarg.h>
22#include <stdio.h>
23
24namespace L4Re {
25
26#ifdef __EXCEPTIONS
27
37[[noreturn]] inline void throw_error(long err, char const *extra = "")
38{
39 switch (err)
40 {
41 case -L4_ENOENT: throw (L4::Element_not_found(extra));
42 case -L4_ENOMEM: throw (L4::Out_of_memory(extra));
43 case -L4_EEXIST: throw (L4::Element_already_exists(extra));
44 case -L4_ERANGE: throw (L4::Bounds_error(extra));
45 default: throw (L4::Runtime_error(err, extra));
46 }
47}
48
49[[noreturn]] inline void throw_error_fmt(long err, char const *const fmt, ...)
50 __attribute__((format(printf, 2, 3)));
51[[noreturn]] inline void throw_error_fmt(long err, char const *const fmt, ...)
52{
53 char extra[80];
54 va_list argp;
55 va_start(argp, fmt);
56 vsnprintf(extra, sizeof(extra), fmt, argp);
57 va_end(argp);
58 throw_error(err, extra);
59}
60
71inline
72long chksys(long err, char const *extra = "", long ret = 0)
73{
74 if (L4_UNLIKELY(err < 0))
75 throw_error(ret ? ret : err, extra);
76
77 return err;
78}
79
92inline
93long chksys(l4_msgtag_t const &t, char const *extra = "",
94 l4_utcb_t *utcb = l4_utcb(), long ret = 0)
95{
96 if (L4_UNLIKELY(t.has_error()))
97 throw_error(ret ? ret : l4_error_u(t, utcb), extra);
98 else if (L4_UNLIKELY(t.label() < 0))
99 throw_error(ret ? ret: t.label(), extra);
100
101 return t.label();
102}
103
115inline
116long chksys(l4_msgtag_t const &t, l4_utcb_t *utcb, char const *extra = "")
117{ return chksys(t, extra, utcb); }
118
119#if 0
120inline
121long chksys(long ret, long err, char const *extra = "")
122{
123 if (L4_UNLIKELY(ret < 0))
124 throw_error(err, extra);
125
126 return ret;
127}
128#endif
129
146template<typename T>
147inline
148#if __cplusplus >= 201103L
149T chkcap(T &&cap, char const *extra = "", long err = -L4_ENOMEM)
150#else
151T chkcap(T cap, char const *extra = "", long err = -L4_ENOMEM)
152#endif
153{
154 if (L4_UNLIKELY(!cap.is_valid()))
155 throw_error(err ? err : cap.cap(), extra);
156
157#if __cplusplus >= 201103L
158 return cxx::forward<T>(cap);
159#else
160 return cap;
161#endif
162}
163
178inline
180chkipc(l4_msgtag_t tag, char const *extra = "",
181 l4_utcb_t *utcb = l4_utcb())
182{
183 if (L4_UNLIKELY(tag.has_error()))
184 chksys(l4_error_u(tag, utcb), extra);
185
186 return tag;
187}
188#endif
189
190}
Access out of bounds.
Definition exceptions:279
Exception for duplicate element insertions.
Definition exceptions:193
Exception for a failed lookup (element not found).
Definition exceptions:221
Exception signalling insufficient memory.
Definition exceptions:178
Exception for an abstract runtime error.
Definition exceptions:129
Error codes.
Base exceptions.
@ L4_EEXIST
Already exists.
Definition err.h:43
@ L4_ENOENT
No such entity.
Definition err.h:34
@ L4_ERANGE
Range error.
Definition err.h:48
@ L4_ENOMEM
No memory.
Definition err.h:39
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition utcb.h:56
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition utcb.h:346
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:275
Common L4 ABI Data Types.
L4Re C++ Interfaces.
Definition cmd_control:14
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition error_helper:72
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition error_helper:149
l4_msgtag_t chkipc(l4_msgtag_t tag, char const *extra="", l4_utcb_t *utcb=l4_utcb())
Test a message tag for IPC errors.
Definition error_helper:180
void throw_error(long err, char const *extra="")
Generate C++ exception.
Definition error_helper:37
Message tag data structure.
Definition types.h:153
long label() const L4_NOTHROW
Get the protocol value.
Definition types.h:157
bool has_error() const L4_NOTHROW
Test if flags indicate an error.
Definition types.h:190