Integer type policy

Integers are for numbers, enabling arithmetic like subtractions and for
loops without getting shot in the foot. Unsigneds are for bitfields.

- "int" for numbers that will always be laughably smaller than four
  billion, and where we don't care about the serialization format.

- "int32" for numbers that will always be laughably smaller than four
  billion, and will be serialized to four bytes.

- "int64" for numbers that may approach four billion or will be
  serialized to eight bytes.

- "uint32" and "uint64" for bitfields, depending on required number of
  bits and serialization format. Likewise "uint8" and "uint16", although
  rare in this project since they don't exist in XDR.

- "int8", "int16" and plain "uint" are almost never useful.
This commit is contained in:
Jakob Borg
2015-01-18 02:12:06 +01:00
parent 221e3eddd5
commit 2c8b627008
30 changed files with 181 additions and 151 deletions

View File

@@ -794,7 +794,7 @@ func toNeedSlice(fs []db.FileInfoTruncated) []map[string]interface{} {
"Version": file.Version,
"LocalVersion": file.LocalVersion,
"NumBlocks": file.NumBlocks,
"Size": db.BlocksToSize(file.NumBlocks),
"Size": db.BlocksToSize(int(file.NumBlocks)),
}
}
return output

View File

@@ -22,7 +22,7 @@ import (
"strings"
)
func memorySize() (uint64, error) {
func memorySize() (int64, error) {
cmd := exec.Command("sysctl", "hw.memsize")
out, err := cmd.Output()
if err != nil {
@@ -32,7 +32,7 @@ func memorySize() (uint64, error) {
if len(fs) != 2 {
return 0, errors.New("sysctl parse error")
}
bytes, err := strconv.ParseUint(fs[1], 10, 64)
bytes, err := strconv.ParseInt(fs[1], 10, 64)
if err != nil {
return 0, err
}

View File

@@ -23,7 +23,7 @@ import (
"strings"
)
func memorySize() (uint64, error) {
func memorySize() (int64, error) {
f, err := os.Open("/proc/meminfo")
if err != nil {
return 0, err
@@ -40,7 +40,7 @@ func memorySize() (uint64, error) {
return 0, errors.New("/proc/meminfo parse error 2")
}
kb, err := strconv.ParseUint(fs[1], 10, 64)
kb, err := strconv.ParseInt(fs[1], 10, 64)
if err != nil {
return 0, err
}

View File

@@ -22,14 +22,14 @@ import (
"strconv"
)
func memorySize() (uint64, error) {
func memorySize() (int64, error) {
cmd := exec.Command("prtconf", "-m")
out, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
mb, err := strconv.ParseUint(string(out), 10, 64)
mb, err := strconv.ParseInt(string(out), 10, 64)
if err != nil {
return 0, err
}

View File

@@ -19,6 +19,6 @@ package main
import "errors"
func memorySize() (uint64, error) {
func memorySize() (int64, error) {
return 0, errors.New("not implemented")
}

View File

@@ -26,7 +26,7 @@ var (
globalMemoryStatusEx, _ = syscall.GetProcAddress(kernel32, "GlobalMemoryStatusEx")
)
func memorySize() (uint64, error) {
func memorySize() (int64, error) {
var memoryStatusEx [64]byte
binary.LittleEndian.PutUint32(memoryStatusEx[:], 64)
p := uintptr(unsafe.Pointer(&memoryStatusEx[0]))
@@ -36,5 +36,5 @@ func memorySize() (uint64, error) {
return 0, callErr
}
return binary.LittleEndian.Uint64(memoryStatusEx[8:]), nil
return int64(binary.LittleEndian.Uint64(memoryStatusEx[8:])), nil
}

View File

@@ -43,7 +43,7 @@ func savePerfStats(file string) {
var prevTime int64
var rusage syscall.Rusage
var memstats runtime.MemStats
var prevIn, prevOut uint64
var prevIn, prevOut int64
t0 := time.Now()
for t := range time.NewTicker(250 * time.Millisecond).C {