• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
6 #define COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
7 
8 #include <errno.h>
9 
10 namespace nacl {
11 namespace nonsfi {
12 
13 // For IRT implementation, the following simple pattern is commonly used.
14 //
15 //   if (syscall(...) < 0)
16 //     return errno;
17 //   return 0;
18 //
19 // This function is a utility to write it in a line as follows:
20 //
21 //   return CheckError(syscall(...));
CheckError(int result)22 inline int CheckError(int result) {
23   return result < 0 ? errno : 0;
24 }
25 
26 // For IRT implementation, another but a similar pattern is also commonly used.
27 //
28 //   T result = syscall(...);
29 //   if (result < 0)
30 //     return errno;
31 //   *output = result;
32 //   return 0;
33 //
34 // This function is a utility to write it in a line as follows:
35 //
36 //   retrun CheckErrorWithResult(syscall(...), output);
37 //
38 // Here, T1 must be a signed integer value, and T2 must be convertible from
39 // T1.
40 template<typename T1, typename T2>
CheckErrorWithResult(const T1 & result,T2 * out)41 int CheckErrorWithResult(const T1& result, T2* out) {
42   if (result < 0)
43     return errno;
44 
45   *out = result;
46   return 0;
47 }
48 
49 }  // namespace nonsfi
50 }  // namespace nacl
51 
52 #endif  // COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
53