L4Re - L4 Runtime Environment
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  * This file is part of TUD:OS and distributed under the terms of the
13  * GNU General Public License 2.
14  * Please see the COPYING-GPL-2 file for details.
15  *
16  * As a special exception, you may use this file as part of a free software
17  * library without restriction. Specifically, if other files instantiate
18  * templates or use macros or inline functions from this file, or you compile
19  * this file and link it with other files to produce an executable, this
20  * file does not by itself cause the resulting executable to be covered by
21  * the GNU General Public License. This exception does not however
22  * invalidate any other reasons why the executable file might be covered by
23  * the GNU General Public License.
24  */
25 #pragma once
26 
27 #include <l4/sys/types.h>
28 #include <l4/cxx/exceptions>
29 #include <l4/cxx/type_traits>
30 #include <l4/sys/err.h>
31 
32 namespace L4Re {
33 
34 #ifdef __EXCEPTIONS
35 
45 [[noreturn]] inline void throw_error(long err, char const *extra = "")
46 {
47  switch (err)
48  {
49  case -L4_ENOENT: throw (L4::Element_not_found(extra));
50  case -L4_ENOMEM: throw (L4::Out_of_memory(extra));
51  case -L4_EEXIST: throw (L4::Element_already_exists(extra));
52  case -L4_ERANGE: throw (L4::Bounds_error(extra));
53  default: throw (L4::Runtime_error(err, extra));
54  }
55 }
56 
67 inline
68 long chksys(long err, char const *extra = "", long ret = 0)
69 {
70  if (L4_UNLIKELY(err < 0))
71  throw_error(ret ? ret : err, extra);
72 
73  return err;
74 }
75 
88 inline
89 long chksys(l4_msgtag_t const &t, char const *extra = "",
90  l4_utcb_t *utcb = l4_utcb(), long ret = 0)
91 {
92  if (L4_UNLIKELY(t.has_error()))
93  throw_error(ret ? ret : l4_error_u(t, utcb), extra);
94  else if (L4_UNLIKELY(t.label() < 0))
95  throw L4::Runtime_error(ret ? ret: t.label(), extra);
96 
97  return t.label();
98 }
99 
111 inline
112 long chksys(l4_msgtag_t const &t, l4_utcb_t *utcb, char const *extra = "")
113 { return chksys(t, extra, utcb); }
114 
115 #if 0
116 inline
117 long chksys(long ret, long err, char const *extra = "")
118 {
119  if (L4_UNLIKELY(ret < 0))
120  throw_error(err, extra);
121 
122  return ret;
123 }
124 #endif
125 
142 template<typename T>
143 inline
144 #if __cplusplus >= 201103L
145 T chkcap(T &&cap, char const *extra = "", long err = -L4_ENOMEM)
146 #else
147 T chkcap(T cap, char const *extra = "", long err = -L4_ENOMEM)
148 #endif
149 {
150  if (L4_UNLIKELY(!cap.is_valid()))
151  throw_error(err ? err : cap.cap(), extra);
152 
153 #if __cplusplus >= 201103L
154  return cxx::forward<T>(cap);
155 #else
156  return cap;
157 #endif
158 }
159 
174 inline
176 chkipc(l4_msgtag_t tag, char const *extra = "",
177  l4_utcb_t *utcb = l4_utcb())
178 {
179  if (L4_UNLIKELY(tag.has_error()))
180  chksys(l4_error_u(tag, utcb), extra);
181 
182  return tag;
183 }
184 #endif
185 
186 }
No such entity.
Definition: err.h:45
Exception signalling insufficient memory.
Definition: exceptions:188
Exception for a failed lookup (element not found).
Definition: exceptions:231
Common L4 ABI Data Types.
Base exceptions.
L4Re C++ Interfaces.
Definition: cmd_control:15
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
Exception for an abstract runtime error.
Definition: exceptions:139
Exception for duplicate element insertions.
Definition: exceptions:203
Access out of bounds.
Definition: exceptions:289
long label() const L4_NOTHROW
Get the protocol value.
Definition: types.h:164
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition: compiler.h:238
Error codes.
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition: error_helper:145
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
No memory.
Definition: err.h:50
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:176
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition: error_helper:68
Range error.
Definition: err.h:58
unsigned has_error() const L4_NOTHROW
Test if flags indicate an error.
Definition: types.h:191
Message tag data structure.
Definition: types.h:159
Already exists.
Definition: err.h:54
void throw_error(long err, char const *extra="")
Generate C++ exception.
Definition: error_helper:45