Commit 0ae3b3b7 authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Refactor magic array size into documented constant

parent 55d0e3d3
......@@ -49,6 +49,12 @@ import (
"unsafe"
)
// arraySize is a fairly large (hopefully large enough) array size for C-backed
// Go arrays. Unfortunately, it is impossible to use non-constant array bounds
// in Go, so we cannot specify the real array size but have to use this static
// one. We're very careful to not exceed the real array bounds, though.
const arraySize = 1<<30 - 1
//export parse_file
func parse_file(input *C.char, twter *C.struct_twter) (*C.struct_twt_file, *C.char) {
s := strings.NewReader(C.GoString(input))
......@@ -80,7 +86,7 @@ func parse_file(input *C.char, twter *C.struct_twter) (*C.struct_twt_file, *C.ch
twtsPtr := C.malloc(C.size_t(len(file.Twts())) * C.size_t(unsafe.Sizeof(uintptr(0))))
// convert the C array to a Go array so we can index it
a := (*[1<<30 - 1]*C.struct_twt)(twtsPtr)
a := (*[arraySize]*C.struct_twt)(twtsPtr)
for i, twt := range file.Twts() {
// a[i].twter = convertTwter(twt.Twter())
......@@ -125,7 +131,7 @@ func freeTwter(t *C.struct_twter) {
func freeTwters(t *C.struct_twter, length C.int) {
if t != nil {
twters := (*[1<<30 - 1]*C.struct_twter)(unsafe.Pointer(t))
twters := (*[arraySize]*C.struct_twter)(unsafe.Pointer(t))
for i := 0; i < int(length); i++ {
freeTwter(twters[i])
}
......@@ -146,7 +152,7 @@ func convertTwt(t types.Twt) *C.struct_twt {
}
mentionsPtr := C.malloc(C.size_t(len(t.Mentions())) * C.size_t(unsafe.Sizeof(uintptr(0))))
mentions := (*[1<<30 - 1]*C.struct_twter)(mentionsPtr)
mentions := (*[arraySize]*C.struct_twter)(mentionsPtr)
for i, mention := range t.Mentions() {
mentions[i] = convertTwter(mention.Twter())
}
......@@ -154,7 +160,7 @@ func convertTwt(t types.Twt) *C.struct_twt {
twt.mentions_len = C.int(len(t.Mentions()))
linksPtr := C.malloc(C.size_t(len(t.Links())) * C.size_t(unsafe.Sizeof(uintptr(0))))
links := (*[1<<30 - 1]*C.struct_link)(linksPtr)
links := (*[arraySize]*C.struct_link)(linksPtr)
for i, link := range t.Links() {
links[i] = convertLink(link)
}
......@@ -162,7 +168,7 @@ func convertTwt(t types.Twt) *C.struct_twt {
twt.links_len = C.int(len(t.Links()))
tagsPtr := C.malloc(C.size_t(len(t.Tags())) * C.size_t(unsafe.Sizeof(uintptr(0))))
tags := (*[1<<30 - 1]*C.struct_link)(tagsPtr)
tags := (*[arraySize]*C.struct_link)(tagsPtr)
for i, tag := range t.Tags() {
tags[i] = convertLink(tag)
}
......@@ -187,7 +193,7 @@ func freeTwt(t *C.struct_twt) {
func freeTwts(t *C.struct_twt, length C.int) {
if t != nil {
twts := (*[1<<30 - 1]*C.struct_twt)(unsafe.Pointer(t))
twts := (*[arraySize]*C.struct_twt)(unsafe.Pointer(t))
for i := 0; i < int(length); i++ {
freeTwt(twts[i])
}
......@@ -241,7 +247,7 @@ func freeLink(l *C.struct_link) {
func freeLinks(l *C.struct_link, length C.int) {
if l != nil {
links := (*[1<<30 - 1]*C.struct_link)(unsafe.Pointer(l))
links := (*[arraySize]*C.struct_link)(unsafe.Pointer(l))
for i := 0; i < int(length); i++ {
freeLink(links[i])
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment