Subversion Repositories Kolibri OS

Rev

Rev 589 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 589 Rev 1122
1
CHECK_FOR_LEAKS = 0
1
CHECK_FOR_LEAKS = 0
2
if CHECK_FOR_LEAKS
2
if CHECK_FOR_LEAKS
3
uglobal
3
uglobal
4
allocatedregions rd 1024
4
allocatedregions rd 1024
5
endg
5
endg
6
iglobal
6
iglobal
7
numallocatedregions dd 0
7
numallocatedregions dd 0
8
endg
8
endg
9
end if
9
end if
10
pgalloc:
10
pgalloc:
11
; in: ecx=size
11
; in: ecx=size
12
; out: eax=pointer or NULL
12
; out: eax=pointer or NULL
13
        push    ebx
13
        push    ebx
14
        push    68
14
        push    68
15
        pop     eax
15
        pop     eax
16
        push    12
16
        push    12
17
        pop     ebx
17
        pop     ebx
18
        int     0x40
18
        int     0x40
19
if CHECK_FOR_LEAKS
19
if CHECK_FOR_LEAKS
20
        test    eax, eax
20
        test    eax, eax
21
        jz      .no
21
        jz      .no
22
.b:
22
.b:
23
        mov     ebx, [numallocatedregions]
23
        mov     ebx, [numallocatedregions]
24
        cmp     ebx, 1024
24
        cmp     ebx, 1024
25
        jb      @f
25
        jb      @f
26
        int3
26
        int3
27
        jmp     $
27
        jmp     $
28
@@:
28
@@:
29
        mov     [allocatedregions+ebx*4], eax
29
        mov     [allocatedregions+ebx*4], eax
30
        inc     [numallocatedregions]
30
        inc     [numallocatedregions]
31
.no:
31
.no:
32
end if
32
end if
33
        pop     ebx
33
        pop     ebx
34
        ret
34
        ret
35
 
35
 
36
pgfree:
36
pgfree:
37
; in: ecx=pointer
37
; in: ecx=pointer
38
; destroys eax
38
; destroys eax
39
if CHECK_FOR_LEAKS
39
if CHECK_FOR_LEAKS
40
        jecxz   .no
40
        jecxz   .no
41
        mov     eax, [numallocatedregions]
41
        mov     eax, [numallocatedregions]
42
@@:
42
@@:
43
        dec     eax
43
        dec     eax
44
        js      .a
44
        js      .a
45
        cmp     [allocatedregions+eax*4], ecx
45
        cmp     [allocatedregions+eax*4], ecx
46
        jnz     @b
46
        jnz     @b
47
        jmp     @f
47
        jmp     @f
48
.a:
48
.a:
49
        int3
49
        int3
50
        jmp     $
50
        jmp     $
51
@@:
51
@@:
52
        dec     [numallocatedregions]
52
        dec     [numallocatedregions]
53
@@:
53
@@:
54
        cmp     eax, [numallocatedregions]
54
        cmp     eax, [numallocatedregions]
55
        jae     @f
55
        jae     @f
56
        push    [allocatedregions+eax*4+4]
56
        push    [allocatedregions+eax*4+4]
57
        pop     [allocatedregions+eax*4]
57
        pop     [allocatedregions+eax*4]
58
        inc     eax
58
        inc     eax
59
        jmp     @b
59
        jmp     @b
60
@@:
60
@@:
61
.no:
61
.no:
62
end if
62
end if
63
        push    ebx
63
        push    ebx
64
        push    68
64
        push    68
65
        pop     eax
65
        pop     eax
66
        push    13
66
        push    13
67
        pop     ebx
67
        pop     ebx
68
        int     0x40
68
        int     0x40
69
        pop     ebx
69
        pop     ebx
70
        ret
70
        ret
71
 
71
 
