15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <errno.h>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace nacl {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace nonsfi {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// For IRT implementation, the following simple pattern is commonly used.
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   if (syscall(...) < 0)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     return errno;
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   return 0;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This function is a utility to write it in a line as follows:
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   return CheckError(syscall(...));
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline int CheckError(int result) {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return result < 0 ? errno : 0;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// For IRT implementation, another but a similar pattern is also commonly used.
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   T result = syscall(...);
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   if (result < 0)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     return errno;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   *output = result;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   return 0;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This function is a utility to write it in a line as follows:
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   retrun CheckErrorWithResult(syscall(...), output);
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Here, T1 must be a signed integer value, and T2 must be convertible from
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// T1.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template<typename T1, typename T2>
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int CheckErrorWithResult(const T1& result, T2* out) {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (result < 0)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return errno;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *out = result;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return 0;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace nonsfi
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace nacl
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // COMPONENTS_NACL_LOADER_NONSFI_IRT_UTIL_H_
53