Skip to content

Commit

Permalink
cextend
Browse files Browse the repository at this point in the history
  • Loading branch information
qiulaidongfeng committed Mar 8, 2022
1 parent f35d001 commit 23be061
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 78 deletions.
30 changes: 0 additions & 30 deletions cextend/C/main.c

This file was deleted.

19 changes: 0 additions & 19 deletions cextend/C/memory.c

This file was deleted.

6 changes: 0 additions & 6 deletions cextend/C/memory.h

This file was deleted.

4 changes: 2 additions & 2 deletions cextend/C/cextend.h → cextend/cextend.h
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CEXTEND
#define CEXTEND
#include <stdint.h>
#include <memory.h>

#include <string.h>
#include <stdlib.h>

#endif // CEXTEND
7 changes: 0 additions & 7 deletions cextend/cextend_linux_amd64.go

This file was deleted.

7 changes: 0 additions & 7 deletions cextend/cextend_windows_amd64.go

This file was deleted.

Binary file removed cextend/lib/libgocetend_win64.a
Binary file not shown.
Binary file removed cextend/lib/libgocextend_linux64.a
Binary file not shown.
24 changes: 17 additions & 7 deletions cextend/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
package cextend

/*
#cgo CFLAGS: -g -O3 -march=corei7 -fPIC -I./C
#include <cextend.h>
#cgo CFLAGS: -g -O3 -march=corei7
#include "cextend.h"
*/
import "C"

Expand All @@ -12,13 +12,23 @@ import (
"unsafe"
)

func Realloc(ptr unsafe.Pointer, size uint) (nptr unsafe.Pointer) {
nptr = C.realloc(ptr, C.size_t(size))
return
}

func Malloc(size uint) (ptr unsafe.Pointer) {
ptr = C.Malloc(C.ulong(size))
ptr = C.malloc(C.size_t(size))
return
}

func Calloc(nitems uint, size uint) (ptr unsafe.Pointer) {
ptr = C.calloc(C.size_t(nitems), C.size_t(size))
return
}

func Free(ptr unsafe.Pointer) {
C.Free(ptr)
C.free(ptr)
return
}

Expand All @@ -27,8 +37,8 @@ func Memcpy(dest, src unsafe.Pointer, n uint) {
cdest := gcdest.Value().(unsafe.Pointer)
gcsrc := cgo.NewHandle(src)
csrc := gcsrc.Value().(unsafe.Pointer)
C.Memcpy(C.uintptr_t(uintptr(cdest)), C.uintptr_t(uintptr(csrc)), C.ulong(n))
gcdest.Delete()
gcsrc.Delete()
defer gcdest.Delete()
defer gcsrc.Delete()
C.memcpy(cdest, csrc, C.size_t(n))
return
}
127 changes: 127 additions & 0 deletions cextend/memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// memory_test
package cextend

import (
"testing"
"unsafe"
)

var (
ptr unsafe.Pointer
)

func TestMalloc(t *testing.T) {
ptr = Malloc(8)
if ptr == nil {
t.Fatal("没有分配内存!")
}
}

func TestFree(t *testing.T) {
defer func() {
err := recover()
if err != nil {
t.Fatal(err)
}
}()
Free(ptr)
}

func TestMemcpy(t *testing.T) {
const size uint = 1000
var a, b [size]int8
for i := uint(0); i < size; i++ {
a[i] = int8(i / (1 << 8))
}
Memcpy(unsafe.Pointer(&b[0]), unsafe.Pointer(&a[0]), size)
for i := uint(0); i < size; i++ {
if !(b[i] == a[i]) {
t.Fatal("复制出现错误!")
}
}
}

func TestCalloc(t *testing.T) {
ptr = Calloc(1, 8)
if ptr == nil {
t.Fatal("没有分配内存!")
}
}

func BenchmarkMallocAndFree(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
for i := 0; i < b.N; i++ {
ptr = Malloc(8)
Free(ptr)
}
}

func BenchmarkCallocAndFree(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
for i := 0; i < b.N; i++ {
ptr = Calloc(1, 8)
Free(ptr)
}
}

func BenchmarkMemcpy_100bit(b *testing.B) {
b.StopTimer()
const size uint = 100
var a, c [size]int8
for i := uint(0); i < size; i++ {
a[i] = int8(i / (1 << 8))
}
b.ReportAllocs()
b.SetBytes(1)
b.StartTimer()
for i := 0; i < b.N; i++ {
Memcpy(unsafe.Pointer(&c[0]), unsafe.Pointer(&a[0]), size)
}
for i := uint(0); i < size; i++ {
if !(c[i] == a[i]) {
b.Fatal("复制出现错误!")
}
}
}

func BenchmarkMemcpy_1000bit(b *testing.B) {
b.StopTimer()
const size uint = 1000
var a, c [size]int8
for i := uint(0); i < size; i++ {
a[i] = int8(i / (1 << 8))
}
b.ReportAllocs()
b.SetBytes(1)
b.StartTimer()
for i := 0; i < b.N; i++ {
Memcpy(unsafe.Pointer(&c[0]), unsafe.Pointer(&a[0]), size)
}
for i := uint(0); i < size; i++ {
if !(c[i] == a[i]) {
b.Fatal("复制出现错误!")
}
}
}

func BenchmarkMemcpy_1000000bit(b *testing.B) {
b.StopTimer()
const size uint = 1000000
var a, c [size]int8
for i := uint(0); i < size; i++ {
a[i] = int8(i / (1 << 8))
}
b.ReportAllocs()
b.SetBytes(1)
b.StartTimer()
for i := 0; i < b.N; i++ {
Memcpy(unsafe.Pointer(&c[0]), unsafe.Pointer(&a[0]), size)
}
for i := uint(0); i < size; i++ {
if !(c[i] == a[i]) {
b.Fatal("复制出现错误!")
}
}
}

0 comments on commit 23be061

Please sign in to comment.