72
pgrealloc:
72
pgrealloc:
73
; in: ecx=size, edx=pointer
73
; in: ecx=size, edx=pointer
74
; out: eax=pointer
74
; out: eax=pointer
75
        push    ebx
75
        push    ebx
76
        push    68
76
        push    68
77
        pop     eax
77
        pop     eax
78
        push    20
78
        push    20
79
        pop     ebx
79
        pop     ebx
80
        int     0x40
80
        int     0x40
81
if CHECK_FOR_LEAKS
81
if CHECK_FOR_LEAKS
82
        test    edx, edx
82
        test    edx, edx
83
        jz      pgalloc.b
83
        jz      pgalloc.b
84
        test    eax, eax
84
        test    eax, eax
85
        jz      .no
85
        jz      .no
86
        cmp     eax, edx
86
        cmp     eax, edx
87
        jz      .no
87
        jz      .no
88
        xor     ebx, ebx
88
        xor     ebx, ebx
89
@@:
89
@@:
90
        cmp     ebx, [numallocatedregions]
90
        cmp     ebx, [numallocatedregions]
91
        jae     .a
91
        jae     .a
92
        cmp     [allocatedregions+ebx*4], edx
92
        cmp     [allocatedregions+ebx*4], edx
93
        jz      @f
93
        jz      @f
94
        inc     ebx
94
        inc     ebx
95
        jmp     @b
95
        jmp     @b
96
@@:
96
@@:
97
        mov     [allocatedregions+ebx*4], eax
97
        mov     [allocatedregions+ebx*4], eax
98
        jmp     .no
98
        jmp     .no
99
.a:
99
.a:
100
        int3
100
        int3
101
        jmp     $
101
        jmp     $
102
.no:
102
.no:
103
end if
103
end if
104
        pop     ebx
104
        pop     ebx
105
        ret
105
        ret
106
 
106
 
107
xpgalloc:
107
xpgalloc:
108
; in: ecx=size
108
; in: ecx=size
109
; out: eax=pointer or NULL
109
; out: eax=pointer or NULL
110
        call    pgalloc
110
        call    pgalloc
111
.common:
111
.common:
112
        test    eax, eax
112
        test    eax, eax
113
        jnz     @f
113
        jnz     @f
114
        call    SayNoMem
114
        call    SayNoMem
115
        xor     eax, eax
115
        xor     eax, eax
116
@@:
116
@@:
117
        ret
117
        ret
118
 
118
 
119
xpgrealloc:
119
xpgrealloc:
120
; in: edx=pointer, ecx=new size
120
; in: edx=pointer, ecx=new size
121
; out: eax=pointer or NULL
121
; out: eax=pointer or NULL
122
        call    pgrealloc
122
        call    pgrealloc
123
        jmp     xpgalloc.common
123
        jmp     xpgalloc.common
124
 
124
 
125
getfreemem:
125
getfreemem:
126
; out: eax=size of free RAM in Kb
126
; out: eax=size of free RAM in Kb
127
        push    ebx
127
        push    ebx
128
        push    18
128
        push    18
129
        pop     eax
129
        pop     eax
130
        push    16
130
        push    16
131
        pop     ebx
131
        pop     ebx
132
        int     0x40
132
        int     0x40
133
        pop     ebx
133
        pop     ebx
134
        ret
134
        ret
135
 
135
 
136
get_error_msg:
136
get_error_msg:
137
; in: eax=error code
137
; in: eax=error code
138
; out: eax=pointer to message (in static buffer)
138
; out: eax=pointer to message (in static buffer)
139
        push    esi edi
139
        push    esi edi
140
        mov     edi, error_msg
140
        mov     edi, error_msg
141
        cmp     eax, 11
141
        cmp     eax, 11
142
        ja      .no1
142
        ja      .no1
143
        mov     esi, [errors1+eax*4]
143
        mov     esi, [errors1+eax*4]
144
        jmp     .copy
144
        jmp     .copy
145
.no1:
145
.no1:
146
        cmp     eax, 30
