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 namespace Priv {
36 inline long __attribute__((__noreturn__)) __runtime_error(long err, char const *extra);
37 
38 inline long __runtime_error(long err, char const *extra)
39 {
40  switch (err)
41  {
42  case -L4_ENOENT: throw (L4::Element_not_found(extra));
43  case -L4_ENOMEM: throw (L4::Out_of_memory(extra));
44  case -L4_EEXIST: throw (L4::Element_already_exists(extra));
45  case -L4_ERANGE: throw (L4::Bounds_error(extra));
46  default: throw (L4::Runtime_error(err, extra));
47  }
48 }
49 
50 }
51 
62 inline
63 long chksys(long err, char const *extra = "", long ret = 0)
64 {
65  if (L4_UNLIKELY(err < 0))
66  Priv::__runtime_error(ret ? ret : err, extra);
67 
68  return err;
69 }
70 
83 inline
84 long chksys(l4_msgtag_t const &t, char const *extra = "",
85  l4_utcb_t *utcb = l4_utcb(), long ret = 0)
86 {
87  if (L4_UNLIKELY(t.has_error()))
88  Priv::__runtime_error(ret ? ret : l4_error_u(t, utcb), extra);
89  else if (L4_UNLIKELY(t.label() < 0))
90  throw L4::Runtime_error(ret ? ret: t.label(), extra);
91 
92  return t.label();
93 }
94 
106 inline
107 long chksys(l4_msgtag_t const &t, l4_utcb_t *utcb, char const *extra = "")
108 { return chksys(t, extra, utcb); }
109 
110 #if 0
111 inline
112 long chksys(long ret, long err, char const *extra = "")
113 {
114  if (L4_UNLIKELY(ret < 0))
115  Priv::__runtime_error(err, extra);
116 
117  return ret;
118 }
119 #endif
120 
137 template<typename T>
138 inline
139 #if __cplusplus >= 201103L
140 T chkcap(T &&cap, char const *extra = "", long err = -L4_ENOMEM)
141 #else
142 T chkcap(T cap, char const *extra = "", long err = -L4_ENOMEM)
143 #endif
144 {
145  if (L4_UNLIKELY(!cap.is_valid()))
146  Priv::__runtime_error(err ? err : cap.cap(), extra);
147 
148 #if __cplusplus >= 201103L
149  return cxx::forward<T>(cap);
150 #else
151  return cap;
152 #endif
153 }
154 
169 inline
171 chkipc(l4_msgtag_t tag, char const *extra = "",
172  l4_utcb_t *utcb = l4_utcb())
173 {
174  if (L4_UNLIKELY(tag.has_error()))
175  chksys(l4_error_u(tag, utcb), extra);
176 
177  return tag;
178 }
179 #endif
180 
181 }
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
long label() const
Get the protocol value.
Definition: types.h:164
Exception for duplicate element insertions.
Definition: exceptions:203
Access out of bounds.
Definition: exceptions:289
unsigned has_error() const
Test if flags indicate an error.
Definition: types.h:191
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition: compiler.h:234
Error codes.
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition: error_helper:140
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:171
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition: error_helper:63
Range error.
Definition: err.h:58
Message tag data structure.
Definition: types.h:159
Already exists.
Definition: err.h:54