/
util_native.go
125 lines (105 loc) · 2.31 KB
/
util_native.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package snmpclient
// #include <stdint.h>
// #include <sys/types.h>
// #include <string.h>
// #include <stdlib.h>
// #include <string.h>
//
// #include "bsnmp/config.h"
// #include <stdlib.h>
// #include "bsnmp/asn1.h"
// #include "bsnmp/snmp.h"
import "C"
import (
"errors"
"expvar"
"strconv"
"sync"
"sync/atomic"
"unsafe"
)
type AtomicInt struct{}
func (v *AtomicInt) String() string {
return strconv.FormatInt(int64(atomic.LoadInt32(&malloc_count)), 10)
}
var malloc_count int32 = 0
func init() {
expvar.Publish("malloc_count", &AtomicInt{})
}
//export IncrementMemory
func IncrementMemory() {
atomic.AddInt32(&malloc_count, 1)
}
//export DecrementMemory
func DecrementMemory() {
atomic.AddInt32(&malloc_count, -1)
}
//export ThrowException
func ThrowException() {
panic("error")
}
func memcpy(dst *C.uint8_t, capacity int, src []byte) error {
if 0 == len(src) {
return nil
}
if capacity < len(src) {
return errors.New("bytes too long.")
}
C.memcpy(unsafe.Pointer(dst), unsafe.Pointer(&src[0]), C.size_t(len(src)))
return nil
}
func readGoBytes(src *C.uint8_t, length C.uint32_t) []byte {
if 0 == length {
return []byte{}
}
return C.GoBytes(unsafe.Pointer(src), C.int(length))
}
func readGoString(src *C.char, capacity int) string {
if 0 == capacity {
return ""
}
length := int(C.strlen(src))
if capacity < length {
panic("string too long.")
return "" //errors.New("string too long.")
}
return C.GoStringN(src, C.int(length))
}
func strcpy(dst *C.char, capacity int, src string) error {
if 0 == len(src) {
return nil
}
if capacity < len(src) {
return errors.New("string too long.")
}
s := C.CString(src)
//IncrementMemory()
C.strcpy(dst, s)
C.free(unsafe.Pointer(s))
//DecrementMemory()
return nil
}
const pdu_cache_size = 20
var (
native_pdu_mutex sync.Mutex
native_pdu_cache = newNativePduBuffer(make([]*C.snmp_pdu_t, pdu_cache_size))
)
func newNativePdu() *C.snmp_pdu_t {
native_pdu_mutex.Lock()
cached := native_pdu_cache.Pop()
native_pdu_mutex.Unlock()
if nil != cached {
return cached
}
return C.snmp_pdu_new()
}
func releaseNativePdu(will_cache *C.snmp_pdu_t) {
C.snmp_pdu_zero(will_cache)
native_pdu_mutex.Lock()
if pdu_cache_size > native_pdu_cache.Size() {
native_pdu_cache.Push(will_cache)
} else {
C.snmp_pdu_destroy(will_cache)
}
native_pdu_mutex.Unlock()
}