Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AllocFast.asm « i386 « Runtime « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: edc8c0a837703ecbb1e02dec8c7a7d1cf03f5412 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
;; Licensed to the .NET Foundation under one or more agreements.
;; The .NET Foundation licenses this file to you under the MIT license.
;; See the LICENSE file in the project root for more information.

        .586
        .model  flat
        option  casemap:none
        .code


include AsmMacros.inc

;; Allocate non-array, non-finalizable object. If the allocation doesn't fit into the current thread's
;; allocation context then automatically fallback to the slow allocation path.
;;  ECX == EEType
FASTCALL_FUNC   RhpNewFast, 4

        ;; edx = GetThread(), TRASHES eax
        INLINE_GETTHREAD edx, eax

        ;;
        ;; ecx contains EEType pointer
        ;;
        mov         eax, [ecx + OFFSETOF__EEType__m_uBaseSize]

        ;;
        ;; eax: base size
        ;; ecx: EEType pointer
        ;; edx: Thread pointer
        ;;

        add         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
        cmp         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_limit]
        ja          AllocFailed

        ;; set the new alloc pointer
        mov         [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr], eax

        ;; calc the new object pointer
        sub         eax, [ecx + OFFSETOF__EEType__m_uBaseSize]

        ;; set the new object's EEType pointer
        mov         [eax], ecx
        ret

AllocFailed:

        ;;
        ;; SLOW PATH, call RhpGcAlloc(EEType *pEEType, UInt32 uFlags, UIntNative cbSize, void * pTransitionFrame)
        ;;
        ;; ecx: EEType pointer
        ;;
        push        ebp
        mov         ebp, esp

        PUSH_COOP_PINVOKE_FRAME edx

        ;; Preserve EEType in ESI.
        mov         esi, ecx

        ;; Push alloc helper arguments
        push        edx                                             ; transition frame
        push        dword ptr [ecx + OFFSETOF__EEType__m_uBaseSize] ; Size
        xor         edx, edx                                        ; Flags
        ;; Passing EEType in ecx

        ;; void* RhpGcAlloc(EEType *pEEType, UInt32 uFlags, UIntNative cbSize, void * pTransitionFrame)
        call        RhpGcAlloc

        ;; Set the new object's EEType pointer on success.
        test        eax, eax
        jz          NewFast_OOM
        mov         [eax + OFFSETOF__Object__m_pEEType], esi

        ;; If the object is bigger than RH_LARGE_OBJECT_SIZE, we must publish it to the BGC
        mov         edx, [esi + OFFSETOF__EEType__m_uBaseSize]
        cmp         edx, RH_LARGE_OBJECT_SIZE
        jb          NewFast_SkipPublish
        mov         ecx, eax            ;; ecx: object
                                        ;; edx: already contains object size
        call        RhpPublishObject    ;; eax: this function returns the object that was passed-in
NewFast_SkipPublish: 

        POP_COOP_PINVOKE_FRAME

        pop         ebp
        ret

NewFast_OOM:
        ;; This is the failure path. We're going to tail-call to a managed helper that will throw
        ;; an out of memory exception that the caller of this allocator understands.

        mov         eax, esi            ; Preserve EEType pointer over POP_COOP_PINVOKE_FRAME

        POP_COOP_PINVOKE_FRAME

        ;; Cleanup our ebp frame
        pop         ebp

        mov         ecx, eax            ; EEType pointer
        xor         edx, edx            ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation

FASTCALL_ENDFUNC

;; Allocate non-array object with finalizer.
;;  ECX == EEType
FASTCALL_FUNC   RhpNewFinalizable, 4
        ;; Create EBP frame.
        push        ebp
        mov         ebp, esp

        PUSH_COOP_PINVOKE_FRAME edx

        ;; Preserve EEType in ESI
        mov         esi, ecx

        ;; Push alloc helper arguments
        push        edx                                             ; transition frame
        push        dword ptr [ecx + OFFSETOF__EEType__m_uBaseSize] ; Size
        mov         edx, GC_ALLOC_FINALIZE                          ; Flags
        ;; Passing EEType in ecx

        ;; void* RhpGcAlloc(EEType *pEEType, UInt32 uFlags, UIntNative cbSize, void * pTransitionFrame)
        call        RhpGcAlloc

        ;; Set the new object's EEType pointer on success.
        test        eax, eax
        jz          NewFinalizable_OOM
        mov         [eax + OFFSETOF__Object__m_pEEType], esi

        ;; If the object is bigger than RH_LARGE_OBJECT_SIZE, we must publish it to the BGC
        mov         edx, [esi + OFFSETOF__EEType__m_uBaseSize]
        cmp         edx, RH_LARGE_OBJECT_SIZE
        jb          NewFinalizable_SkipPublish
        mov         ecx, eax            ;; ecx: object
                                        ;; edx: already contains object size
        call        RhpPublishObject    ;; eax: this function returns the object that was passed-in
NewFinalizable_SkipPublish: 

        POP_COOP_PINVOKE_FRAME

        ;; Collapse EBP frame and return
        pop         ebp
        ret
        
