system.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. {
  2. ****************************************************************************
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2005 by Free Pascal development team
  5. Free Pascal - OS/2 runtime library
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. ****************************************************************************}
  12. unit system;
  13. interface
  14. {$ifdef SYSTEMDEBUG}
  15. {$define SYSTEMEXCEPTIONDEBUG}
  16. {.$define IODEBUG}
  17. {.$define DEBUGENVIRONMENT}
  18. {.$define DEBUGARGUMENTS}
  19. {$endif SYSTEMDEBUG}
  20. { $DEFINE OS2EXCEPTIONS}
  21. {$I systemh.inc}
  22. {$IFDEF OS2EXCEPTIONS}
  23. (* Types and constants for exception handler support *)
  24. type
  25. {x} PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  26. {x} TEXCEPTION_FRAME = record
  27. {x} next : PEXCEPTION_FRAME;
  28. {x} handler : pointer;
  29. {x} end;
  30. {$ENDIF OS2EXCEPTIONS}
  31. const
  32. LineEnding = #13#10;
  33. { LFNSupport is defined separately below!!! }
  34. DirectorySeparator = '\';
  35. DriveSeparator = ':';
  36. PathSeparator = ';';
  37. { FileNameCaseSensitive is defined separately below!!! }
  38. MaxExitCode = 65535;
  39. MaxPathLen = 256;
  40. type Tos=(osDOS,osOS2,osDPMI);
  41. const os_mode: Tos = osOS2;
  42. first_meg: pointer = nil;
  43. {$IFDEF OS2EXCEPTIONS}
  44. {x} System_exception_frame : PEXCEPTION_FRAME =nil;
  45. {$ENDIF OS2EXCEPTIONS}
  46. type TByteArray = array [0..$ffff] of byte;
  47. PByteArray = ^TByteArray;
  48. TSysThreadIB = record
  49. TID,
  50. Priority,
  51. Version: cardinal;
  52. MCCount,
  53. MCForceFlag: word;
  54. end;
  55. PSysThreadIB = ^TSysThreadIB;
  56. TThreadInfoBlock = record
  57. PExChain,
  58. Stack,
  59. StackLimit: pointer;
  60. TIB2: PSysThreadIB;
  61. Version,
  62. Ordinal: cardinal;
  63. end;
  64. PThreadInfoBlock = ^TThreadInfoBlock;
  65. PPThreadInfoBlock = ^PThreadInfoBlock;
  66. TProcessInfoBlock = record
  67. PID,
  68. ParentPid,
  69. Handle: cardinal;
  70. Cmd,
  71. Env: PByteArray;
  72. Status,
  73. ProcType: cardinal;
  74. end;
  75. PProcessInfoBlock = ^TProcessInfoBlock;
  76. PPProcessInfoBlock = ^PProcessInfoBlock;
  77. const UnusedHandle=-1;
  78. StdInputHandle=0;
  79. StdOutputHandle=1;
  80. StdErrorHandle=2;
  81. LFNSupport: boolean = true;
  82. FileNameCaseSensitive: boolean = false;
  83. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  84. sLineBreak = LineEnding;
  85. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  86. var
  87. { C-compatible arguments and environment }
  88. argc : longint;
  89. argv : ppchar;
  90. envp : ppchar;
  91. EnvC: cardinal;
  92. (* Pointer to the block of environment variables - used e.g. in unit Dos. *)
  93. Environment: PChar;
  94. var
  95. (* Type / run mode of the current process: *)
  96. (* 0 .. full screen OS/2 session *)
  97. (* 1 .. DOS session *)
  98. (* 2 .. VIO windowable OS/2 session *)
  99. (* 3 .. Presentation Manager OS/2 session *)
  100. (* 4 .. detached (background) OS/2 process *)
  101. ApplicationType: cardinal;
  102. (* Is allocation of memory above 512 MB address limit allowed? Initialized *)
  103. (* during initialization of system unit according to capabilities of the *)
  104. (* underlying OS/2 version, can be overridden by user - heap is allocated *)
  105. (* for all threads, so the setting isn't declared as a threadvar and *)
  106. (* should be only changed at the beginning of the main thread if needed. *)
  107. UseHighMem: boolean;
  108. StackTop : PtrUInt;
  109. procedure SetDefaultOS2FileType (FType: ShortString);
  110. procedure SetDefaultOS2Creator (Creator: ShortString);
  111. implementation
  112. {$I system.inc}
  113. {****************************************************************************
  114. Miscellaneous related routines.
  115. ****************************************************************************}
  116. procedure system_exit;
  117. begin
  118. DosExit (1{process}, exitcode);
  119. end;
  120. {$ASMMODE ATT}
  121. function paramcount:longint;assembler;
  122. asm
  123. movl argc,%eax
  124. decl %eax
  125. end {['EAX']};
  126. function args:pointer;assembler;
  127. asm
  128. movl argv,%eax
  129. end {['EAX']};
  130. function paramstr(l:longint):string;
  131. var p:^Pchar;
  132. begin
  133. if (l>=0) and (l<=paramcount) then
  134. begin
  135. p:=args;
  136. paramstr:=strpas(p[l]);
  137. end
  138. else paramstr:='';
  139. end;
  140. procedure randomize;
  141. var
  142. dt: TSysDateTime;
  143. begin
  144. // Hmm... Lets use timer
  145. DosGetDateTime(dt);
  146. randseed:=dt.hour+(dt.minute shl 8)+(dt.second shl 16)+(dt.sec100 shl 32);
  147. end;
  148. {$ASMMODE ATT}
  149. {*****************************************************************************
  150. System unit initialization.
  151. ****************************************************************************}
  152. {****************************************************************************
  153. Error Message writing using messageboxes
  154. ****************************************************************************}
  155. type
  156. TWinMessageBox = function (Parent, Owner: cardinal;
  157. BoxText, BoxTitle: PChar; Identity, Style: cardinal): cardinal; cdecl;
  158. TWinInitialize = function (Options: cardinal): cardinal; cdecl;
  159. TWinCreateMsgQueue = function (Handle: cardinal; cmsg: longint): cardinal;
  160. cdecl;
  161. const
  162. ErrorBufferLength = 1024;
  163. mb_OK = $0000;
  164. mb_Error = $0040;
  165. mb_Moveable = $4000;
  166. MBStyle = mb_OK or mb_Error or mb_Moveable;
  167. WinInitialize: TWinInitialize = nil;
  168. WinCreateMsgQueue: TWinCreateMsgQueue = nil;
  169. WinMessageBox: TWinMessageBox = nil;
  170. EnvSize: cardinal = 0;
  171. var
  172. ErrorBuf: array [0..ErrorBufferLength] of char;
  173. ErrorLen: longint;
  174. PMWinHandle: cardinal;
  175. function ErrorWrite (var F: TextRec): integer;
  176. {
  177. An error message should always end with #13#10#13#10
  178. }
  179. var
  180. P: PChar;
  181. I: longint;
  182. begin
  183. if F.BufPos > 0 then
  184. begin
  185. if F.BufPos + ErrorLen > ErrorBufferLength then
  186. I := ErrorBufferLength - ErrorLen
  187. else
  188. I := F.BufPos;
  189. Move (F.BufPtr^, ErrorBuf [ErrorLen], I);
  190. Inc (ErrorLen, I);
  191. ErrorBuf [ErrorLen] := #0;
  192. end;
  193. if ErrorLen > 3 then
  194. begin
  195. P := @ErrorBuf [ErrorLen];
  196. for I := 1 to 4 do
  197. begin
  198. Dec (P);
  199. if not (P^ in [#10, #13]) then
  200. break;
  201. end;
  202. end;
  203. if ErrorLen = ErrorBufferLength then
  204. I := 4;
  205. if (I = 4) then
  206. begin
  207. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  208. ErrorLen := 0;
  209. end;
  210. F.BufPos := 0;
  211. ErrorWrite := 0;
  212. end;
  213. function ErrorClose (var F: TextRec): integer;
  214. begin
  215. if ErrorLen > 0 then
  216. begin
  217. WinMessageBox (0, 0, @ErrorBuf, PChar ('Error'), 0, MBStyle);
  218. ErrorLen := 0;
  219. end;
  220. ErrorLen := 0;
  221. ErrorClose := 0;
  222. end;
  223. function ErrorOpen (var F: TextRec): integer;
  224. begin
  225. TextRec(F).InOutFunc := @ErrorWrite;
  226. TextRec(F).FlushFunc := @ErrorWrite;
  227. TextRec(F).CloseFunc := @ErrorClose;
  228. ErrorOpen := 0;
  229. end;
  230. procedure AssignError (var T: Text);
  231. begin
  232. Assign (T, '');
  233. TextRec (T).OpenFunc := @ErrorOpen;
  234. Rewrite (T);
  235. end;
  236. procedure SysInitStdIO;
  237. begin
  238. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  239. displayed in a messagebox }
  240. (*
  241. StdInputHandle := longint(GetStdHandle(cardinal(STD_INPUT_HANDLE)));
  242. StdOutputHandle := longint(GetStdHandle(cardinal(STD_OUTPUT_HANDLE)));
  243. StdErrorHandle := longint(GetStdHandle(cardinal(STD_ERROR_HANDLE)));
  244. if not IsConsole then
  245. begin
  246. if (DosLoadModule (nil, 0, 'PMWIN', PMWinHandle) = 0) and
  247. (DosQueryProcAddr (PMWinHandle, 789, nil, pointer (WinMessageBox)) = 0)
  248. and
  249. (DosQueryProcAddr (PMWinHandle, 763, nil, pointer (WinInitialize)) = 0)
  250. and
  251. (DosQueryProcAddr (PMWinHandle, 716, nil, pointer (WinCreateMsgQueue))
  252. = 0)
  253. then
  254. begin
  255. WinInitialize (0);
  256. WinCreateMsgQueue (0, 0);
  257. end
  258. else
  259. HandleError (2);
  260. AssignError (StdErr);
  261. AssignError (StdOut);
  262. Assign (Output, '');
  263. Assign (Input, '');
  264. end
  265. else
  266. begin
  267. *)
  268. OpenStdIO (Input, fmInput, StdInputHandle);
  269. OpenStdIO (Output, fmOutput, StdOutputHandle);
  270. OpenStdIO (ErrOutput, fmOutput, StdErrorHandle);
  271. OpenStdIO (StdOut, fmOutput, StdOutputHandle);
  272. OpenStdIO (StdErr, fmOutput, StdErrorHandle);
  273. (*
  274. end;
  275. *)
  276. end;
  277. function strcopy(dest,source : pchar) : pchar;assembler;
  278. var
  279. saveeax,saveesi,saveedi : longint;
  280. asm
  281. movl %edi,saveedi
  282. movl %esi,saveesi
  283. {$ifdef REGCALL}
  284. movl %eax,saveeax
  285. movl %edx,%edi
  286. {$else}
  287. movl source,%edi
  288. {$endif}
  289. testl %edi,%edi
  290. jz .LStrCopyDone
  291. leal 3(%edi),%ecx
  292. andl $-4,%ecx
  293. movl %edi,%esi
  294. subl %edi,%ecx
  295. {$ifdef REGCALL}
  296. movl %eax,%edi
  297. {$else}
  298. movl dest,%edi
  299. {$endif}
  300. jz .LStrCopyAligned
  301. .LStrCopyAlignLoop:
  302. movb (%esi),%al
  303. incl %edi
  304. incl %esi
  305. testb %al,%al
  306. movb %al,-1(%edi)
  307. jz .LStrCopyDone
  308. decl %ecx
  309. jnz .LStrCopyAlignLoop
  310. .balign 16
  311. .LStrCopyAligned:
  312. movl (%esi),%eax
  313. movl %eax,%edx
  314. leal 0x0fefefeff(%eax),%ecx
  315. notl %edx
  316. addl $4,%esi
  317. andl %edx,%ecx
  318. andl $0x080808080,%ecx
  319. jnz .LStrCopyEndFound
  320. movl %eax,(%edi)
  321. addl $4,%edi
  322. jmp .LStrCopyAligned
  323. .LStrCopyEndFound:
  324. testl $0x0ff,%eax
  325. jz .LStrCopyByte
  326. testl $0x0ff00,%eax
  327. jz .LStrCopyWord
  328. testl $0x0ff0000,%eax
  329. jz .LStrCopy3Bytes
  330. movl %eax,(%edi)
  331. jmp .LStrCopyDone
  332. .LStrCopy3Bytes:
  333. xorb %dl,%dl
  334. movw %ax,(%edi)
  335. movb %dl,2(%edi)
  336. jmp .LStrCopyDone
  337. .LStrCopyWord:
  338. movw %ax,(%edi)
  339. jmp .LStrCopyDone
  340. .LStrCopyByte:
  341. movb %al,(%edi)
  342. .LStrCopyDone:
  343. {$ifdef REGCALL}
  344. movl saveeax,%eax
  345. {$else}
  346. movl dest,%eax
  347. {$endif}
  348. movl saveedi,%edi
  349. movl saveesi,%esi
  350. end;
  351. threadvar
  352. DefaultCreator: ShortString;
  353. DefaultFileType: ShortString;
  354. procedure SetDefaultOS2FileType (FType: ShortString);
  355. begin
  356. {$WARNING Not implemented yet!}
  357. DefaultFileType := FType;
  358. end;
  359. procedure SetDefaultOS2Creator (Creator: ShortString);
  360. begin
  361. {$WARNING Not implemented yet!}
  362. DefaultCreator := Creator;
  363. end;
  364. procedure InitEnvironment;
  365. var env_count : longint;
  366. dos_env,cp : pchar;
  367. begin
  368. env_count:=0;
  369. cp:=environment;
  370. while cp ^ <> #0 do
  371. begin
  372. inc(env_count);
  373. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  374. inc(longint(cp)); { skip to next character }
  375. end;
  376. envp := sysgetmem((env_count+1) * sizeof(pchar));
  377. envc := env_count;
  378. if (envp = nil) then exit;
  379. cp:=environment;
  380. env_count:=0;
  381. while cp^ <> #0 do
  382. begin
  383. envp[env_count] := sysgetmem(strlen(cp)+1);
  384. strcopy(envp[env_count], cp);
  385. {$IfDef DEBUGENVIRONMENT}
  386. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  387. {$EndIf}
  388. inc(env_count);
  389. while (cp^ <> #0) do
  390. inc(longint(cp)); { skip to NUL }
  391. inc(longint(cp)); { skip to next character }
  392. end;
  393. envp[env_count]:=nil;
  394. end;
  395. var
  396. (* Initialized by system unit initialization *)
  397. PIB: PProcessInfoBlock;
  398. procedure InitArguments;
  399. var
  400. arglen,
  401. count : PtrInt;
  402. argstart,
  403. pc,arg : pchar;
  404. quote : char;
  405. argvlen : PtrInt;
  406. procedure allocarg(idx,len: PtrInt);
  407. var
  408. oldargvlen : PtrInt;
  409. begin
  410. if idx>=argvlen then
  411. begin
  412. oldargvlen:=argvlen;
  413. argvlen:=(idx+8) and (not 7);
  414. sysreallocmem(argv,argvlen*sizeof(pointer));
  415. { fillchar(argv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);}
  416. end;
  417. { use realloc to reuse already existing memory }
  418. { always allocate, even if length is zero, since }
  419. { the arg. is still present! }
  420. ArgV [Idx] := SysAllocMem (Succ (Len));
  421. end;
  422. begin
  423. CmdLine := SysAllocMem (MaxPathLen);
  424. ArgV := SysAllocMem (8 * SizeOf (pointer));
  425. ArgLen := StrLen (PChar (PIB^.Cmd));
  426. Inc (ArgLen);
  427. if DosQueryModuleName (PIB^.Handle, MaxPathLen, CmdLine) = 0 then
  428. ArgVLen := Succ (StrLen (CmdLine))
  429. else
  430. (* Error occurred - use program name from command line as fallback. *)
  431. begin
  432. Move (PIB^.Cmd^, CmdLine, ArgLen);
  433. ArgVLen := ArgLen;
  434. end;
  435. { Get ArgV [0] }
  436. ArgV [0] := SysAllocMem (ArgVLen);
  437. Move (CmdLine^, ArgV [0]^, ArgVLen);
  438. Count := 1;
  439. (* PC points to leading space after program name on command line *)
  440. PC := PChar (PIB^.Cmd) + ArgLen;
  441. (* ArgLen contains size of command line arguments including leading space. *)
  442. ArgLen := Succ (StrLen (PC));
  443. SysReallocMem (CmdLine, ArgVLen + ArgLen);
  444. Move (PC^, CmdLine [ArgVLen], Succ (ArgLen));
  445. (* ArgV has space for 8 parameters from the first allocation. *)
  446. ArgVLen := 8;
  447. { process arguments }
  448. while pc^<>#0 do
  449. begin
  450. { skip leading spaces }
  451. while pc^ in [#1..#32] do
  452. inc(pc);
  453. if pc^=#0 then
  454. break;
  455. { calc argument length }
  456. quote:=' ';
  457. argstart:=pc;
  458. arglen:=0;
  459. while (pc^<>#0) do
  460. begin
  461. case pc^ of
  462. #1..#32 :
  463. begin
  464. if quote<>' ' then
  465. inc(arglen)
  466. else
  467. break;
  468. end;
  469. '"' :
  470. begin
  471. if quote<>'''' then
  472. begin
  473. if pchar(pc+1)^<>'"' then
  474. begin
  475. if quote='"' then
  476. quote:=' '
  477. else
  478. quote:='"';
  479. end
  480. else
  481. inc(pc);
  482. end
  483. else
  484. inc(arglen);
  485. end;
  486. '''' :
  487. begin
  488. if quote<>'"' then
  489. begin
  490. if pchar(pc+1)^<>'''' then
  491. begin
  492. if quote='''' then
  493. quote:=' '
  494. else
  495. quote:='''';
  496. end
  497. else
  498. inc(pc);
  499. end
  500. else
  501. inc(arglen);
  502. end;
  503. else
  504. inc(arglen);
  505. end;
  506. inc(pc);
  507. end;
  508. { copy argument }
  509. { Don't copy the first one, it is already there.}
  510. If Count<>0 then
  511. begin
  512. allocarg(count,arglen);
  513. quote:=' ';
  514. pc:=argstart;
  515. arg:=argv[count];
  516. while (pc^<>#0) do
  517. begin
  518. case pc^ of
  519. #1..#32 :
  520. begin
  521. if quote<>' ' then
  522. begin
  523. arg^:=pc^;
  524. inc(arg);
  525. end
  526. else
  527. break;
  528. end;
  529. '"' :
  530. begin
  531. if quote<>'''' then
  532. begin
  533. if pchar(pc+1)^<>'"' then
  534. begin
  535. if quote='"' then
  536. quote:=' '
  537. else
  538. quote:='"';
  539. end
  540. else
  541. inc(pc);
  542. end
  543. else
  544. begin
  545. arg^:=pc^;
  546. inc(arg);
  547. end;
  548. end;
  549. '''' :
  550. begin
  551. if quote<>'"' then
  552. begin
  553. if pchar(pc+1)^<>'''' then
  554. begin
  555. if quote='''' then
  556. quote:=' '
  557. else
  558. quote:='''';
  559. end
  560. else
  561. inc(pc);
  562. end
  563. else
  564. begin
  565. arg^:=pc^;
  566. inc(arg);
  567. end;
  568. end;
  569. else
  570. begin
  571. arg^:=pc^;
  572. inc(arg);
  573. end;
  574. end;
  575. inc(pc);
  576. end;
  577. arg^:=#0;
  578. end;
  579. {$IfDef DEBUGARGUMENTS}
  580. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  581. {$EndIf}
  582. inc(count);
  583. end;
  584. { get argc and create an nil entry }
  585. argc:=count;
  586. allocarg(argc,0);
  587. { free unused memory }
  588. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  589. end;
  590. function GetFileHandleCount: longint;
  591. var L1: longint;
  592. L2: cardinal;
  593. begin
  594. L1 := 0; (* Don't change the amount, just check. *)
  595. if DosSetRelMaxFH (L1, L2) <> 0 then GetFileHandleCount := 50
  596. else GetFileHandleCount := L2;
  597. end;
  598. function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
  599. begin
  600. CheckInitialStkLen := StkLen;
  601. end;
  602. var TIB: PThreadInfoBlock;
  603. RC: cardinal;
  604. ErrStr: string;
  605. P: pointer;
  606. begin
  607. IsLibrary := FALSE;
  608. (* Initialize the amount of file handles *)
  609. FileHandleCount := GetFileHandleCount;
  610. DosGetInfoBlocks (@TIB, @PIB);
  611. StackBottom := TIB^.Stack;
  612. StackTop := PtrUInt (TIB^.StackLimit);
  613. StackLength := CheckInitialStkLen (InitialStkLen);
  614. {Set type of application}
  615. ApplicationType := PIB^.ProcType;
  616. ProcessID := PIB^.PID;
  617. ThreadID := TIB^.TIB2^.TID;
  618. IsConsole := ApplicationType <> 3;
  619. ExitProc := nil;
  620. {Initialize the heap.}
  621. (* Logic is following:
  622. The heap is initially restricted to low address space (< 512 MB).
  623. If underlying OS/2 version allows using more than 512 MB per process
  624. (OS/2 WarpServer for e-Business, eComStation, possibly OS/2 Warp 4.0
  625. with FP13 and above as well), use of this high memory is allowed for
  626. future memory allocations at the end of System unit initialization.
  627. The consequences are that the compiled application can allocate more
  628. memory, but it must make sure to use direct DosAllocMem calls if it
  629. needs a memory block for some system API not supporting high memory.
  630. This is probably no problem for direct calls to these APIs, but
  631. there might be situations when a memory block needs to be passed
  632. to a 3rd party DLL which in turn calls such an API call. In case
  633. of problems usage of high memory can be turned off by setting
  634. UseHighMem to false - the program should change the setting at its
  635. very beginning (e.g. in initialization section of the first unit
  636. listed in the "uses" section) to avoid having preallocated memory
  637. from the high memory region before changing value of this variable. *)
  638. InitHeap;
  639. { ... and exceptions }
  640. SysInitExceptions;
  641. { ... and I/O }
  642. SysInitStdIO;
  643. { no I/O-Error }
  644. inoutres:=0;
  645. {Initialize environment (must be after InitHeap because allocates memory)}
  646. Environment := pointer (PIB^.Env);
  647. InitEnvironment;
  648. InitArguments;
  649. DefaultCreator := '';
  650. DefaultFileType := '';
  651. InitSystemThreads;
  652. InitVariantManager;
  653. {$ifdef HASWIDESTRING}
  654. InitWideStringManager;
  655. {$endif HASWIDESTRING}
  656. {$IFDEF EXTDUMPGROW}
  657. { Int_HeapSize := high (cardinal);}
  658. {$ENDIF EXTDUMPGROW}
  659. RC := DosAllocMem (P, 4096, $403);
  660. if RC = 87 then
  661. (* Using of high memory address space (> 512 MB) *)
  662. (* is not supported on this system. *)
  663. UseHighMem := false
  664. else
  665. begin
  666. UseHighMem := true;
  667. if RC <> 0 then
  668. begin
  669. Str (RC, ErrStr);
  670. ErrStr := 'Error during heap initialization (DosAllocMem - ' + ErrStr + ')!!'#13#10;
  671. if IsConsole then
  672. DosWrite (2, @ErrStr [1], Length (ErrStr), RC);
  673. HandleError (204);
  674. end
  675. else
  676. DosFreeMem (P);
  677. end;
  678. end.