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