-
Notifications
You must be signed in to change notification settings - Fork 9
/
typed_carray.go
178 lines (145 loc) · 4.6 KB
/
typed_carray.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @brief Typed C-Array (binary array) IPC buffer support
*
* @file typed_carray.go
*/
/* -----------------------------------------------------------------------------
* Enduro/X Middleware Platform for Distributed Transaction Processing
* Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
* Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
* This software is released under one of the following licenses:
* LGPL or Mavimax's license for commercial use.
* See LICENSE file for full text.
*
* C (as designed by Dennis Ritchie and later authors) language code is licensed
* under Enduro/X Modified GNU Affero General Public License, version 3.
* See LICENSE_C file for full text.
* -----------------------------------------------------------------------------
* LGPL license:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, version 3 as published
* by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 3
* for more details.
*
* You should have received a copy of the Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -----------------------------------------------------------------------------
* A commercial use license is available from Mavimax, Ltd
* -----------------------------------------------------------------------------
*/
package atmi
/*
#cgo pkg-config: atmisrvinteg
#include <xatmi.h>
#include <string.h>
#include <stdlib.h>
#include <ubf.h>
void * c_get_void_ptr(char * ptr)
{
return (void *)ptr;
}
void c_copy_data_to_go(const void *go_data, char *c_data, long nbytes)
{
memcpy((char *)go_data, c_data, nbytes);
}
void c_copy_data_to_c(char *c_data, const void *go_data, long nbytes)
{
memcpy(c_data, go_data, nbytes);
}
*/
import "C"
import "unsafe"
//UBF Buffer
type TypedCarray struct {
Buf *ATMIBuf
}
//Return The ATMI buffer to caller
func (u *TypedCarray) GetBuf() *ATMIBuf {
return u.Buf
}
/*
22/06/2018 - benchmark optimisations
func cpyGo2C(c *C.char, b []byte) {
for i := 0; i < len(b); i++ {
*(*C.char)(unsafe.Pointer(uintptr(C.c_get_void_ptr(c)) + uintptr(i))) = C.char(b[i])
}
}
*/
//Allocate new string buffer
//@param s - source string
func (ac *ATMICtx) NewCarray(b []byte) (*TypedCarray, ATMIError) {
var buf TypedCarray
if ptr, err := ac.TpAlloc("CARRAY", "", int64(len(b))); nil != err {
return nil, err
} else {
buf.Buf = ptr
/* Copy off the bytes to C buf */
/* cpyGo2C(buf.Buf.C_ptr, b) - optimizations */
buf.Buf.C_len = C.long(len(b))
C.c_copy_data_to_c(buf.Buf.C_ptr, unsafe.Pointer(&b[0]), buf.Buf.C_len)
buf.Buf.TpSetCtxt(ac)
return &buf, nil
}
}
//Get the String Handler
func (ac *ATMICtx) CastToCarray(abuf *ATMIBuf) (*TypedCarray, ATMIError) {
var buf TypedCarray
buf.Buf = abuf
return &buf, nil
}
//Get the string value out from buffer
//@return String value
func (s *TypedCarray) GetBytes() []byte {
defer s.Buf.nop()
b := make([]byte, s.Buf.C_len)
/*
22/06/2018 - benchmark optimisations
for i := 0; i < len(b); i++ {
b[i] = byte(*(*C.char)(unsafe.Pointer(uintptr(C.c_get_void_ptr(s.Buf.C_ptr)) + uintptr(i))))
}
*/
if s.Buf.C_len > 0 {
C.c_copy_data_to_go(unsafe.Pointer(&b[0]), s.Buf.C_ptr, s.Buf.C_len)
}
return b
}
//@param str String value
func (s *TypedCarray) SetBytes(b []byte) ATMIError {
new_size := int64(len(b))
if cur_size, err := s.Buf.Ctx.TpTypes(s.Buf, nil, nil); nil != err {
return err
} else {
if cur_size >= new_size {
/* cpyGo2C(s.Buf.C_ptr, b) */
if new_size > 0 {
C.c_copy_data_to_c(s.Buf.C_ptr, unsafe.Pointer(&b[0]), C.long(new_size))
}
s.Buf.C_len = C.long(new_size)
} else if err := s.Buf.TpRealloc(new_size); nil != err {
return err
} else {
/* cpyGo2C(s.Buf.C_ptr, b)*/
if new_size > 0 {
C.c_copy_data_to_c(s.Buf.C_ptr, unsafe.Pointer(&b[0]), C.long(new_size))
}
s.Buf.C_len = C.long(new_size)
}
}
s.Buf.nop()
return nil
}
///////////////////////////////////////////////////////////////////////////////////
// Wrappers for memory management
///////////////////////////////////////////////////////////////////////////////////
func (u *TypedCarray) TpRealloc(size int64) ATMIError {
return u.Buf.TpRealloc(size)
}
/* vim: set ts=4 sw=4 et smartindent: */