146
        cmp     eax, 30
147
        jb      .no2
147
        jb      .no2
148
        cmp     eax, 32
148
        cmp     eax, 32
149
        ja      .no2
149
        ja      .no2
150
        mov     esi, [errors2+(eax-30)*4]
150
        mov     esi, [errors2+(eax-30)*4]
151
.copy:
151
.copy:
152
        lodsb
152
        lodsb
153
        stosb
153
        stosb
154
        test    al, al
154
        test    al, al
155
        jnz     .copy
155
        jnz     .copy
156
.ret:
156
.ret:
157
        mov     eax, error_msg
157
        mov     eax, error_msg
158
        pop     edi esi
158
        pop     edi esi
159
        ret
159
        ret
160
.no2:
160
.no2:
161
        mov     esi, aUnknownError
161
        mov     esi, aUnknownError
162
        push    eax
162
        push    eax
163
@@:
163
@@:
164
        lodsb
164
        lodsb
165
        stosb
165
        stosb
166
        test    al, al
166
        test    al, al
167
        jnz     @b
167
        jnz     @b
168
        pop     eax
168
        pop     eax
169
        dec     edi
169
        dec     edi
170
        push    edx ecx
170
        push    edx ecx
-
 
171
        push    -'0'
171
        test    eax, eax
172
        test    eax, eax
172
        jns     @f
173
        jns     @f
173
        mov     byte [edi], '-'
174
        mov     byte [edi], '-'
174
        inc     edi
175
        inc     edi
175
        neg     eax
176
        neg     eax
176
@@:
177
@@:
177
        xor     edx, edx
178
        xor     edx, edx
178
        mov     ecx, 10
179
        mov     ecx, 10
179
        div     ecx
180
        div     ecx
180
        add     edx, '0'
-
 
181
        mov     byte [edi], dl
-
 
182
        inc     edi
181
        push    edx
183
        test    eax, eax
182
        test    eax, eax
184
        jnz     @b
183
        jnz     @b
-
 
184
@@:
185
        pop     ecx edx
185
        pop     eax
-
 
186
        add     al, '0'
186
        stosb
187
        stosb
-
 
188
        jnz     @b
-
 
189
        pop     ecx edx
187
        jmp     .ret
190
        jmp     .ret
188
 
191
 
189
libini_alloc:
192
libini_alloc:
190
        push    ecx
193
        push    ecx
191
        mov     ecx, [esp+8]
194
        mov     ecx, [esp+8]
192
        call    xpgalloc
195
        call    xpgalloc
193
        pop     ecx
196
        pop     ecx
194
        ret     4
197
        ret     4
195
libini_free:
198
libini_free:
196
        push    ecx
199
        push    ecx
197
        mov     ecx, [esp+8]
200
        mov     ecx, [esp+8]
198
        call    pgfree
201
        call    pgfree
199
        pop     ecx
202
        pop     ecx
200
        ret     4
203
        ret     4
201
libini_realloc:
204
libini_realloc:
202
        push    ecx edx
205
        push    ecx edx
203
        mov     edx, [esp+8+4]
206
        mov     edx, [esp+8+4]
204
        mov     ecx, [esp+8+8]
207
        mov     ecx, [esp+8+8]
205
        call    xpgrealloc
208
        call    xpgrealloc
206
        pop     edx ecx
209
        pop     edx ecx
207
        ret     8
210
        ret     8
208
 
211
 
209
libini_dllload:
212
libini_dllload:
210
        push    esi
213
        push    esi
211
        mov     esi, [esp+8]
214
        mov     esi, [esp+8]
212
.lib:
215
.lib:
213
        lodsd
216
        lodsd
214
        test    eax, eax
217
        test    eax, eax
215
        jz      .ret
218
        jz      .ret
216
        push    eax
219
        push    eax
217
        lodsd
220
        lodsd
218
        xchg    esi, [esp]
