Subversion Repositories Kolibri OS

Rev

Rev 1635 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1333 diamond 1
; Tests of malloc()/free() from the kernel heap.
2
; This file is not included in the kernel, it is just test application.
3
use32
4
	db	'MENUET01'
5
	dd	1, start, i_end, mem, mem, 0, 0
6
 
7
start:
8
; Zero-initialize uglobals (as in kernel at boot)
9
	mov	ecx, (zeroend - zerostart + 3) / 4
10
	xor	eax, eax
11
	mov	edi, zerostart
12
	rep	stosd
13
; Initialize small heap (as in kernel at boot)
14
	call	init_malloc
15
; Run tests
16
	call	run_test1
17
	call	run_test2
18
	call	run_test3
19
; All is OK, return
20
	or	eax, -1
21
	int	0x40
22
 
23
run_test1:
24
; basic test
25
	mov	eax, 1
26
	call	malloc_with_test
27
	mov	byte [eax], 0xDD
28
	mov	esi, eax
29
	mov	eax, 1
30
	call	malloc_with_test
31
	cmp	byte [esi], 0xDD
32
	jnz	memory_destroyed
33
	mov	byte [eax], 0xEE
34
	xchg	eax, esi
35
	call	free
36
	cmp	byte [esi], 0xEE
37
	jnz	memory_destroyed
38
	xchg	eax, esi
39
	call	free
40
	ret
41
 
42
run_test2:
43
	ret
44
 
45
run_test3:
46
; 1024000 times run random operation.
47
; Randomly select malloc(random size from 1 to 1023)
48
; or free(random of previously allocated areas)
49
	mov	edi, 0x12345678
50
	xor	esi, esi	; 0 areas allocated
51
	mov	ebx, 1024000
52
.loop:
53
	imul	edi, 1103515245
54
	add	edi, 12345
55
	mov	eax, edi
56
	shr	eax, 16
57
	test	ebx, 64
58
	jz	.prefer_free
59
.prefer_malloc:
60
	test	eax, 3
61
	jz	.free
62
	jmp	@f
63
.prefer_free:
64
	test	eax, 3
65
	jnz	.free
66
@@:
67
	shr	eax, 2
68
	and	eax, 1023
69
	jz	.loop
70
	push	ebx
71
	push	eax
72
;	mov	ecx, [saved_state_num]
73
;	mov	[saved_state+ecx*8], eax
74
	call	malloc_with_test
75
;	mov	ecx, [saved_state_num]
76
;	mov	[saved_state+ecx*8+4], eax
77
;	inc	[saved_state_num]
78
	pop	ecx
79
	pop	ebx
80
	inc	esi
81
	push	ecx eax
82
	push	edi
83
	mov	edi, eax
84
	mov	eax, esi
85
	rep	stosb
86
	pop	edi
87
	jmp	.common
88
.free:
89
	test	esi, esi
90
	jz	.loop
91
	xor	edx, edx
92
	div	esi
93
	sub	edx, esi
94
	neg	edx
95
	dec	edx
96
	mov	eax, [esp+edx*8]
97
;	mov	ecx, [saved_state_num]
98
;	mov	[saved_state+ecx*8], -1
99
;	mov	[saved_state+ecx*8+4], eax
100
;	inc	[saved_state_num]
101
	mov	ecx, [esp+edx*8+4]
102
	push	edi eax
103
	mov	edi, eax
104
	mov	al, [edi]
105
	repz	scasb
106
	jnz	memory_destroyed
107
	pop	eax edi
108
	push	ebx edx
109
	call	free
110
	pop	edx ebx
111
	dec	esi
112
	pop	eax ecx
113
	push	edi
114
	lea	edi, [esp+4]
115
@@:
116
	dec	edx
117
	js	@f
118
	xchg	eax, [edi]
119
	xchg	ecx, [edi+4]
120
	add	edi, 8
121
	jmp	@b
122
@@:
123
	pop	edi
124
.common:
125
	dec	ebx
126
	jnz	.loop
127
@@:
128
	dec	esi
129
	js	@f
130
	pop	eax ecx
131
	call	free
132
	jmp	@b
133
@@:
134
	ret
135
 
136
malloc_with_test:
137
; calls malloc() and checks returned value
138
	call	malloc
139
	test	eax, eax
140
	jz	generic_malloc_fail
141
	call	check_mutex
142
	call	check_range
143
	ret
144
 
145
; Stubs for kernel procedures used by heap code
146
wait_mutex:
147
	inc	dword [ebx]
148
	ret
149
 
150
kernel_alloc:
151
	cmp	dword [esp+4], bufsize
152
	jnz	error1
153
	mov	eax, buffer
154
	ret	4
155
 
156
macro $Revision [args]
157
{
158
}
159
 
160
; Error handlers
161
error1:
162
	mov	eax, 1
163
	jmp	error_with_code
164
 
165
generic_malloc_fail:
166
	mov	eax, 2
167
	jmp	error_with_code
168
 
169
check_mutex:
170
	cmp	[mst.mutex], 0
171
	jnz	@f
172
	ret
173
@@:
174
	mov	eax, 3
175
	jmp	error_with_code
176
 
177
check_range:
178
	cmp	eax, buffer
179
	jb	@f
180
	cmp	eax, buffer+bufsize
181
	jae	@f
182
	ret
183
@@:
184
	mov	eax, 4
185
	jmp	error_with_code
186
 
187
memory_destroyed:
188
	mov	eax, 5
189
	jmp	error_with_code
190
 
191
error_with_code:
192
	mov	edx, saved_state_num
193
; eax = error code
194
; 1 signals error in testing code (wrong bufsize)
195
; 2 = malloc() returned NULL
196
; 3 = mutex not released
197
; 4 = weird returned value from malloc()
198
; 5 = memory destroyed by malloc() or free()
199
	int3	; simplest way to report error
200
	jmp	$-1	; just in case
201
 
202
; Include main heap code
203
include '../proc32.inc'
204
include '../const.inc'
205
include 'malloc.inc'
206
 
207
i_end:
208
 
209
align 4
210
zerostart:
211
mst	MEM_STATE
212
 
213
align 16
214
bufsize = 0x40000	; change if malloc.inc changes
215
buffer	rb	bufsize
216
zeroend:
217
 
218
saved_state_num	dd	?
219
saved_state	rd	0x10000
220
 
221
align 4
222
	rb	0x10000	; for stack
223
mem: