Skip to content

Commit

Permalink
fix cgo array to slice casting
Browse files Browse the repository at this point in the history
Use unsafe.Slice instead of using a type of array of size 1 << 30.

Fix error on i686-w64-mingw32:
compile.go:407:30: type [1073741824]*_Ctype_char larger than address space
  • Loading branch information
starius committed Jan 1, 2025
1 parent c15d8d5 commit 93058c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions internal/ch/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ func CompileExtMulti(p Patterns, mode CompileMode, info *hs.PlatformInfo,
patterns := p.Patterns()

cexprs := (**C.char)(C.calloc(C.size_t(len(patterns)), C.size_t(unsafe.Sizeof(uintptr(0)))))
exprs := (*[1 << 30]*C.char)(unsafe.Pointer(cexprs))[:len(patterns):len(patterns)]
exprs := unsafe.Slice(cexprs, len(patterns))

cflags := (*C.uint)(C.calloc(C.size_t(len(patterns)), C.size_t(unsafe.Sizeof(C.uint(0)))))
flags := (*[1 << 30]C.uint)(unsafe.Pointer(cflags))[:len(patterns):len(patterns)]
flags := unsafe.Slice(cflags, len(patterns))

cids := (*C.uint)(C.calloc(C.size_t(len(patterns)), C.size_t(unsafe.Sizeof(C.uint(0)))))
ids := (*[1 << 30]C.uint)(unsafe.Pointer(cids))[:len(patterns):len(patterns)]
ids := unsafe.Slice(cids, len(patterns))

for i, pattern := range patterns {
exprs[i] = C.CString(pattern.Expression)
Expand Down
8 changes: 4 additions & 4 deletions internal/hs/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,16 @@ func CompileMulti(input Patterns, mode ModeFlag, info *PlatformInfo) (Database,
count := len(patterns)

cexprs := (**C.char)(C.calloc(C.size_t(count), C.size_t(unsafe.Sizeof(uintptr(0)))))
exprs := (*[1 << 30]*C.char)(unsafe.Pointer(cexprs))[:count:count]
exprs := unsafe.Slice(cexprs, count)

cflags := (*C.uint)(C.calloc(C.size_t(count), C.size_t(unsafe.Sizeof(C.uint(0)))))
flags := (*[1 << 30]C.uint)(unsafe.Pointer(cflags))[:count:count]
flags := unsafe.Slice(cflags, count)

cids := (*C.uint)(C.calloc(C.size_t(count), C.size_t(unsafe.Sizeof(C.uint(0)))))
ids := (*[1 << 30]C.uint)(unsafe.Pointer(cids))[:count:count]
ids := unsafe.Slice(cids, count)

cexts := (**C.hs_expr_ext_t)(C.calloc(C.size_t(count), C.size_t(unsafe.Sizeof(uintptr(0)))))
exts := (*[1 << 30]*C.hs_expr_ext_t)(unsafe.Pointer(cexts))[:count:count]
exts := unsafe.Slice(cexts, count)

for i, pattern := range patterns {
exprs[i] = C.CString(pattern.Expr)
Expand Down
8 changes: 4 additions & 4 deletions internal/hs/compile_v52.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ func CompileLitMulti(input Literals, mode ModeFlag, info *PlatformInfo) (Databas
count := len(literals)

cexprs := (**C.char)(C.calloc(C.size_t(len(literals)), C.size_t(unsafe.Sizeof(uintptr(0)))))
exprs := (*[1 << 30]*C.char)(unsafe.Pointer(cexprs))[:len(literals):len(literals)]
exprs := unsafe.Slice(cexprs, len(literals))

clens := (*C.size_t)(C.calloc(C.size_t(len(literals)), C.size_t(unsafe.Sizeof(uintptr(0)))))
lens := (*[1 << 30]C.size_t)(unsafe.Pointer(clens))[:len(literals):len(literals)]
lens := unsafe.Slice(clens, len(literals))

cflags := (*C.uint)(C.calloc(C.size_t(len(literals)), C.size_t(unsafe.Sizeof(C.uint(0)))))
flags := (*[1 << 30]C.uint)(unsafe.Pointer(cflags))[:len(literals):len(literals)]
flags := unsafe.Slice(cflags, len(literals))

cids := (*C.uint)(C.calloc(C.size_t(len(literals)), C.size_t(unsafe.Sizeof(C.uint(0)))))
ids := (*[1 << 30]C.uint)(unsafe.Pointer(cids))[:len(literals):len(literals)]
ids := unsafe.Slice(cids, len(literals))

for i, lit := range literals {
exprs[i] = C.CString(lit.Expr)
Expand Down

0 comments on commit 93058c6

Please sign in to comment.