221
        xchg    esi, [esp]
219
        xor     ebp, ebp        ; no version control
222
        xor     ebp, ebp        ; no version control
220
        call    load_dll_and_import
223
        call    load_dll_and_import
221
        pop     esi
224
        pop     esi
222
        test    eax, eax
225
        test    eax, eax
223
        jz      .lib
226
        jz      .lib
224
.ret:
227
.ret:
225
        pop     esi
228
        pop     esi
226
        ret     4
229
        ret     4
227
 
230
 
228
load_dll_and_import:
231
load_dll_and_import:
229
        cmp     byte [eax], '/'
232
        cmp     byte [eax], '/'
230
        jz      .do
233
        jz      .do
231
        push    esi
234
        push    esi
232
        mov     edi, saved_file_name
235
        mov     edi, saved_file_name
233
        push    edi
236
        push    edi
234
        mov     esi, standard_dll_path
237
        mov     esi, standard_dll_path
235
        mov     ecx, standard_dll_path_size
238
        mov     ecx, standard_dll_path_size
236
        rep     movsb
239
        rep     movsb
237
        mov     esi, eax
240
        mov     esi, eax
238
        mov     ecx, 1024-standard_dll_path_size
241
        mov     ecx, 1024-standard_dll_path_size
239
@@:
242
@@:
240
        lodsb
243
        lodsb
241
        stosb
244
        stosb
242
        test    al, al
245
        test    al, al
243
        loopnz  @b
246
        loopnz  @b
244
        pop     eax
247
        pop     eax
245
        pop     esi
248
        pop     esi
246
        jz      .do
249
        jz      .do
247
.big:
250
.big:
248
        push    esi
251
        push    esi
249
        mov     edi, aFileNameTooBig
252
        mov     edi, aFileNameTooBig
250
.sayerr:
253
.sayerr:
251
        push    dword aCannotLoadDLL
254
        push    dword aCannotLoadDLL
252
        push    edi
255
        push    edi
253
        mov     eax, esp
256
        mov     eax, esp
254
        push    dword aOk
257
        push    dword aOk
255
        push    esp
258
        push    esp
256
        push    1
259
        push    1
257
        push    eax
260
        push    eax
258
        push    3
261
        push    3
259
        call    SayErr
262
        call    SayErr
260
        add     esp, 16
263
        add     esp, 16
261
        xor     eax, eax
264
        xor     eax, eax
262
        inc     eax
265
        inc     eax
263
        ret
266
        ret
264
.do:
267
.do:
265
        push    eax
268
        push    eax
266
        mov     ecx, eax
269
        mov     ecx, eax
267
        push    68
270
        push    68
268
        pop     eax
271
        pop     eax
269
        push    19
272
        push    19
270
        pop     ebx
273
        pop     ebx
271
        int     0x40
274
        int     0x40
272
        mov     edi, aInvalidDLL
275
        mov     edi, aInvalidDLL
273
        test    eax, eax
276
        test    eax, eax
274
        jz      .sayerr
277
        jz      .sayerr
275
        mov     edx, eax
278
        mov     edx, eax
276
        cmp     ebp, -1
279
        cmp     ebp, -1
277
        jnz     @f
280
        jnz     @f
278
        pop     eax
281
        pop     eax
279
        xor     eax, eax
282
        xor     eax, eax
280
        ret
283
        ret
281
@@:
284
@@:
282
; initialize import
285
; initialize import
283
        mov     edi, aMissingExport
286
        mov     edi, aMissingExport
284
.import_loop:
287
.import_loop:
285
        lodsd
288
        lodsd
286
        test    eax, eax
289
        test    eax, eax
287
        jz      .import_done
290
        jz      .import_done
288
        call    .find_exported_function
291
        call    .find_exported_function
289
        jc      .sayerr
292
        jc      .sayerr
290
        mov     [esi-4], eax
293
        mov     [esi-4], eax
291
        jmp     .import_loop
