Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix go:linkname issue for go 1.23 #344

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BaiduPCS-Go
Binary file not shown.
7 changes: 6 additions & 1 deletion pcsutil/cachepool/malloc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//go:build !go1.23
// +build !go1.23

package cachepool

import (
"reflect"
"unsafe"
)

// 函数声明可以省略主体。 这样的声明为Go外部实现的功能(例如汇编例程)提供了签名。这是在汇编中实现函数的方式。

//go:linkname mallocgc runtime.mallocgc
func mallocgc(size uintptr, typ uintptr, needzero bool) unsafe.Pointer

Expand All @@ -30,4 +35,4 @@ func RawMallocByteSlice(size int) []byte {
Cap: size,
}))
return b
}
}
31 changes: 31 additions & 0 deletions pcsutil/cachepool/malloc_go_1.23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build go1.23
// +build go1.23

package cachepool

import (
"unsafe"
)

// 说明:
// 由于GO 1.23版本取消了 go:linkname 的支持,所以1.23以及以上版本需要使用本文件替代原始文件 malloc.go

// RawByteSlice point to runtime.rawbyteslice
func RawByteSlice(size int) (b []byte) {
bytesArray := make([]byte, size)
return bytesArray
}

// RawMalloc allocates a new slice. The slice is not zeroed.
func RawMalloc(size int) unsafe.Pointer {
bytesArray := make([]byte, size)
// 使用unsafe.Pointer获取字节数组的指针
bytesPtr := unsafe.Pointer(&bytesArray[0])
return bytesPtr
}

// RawMallocByteSlice allocates a new byte slice. The slice is not zeroed.
func RawMallocByteSlice(size int) []byte {
bytesArray := make([]byte, size)
return bytesArray
}