NewFinalizable_OOM:
        ;; This is the failure path. We're going to tail-call to a managed helper that will throw
        ;; an out of memory exception that the caller of this allocator understands.

        mov         eax, esi            ; Preserve EEType pointer over POP_COOP_PINVOKE_FRAME

        POP_COOP_PINVOKE_FRAME

        ;; Cleanup our ebp frame
        pop         ebp
        
        mov         ecx, eax            ; EEType pointer
        xor         edx, edx            ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation
        
FASTCALL_ENDFUNC

;; Allocate a new string.
;;  ECX == EEType
;;  EDX == element count
FASTCALL_FUNC   RhNewString, 8

        push        ecx
        push        edx

        ;; Make sure computing the aligned overall allocation size won't overflow
        cmp         edx, ((0FFFFFFFFh - STRING_BASE_SIZE - 3) / STRING_COMPONENT_SIZE)
        ja          StringSizeOverflow

        ; Compute overall allocation size (align(base size + (element size * elements), 4)).
        lea         eax, [(edx * STRING_COMPONENT_SIZE) + (STRING_BASE_SIZE + 3)]
        and         eax, -4

        ; ECX == EEType
        ; EAX == allocation size
        ; EDX == scratch

        INLINE_GETTHREAD    edx, ecx        ; edx = GetThread(), TRASHES ecx

        ; ECX == scratch
        ; EAX == allocation size
        ; EDX == thread

        mov         ecx, eax
        add         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
        jc          StringAllocContextOverflow
        cmp         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_limit]
        ja          StringAllocContextOverflow

        ; ECX == allocation size
        ; EAX == new alloc ptr
        ; EDX == thread

        ; set the new alloc pointer
        mov         [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr], eax

        ; calc the new object pointer
        sub         eax, ecx

        pop         edx
        pop         ecx

        ; set the new object's EEType pointer and element count
        mov         [eax + OFFSETOF__Object__m_pEEType], ecx
        mov         [eax + OFFSETOF__String__m_Length], edx
        ret

StringAllocContextOverflow:
        ; ECX == string size
        ;   original ECX pushed
        ;   original EDX pushed

        ; Re-push original ECX
        push        [esp + 4]

        ; Create EBP frame.
        mov         [esp + 8], ebp
        lea         ebp, [esp + 8]

        PUSH_COOP_PINVOKE_FRAME edx

        ; Preserve the string size in edi
        mov         edi, ecx

        ; Get the EEType and put it in ecx.
        mov         ecx, dword ptr [ebp - 8]

        ; Push alloc helper arguments (thread, size, flags, EEType).
        push        edx                                             ; transition frame
        push        edi                                             ; Size
        xor         edx, edx                                        ; Flags
        ;; Passing EEType in ecx

        ;; void* RhpGcAlloc(EEType *pEEType, UInt32 uFlags, UIntNative cbSize, void * pTransitionFrame)
        call        RhpGcAlloc

        ; Set the new object's EEType pointer and length on success.
        test        eax, eax
        jz          StringOutOfMemoryWithFrame

        mov         ecx, [ebp - 8]
        mov         edx, [ebp - 4]
        mov         [eax + OFFSETOF__Object__m_pEEType], ecx
        mov         [eax + OFFSETOF__String__m_Length], edx

        ;; If the object is bigger than RH_LARGE_OBJECT_SIZE, we must publish it to the BGC
        cmp         edi, RH_LARGE_OBJECT_SIZE
        jb          NewString_SkipPublish
        mov         ecx, eax            ;; ecx: object
        mov         edx, edi            ;; edx: object size
        call        RhpPublishObject    ;; eax: this function returns the object that was passed-in
NewString_SkipPublish: 

        POP_COOP_PINVOKE_FRAME
        add         esp, 8          ; pop ecx / edx
        pop         ebp
        ret

StringOutOfMemoryWithFrame:
        ; This is the OOM failure path. We're going to tail-call to a managed helper that will throw
        ; an out of memory exception that the caller of this allocator understands.

        mov         eax, [ebp - 8]  ; Preserve EEType pointer over POP_COOP_PINVOKE_FRAME

        POP_COOP_PINVOKE_FRAME
        add         esp, 8          ; pop ecx / edx
        pop         ebp             ; restore ebp

        mov         ecx, eax        ; EEType pointer
        xor         edx, edx        ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation

StringSizeOverflow:
        ;; We get here if the size of the final string object can't be represented as an unsigned 
        ;; 32-bit value. We're going to tail-call to a managed helper that will throw
        ;; an OOM exception that the caller of this allocator understands.

        add         esp, 8          ; pop ecx / edx

        ;; ecx holds EEType pointer already
        xor         edx, edx            ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation

FASTCALL_ENDFUNC


;; Allocate one dimensional, zero based array (SZARRAY).
;;  ECX == EEType
;;  EDX == element count
FASTCALL_FUNC   RhpNewArray, 8

        push        ecx
        push        edx

        ; Compute overall allocation size (align(base size + (element size * elements), 4)).
        ; if the element count is <= 0x10000, no overflow is possible because the component size is
        ; <= 0xffff, and thus the product is <= 0xffff0000, and the base size for the worst case
        ; (32 dimensional MdArray) is less than 0xffff.
        movzx       eax, word ptr [ecx + OFFSETOF__EEType__m_usComponentSize]
        cmp         edx,010000h
        ja          ArraySizeBig
        mul         edx
        add         eax, [ecx + OFFSETOF__EEType__m_uBaseSize]
        add         eax, 3
