Last active
October 27, 2016 12:41
-
-
Save caelifer/b82e8429e464de39edc50cc21df3123c to your computer and use it in GitHub Desktop.
Getting page size on POSIX system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build !windows | |
package main | |
import ( | |
"fmt" | |
"log" | |
) | |
/* | |
#include <errno.h> | |
#include <string.h> | |
#include <unistd.h> | |
int getErrno(void) { | |
return errno; | |
} | |
*/ | |
import "C" | |
func main() { | |
sz, err := getPageSize() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Page size: %d\n", sz) | |
} | |
func getPageSize() (int, error) { | |
sz := int(C.sysconf(C._SC_PAGESIZE)) | |
errno := C.getErrno() | |
if errno == C.EINVAL { | |
return -1, fmt.Errorf("C.sysconf: %s", C.GoString(C.strerror(errno))) | |
} | |
return sz, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This program does not work on Windows. On RHEL 6 Linux and macOS Sierra it outputs: