2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <sys/types.h>
31 #include <linux/types.h>
35 /* Operating System Interface */
37 ssize_t os_read(int fd, void *buf, size_t count)
39 return read(fd, buf, count);
42 ssize_t os_write(int fd, const void *buf, size_t count)
44 return write(fd, buf, count);
47 int os_open(const char *pathname, int flags)
49 return open(pathname, flags);
57 void os_exit(int exit_code)
62 /* Restore tty state when we exit */
63 static struct termios orig_term;
65 static void os_fd_restore(void)
67 tcsetattr(0, TCSANOW, &orig_term);
70 /* Put tty into raw mode so <tab> and <ctrl+c> work */
71 void os_tty_raw(int fd)
80 /* If not a tty, don't complain */
81 if (tcgetattr(fd, &orig_term))
85 term.c_iflag = IGNBRK | IGNPAR;
86 term.c_oflag = OPOST | ONLCR;
87 term.c_cflag = CS8 | CREAD | CLOCAL;
89 if (tcsetattr(fd, TCSANOW, &term))
92 atexit(os_fd_restore);
95 void *os_malloc(size_t length)
97 return mmap(NULL, length, PROT_READ | PROT_WRITE,
98 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
101 void os_usleep(unsigned long usec)
106 u64 os_get_nsec(void)
108 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK)
110 if (EINVAL == clock_gettime(CLOCK_MONOTONIC, &tp)) {
113 gettimeofday(&tv, NULL);
114 tp.tv_sec = tv.tv_sec;
115 tp.tv_nsec = tv.tv_usec * 1000;
117 return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
120 gettimeofday(&tv, NULL);
121 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;