forked from hyper0x/goc2p
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
8,989 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
bin**/* | ||
pkg**/* | ||
logs**/* | ||
pprof**/* | ||
_obj | ||
*.exe | ||
*.out | ||
*.di | ||
*.callgrind | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
cgolib "basic/cgo/lib" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
input := float32(2.33) | ||
output, err := cgolib.Sqrt(input) | ||
if err != nil { | ||
fmt.Errorf("Error: %s\n", err) | ||
} | ||
fmt.Printf("The square root of %f is %f.\n", input, output) | ||
|
||
cgolib.Print("ABC\n") | ||
|
||
cgolib.CallCFunc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package lib | ||
|
||
/* | ||
#include <stdio.h> | ||
extern void CFunction1(); | ||
*/ | ||
import "C" | ||
|
||
import "fmt" | ||
|
||
//export GoFunction1 | ||
func GoFunction1() { | ||
fmt.Println("GoFunction1() is called.") | ||
} | ||
|
||
func CallCFunc() { | ||
C.CFunction1() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package lib | ||
|
||
/* | ||
#include <stdio.h> | ||
void CFunction1() { | ||
printf("CFunction1() is called.\n"); | ||
GoFunction1(); | ||
} | ||
*/ | ||
import "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package lib | ||
|
||
/* | ||
#cgo LDFLAGS: -lm | ||
#include <math.h> | ||
*/ | ||
import "C" | ||
|
||
func Sqrt(p float32) (float32, error) { | ||
result, err := C.sqrt(C.double(p)) | ||
return float32(result), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package lib | ||
|
||
/* | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
void myprint(char* s) { | ||
printf("%s", s); | ||
} | ||
*/ | ||
import "C" | ||
import "unsafe" | ||
|
||
func Print(s string) { | ||
cs := C.CString(s) | ||
defer C.free(unsafe.Pointer(cs)) | ||
C.myprint(cs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lib | ||
|
||
/* | ||
#include <stdlib.h> | ||
*/ | ||
import "C" | ||
|
||
func Random() int { | ||
return int(C.rand()) | ||
} | ||
|
||
func Seed(i int) { | ||
C.srand(C.uint(i)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package map1 | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
type ConcurrentMap interface { | ||
GenericMap | ||
} | ||
|
||
type myConcurrentMap struct { | ||
m map[interface{}]interface{} | ||
keyType reflect.Type | ||
elemType reflect.Type | ||
rwmutex sync.RWMutex | ||
} | ||
|
||
func (cmap *myConcurrentMap) Get(key interface{}) interface{} { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
return cmap.m[key] | ||
} | ||
|
||
func (cmap *myConcurrentMap) isAcceptablePair(k, e interface{}) bool { | ||
if k == nil || reflect.TypeOf(k) != cmap.keyType { | ||
return false | ||
} | ||
if e == nil || reflect.TypeOf(e) != cmap.elemType { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (cmap *myConcurrentMap) Put(key interface{}, elem interface{}) (interface{}, bool) { | ||
if !cmap.isAcceptablePair(key, elem) { | ||
return nil, false | ||
} | ||
cmap.rwmutex.Lock() | ||
defer cmap.rwmutex.Unlock() | ||
oldElem := cmap.m[key] | ||
cmap.m[key] = elem | ||
return oldElem, true | ||
} | ||
|
||
func (cmap *myConcurrentMap) Remove(key interface{}) interface{} { | ||
cmap.rwmutex.Lock() | ||
defer cmap.rwmutex.Unlock() | ||
oldElem := cmap.m[key] | ||
delete(cmap.m, key) | ||
return oldElem | ||
} | ||
|
||
func (cmap *myConcurrentMap) Clear() { | ||
cmap.rwmutex.Lock() | ||
defer cmap.rwmutex.Unlock() | ||
cmap.m = make(map[interface{}]interface{}) | ||
} | ||
|
||
func (cmap *myConcurrentMap) Len() int { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
return len(cmap.m) | ||
} | ||
|
||
func (cmap *myConcurrentMap) Contains(key interface{}) bool { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
_, ok := cmap.m[key] | ||
return ok | ||
} | ||
|
||
func (cmap *myConcurrentMap) Keys() []interface{} { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
initialLen := len(cmap.m) | ||
keys := make([]interface{}, initialLen) | ||
index := 0 | ||
for k, _ := range cmap.m { | ||
keys[index] = k | ||
index++ | ||
} | ||
return keys | ||
} | ||
|
||
func (cmap *myConcurrentMap) Elems() []interface{} { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
initialLen := len(cmap.m) | ||
elems := make([]interface{}, initialLen) | ||
index := 0 | ||
for _, v := range cmap.m { | ||
elems[index] = v | ||
index++ | ||
} | ||
return elems | ||
} | ||
|
||
func (cmap *myConcurrentMap) ToMap() map[interface{}]interface{} { | ||
cmap.rwmutex.RLock() | ||
defer cmap.rwmutex.RUnlock() | ||
replica := make(map[interface{}]interface{}) | ||
for k, v := range cmap.m { | ||
replica[k] = v | ||
} | ||
return replica | ||
} | ||
|
||
func (cmap *myConcurrentMap) KeyType() reflect.Type { | ||
return cmap.keyType | ||
} | ||
|
||
func (cmap *myConcurrentMap) ElemType() reflect.Type { | ||
return cmap.elemType | ||
} | ||
|
||
func (cmap *myConcurrentMap) String() string { | ||
var buf bytes.Buffer | ||
buf.WriteString("ConcurrentMap<") | ||
buf.WriteString(cmap.keyType.Kind().String()) | ||
buf.WriteString(",") | ||
buf.WriteString(cmap.elemType.Kind().String()) | ||
buf.WriteString(">{") | ||
first := true | ||
for k, v := range cmap.m { | ||
if first { | ||
first = false | ||
} else { | ||
buf.WriteString(" ") | ||
} | ||
buf.WriteString(fmt.Sprintf("%v", k)) | ||
buf.WriteString(":") | ||
buf.WriteString(fmt.Sprintf("%v", v)) | ||
} | ||
buf.WriteString("}") | ||
return buf.String() | ||
} | ||
|
||
func NewConcurrentMap(keyType, elemType reflect.Type) ConcurrentMap { | ||
return &myConcurrentMap{ | ||
keyType: keyType, | ||
elemType: elemType, | ||
m: make(map[interface{}]interface{})} | ||
} |
Oops, something went wrong.