294
        jmp     .import_loop
292
.import_done:
295
.import_done:
293
; check version
296
; check version
294
        test    ebp, ebp
297
        test    ebp, ebp
295
        jz      .version_ok
298
        jz      .version_ok
296
        mov     edi, aIncompatibleVersion
299
        mov     edi, aIncompatibleVersion
297
        mov     eax, aVersion
300
        mov     eax, aVersion
298
        call    .find_exported_function
301
        call    .find_exported_function
299
        jc      .sayerr
302
        jc      .sayerr
300
        cmp     ax, bp
303
        cmp     ax, bp
301
        jb      .sayerr
304
        jb      .sayerr
302
        shr     eax, 16
305
        shr     eax, 16
303
        cmp     eax, ebp
306
        cmp     eax, ebp
304
        ja      .sayerr
307
        ja      .sayerr
305
.version_ok:
308
.version_ok:
306
; initialize library
309
; initialize library
307
        mov     eax, aStart
310
        mov     eax, aStart
308
        call    .find_exported_function
311
        call    .find_exported_function
309
        jc      @f
312
        jc      @f
310
        push    1       ; DLL_ENTRY
313
        push    1       ; DLL_ENTRY
311
        call    eax
314
        call    eax
312
.ret0:
315
.ret0:
313
        pop     eax
316
        pop     eax
314
        xor     eax, eax
317
        xor     eax, eax
315
        ret
318
        ret
316
@@:
319
@@:
317
        mov     eax, aLibInit
320
        mov     eax, aLibInit
318
        call    .find_exported_function
321
        call    .find_exported_function
319
        jc      .ret0
322
        jc      .ret0
320
        mov     esi, eax
323
        mov     esi, eax
321
        mov     eax, libini_alloc
324
        mov     eax, libini_alloc
322
        mov     ebx, libini_free
325
        mov     ebx, libini_free
323
        mov     ecx, libini_realloc
326
        mov     ecx, libini_realloc
324
        mov     edx, libini_dllload
327
        mov     edx, libini_dllload
325
        call    esi
328
        call    esi
326
        mov     edi, aInitFailed
329
        mov     edi, aInitFailed
327
        test    eax, eax
330
        test    eax, eax
328
        jnz     .sayerr
331
        jnz     .sayerr
329
        jmp     .ret0
332
        jmp     .ret0
330
 
333
 
331
.find_exported_function:
334
.find_exported_function:
332
        push    edx
335
        push    edx
333
.import_loop_i:
336
.import_loop_i:
334
        mov     ebx, [edx]
337
        mov     ebx, [edx]
335
        test    ebx, ebx
338
        test    ebx, ebx
336
        jz      .import_notfound
339
        jz      .import_notfound
337
        push    eax
340
        push    eax
338
@@:
341
@@:
339
        mov     cl, [eax]
342
        mov     cl, [eax]
340
        cmp     cl, [ebx]
343
        cmp     cl, [ebx]
341
        jnz     .import_find_next
344
        jnz     .import_find_next
342
        test    cl, cl
345
        test    cl, cl
343
        jz      .import_found
346
        jz      .import_found
344
        inc     eax
347
        inc     eax
345
        inc     ebx
348
        inc     ebx
346
        jmp     @b
349
        jmp     @b
347
.import_find_next:
350
.import_find_next:
348
        pop     eax
351
        pop     eax
349
        add     edx, 8
352
        add     edx, 8
350
        jmp     .import_loop_i
353
        jmp     .import_loop_i
351
.import_found:
354
.import_found:
352
        pop     eax
355
        pop     eax
353
        mov     eax, [edx+4]
356
        mov     eax, [edx+4]
354
        pop     edx
357
        pop     edx
355
        ret
358
        ret
356
.import_notfound:
359
.import_notfound:
357
        pop     edx
360
        pop     edx
358
        stc
361
        stc
359
        ret
362
        ret