Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / nacl / loader / nonsfi / irt_util.h
blob81a8e9256665df51429b4e418a02873bb32f21d7
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.
5 #ifndef COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
6 #define COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
8 #include <errno.h>
10 namespace nacl {
11 namespace nonsfi {
13 // For IRT implementation, the following simple pattern is commonly used.
15 // if (syscall(...) < 0)
16 // return errno;
17 // return 0;
19 // This function is a utility to write it in a line as follows:
21 // return CheckError(syscall(...));
22 inline int CheckError(int result) {
23 return result < 0 ? errno : 0;
26 // For IRT implementation, another but a similar pattern is also commonly used.
28 // T result = syscall(...);
29 // if (result < 0)
30 // return errno;
31 // *output = result;
32 // return 0;
34 // This function is a utility to write it in a line as follows:
36 // retrun CheckErrorWithResult(syscall(...), output);
38 // Here, T1 must be a signed integer value, and T2 must be convertible from
39 // T1.
40 template<typename T1, typename T2>
41 int CheckErrorWithResult(const T1& result, T2* out) {
42 if (result < 0)
43 return errno;
45 *out = result;
46 return 0;
49 } // namespace nonsfi
50 } // namespace nacl
52 #endif // COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_