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/sys/err.h>
30 
31 namespace L4Re {
32 
33 #ifdef __EXCEPTIONS
34 namespace Priv {
35 inline long __attribute__((__noreturn__)) __runtime_error(long err, char const *extra);
36 
37 inline long __runtime_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 }
50 
61 inline
62 long chksys(long err, char const *extra = "", long ret = 0)
63 {
64  if (L4_UNLIKELY(err < 0))
65  Priv::__runtime_error(ret ? ret : err, extra);
66 
67  return err;
68 }
69 
82 inline
83 long chksys(l4_msgtag_t const &t, char const *extra = "",
84  l4_utcb_t *utcb = l4_utcb(), long ret = 0)
85 {
86  if (L4_UNLIKELY(t.has_error()))
87  Priv::__runtime_error(ret ? ret : l4_error_u(t, utcb), extra);
88  else if (L4_UNLIKELY(t.label() < 0))
89  throw L4::Runtime_error(ret ? ret: t.label(), extra);
90 
91  return t.label();
92 }
93 
105 inline
106 long chksys(l4_msgtag_t const &t, l4_utcb_t *utcb, char const *extra = "")
107 { return chksys(t, extra, utcb); }
108 
109 #if 0
110 inline
111 long chksys(long ret, long err, char const *extra = "")
112 {
113  if (L4_UNLIKELY(ret < 0))
114  Priv::__runtime_error(err, extra);
115 
116  return ret;
117 }
118 #endif
119 
136 template<typename T>
137 inline
138 #if __cplusplus >= 201103L
139 T chkcap(T &&cap, char const *extra = "", long err = -L4_ENOMEM)
140 #else
141 T chkcap(T cap, char const *extra = "", long err = -L4_ENOMEM)
142 #endif
143 {
144  if (L4_UNLIKELY(!cap.is_valid()))
145  Priv::__runtime_error(err ? err : cap.cap(), extra);
146 
147  return cap;
148 }
149 #endif
150 
151 }
No such entity.
Definition: err.h:45
Exception signalling insufficient memory.
Definition: exceptions:188
Exception for a failed lookup (element not found).
Definition: exceptions:232
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:163
Exception for duplicate element insertions.
Definition: exceptions:203
Access out of bounds.
Definition: exceptions:290
unsigned has_error() const
Test if flags indicate an error.
Definition: types.h:190
#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:139
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
No memory.
Definition: err.h:50
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition: error_helper:62
Range error.
Definition: err.h:58
Message tag data structure.
Definition: types.h:158
Already exists.
Definition: err.h:54