forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.go
412 lines (349 loc) · 10 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package gnolang
import (
"fmt"
)
// 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
visitCount int64 // times objects are visited for gc
gc func() (int64, bool) // gc callback
}
// 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
_allocStringValue = 16
_allocStructValue = 160
_allocArrayValue = 184
_allocSliceValue = 40
_allocFuncValue = 196
_allocMapValue = 152
_allocBoundMethodValue = 184
_allocBlock = 480
_allocPackageValue = 248
_allocNativeValue = 48
_allocTypeValue = 16
_allocTypedValue = 40
_allocRefValue = 72
_allocBigint = 200 // XXX
_allocBigdec = 200 // XXX
_allocType = 200 // XXX
_allocAny = 200 // XXX
)
const (
allocString = _allocBase + _allocPointer + _allocStringValue
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
allocRefValue = _allocBase + +_allocRefValue
allocType = _allocBase + _allocPointer + _allocType
allocDataByte = 1
allocPackage = _allocBase + _allocPointer + _allocPackageValue
allocAmino = _allocBase + _allocPointer + _allocAny
allocAminoByte = 10 // XXX
allocHeapItem = _allocBase + _allocPointer + _allocTypedValue
)
func NewAllocator(maxBytes int64) *Allocator {
if maxBytes == 0 {
return nil
}
return &Allocator{
maxBytes: maxBytes,
}
}
func (alloc *Allocator) SetGCCallback(f func() (int64, bool)) {
alloc.gc = f
}
func (alloc *Allocator) MemStats() string {
if alloc == nil {
return "nil allocator"
} else {
return fmt.Sprintf("Allocator{maxBytes:%d, bytes:%d}", alloc.maxBytes, alloc.bytes)
}
}
func (alloc *Allocator) Status() (maxBytes int64, bytes int64) {
return alloc.maxBytes, alloc.bytes
}
func (alloc *Allocator) Reset() *Allocator {
if alloc == nil {
return nil
}
alloc.bytes = 0
return alloc
}
func (alloc *Allocator) Fork() *Allocator {
if alloc == nil {
return nil
}
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 {
if left, ok := alloc.gc(); !ok {
panic("should not happen, allocation limit exceeded while gc.")
} else { // retry
if debug {
debug.Printf("%d left after GC, required size: %d\n", left, size)
}
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: 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)
}
func (alloc *Allocator) AllocateHeapItem() {
alloc.Allocate(allocHeapItem)
}
//----------------------------------------
// constructor utilities.
func (alloc *Allocator) NewString(s string) StringValue {
alloc.AllocateString(int64(len(s)))
return StringValue(s)
}
func (alloc *Allocator) NewListArray(n int) *ArrayValue {
if n < 0 {
panic(&Exception{Value: typedString("len out of range")})
}
alloc.AllocateListArray(int64(n))
return &ArrayValue{
List: make([]TypedValue, n),
}
}
func (alloc *Allocator) NewDataArray(n int) *ArrayValue {
if n < 0 {
panic(&Exception{Value: typedString("len out of range")})
}
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,
}
}
// NewSliceFromList allocates a new slice with the underlying array value
// populated from `list`. This should not be called from areas in the codebase
// that are doing allocations with potentially large user provided values, e.g.
// `make()` and `append()`. Using `Alloc.NewListArray` can be used is most cases
// to allocate the space for the `TypedValue` list before doing the allocation
// in the go runtime -- see the `make()` code in uverse.go.
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),
}
}
// NewSliceFromData allocates a new slice with the underlying data array
// value populated from `data`. See the doc for `NewSliceFromList` for
// correct usage notes.
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) NewType(t Type) Type {
alloc.AllocateType()
return t
}
func (alloc *Allocator) NewHeapItem(tv TypedValue) *HeapItemValue {
alloc.AllocateHeapItem()
return &HeapItemValue{Value: tv}
}
// -----------------------------------------------
func (pv *PackageValue) GetShallowSize() int64 {
return allocPackage
}
func (b *Block) GetShallowSize() int64 {
return allocBlock
}
func (av *ArrayValue) GetShallowSize() int64 {
if av.Data != nil {
return allocArray + int64(len(av.Data))
} else {
return allocArray
}
}
func (sv *StructValue) GetShallowSize() int64 {
return allocStruct
}
func (mv *MapValue) GetShallowSize() int64 {
return allocMap
}
func (bmv *BoundMethodValue) GetShallowSize() int64 {
return allocBoundMethod
}
func (hiv *HeapItemValue) GetShallowSize() int64 {
return allocHeapItem
}
func (rv RefValue) GetShallowSize() int64 {
fmt.Println("---refValue, get shallow size...")
return allocRefValue
}
func (pv PointerValue) GetShallowSize() int64 {
return allocPointer
}
func (sv *SliceValue) GetShallowSize() int64 {
return allocSlice
}
// Only count for closures.
func (fv *FuncValue) GetShallowSize() int64 {
if fv.IsClosure {
return allocFunc
}
return 0
}
func (sv StringValue) GetShallowSize() int64 {
return allocString + allocStringByte*int64(len(sv))
}
func (bv BigintValue) GetShallowSize() int64 {
return allocBigint
}
func (bv BigdecValue) GetShallowSize() int64 {
return allocBigdec
}
func (dbv DataByteValue) GetShallowSize() int64 {
return allocDataByte
}
// Do not count during recalculation,
// as the type should pre-exist.
func (tv TypeValue) GetShallowSize() int64 {
return 0
}