Skip to content

Commit

Permalink
Use pointers instead of interface/slice. Fixes #748 (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamphaus authored and elliotchance committed Jun 25, 2018
1 parent d11c376 commit 3498e1a
Show file tree
Hide file tree
Showing 36 changed files with 936 additions and 554 deletions.
4 changes: 2 additions & 2 deletions darwin/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func BuiltinExpect(a, b int32) int32 {

// AssertRtn handles __assert_rtn().
func AssertRtn(
functionName, filePath []byte,
functionName, filePath *byte,
lineNumber int32,
expression []byte,
expression *byte,
) bool {
fmt.Fprintf(
os.Stderr,
Expand Down
2 changes: 1 addition & 1 deletion darwin/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ func SincospiStret(x float64) Double2 {
return Double2{0, 0}
}

func NaN(s []byte) float64 {
func NaN(s *byte) float64 {
return math.NaN()
}
8 changes: 4 additions & 4 deletions darwin/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package darwin
import "github.com/elliotchance/c2go/noarch"

// BuiltinVsprintfChk - implementation __builtin___vsprintf_chk
func BuiltinVsprintfChk(buffer []byte, _ int32, n int32, format []byte, args noarch.VaList) int32 {
func BuiltinVsprintfChk(buffer *byte, _ int32, n int32, format *byte, args noarch.VaList) int32 {
return noarch.Sprintf(buffer, format, args.Args)
}

// BuiltinVsnprintfChk - implementation __builtin___vsnprintf_chk
func BuiltinVsnprintfChk(buffer []byte, n int32, _ int32, _ int32, format []byte, args noarch.VaList) int32 {
func BuiltinVsnprintfChk(buffer *byte, n int32, _ int32, _ int32, format *byte, args noarch.VaList) int32 {
return noarch.Sprintf(buffer, format, args.Args)
}

// BuiltinSprintfChk - implementation __builtin___sprintf_chk
func BuiltinSprintfChk(buffer []byte, _ int32, n int32, format []byte, args ...interface{}) int32 {
func BuiltinSprintfChk(buffer *byte, _ int32, n int32, format *byte, args ...interface{}) int32 {
return noarch.Sprintf(buffer, format, args)
}

// BuiltinSnprintfChk - implementation __builtin___snprintf_chk
func BuiltinSnprintfChk(buffer []byte, n int32, _ int32, _ int32, format []byte, args ...interface{}) int32 {
func BuiltinSnprintfChk(buffer *byte, n int32, _ int32, _ int32, format *byte, args ...interface{}) int32 {
return noarch.Sprintf(buffer, format, args)
}
13 changes: 7 additions & 6 deletions darwin/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ package darwin

import (
"github.com/elliotchance/c2go/noarch"
"unsafe"
)

// BuiltinStrcpy is for __builtin___strcpy_chk.
// https://opensource.apple.com/source/Libc/Libc-498/include/secure/_string.h
func BuiltinStrcpy(dest, src []byte, size int32) []byte {
func BuiltinStrcpy(dest, src *byte, size int32) *byte {
return noarch.Strcpy(dest, src)
}

// BuiltinObjectSize is for __builtin_object_size.
// https://github.com/elliotchance/c2go/issues/359
func BuiltinObjectSize(ptr []byte, theType int32) int32 {
func BuiltinObjectSize(ptr *byte, theType int32) int32 {
return 5
}

// BuiltinStrncpy is for __builtin___strncpy_chk.
// https://opensource.apple.com/source/Libc/Libc-498/include/secure/_string.h
func BuiltinStrncpy(dest, src []byte, len, size int32) []byte {
func BuiltinStrncpy(dest, src *byte, len, size int32) *byte {
return noarch.Strncpy(dest, src, len)
}

// BuiltinStrcat is for __builtin___strcat_chk
// https://opensource.apple.com/source/Libc/Libc-763.12/include/secure/_string.h.auto.html
func BuiltinStrcat(dest, src []byte, _ int32) []byte {
func BuiltinStrcat(dest, src *byte, _ int32) *byte {
return noarch.Strcat(dest, src)
}

// Memset is for __builtin___memset_chk
// https://opensource.apple.com/source/Libc/Libc-498/include/secure/_string.h
func Memset(dst interface{}, val int32, size int32, _ int32) interface{} {
func Memset(dst unsafe.Pointer, val int32, size int32, _ int32) unsafe.Pointer {
return noarch.Memset(dst, val, size)
}

// Memcpy is for __builtin___memcpy_chk and __builtin___memmove_chk
//// https://opensource.apple.com/source/Libc/Libc-498/include/secure/_string.h
func Memcpy(dst interface{}, src interface{}, size int32, _ int32) interface{} {
func Memcpy(dst, src unsafe.Pointer, size int32, _ int32) unsafe.Pointer {
return noarch.Memcpy(dst, src, size)
}
4 changes: 2 additions & 2 deletions linux/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

// AssertFail handles __assert_fail().
func AssertFail(
expression, filePath []byte,
expression, filePath *byte,
lineNumber uint32,
functionName []byte,
functionName *byte,
) bool {
fmt.Fprintf(
os.Stderr,
Expand Down
5 changes: 3 additions & 2 deletions linux/ctype.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linux

import (
"unicode"
"unsafe"
)

var characterTable []uint16
Expand Down Expand Up @@ -82,12 +83,12 @@ func generateCharacterTable() {
}

// CtypeLoc handles __ctype_b_loc(). It returns a character table.
func CtypeLoc() [][]uint16 {
func CtypeLoc() **uint16 {
if len(characterTable) == 0 {
generateCharacterTable()
}

return [][]uint16{characterTable}
return (**uint16)(unsafe.Pointer(&characterTable))
}

// ToLower handles tolower().
Expand Down
11 changes: 5 additions & 6 deletions noarch/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package noarch

import (
"strings"
"unsafe"
)

const (
Expand Down Expand Up @@ -293,12 +292,12 @@ func init() {
}

// Strerror translates an errno error code into an error message.
func Strerror(errno int32) []byte {
func Strerror(errno int32) *byte {
b, ok := err2bytes[int(errno)]
if ok {
return b
return &b[0]
}
return err2bytes[0]
return &err2bytes[0][0]
}

var currentErrno int32
Expand All @@ -321,6 +320,6 @@ func setCurrentErrno(errno int32) {
}

// Errno returns a pointer to the current errno.
func Errno() []int32 {
return (*[1]int32)(unsafe.Pointer(&currentErrno))[:]
func Errno() *int32 {
return &currentErrno
}
Loading

0 comments on commit 3498e1a

Please sign in to comment.