2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
Copyright (c) 2018, 2020, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
2016-10-24 01:30:27 +02:00
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE API;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
IMPORT SYSTEM, K := KOSAPI;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
CONST
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
eol* = 0DX + 0AX;
|
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
MAX_SIZE = 16 * 400H;
|
|
|
|
HEAP_SIZE = 1 * 100000H;
|
2018-03-23 23:01:12 +01:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
_new = 1;
|
|
|
|
_dispose = 2;
|
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
SizeOfHeader = 36;
|
|
|
|
|
2018-03-23 23:01:12 +01:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
TYPE
|
|
|
|
|
|
|
|
CRITICAL_SECTION = ARRAY 2 OF INTEGER;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
heap, endheap: INTEGER;
|
|
|
|
pockets: ARRAY MAX_SIZE DIV 32 + 1 OF INTEGER;
|
|
|
|
|
|
|
|
CriticalSection: CRITICAL_SECTION;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
_import*, multi: BOOLEAN;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
base*: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
PROCEDURE [stdcall] zeromem* (dwords: INTEGER; adr: INTEGER);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.CODE(
|
|
|
|
0FCH, (* cld *)
|
|
|
|
031H, 0C0H, (* xor eax, eax *)
|
|
|
|
057H, (* push edi *)
|
|
|
|
08BH, 07DH, 00CH, (* mov edi, dword [ebp + 12] *)
|
|
|
|
08BH, 04DH, 008H, (* mov ecx, dword [ebp + 8] *)
|
|
|
|
0F3H, 0ABH, (* rep stosd *)
|
|
|
|
05FH (* pop edi *)
|
|
|
|
)
|
2018-03-23 23:01:12 +01:00
|
|
|
END zeromem;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
|
|
|
PROCEDURE mem_commit* (adr, size: INTEGER);
|
|
|
|
VAR
|
|
|
|
tmp: INTEGER;
|
2016-11-01 13:23:58 +01:00
|
|
|
BEGIN
|
2017-11-16 08:01:07 +01:00
|
|
|
FOR tmp := adr TO adr + size - 1 BY 4096 DO
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.PUT(tmp, 0)
|
2017-11-16 08:01:07 +01:00
|
|
|
END
|
2018-03-23 23:01:12 +01:00
|
|
|
END mem_commit;
|
2016-11-01 13:23:58 +01:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
|
|
|
PROCEDURE switch_task;
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc2(68, 1)
|
2017-11-16 08:01:07 +01:00
|
|
|
END switch_task;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE futex_create (ptr: INTEGER): INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
RETURN K.sysfunc3(77, 0, ptr)
|
2017-11-16 08:01:07 +01:00
|
|
|
END futex_create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE futex_wait (futex, value, timeout: INTEGER);
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc5(77, 2, futex, value, timeout)
|
2017-11-16 08:01:07 +01:00
|
|
|
END futex_wait;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE futex_wake (futex, number: INTEGER);
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc4(77, 3, futex, number)
|
2017-11-16 08:01:07 +01:00
|
|
|
END futex_wake;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE EnterCriticalSection* (VAR CriticalSection: CRITICAL_SECTION);
|
|
|
|
BEGIN
|
|
|
|
switch_task;
|
|
|
|
futex_wait(CriticalSection[0], 1, 10000);
|
|
|
|
CriticalSection[1] := 1
|
|
|
|
END EnterCriticalSection;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE LeaveCriticalSection* (VAR CriticalSection: CRITICAL_SECTION);
|
|
|
|
BEGIN
|
|
|
|
CriticalSection[1] := 0;
|
|
|
|
futex_wake(CriticalSection[0], 1)
|
|
|
|
END LeaveCriticalSection;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE InitializeCriticalSection* (VAR CriticalSection: CRITICAL_SECTION);
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
CriticalSection[0] := futex_create(SYSTEM.ADR(CriticalSection[1]));
|
2017-11-16 08:01:07 +01:00
|
|
|
CriticalSection[1] := 0
|
|
|
|
END InitializeCriticalSection;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE __NEW (size: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
res, idx, temp: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
IF size <= MAX_SIZE THEN
|
|
|
|
idx := ASR(size, 5);
|
|
|
|
res := pockets[idx];
|
|
|
|
IF res # 0 THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.GET(res, pockets[idx]);
|
|
|
|
SYSTEM.PUT(res, size);
|
2017-11-16 08:01:07 +01:00
|
|
|
INC(res, 4)
|
2017-11-02 17:36:50 +01:00
|
|
|
ELSE
|
2017-11-16 08:01:07 +01:00
|
|
|
temp := 0;
|
|
|
|
IF heap + size >= endheap THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
IF K.sysfunc2(18, 16) > ASR(HEAP_SIZE, 10) THEN
|
|
|
|
temp := K.sysfunc3(68, 12, HEAP_SIZE)
|
2017-11-16 08:01:07 +01:00
|
|
|
ELSE
|
|
|
|
temp := 0
|
|
|
|
END;
|
|
|
|
IF temp # 0 THEN
|
|
|
|
mem_commit(temp, HEAP_SIZE);
|
|
|
|
heap := temp;
|
|
|
|
endheap := heap + HEAP_SIZE
|
|
|
|
ELSE
|
|
|
|
temp := -1
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
IF (heap # 0) & (temp # -1) THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.PUT(heap, size);
|
2017-11-16 08:01:07 +01:00
|
|
|
res := heap + 4;
|
|
|
|
heap := heap + size
|
|
|
|
ELSE
|
|
|
|
res := 0
|
|
|
|
END
|
|
|
|
END
|
|
|
|
ELSE
|
2019-03-11 09:59:55 +01:00
|
|
|
IF K.sysfunc2(18, 16) > ASR(size, 10) THEN
|
|
|
|
res := K.sysfunc3(68, 12, size);
|
2017-11-16 08:01:07 +01:00
|
|
|
IF res # 0 THEN
|
|
|
|
mem_commit(res, size);
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.PUT(res, size);
|
2017-11-16 08:01:07 +01:00
|
|
|
INC(res, 4)
|
|
|
|
END
|
2017-11-02 17:36:50 +01:00
|
|
|
ELSE
|
2017-11-16 08:01:07 +01:00
|
|
|
res := 0
|
2017-11-02 17:36:50 +01:00
|
|
|
END
|
2017-11-16 08:01:07 +01:00
|
|
|
END;
|
2018-03-23 23:01:12 +01:00
|
|
|
IF (res # 0) & (size <= MAX_SIZE) THEN
|
2017-11-16 08:01:07 +01:00
|
|
|
zeromem(ASR(size, 2) - 1, res)
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2017-11-16 08:01:07 +01:00
|
|
|
RETURN res
|
|
|
|
END __NEW;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE __DISPOSE (ptr: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
size, idx: INTEGER;
|
|
|
|
BEGIN
|
|
|
|
DEC(ptr, 4);
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.GET(ptr, size);
|
2017-11-16 08:01:07 +01:00
|
|
|
IF size <= MAX_SIZE THEN
|
|
|
|
idx := ASR(size, 5);
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.PUT(ptr, pockets[idx]);
|
2017-11-16 08:01:07 +01:00
|
|
|
pockets[idx] := ptr
|
2016-10-24 01:30:27 +02:00
|
|
|
ELSE
|
2019-03-11 09:59:55 +01:00
|
|
|
size := K.sysfunc3(68, 13, ptr)
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2017-11-16 08:01:07 +01:00
|
|
|
RETURN 0
|
|
|
|
END __DISPOSE;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE NEW_DISPOSE (func, arg: INTEGER): INTEGER;
|
|
|
|
VAR
|
|
|
|
res: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2018-03-23 23:01:12 +01:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
IF multi THEN
|
|
|
|
EnterCriticalSection(CriticalSection)
|
|
|
|
END;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
|
|
|
IF func = _new THEN
|
|
|
|
res := __NEW(arg)
|
|
|
|
ELSIF func = _dispose THEN
|
|
|
|
res := __DISPOSE(arg)
|
2018-03-23 23:01:12 +01:00
|
|
|
END;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
IF multi THEN
|
|
|
|
LeaveCriticalSection(CriticalSection)
|
|
|
|
END
|
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
RETURN res
|
2018-03-23 23:01:12 +01:00
|
|
|
END NEW_DISPOSE;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE _NEW* (size: INTEGER): INTEGER;
|
|
|
|
RETURN NEW_DISPOSE(_new, size)
|
2016-10-24 01:30:27 +02:00
|
|
|
END _NEW;
|
2018-03-23 23:01:12 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
PROCEDURE _DISPOSE* (ptr: INTEGER): INTEGER;
|
|
|
|
RETURN NEW_DISPOSE(_dispose, ptr)
|
2016-10-24 01:30:27 +02:00
|
|
|
END _DISPOSE;
|
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE exit* (p1: INTEGER);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc1(-1)
|
|
|
|
END exit;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE exit_thread* (p1: INTEGER);
|
2017-11-02 17:36:50 +01:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc1(-1)
|
|
|
|
END exit_thread;
|
2017-11-02 17:36:50 +01:00
|
|
|
|
2017-11-16 08:01:07 +01:00
|
|
|
|
|
|
|
PROCEDURE OutChar (c: CHAR);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc3(63, 1, ORD(c))
|
2018-03-23 23:01:12 +01:00
|
|
|
END OutChar;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE OutLn;
|
|
|
|
BEGIN
|
|
|
|
OutChar(0DX);
|
|
|
|
OutChar(0AX)
|
|
|
|
END OutLn;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE OutStr (pchar: INTEGER);
|
2017-11-16 08:01:07 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
IF pchar # 0 THEN
|
2017-11-16 08:01:07 +01:00
|
|
|
REPEAT
|
2019-03-11 09:59:55 +01:00
|
|
|
SYSTEM.GET(pchar, c);
|
2017-11-16 08:01:07 +01:00
|
|
|
IF c # 0X THEN
|
|
|
|
OutChar(c)
|
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
INC(pchar)
|
|
|
|
UNTIL c = 0X
|
|
|
|
END
|
|
|
|
END OutStr;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE DebugMsg* (lpText, lpCaption: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
IF lpCaption # 0 THEN
|
|
|
|
OutLn;
|
|
|
|
OutStr(lpCaption);
|
2017-11-16 08:01:07 +01:00
|
|
|
OutChar(":");
|
2019-03-11 09:59:55 +01:00
|
|
|
OutLn
|
2017-11-16 08:01:07 +01:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
OutStr(lpText);
|
2017-11-16 08:01:07 +01:00
|
|
|
IF lpCaption # 0 THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
OutLn
|
2017-11-16 08:01:07 +01:00
|
|
|
END
|
2018-03-23 23:01:12 +01:00
|
|
|
END DebugMsg;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE OutString (s: ARRAY OF CHAR);
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
i := 0;
|
|
|
|
WHILE (i < LEN(s)) & (s[i] # 0X) DO
|
|
|
|
OutChar(s[i]);
|
|
|
|
INC(i)
|
|
|
|
END
|
|
|
|
END OutString;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE imp_error;
|
|
|
|
BEGIN
|
|
|
|
OutString("import error: ");
|
|
|
|
IF K.imp_error.error = 1 THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
OutString("can't load '"); OutString(K.imp_error.lib)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF K.imp_error.error = 2 THEN
|
2020-10-13 09:58:51 +02:00
|
|
|
OutString("not found '"); OutString(K.imp_error.proc); OutString("' in '"); OutString(K.imp_error.lib)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
2020-10-13 09:58:51 +02:00
|
|
|
OutString("'");
|
2019-03-11 09:59:55 +01:00
|
|
|
OutLn
|
|
|
|
END imp_error;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE init* (import_, code: INTEGER);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
|
|
|
multi := FALSE;
|
2019-10-06 19:55:12 +02:00
|
|
|
base := code - SizeOfHeader;
|
2019-03-11 09:59:55 +01:00
|
|
|
K.sysfunc2(68, 11);
|
|
|
|
InitializeCriticalSection(CriticalSection);
|
|
|
|
K._init;
|
2020-10-13 09:58:51 +02:00
|
|
|
_import := (K.dll_Load(import_) = 0) & (K.imp_error.error = 0);
|
|
|
|
IF ~_import THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
imp_error
|
|
|
|
END
|
2018-03-23 23:01:12 +01:00
|
|
|
END init;
|
2017-11-16 08:01:07 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE SetMultiThr* (value: BOOLEAN);
|
|
|
|
BEGIN
|
|
|
|
multi := value
|
|
|
|
END SetMultiThr;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetTickCount* (): INTEGER;
|
|
|
|
RETURN K.sysfunc2(26, 9) * 10
|
|
|
|
END GetTickCount;
|
|
|
|
|
|
|
|
|
2020-05-25 22:48:33 +02:00
|
|
|
PROCEDURE dllentry* (hinstDLL, fdwReason, lpvReserved: INTEGER): INTEGER;
|
|
|
|
RETURN 0
|
|
|
|
END dllentry;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE sofinit*;
|
|
|
|
END sofinit;
|
|
|
|
|
|
|
|
|
|
|
|
END API.
|