-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AllocFast.asm
247 lines (173 loc) · 7.45 KB
/
AllocFast.asm
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
;; Licensed to the .NET Foundation under one or more agreements.
;; The .NET Foundation licenses this file to you under the MIT license.
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.
;; RCX == MethodTable
LEAF_ENTRY RhpNewFast, _TEXT
;; rdx = GetThread(), TRASHES rax
INLINE_GETTHREAD rdx, rax
;;
;; rcx contains MethodTable pointer
;;
mov r8d, [rcx + OFFSETOF__MethodTable__m_uBaseSize]
;;
;; eax: base size
;; rcx: MethodTable pointer
;; rdx: Thread pointer
;;
mov rax, [rdx + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
add r8, rax
cmp r8, [rdx + OFFSETOF__Thread__m_alloc_context__alloc_limit]
ja RhpNewFast_RarePath
;; set the new alloc pointer
mov [rdx + OFFSETOF__Thread__m_alloc_context__alloc_ptr], r8
;; set the new object's MethodTable pointer
mov [rax], rcx
ret
RhpNewFast_RarePath:
xor edx, edx
jmp RhpNewObject
LEAF_END RhpNewFast, _TEXT
;; Allocate non-array object with finalizer
;; RCX == MethodTable
LEAF_ENTRY RhpNewFinalizable, _TEXT
mov edx, GC_ALLOC_FINALIZE
jmp RhpNewObject
LEAF_END RhpNewFinalizable, _TEXT
;; Allocate non-array object
;; RCX == MethodTable
;; EDX == alloc flags
NESTED_ENTRY RhpNewObject, _TEXT
PUSH_COOP_PINVOKE_FRAME r9
END_PROLOGUE
; R9: transition frame
;; Preserve the MethodTable in RSI
mov rsi, rcx
xor r8d, r8d ; numElements
;; Call the rest of the allocation helper.
;; void* RhpGcAlloc(MethodTable *pEEType, uint32_t uFlags, uintptr_t numElements, void * pTransitionFrame)
call RhpGcAlloc
test rax, rax
jz NewOutOfMemory
POP_COOP_PINVOKE_FRAME
ret
NewOutOfMemory:
;; 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 rcx, rsi ; MethodTable pointer
xor edx, edx ; Indicate that we should throw OOM.
POP_COOP_PINVOKE_FRAME
jmp RhExceptionHandling_FailedAllocation
NESTED_END RhpNewObject, _TEXT
;; Allocate a string.
;; RCX == MethodTable
;; EDX == character/element count
LEAF_ENTRY RhNewString, _TEXT
; we want to limit the element count to the non-negative 32-bit int range
cmp rdx, MAX_STRING_LENGTH
ja StringSizeOverflow
; Compute overall allocation size (align(base size + (element size * elements), 8)).
lea rax, [(rdx * STRING_COMPONENT_SIZE) + (STRING_BASE_SIZE + 7)]
and rax, -8
; rax == string size
; rcx == MethodTable
; rdx == element count
INLINE_GETTHREAD r10, r8
mov r8, rax
add rax, [r10 + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
jc RhpNewArrayRare
; rax == new alloc ptr
; rcx == MethodTable
; rdx == element count
; r8 == array size
; r10 == thread
cmp rax, [r10 + OFFSETOF__Thread__m_alloc_context__alloc_limit]
ja RhpNewArrayRare
mov [r10 + OFFSETOF__Thread__m_alloc_context__alloc_ptr], rax
; calc the new object pointer
sub rax, r8
mov [rax + OFFSETOF__Object__m_pEEType], rcx
mov [rax + OFFSETOF__String__m_Length], edx
ret
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.
; rcx holds MethodTable pointer already
xor edx, edx ; Indicate that we should throw OOM.
jmp RhExceptionHandling_FailedAllocation
LEAF_END RhNewString, _TEXT
;; Allocate one dimensional, zero based array (SZARRAY).
;; RCX == MethodTable
;; EDX == element count
LEAF_ENTRY RhpNewArray, _TEXT
; we want to limit the element count to the non-negative 32-bit int range
cmp rdx, 07fffffffh
ja ArraySizeOverflow
; save element count
mov r8, rdx
; Compute overall allocation size (align(base size + (element size * elements), 8)).
movzx eax, word ptr [rcx + OFFSETOF__MethodTable__m_usComponentSize]
mul rdx
mov edx, [rcx + OFFSETOF__MethodTable__m_uBaseSize]
add rax, rdx
add rax, 7
and rax, -8
mov rdx, r8
; rax == array size
; rcx == MethodTable
; rdx == element count
INLINE_GETTHREAD r10, r8
mov r8, rax
add rax, [r10 + OFFSETOF__Thread__m_alloc_context__alloc_ptr]
jc RhpNewArrayRare
; rax == new alloc ptr
; rcx == MethodTable
; rdx == element count
; r8 == array size
; r10 == thread
cmp rax, [r10 + OFFSETOF__Thread__m_alloc_context__alloc_limit]
ja RhpNewArrayRare
mov [r10 + OFFSETOF__Thread__m_alloc_context__alloc_ptr], rax
; calc the new object pointer
sub rax, r8
mov [rax + OFFSETOF__Object__m_pEEType], rcx
mov [rax + OFFSETOF__Array__m_Length], edx
ret
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.
; rcx holds MethodTable pointer already
mov edx, 1 ; Indicate that we should throw OverflowException
jmp RhExceptionHandling_FailedAllocation
LEAF_END RhpNewArray, _TEXT
NESTED_ENTRY RhpNewArrayRare, _TEXT
; rcx == MethodTable
; rdx == element count
PUSH_COOP_PINVOKE_FRAME r9
END_PROLOGUE
; r9: transition frame
; Preserve the MethodTable in RSI
mov rsi, rcx
; passing MethodTable in rcx
mov r8, rdx ; numElements
xor rdx, rdx ; uFlags
; passing pTransitionFrame in r9
; Call the rest of the allocation helper.
; void* RhpGcAlloc(MethodTable *pEEType, uint32_t uFlags, uintptr_t numElements, void * pTransitionFrame)
call RhpGcAlloc
test rax, rax
jz ArrayOutOfMemory
POP_COOP_PINVOKE_FRAME
ret
ArrayOutOfMemory:
;; 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 rcx, rsi ; MethodTable pointer
xor edx, edx ; Indicate that we should throw OOM.
POP_COOP_PINVOKE_FRAME
jmp RhExceptionHandling_FailedAllocation
NESTED_END RhpNewArrayRare, _TEXT
END