diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-01-02 11:00:31 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-01-02 11:00:31 +0100 |
| commit | 18d8f06226e7d1ef4c6f2023f246f7cb0cf8783d (patch) | |
| tree | ff87af7374c84e0d62280859486075bf1dff5b08 /exports | |
| parent | 240cf78bdf9f8f9e383d5fa45b04fd6f1fb1ecc5 (diff) | |
Exports: Use unsafe.Slice for C slice conversion
This is possible now as we require Go 1.17
Diffstat (limited to 'exports')
| -rw-r--r-- | exports/disco.go | 8 | ||||
| -rw-r--r-- | exports/servers.go | 18 |
2 files changed, 13 insertions, 13 deletions
diff --git a/exports/disco.go b/exports/disco.go index 51de2f5..e18df3d 100644 --- a/exports/disco.go +++ b/exports/disco.go @@ -69,7 +69,7 @@ func getCPtrDiscoOrganizations( organizationsPtr := (**C.discoveryOrganization)( C.malloc(totalOrganizations * C.size_t(unsafe.Sizeof(uintptr(0)))), ) - cOrganizations := (*[1<<30 - 1]*C.discoveryOrganization)(unsafe.Pointer(organizationsPtr))[:totalOrganizations:totalOrganizations] + cOrganizations := unsafe.Slice(organizationsPtr, totalOrganizations) index := 0 for _, organization := range organizations.List { cOrganization := getCPtrDiscoOrganization(state, &organization) @@ -112,7 +112,7 @@ func getCPtrDiscoServers( serversPtr := (**C.discoveryServer)( C.malloc(totalServers * C.size_t(unsafe.Sizeof(uintptr(0)))), ) - cServers := (*[1<<30 - 1]*C.discoveryServer)(unsafe.Pointer(serversPtr)) + cServers := unsafe.Slice(serversPtr, totalServers) index := 0 for _, server := range servers.List { cServer := getCPtrDiscoServer(state, &server) @@ -147,7 +147,7 @@ func freeDiscoServer(cServer *C.discoveryServer) { //export FreeDiscoServers func FreeDiscoServers(cServers *C.discoveryServers) { if cServers.total_servers > 0 { - servers := (*[1<<30 - 1]*C.discoveryServer)(unsafe.Pointer(cServers.servers))[:cServers.total_servers:cServers.total_servers] + servers := unsafe.Slice(cServers.servers, cServers.total_servers) for i := C.size_t(0); i < cServers.total_servers; i++ { freeDiscoServer(servers[i]) } @@ -159,7 +159,7 @@ func FreeDiscoServers(cServers *C.discoveryServers) { //export FreeDiscoOrganizations func FreeDiscoOrganizations(cOrganizations *C.discoveryOrganizations) { if cOrganizations.total_organizations > 0 { - organizations := (*[1<<30 - 1]*C.discoveryOrganization)(unsafe.Pointer(cOrganizations.organizations))[:cOrganizations.total_organizations:cOrganizations.total_organizations] + organizations := unsafe.Slice(cOrganizations.organizations, cOrganizations.total_organizations) for i := C.size_t(0); i < cOrganizations.total_organizations; i++ { freeDiscoOrganization(organizations[i]) } diff --git a/exports/servers.go b/exports/servers.go index 00447db..662808c 100644 --- a/exports/servers.go +++ b/exports/servers.go @@ -87,8 +87,8 @@ func getCPtrProfiles(serverProfiles *server.ProfileInfo) *C.serverProfiles { // If we have profiles (which we should), we allocate the struct with malloc and the size of a pointer // We then fill the struct by converting it to a go slice and get a C pointer for each profile if totalProfiles > 0 { - profilesPtr := C.malloc(totalProfiles * C.size_t(unsafe.Sizeof(uintptr(0)))) - profiles := (*[1<<30 - 1]*C.serverProfile)(unsafe.Pointer(profilesPtr))[:totalProfiles:totalProfiles] + profilesPtr := (**C.serverProfile)(C.malloc(totalProfiles * C.size_t(unsafe.Sizeof(uintptr(0))))) + profiles := unsafe.Slice(profilesPtr, totalProfiles) index := 0 for _, profile := range goProfiles { profiles[index] = getCPtrProfile(&profile) @@ -108,7 +108,7 @@ func FreeProfiles(profiles *C.serverProfiles) { // We should only free the profiles if we have them (which we should) if profiles.total_profiles > 0 { // Convert it to a go slice - profilesSlice := (*[1<<30 - 1]*C.serverProfile)(unsafe.Pointer(profiles.profiles))[:profiles.total_profiles:profiles.total_profiles] + profilesSlice := unsafe.Slice(profiles.profiles, profiles.total_profiles) // Loop through the pointers and free th allocated strings and the struct itself for i := C.size_t(0); i < profiles.total_profiles; i++ { C.free(unsafe.Pointer(profilesSlice[i].id)) @@ -131,9 +131,9 @@ func getCPtrListStrings(allStrings []string) (C.size_t, **C.char) { // If we have strings // Allocate memory for the strings array if totalStrings > 0 { - stringsPtr := C.malloc(totalStrings * C.size_t(unsafe.Sizeof(uintptr(0)))) + stringsPtr := (**C.char)(C.malloc(totalStrings * C.size_t(unsafe.Sizeof(uintptr(0))))) // Go slice conversion - cStrings := (*[1<<30 - 1]*C.char)(unsafe.Pointer(stringsPtr))[:totalStrings:totalStrings] + cStrings := unsafe.Slice(stringsPtr, totalStrings) // Loop through and allocate the string for each contact for index, string := range allStrings { @@ -153,7 +153,7 @@ func freeCListStrings(allStrings **C.char, totalStrings C.size_t) { // By converting to a Go slice, and freeing them ony by one // At last free the pointer itself if totalStrings > 0 { - stringsSlice := (*[1<<30 - 1]*C.char)(unsafe.Pointer(allStrings))[:totalStrings:totalStrings] + stringsSlice := unsafe.Slice(allStrings, totalStrings) for i := C.size_t(0); i < totalStrings; i++ { C.free(unsafe.Pointer(stringsSlice[i])) } @@ -231,7 +231,7 @@ func getCPtrServers( // If we have servers, which is not always the case if totalServers > 0 { serversPtr := (**C.server)(C.malloc(totalServers * C.size_t(unsafe.Sizeof(uintptr(0))))) - servers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(serversPtr))[:totalServers:totalServers] + servers := unsafe.Slice(serversPtr, totalServers) index := 0 for _, currentServer := range serverMap { cServer := getCPtrServer(state, ¤tServer.Basic) @@ -250,7 +250,7 @@ func getCPtrServers( func FreeServers(cServers *C.servers) { // Free the custom servers if there are any if cServers.total_custom > 0 { - customServers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(cServers.custom_servers))[:cServers.total_custom:cServers.total_custom] + customServers := unsafe.Slice(cServers.custom_servers, cServers.total_custom) for i := C.size_t(0); i < cServers.total_custom; i++ { FreeServer(customServers[i]) } @@ -258,7 +258,7 @@ func FreeServers(cServers *C.servers) { } // Free the institute access servers if there are any if cServers.total_institute > 0 { - instituteServers := (*[1<<30 - 1]*C.server)(unsafe.Pointer(cServers.institute_servers))[:cServers.total_institute:cServers.total_institute] + instituteServers := unsafe.Slice(cServers.institute_servers, cServers.total_institute) for i := C.size_t(0); i < cServers.total_institute; i++ { FreeServer(instituteServers[i]) |