ArrayAlignSize:
        and         eax, -4

        ; ECX == EEType
        ; EAX == array size
        ; EDX == scratch

        INLINE_GETTHREAD    edx, ecx        ; edx = GetThread(), TRASHES ecx

        ; ECX == scratch
        ; EAX == array size
        ; EDX == thread

        mov         ecx, eax
        add         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
        jc          ArrayAllocContextOverflow
        cmp         eax, [edx + OFFSETOF__Thread__m_alloc_context__alloc_limit]
        ja          ArrayAllocContextOverflow

        ; ECX == array size
        ; EAX == new alloc ptr
        ; EDX == thread

        ; set the new alloc pointer
        mov         [edx + OFFSETOF__Thread__m_alloc_context__alloc_ptr], eax

        ; calc the new object pointer
        sub         eax, ecx

        pop         edx
        pop         ecx

        ; set the new object's EEType pointer and element count
        mov         [eax + OFFSETOF__Object__m_pEEType], ecx
        mov         [eax + OFFSETOF__Array__m_Length], edx
        ret

ArraySizeBig:
        ; Compute overall allocation size (align(base size + (element size * elements), 4)).
        ; if the element count is negative, it's an overflow, otherwise it's out of memory
        cmp         edx, 0
        jl          ArraySizeOverflow
        mul         edx
        jc          ArrayOutOfMemoryNoFrame
        add         eax, [ecx + OFFSETOF__EEType__m_uBaseSize]
        jc          ArrayOutOfMemoryNoFrame
        add         eax, 3
        jc          ArrayOutOfMemoryNoFrame
        jmp         ArrayAlignSize

ArrayAllocContextOverflow:
        ; ECX == array size
        ;   original ECX pushed
        ;   original EDX pushed

        ; Re-push original ECX
        push        [esp + 4]

        ; Create EBP frame.
        mov         [esp + 8], ebp
        lea         ebp, [esp + 8]

        PUSH_COOP_PINVOKE_FRAME edx

        ; Preserve the array size in edi
        mov         edi, ecx

        ; Get the EEType and put it in ecx.
        mov         ecx, dword ptr [ebp - 8]

        ; Push alloc helper arguments (thread, size, flags, EEType).
        push        edx                                             ; transition frame
        push        edi                                             ; Size
        xor         edx, edx                                        ; Flags
        ;; Passing EEType in ecx

        ;; void* RhpGcAlloc(EEType *pEEType, UInt32 uFlags, UIntNative cbSize, void * pTransitionFrame)
        call        RhpGcAlloc

        ; Set the new object's EEType pointer and length on success.
        test        eax, eax
        jz          ArrayOutOfMemoryWithFrame

        mov         ecx, [ebp - 8]
        mov         edx, [ebp - 4]
        mov         [eax + OFFSETOF__Object__m_pEEType], ecx
        mov         [eax + OFFSETOF__Array__m_Length], edx

        ;; If the object is bigger than RH_LARGE_OBJECT_SIZE, we must publish it to the BGC
        cmp         edi, RH_LARGE_OBJECT_SIZE
        jb          NewArray_SkipPublish
        mov         ecx, eax            ;; ecx: object
        mov         edx, edi            ;; edx: object size
        call        RhpPublishObject    ;; eax: this function returns the object that was passed-in
NewArray_SkipPublish: 

        POP_COOP_PINVOKE_FRAME
        add         esp, 8          ; pop ecx / edx
        pop         ebp
        ret

ArrayOutOfMemoryWithFrame:
        ; This is the OOM failure path. We're going to tail-call to a managed helper that will throw
        ; an out of memory exception that the caller of this allocator understands.

        mov         eax, [ebp - 8]  ; Preserve EEType pointer over POP_COOP_PINVOKE_FRAME

        POP_COOP_PINVOKE_FRAME
        add         esp, 8          ; pop ecx / edx
        pop         ebp             ; restore ebp

        mov         ecx, eax        ; EEType pointer
        xor         edx, edx        ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation

ArrayOutOfMemoryNoFrame:
        add         esp, 8          ; pop ecx / edx

        ; ecx holds EEType pointer already
        xor         edx, edx        ; Indicate that we should throw OOM.
        jmp         RhExceptionHandling_FailedAllocation

ArraySizeOverflow:
        ; We get here if the size of the final array object can't be represented as an unsigned 
        ; 32-bit value. We're going to tail-call to a managed helper that will throw
        ; an overflow exception that the caller of this allocator understands.

        add         esp, 8          ; pop ecx / edx

        ; ecx holds EEType pointer already
        mov         edx, 1          ; Indicate that we should throw OverflowException
        jmp         RhExceptionHandling_FailedAllocation

FASTCALL_ENDFUNC

        end