forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.go
294 lines (252 loc) · 7.1 KB
/
alloc.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package gno
import "reflect"
// Keeps track of in-memory allocations.
// In the future, allocations within realm boundaries will be
// (optionally?) condensed (objects to be GC'd will be discarded),
// but for now, allocations strictly increment across the whole tx.
type Allocator struct {
maxBytes int64
bytes int64
}
// for gonative, which doesn't consider the allocator.
var nilAllocator = (*Allocator)(nil)
const (
// go elemental
_allocBase = 24 // defensive... XXX
_allocPointer = 8
// gno types
_allocSlice = 24
_allocPointerValue = 40
_allocStructValue = 152
_allocArrayValue = 176
_allocSliceValue = 40
_allocFuncValue = 136
_allocMapValue = 144
_allocBoundMethodValue = 176
_allocBlock = 464
_allocNativeValue = 48
_allocTypeValue = 16
_allocTypedValue = 40
_allocBigint = 200 // XXX
_allocBigdec = 200 // XXX
_allocType = 200 // XXX
_allocAny = 200 // XXX
)
const (
allocString = _allocBase
allocStringByte = 1
allocBigint = _allocBase + _allocPointer + _allocBigint
allocBigintByte = 1
allocBigdec = _allocBase + _allocPointer + _allocBigdec
allocBigdecByte = 1
allocPointer = _allocBase
allocArray = _allocBase + _allocPointer + _allocArrayValue
allocArrayItem = _allocTypedValue
allocSlice = _allocBase + _allocPointer + _allocSliceValue
allocStruct = _allocBase + _allocPointer + _allocStructValue
allocStructField = _allocTypedValue
allocFunc = _allocBase + _allocPointer + _allocFuncValue
allocMap = _allocBase + _allocPointer + _allocMapValue
allocMapItem = _allocTypedValue * 3 // XXX
allocBoundMethod = _allocBase + _allocPointer + _allocBoundMethodValue
allocBlock = _allocBase + _allocPointer + _allocBlock
allocBlockItem = _allocTypedValue
allocNative = _allocBase + _allocPointer + _allocNativeValue
allocType = _allocBase + _allocPointer + _allocType
// allocDataByte = 1
// allocPackge = 1
allocAmino = _allocBase + _allocPointer + _allocAny
allocAminoByte = 10 // XXX
)
func NewAllocator(maxBytes int64) *Allocator {
if maxBytes == 0 {
return nil
}
return &Allocator{
maxBytes: maxBytes,
}
}
func (alloc *Allocator) Status() (maxBytes int64, bytes int64) {
return alloc.maxBytes, alloc.bytes
}
func (alloc *Allocator) Reset() *Allocator {
if alloc == nil {
return nil
} else {
alloc.bytes = 0
return alloc
}
}
func (alloc *Allocator) Fork() *Allocator {
if alloc == nil {
return nil
} else {
return &Allocator{
maxBytes: alloc.maxBytes,
bytes: alloc.bytes,
}
}
}
func (alloc *Allocator) Allocate(size int64) {
if alloc == nil {
// this can happen for map items just prior to assignment.
return
}
alloc.bytes += size
if alloc.bytes > alloc.maxBytes {
panic("allocation limit exceeded")
}
}
func (alloc *Allocator) AllocateString(size int64) {
alloc.Allocate(allocString + allocStringByte*size)
}
func (alloc *Allocator) AllocatePointer() {
alloc.Allocate(allocPointer)
}
func (alloc *Allocator) AllocateDataArray(size int64) {
alloc.Allocate(allocArray + size)
}
func (alloc *Allocator) AllocateListArray(items int64) {
alloc.Allocate(allocArray + allocArrayItem*items)
}
func (alloc *Allocator) AllocateSlice() {
alloc.Allocate(allocSlice)
}
// NOTE: fields must be allocated separately.
func (alloc *Allocator) AllocateStruct() {
alloc.Allocate(allocStruct)
}
func (alloc *Allocator) AllocateStructFields(fields int64) {
alloc.Allocate(allocStructField * fields)
}
func (alloc *Allocator) AllocateFunc() {
alloc.Allocate(allocFunc)
}
func (alloc *Allocator) AllocateMap(items int64) {
alloc.Allocate(allocMap + allocMapItem*items)
}
func (alloc *Allocator) AllocateMapItem() {
alloc.Allocate(allocMapItem)
}
func (alloc *Allocator) AllocateBoundMethod() {
alloc.Allocate(allocBoundMethod)
}
func (alloc *Allocator) AllocateBlock(items int64) {
alloc.Allocate(allocBlock + allocBlockItem*items)
}
func (alloc *Allocator) AllocateBlockItems(items int64) {
alloc.Allocate(allocBlockItem * items)
}
// NOTE: does not allocate for the underlying value.
func (alloc *Allocator) AllocateNative() {
alloc.Allocate(allocNative)
}
/* NOTE: Not used, account for with AllocatePointer.
func (alloc *Allocator) AllocateDataByte() {
alloc.Allocate(allocDataByte)
}
*/
func (alloc *Allocator) AllocateType() {
alloc.Allocate(allocType)
}
// NOTE: a reasonable max-bounds calculation for simplicity.
func (alloc *Allocator) AllocateAmino(l int64) {
alloc.Allocate(allocAmino + allocAminoByte*l)
}
//----------------------------------------
// constructor utilities.
func (alloc *Allocator) NewString(s string) StringValue {
alloc.AllocateString(int64(len(s)))
return StringValue(s)
}
func (alloc *Allocator) NewListArray(n int) *ArrayValue {
alloc.AllocateListArray(int64(n))
return &ArrayValue{
List: make([]TypedValue, n),
}
}
func (alloc *Allocator) NewDataArray(n int) *ArrayValue {
alloc.AllocateDataArray(int64(n))
return &ArrayValue{
Data: make([]byte, n),
}
}
func (alloc *Allocator) NewArrayFromData(data []byte) *ArrayValue {
av := alloc.NewDataArray(len(data))
copy(av.Data, data)
return av
}
func (alloc *Allocator) NewSlice(base Value, offset, length, maxcap int) *SliceValue {
alloc.AllocateSlice()
return &SliceValue{
Base: base,
Offset: offset,
Length: length,
Maxcap: maxcap,
}
}
// NOTE: also allocates the underlying array from list.
func (alloc *Allocator) NewSliceFromList(list []TypedValue) *SliceValue {
alloc.AllocateSlice()
alloc.AllocateListArray(int64(cap(list)))
fullList := list[:cap(list)]
return &SliceValue{
Base: &ArrayValue{
List: fullList,
},
Offset: 0,
Length: len(list),
Maxcap: cap(list),
}
}
// NOTE: also allocates the underlying array from data.
func (alloc *Allocator) NewSliceFromData(data []byte) *SliceValue {
alloc.AllocateSlice()
alloc.AllocateDataArray(int64(cap(data)))
fullData := data[:cap(data)]
return &SliceValue{
Base: &ArrayValue{
Data: fullData,
},
Offset: 0,
Length: len(data),
Maxcap: cap(data),
}
}
// NOTE: fields must be allocated (e.g. from NewStructFields)
func (alloc *Allocator) NewStruct(fields []TypedValue) *StructValue {
alloc.AllocateStruct()
return &StructValue{
Fields: fields,
}
}
func (alloc *Allocator) NewStructFields(fields int) []TypedValue {
alloc.AllocateStructFields(int64(fields))
return make([]TypedValue, fields)
}
// NOTE: fields will be allocated.
func (alloc *Allocator) NewStructWithFields(fields ...TypedValue) *StructValue {
tvs := alloc.NewStructFields(len(fields))
copy(tvs, fields)
return alloc.NewStruct(tvs)
}
func (alloc *Allocator) NewMap(size int) *MapValue {
alloc.AllocateMap(int64(size))
mv := &MapValue{}
mv.MakeMap(size)
return mv
}
func (alloc *Allocator) NewBlock(source BlockNode, parent *Block) *Block {
alloc.AllocateBlock(int64(source.GetNumNames()))
return NewBlock(source, parent)
}
func (alloc *Allocator) NewNative(rv reflect.Value) *NativeValue {
alloc.AllocateNative()
return &NativeValue{
Value: rv,
}
}
func (alloc *Allocator) NewType(t Type) Type {
alloc.AllocateType()
return t
}