Ian Lance Taylor | 2f0061c | 2011-10-23 10:44:39 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package syscall contains an interface to the low-level operating system |
| 6 | // primitives. The details vary depending on the underlying system. |
| 7 | // Its primary use is inside other packages that provide a more portable |
| 8 | // interface to the system, such as "os", "time" and "net". Use those |
| 9 | // packages rather than this one if you can. |
| 10 | // For details of the functions and data types in this package consult |
| 11 | // the manuals for the appropriate operating system. |
Ian Lance Taylor | 83ef2ed | 2011-12-12 15:37:11 -0800 | [diff] [blame] | 12 | // These calls return err == nil to indicate success; otherwise |
| 13 | // err is an operating system error describing the failure. |
| 14 | // On most systems, that error has type syscall.Errno. |
Ian Lance Taylor | 2f0061c | 2011-10-23 10:44:39 -0700 | [diff] [blame] | 15 | package syscall |
| 16 | |
| 17 | import "unsafe" |
| 18 | |
| 19 | // StringByteSlice returns a NUL-terminated slice of bytes |
| 20 | // containing the text of s. |
| 21 | func StringByteSlice(s string) []byte { |
| 22 | a := make([]byte, len(s)+1) |
| 23 | copy(a, s) |
| 24 | return a |
| 25 | } |
| 26 | |
| 27 | // StringBytePtr returns a pointer to a NUL-terminated array of bytes |
| 28 | // containing the text of s. |
| 29 | func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] } |
| 30 | |
| 31 | // Single-word zero for use when we need a valid pointer to 0 bytes. |
| 32 | // See mksyscall.pl. |
| 33 | var _zero uintptr |
| 34 | |
| 35 | var dummy *byte |
Ian Lance Taylor | 8cf10fb | 2012-02-09 00:06:37 -0800 | [diff] [blame] | 36 | |
Ian Lance Taylor | 2f0061c | 2011-10-23 10:44:39 -0700 | [diff] [blame] | 37 | const sizeofPtr uintptr = uintptr(unsafe.Sizeof(dummy)) |
Ian Lance Taylor | 3043522 | 2012-01-25 13:47:12 -0800 | [diff] [blame] | 38 | |
| 39 | func (ts *Timespec) Unix() (sec int64, nsec int64) { |
| 40 | return int64(ts.Sec), int64(ts.Nsec) |
| 41 | } |
| 42 | |
| 43 | func (tv *Timeval) Unix() (sec int64, nsec int64) { |
| 44 | return int64(tv.Sec), int64(tv.Usec) * 1000 |
| 45 | } |
Ian Lance Taylor | 8cf10fb | 2012-02-09 00:06:37 -0800 | [diff] [blame] | 46 | |
| 47 | func (ts *Timespec) Nano() int64 { |
| 48 | return int64(ts.Sec)*1e9 + int64(ts.Nsec) |
| 49 | } |
| 50 | |
| 51 | func (tv *Timeval) Nano() int64 { |
| 52 | return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 |
| 53 | } |