system.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2008 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. For details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. Local types
  12. ****************************************************************************}
  13. {
  14. TextRec and FileRec are put in a separate file to make it available to other
  15. units without putting it explicitly in systemh.
  16. This way we keep TP compatibility, and the TextRec definition is available
  17. for everyone who needs it.
  18. }
  19. {$i filerec.inc}
  20. {$i textrec.inc}
  21. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  22. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  23. {$if High(errorcode)<>maxExitCode}
  24. {$define FPC_LIMITED_EXITCODE}
  25. {$endif}
  26. {$else}
  27. {$define FPC_LIMITED_EXITCODE}
  28. {$endif FPC_OBJFPC_EXTENDED_IF}
  29. {$endif FPC_HAS_FEATURE_EXITCODE}
  30. Procedure HandleError (Errno : Longint); forward;
  31. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  32. type
  33. FileFunc = Procedure(var t : TextRec);
  34. const
  35. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  36. { Random / Randomize constants }
  37. OldRandSeed : Cardinal = 0;
  38. { For Error Handling.}
  39. ErrorBase : Pointer = nil;
  40. { Used by the ansi/widestrings and maybe also other things in the future }
  41. var
  42. { widechar, because also used by widestring -> pwidechar conversions }
  43. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  44. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  45. { if the OS does the stack checking, we don't need any stklen from the
  46. main program }
  47. initialstklen : SizeUint;external name '__stklen';
  48. {$endif FPC_NO_GENERIC_STACK_CHECK}
  49. { checks whether the given suggested size for the stack of the current
  50. thread is acceptable. If this is the case, returns it unaltered.
  51. Otherwise it should return an acceptable value.
  52. Operating systems that automatically expand their stack on demand, should
  53. simply return a very large value.
  54. Operating systems which do not have a possibility to retrieve stack size
  55. information, should simply return the given stklen value (This is the default
  56. implementation).
  57. }
  58. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  59. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  60. {$endif FPC_HAS_FEATURE_STACKCHECK}
  61. {*****************************************************************************
  62. OS dependent Helpers/Syscalls
  63. *****************************************************************************}
  64. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  65. it isn't, and is used before the actual definition is encountered }
  66. {$ifdef FPC_HAS_FEATURE_FILEIO}
  67. function do_isdevice(handle:thandle):boolean;forward;
  68. {$endif FPC_HAS_FEATURE_FILEIO}
  69. {$i sysos.inc}
  70. {****************************************************************************
  71. Include processor specific routines
  72. ****************************************************************************}
  73. {$ifdef FPC_USE_LIBC}
  74. { Under Haiku, bcopy cause a problem when searching for include file
  75. in the compiler. So, we use the internal implementation for now
  76. under BeOS and Haiku. }
  77. {$ifndef BEOS}
  78. { prefer libc implementations over our own, as they're most likely faster }
  79. {$i cgeneric.inc}
  80. { is now declared as external reference to another routine in the interface }
  81. {$i cgenstr.inc}
  82. {$endif}
  83. {$endif FPC_USE_LIBC}
  84. {$ifdef cpui386}
  85. {$ifdef SYSPROCDEFINED}
  86. {$Error Can't determine processor type !}
  87. {$endif}
  88. {$i i386.inc} { Case dependent, don't change }
  89. {$endif cpui386}
  90. {$ifdef cpum68k}
  91. {$ifdef SYSPROCDEFINED}
  92. {$Error Can't determine processor type !}
  93. {$endif}
  94. {$i m68k.inc} { Case dependent, don't change }
  95. {$define SYSPROCDEFINED}
  96. {$endif cpum68k}
  97. {$ifdef cpux86_64}
  98. {$ifdef SYSPROCDEFINED}
  99. {$Error Can't determine processor type !}
  100. {$endif}
  101. {$i x86_64.inc} { Case dependent, don't change }
  102. {$define SYSPROCDEFINED}
  103. {$endif cpux86_64}
  104. {$ifdef cpupowerpc32}
  105. {$ifdef SYSPROCDEFINED}
  106. {$Error Can't determine processor type !}
  107. {$endif}
  108. {$i powerpc.inc} { Case dependent, don't change }
  109. {$define SYSPROCDEFINED}
  110. {$endif cpupowerpc32}
  111. {$ifdef cpupowerpc64}
  112. {$ifdef SYSPROCDEFINED}
  113. {$Error Can't determine processor type !}
  114. {$endif}
  115. {$i powerpc64.inc} { Case dependent, don't change }
  116. {$define SYSPROCDEFINED}
  117. {$endif cpupowerpc64}
  118. {$ifdef cpualpha}
  119. {$ifdef SYSPROCDEFINED}
  120. {$Error Can't determine processor type !}
  121. {$endif}
  122. {$i alpha.inc} { Case dependent, don't change }
  123. {$define SYSPROCDEFINED}
  124. {$endif cpualpha}
  125. {$ifdef cpuiA64}
  126. {$ifdef SYSPROCDEFINED}
  127. {$Error Can't determine processor type !}
  128. {$endif}
  129. {$i ia64.inc} { Case dependent, don't change }
  130. {$define SYSPROCDEFINED}
  131. {$endif cpuiA64}
  132. {$ifdef cpusparc}
  133. {$ifdef SYSPROCDEFINED}
  134. {$Error Can't determine processor type !}
  135. {$endif}
  136. {$i sparc.inc} { Case dependent, don't change }
  137. {$define SYSPROCDEFINED}
  138. {$endif cpusparc}
  139. {$ifdef cpuarm}
  140. {$ifdef SYSPROCDEFINED}
  141. {$Error Can't determine processor type !}
  142. {$endif}
  143. {$i arm.inc} { Case dependent, don't change }
  144. {$define SYSPROCDEFINED}
  145. {$endif cpuarm}
  146. {$ifdef cpuavr}
  147. {$ifdef SYSPROCDEFINED}
  148. {$Error Can't determine processor type !}
  149. {$endif}
  150. {$i avr.inc} { Case dependent, don't change }
  151. {$define SYSPROCDEFINED}
  152. {$endif cpuavr}
  153. procedure fillchar(var x;count : SizeInt;value : boolean);
  154. begin
  155. fillchar(x,count,byte(value));
  156. end;
  157. procedure fillchar(var x;count : SizeInt;value : char);
  158. begin
  159. fillchar(x,count,byte(value));
  160. end;
  161. procedure FillByte (var x;count : SizeInt;value : byte );
  162. begin
  163. FillChar (X,Count,VALUE);
  164. end;
  165. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  166. begin
  167. IndexChar:=IndexByte(Buf,Len,byte(B));
  168. end;
  169. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  170. begin
  171. CompareChar:=CompareByte(buf1,buf2,len);
  172. end;
  173. Function StrLCopy(Dest,Source: PRtlChar; MaxLen: SizeInt): PRtlChar;
  174. var
  175. counter: SizeInt;
  176. Begin
  177. counter := 0;
  178. while (Source^ <> #0) and (counter < MaxLen) do
  179. Begin
  180. Dest^ := Source^;
  181. Inc(Dest);
  182. Inc(Source);
  183. Inc(counter);
  184. end;
  185. { terminate the string }
  186. Dest^ := #0;
  187. StrLCopy := Dest;
  188. end;
  189. { Include generic pascal only routines which are not defined in the processor
  190. specific include file }
  191. {$I generic.inc}
  192. {****************************************************************************
  193. Set Handling
  194. ****************************************************************************}
  195. { Include set support which is processor specific}
  196. {$i set.inc}
  197. { Include generic pascal routines for sets if the processor }
  198. { specific routines are not available. }
  199. {$i genset.inc}
  200. {****************************************************************************
  201. Math Routines
  202. ****************************************************************************}
  203. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  204. begin
  205. Hi := b shr 4
  206. end;
  207. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  208. begin
  209. Lo := b and $0f
  210. end;
  211. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  212. Begin
  213. Swap := SwapEndian(X);
  214. End;
  215. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  216. Begin
  217. Swap := SwapEndian(X);
  218. End;
  219. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  220. Begin
  221. Swap:=(X and $ffff) shl 16 + (X shr 16)
  222. End;
  223. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  224. Begin
  225. Swap:=(X and $ffff) shl 16 + (X shr 16)
  226. End;
  227. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  228. Begin
  229. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  230. End;
  231. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  232. Begin
  233. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  234. End;
  235. {$ifdef SUPPORT_DOUBLE}
  236. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  237. begin
  238. D:=real2double(b);
  239. end;
  240. {$endif SUPPORT_DOUBLE}
  241. {$ifdef SUPPORT_EXTENDED}
  242. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  243. begin
  244. e:=real2double(b);
  245. end;
  246. {$endif SUPPORT_EXTENDED}
  247. {$ifndef FPUNONE}
  248. {$ifdef FPC_USE_LIBC}
  249. { Include libc versions }
  250. {$i cgenmath.inc}
  251. {$endif FPC_USE_LIBC}
  252. { Include processor specific routines }
  253. {$I math.inc}
  254. { Include generic version }
  255. {$I genmath.inc}
  256. {$endif}
  257. {$i gencurr.inc}
  258. function aligntoptr(p : pointer) : pointer;inline;
  259. begin
  260. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  261. result:=align(p,sizeof(p));
  262. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  263. result:=p;
  264. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  265. end;
  266. {****************************************************************************
  267. Subroutines for String handling
  268. ****************************************************************************}
  269. { Needs to be before RTTI handling }
  270. {$i sstrings.inc}
  271. { requires sstrings.inc for initval }
  272. {$I int64p.inc}
  273. {$I int64.inc}
  274. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  275. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  276. {$i astrings.inc}
  277. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  278. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  279. { this is for bootstrappung with 2.2.x }
  280. {$ifdef VER2_2}
  281. {$i wustring22.inc}
  282. {$else VER2_2}
  283. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  284. {$i wstrings.inc}
  285. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  286. {$i ustrings.inc}
  287. {$endif VER2_2}
  288. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  289. {$i aliases.inc}
  290. {*****************************************************************************
  291. Dynamic Array support
  292. *****************************************************************************}
  293. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  294. {$i dynarr.inc}
  295. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  296. {*****************************************************************************
  297. Object Pascal support
  298. *****************************************************************************}
  299. {$ifdef FPC_HAS_FEATURE_CLASSES}
  300. {$i objpas.inc}
  301. {$endif FPC_HAS_FEATURE_CLASSES}
  302. {*****************************************************************************
  303. Variant support
  304. *****************************************************************************}
  305. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  306. {$i variant.inc}
  307. {$endif FPC_HAS_FEATURE_VARIANTS}
  308. {****************************************************************************
  309. Run-Time Type Information (RTTI)
  310. ****************************************************************************}
  311. {$ifdef FPC_HAS_FEATURE_RTTI}
  312. {$i rtti.inc}
  313. {$endif FPC_HAS_FEATURE_VARIANTS}
  314. {----------------------------------------------------------------------
  315. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  316. Pseudo-Random Number Generator.
  317. What is Mersenne Twister?
  318. Mersenne Twister(MT) is a pseudorandom number generator developped by
  319. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  320. 1996-1997. MT has the following merits:
  321. It is designed with consideration on the flaws of various existing
  322. generators.
  323. Far longer period and far higher order of equidistribution than any
  324. other implemented generators. (It is proved that the period is 2^19937-1,
  325. and 623-dimensional equidistribution property is assured.)
  326. Fast generation. (Although it depends on the system, it is reported that
  327. MT is sometimes faster than the standard ANSI-C library in a system
  328. with pipeline and cache memory.)
  329. Efficient use of the memory. (The implemented C-code mt19937.c
  330. consumes only 624 words of working area.)
  331. home page
  332. http://www.math.keio.ac.jp/~matumoto/emt.html
  333. original c source
  334. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  335. Coded by Takuji Nishimura, considering the suggestions by
  336. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  337. This library is free software; you can redistribute it and/or
  338. modify it under the terms of the GNU Library General Public
  339. License as published by the Free Software Foundation; either
  340. version 2 of the License, or (at your option) any later
  341. version.
  342. This library is distributed in the hope that it will be useful,
  343. but WITHOUT ANY WARRANTY; without even the implied warranty of
  344. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  345. See the GNU Library General Public License for more details.
  346. You should have received a copy of the GNU Library General
  347. Public License along with this library; if not, write to the
  348. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  349. 02111-1307 USA
  350. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  351. When you use this, send an email to: [email protected]
  352. with an appropriate reference to your work.
  353. REFERENCE
  354. M. Matsumoto and T. Nishimura,
  355. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  356. Pseudo-Random Number Generator",
  357. ACM Transactions on Modeling and Computer Simulation,
  358. Vol. 8, No. 1, January 1998, pp 3--30.
  359. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  360. http://www.rksolution.cz/delphi/tips.htm
  361. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  362. 2003/10/26: adapted to use the improved intialisation mentioned at
  363. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  364. removed the assembler code
  365. ----------------------------------------------------------------------}
  366. {$R-} {range checking off}
  367. {$Q-} {overflow checking off}
  368. { Period parameter }
  369. Const
  370. MT19937N=624;
  371. Type
  372. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  373. { Period parameters }
  374. const
  375. MT19937M=397;
  376. MT19937MATRIX_A =$9908b0df; // constant vector a
  377. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  378. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  379. { Tempering parameters }
  380. TEMPERING_MASK_B=longint($9d2c5680);
  381. TEMPERING_MASK_C=longint($efc60000);
  382. VAR
  383. mt : tMT19937StateArray;
  384. const
  385. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  386. { Initializing the array with a seed }
  387. procedure sgenrand_MT19937(seed: longint);
  388. var
  389. i: longint;
  390. begin
  391. mt[0] := seed;
  392. for i := 1 to MT19937N-1 do
  393. begin
  394. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  395. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  396. { In the previous versions, MSBs of the seed affect }
  397. { only MSBs of the array mt[]. }
  398. { 2002/01/09 modified by Makoto Matsumoto }
  399. end;
  400. mti := MT19937N;
  401. end;
  402. function genrand_MT19937: longint;
  403. const
  404. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  405. var
  406. y: longint;
  407. kk: longint;
  408. begin
  409. if RandSeed<>OldRandSeed then
  410. mti:=MT19937N+1;
  411. if (mti >= MT19937N) { generate MT19937N longints at one time }
  412. then begin
  413. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  414. begin
  415. sgenrand_MT19937(randseed); // default initial seed is used
  416. { hack: randseed is not used more than once in this algorithm. Most }
  417. { user changes are re-initialising reandseed with the value it had }
  418. { at the start -> with the "not", we will detect this change. }
  419. { Detecting other changes is not useful, since the generated }
  420. { numbers will be different anyway. }
  421. randseed := not(randseed);
  422. oldrandseed := randseed;
  423. end;
  424. for kk:=0 to MT19937N-MT19937M-1 do begin
  425. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  426. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  427. end;
  428. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  429. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  430. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  431. end;
  432. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  433. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  434. mti := 0;
  435. end;
  436. y := mt[mti]; inc(mti);
  437. y := y xor (y shr 11);
  438. y := y xor (y shl 7) and TEMPERING_MASK_B;
  439. y := y xor (y shl 15) and TEMPERING_MASK_C;
  440. y := y xor (y shr 18);
  441. Result := y;
  442. end;
  443. function random(l:longint): longint;
  444. begin
  445. { otherwise we can return values = l (JM) }
  446. if (l < 0) then
  447. inc(l);
  448. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  449. end;
  450. function random(l:int64): int64;
  451. begin
  452. { always call random, so the random generator cycles (TP-compatible) (JM) }
  453. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  454. if (l<>0) then
  455. random := random mod l
  456. else
  457. random := 0;
  458. end;
  459. {$ifndef FPUNONE}
  460. function random: extended;
  461. begin
  462. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  463. end;
  464. {$endif}
  465. {****************************************************************************
  466. Memory Management
  467. ****************************************************************************}
  468. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  469. Begin
  470. ptr:=farpointer((sel shl 4)+off);
  471. End;
  472. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  473. Begin
  474. Cseg:=0;
  475. End;
  476. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  477. Begin
  478. Dseg:=0;
  479. End;
  480. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  481. Begin
  482. Sseg:=0;
  483. End;
  484. {*****************************************************************************
  485. Directory support.
  486. *****************************************************************************}
  487. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS) and defined(FPC_RTLSTRING_UTF16)}
  488. Procedure getdir(drivenr:byte;Var dir:AnsiString);
  489. { this is needed to also allow ansistrings, the RtlString version is
  490. OS dependent }
  491. var
  492. s : RtlString;
  493. begin
  494. getdir(drivenr,s);
  495. dir:=s;
  496. end;
  497. {$endif}
  498. Procedure getdir(drivenr:byte;Var dir:ShortString);
  499. { this is needed to also allow shortstrings, the RtlString version is
  500. OS dependent }
  501. var
  502. s : RtlString;
  503. begin
  504. getdir(drivenr,s);
  505. dir:=s;
  506. end;
  507. {$ifopt R+}
  508. {$define RangeCheckWasOn}
  509. {$R-}
  510. {$endif opt R+}
  511. {$ifopt I+}
  512. {$define IOCheckWasOn}
  513. {$I-}
  514. {$endif opt I+}
  515. {$ifopt Q+}
  516. {$define OverflowCheckWasOn}
  517. {$Q-}
  518. {$endif opt Q+}
  519. {*****************************************************************************
  520. Miscellaneous
  521. *****************************************************************************}
  522. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  523. begin
  524. HandleErrorFrame(201,get_frame);
  525. end;
  526. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  527. begin
  528. HandleErrorFrame(200,get_frame);
  529. end;
  530. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  531. begin
  532. HandleErrorFrame(215,get_frame);
  533. end;
  534. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  535. begin
  536. HandleErrorFrame(6,get_frame);
  537. end;
  538. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  539. var
  540. l : longint;
  541. HInoutRes : PWord;
  542. begin
  543. HInOutRes:=@InoutRes;
  544. if HInOutRes^<>0 then
  545. begin
  546. l:=HInOutRes^;
  547. HInOutRes^:=0;
  548. HandleErrorFrame(l,get_frame);
  549. end;
  550. end;
  551. Function IOResult:Word;
  552. var
  553. HInoutRes : PWord;
  554. Begin
  555. HInoutRes:=@InoutRes;
  556. IOResult:=HInOutRes^;
  557. HInOutRes^:=0;
  558. End;
  559. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  560. begin
  561. (* ThreadID is stored in a threadvar and made available in interface *)
  562. (* to allow setup of this value during thread initialization. *)
  563. GetThreadID := ThreadID;
  564. end;
  565. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  566. begin
  567. if res<0 then
  568. begin
  569. if assigned(SafeCallErrorProc) then
  570. SafeCallErrorProc(res,get_frame);
  571. HandleErrorFrame(229,get_frame);
  572. end;
  573. result:=res;
  574. end;
  575. {*****************************************************************************
  576. Stack check code
  577. *****************************************************************************}
  578. { be compatible with old code }
  579. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  580. {$define NO_GENERIC_STACK_CHECK}
  581. {$endif FPC_NO_GENERIC_STACK_CHECK}
  582. {$IFNDEF NO_GENERIC_STACK_CHECK}
  583. {$IFOPT S+}
  584. {$DEFINE STACKCHECK}
  585. {$ENDIF}
  586. {$S-}
  587. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  588. var
  589. c : Pointer;
  590. begin
  591. { Avoid recursive calls when called from the exit routines }
  592. if StackError then
  593. exit;
  594. c := Sptr - (stack_size + STACK_MARGIN);
  595. if (c <= StackBottom) then
  596. begin
  597. StackError:=true;
  598. HandleError(202);
  599. end;
  600. end;
  601. {$IFDEF STACKCHECK}
  602. {$S+}
  603. {$ENDIF}
  604. {$UNDEF STACKCHECK}
  605. {$ENDIF NO_GENERIC_STACK_CHECK}
  606. {*****************************************************************************
  607. Initialization / Finalization
  608. *****************************************************************************}
  609. const
  610. maxunits=1024; { See also files.pas of the compiler source }
  611. type
  612. TInitFinalRec=record
  613. InitProc,
  614. FinalProc : TProcedure;
  615. end;
  616. TInitFinalTable = record
  617. TableCount,
  618. InitCount : longint;
  619. Procs : array[1..maxunits] of TInitFinalRec;
  620. end;
  621. PInitFinalTable = ^TInitFinalTable;
  622. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  623. var
  624. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  625. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  626. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  627. var
  628. i : longint;
  629. begin
  630. { call cpu/fpu initialisation routine }
  631. fpc_cpuinit;
  632. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  633. InitFinalTable
  634. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  635. begin
  636. for i:=1 to TableCount do
  637. begin
  638. if assigned(Procs[i].InitProc) then
  639. Procs[i].InitProc();
  640. InitCount:=i;
  641. end;
  642. end;
  643. if assigned(InitProc) then
  644. TProcedure(InitProc)();
  645. end;
  646. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  647. begin
  648. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  649. InitFinalTable
  650. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  651. begin
  652. while (InitCount>0) do
  653. begin
  654. // we've to decrement the cound before calling the final. code
  655. // else a halt in the final. code leads to a endless loop
  656. dec(InitCount);
  657. if assigned(Procs[InitCount+1].FinalProc) then
  658. Procs[InitCount+1].FinalProc();
  659. end;
  660. end;
  661. end;
  662. {*****************************************************************************
  663. Error / Exit / ExitProc
  664. *****************************************************************************}
  665. Procedure system_exit;forward;
  666. {$ifdef FPC_HAS_FEATURE_HEAP}
  667. {$ifndef HAS_MEMORYMANAGER}
  668. //not needed if independant memory manager
  669. Procedure FinalizeHeap;forward;
  670. {$endif HAS_MEMORYMANAGER}
  671. {$endif FPC_HAS_FEATURE_HEAP}
  672. Procedure InternalExit;
  673. var
  674. current_exit : Procedure;
  675. pstdout : ^Text;
  676. {$if defined(OS2)}
  677. i : longint;
  678. {$endif}
  679. Begin
  680. {$ifdef SYSTEMDEBUG}
  681. writeln('InternalExit');
  682. {$endif SYSTEMDEBUG}
  683. while exitProc<>nil Do
  684. Begin
  685. InOutRes:=0;
  686. current_exit:=tProcedure(exitProc);
  687. exitProc:=nil;
  688. current_exit();
  689. End;
  690. { Finalize units }
  691. FinalizeUnits;
  692. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  693. { Show runtime error and exit }
  694. pstdout:=@stdout;
  695. If erroraddr<>nil Then
  696. Begin
  697. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  698. { to get a nice symify }
  699. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  700. dump_stack(pstdout^,ErrorBase);
  701. Writeln(pstdout^,'');
  702. End;
  703. { Make sure that all output is written to the redirected file }
  704. if Textrec(Output).Mode=fmOutput then
  705. Flush(Output);
  706. if Textrec(ErrOutput).Mode=fmOutput then
  707. Flush(ErrOutput);
  708. if Textrec(pstdout^).Mode=fmOutput then
  709. Flush(pstdout^);
  710. if Textrec(StdErr).Mode=fmOutput then
  711. Flush(StdErr);
  712. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  713. {$if defined(OS2)}
  714. { finally release the heap if possible, especially
  715. important for DLLs }
  716. for i:=0 to argc do
  717. sysfreemem(argv[i]);
  718. sysfreemem(argv);
  719. {$endif}
  720. {$ifdef LINUX}
  721. {sysfreemem already checks for nil}
  722. sysfreemem(calculated_cmdline);
  723. {$endif}
  724. {$ifdef BSD}
  725. sysfreemem(cmdline);
  726. {$endif}
  727. {$ifdef FPC_HAS_FEATURE_HEAP}
  728. {$ifndef HAS_MEMORYMANAGER}
  729. FinalizeHeap;
  730. {$endif HAS_MEMORYMANAGER}
  731. {$endif FPC_HAS_FEATURE_HEAP}
  732. End;
  733. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  734. begin
  735. InternalExit;
  736. System_exit;
  737. end;
  738. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  739. begin
  740. InternalExit;
  741. end;
  742. Procedure Halt(ErrNum: Byte);
  743. Begin
  744. ExitCode:=Errnum;
  745. Do_Exit;
  746. end;
  747. function SysBackTraceStr (Addr: Pointer): ShortString;
  748. begin
  749. SysBackTraceStr:=' $'+hexstr(addr);
  750. end;
  751. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPU86} register; {$endif}
  752. begin
  753. If pointer(ErrorProc)<>Nil then
  754. ErrorProc(Errno,addr,frame);
  755. errorcode:=word(Errno);
  756. erroraddr:=addr;
  757. errorbase:=frame;
  758. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  759. if ExceptAddrStack <> nil then
  760. raise TObject(nil) at addr,frame;
  761. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  762. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  763. {$ifdef FPC_LIMITED_EXITCODE}
  764. if errorcode > maxExitCode then
  765. halt(255)
  766. else
  767. {$endif FPC_LIMITED_EXITCODE}
  768. halt(errorcode);
  769. {$else FPC_HAS_FEATURE_EXITCODE}
  770. halt;
  771. {$endif FPC_HAS_FEATURE_EXITCODE}
  772. end;
  773. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  774. {
  775. Procedure to handle internal errors, i.e. not user-invoked errors
  776. Internal function should ALWAYS call HandleError instead of RunError.
  777. Can be used for exception handlers to specify the frame
  778. }
  779. begin
  780. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  781. end;
  782. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  783. {
  784. Procedure to handle internal errors, i.e. not user-invoked errors
  785. Internal function should ALWAYS call HandleError instead of RunError.
  786. }
  787. begin
  788. HandleErrorFrame(Errno,get_frame);
  789. end;
  790. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  791. begin
  792. errorcode:=w;
  793. erroraddr:=get_caller_addr(get_frame);
  794. errorbase:=get_caller_frame(get_frame);
  795. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  796. {$ifdef FPC_LIMITED_EXITCODE}
  797. if errorcode > maxExitCode then
  798. halt(255)
  799. else
  800. {$endif FPC_LIMITED_EXITCODE}
  801. halt(errorcode);
  802. {$else FPC_HAS_FEATURE_EXITCODE}
  803. halt;
  804. {$endif FPC_HAS_FEATURE_EXITCODE}
  805. end;
  806. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  807. Begin
  808. RunError (0);
  809. End;
  810. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  811. Begin
  812. Halt(0);
  813. End;
  814. Procedure Error(RunTimeError : TRunTimeError);
  815. begin
  816. RunError(RuntimeErrorExitCodes[RunTimeError]);
  817. end;
  818. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  819. Procedure dump_stack(var f : text;bp : Pointer);
  820. var
  821. i : Longint;
  822. prevbp : Pointer;
  823. is_dev : boolean;
  824. caller_frame,
  825. caller_addr : Pointer;
  826. Begin
  827. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  828. try
  829. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  830. prevbp:=bp-1;
  831. i:=0;
  832. is_dev:=do_isdevice(textrec(f).Handle);
  833. while bp > prevbp Do
  834. Begin
  835. caller_addr := get_caller_addr(bp);
  836. caller_frame := get_caller_frame(bp);
  837. if (caller_addr=nil) then
  838. break;
  839. Writeln(f,BackTraceStrFunc(caller_addr));
  840. if (caller_frame=nil) then
  841. break;
  842. Inc(i);
  843. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  844. break;
  845. prevbp:=bp;
  846. bp:=caller_frame;
  847. End;
  848. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  849. except
  850. { prevent endless dump if an exception occured }
  851. end;
  852. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  853. End;
  854. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  855. procedure DumpExceptionBackTrace(var f:text);
  856. var
  857. FrameNumber,
  858. FrameCount : longint;
  859. Frames : PPointer;
  860. begin
  861. if RaiseList=nil then
  862. exit;
  863. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  864. FrameCount:=RaiseList^.Framecount;
  865. Frames:=RaiseList^.Frames;
  866. for FrameNumber := 0 to FrameCount-1 do
  867. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  868. end;
  869. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  870. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  871. {$ifdef FPC_HAS_FEATURE_HEAP}
  872. Type
  873. PExitProcInfo = ^TExitProcInfo;
  874. TExitProcInfo = Record
  875. Next : PExitProcInfo;
  876. SaveExit : Pointer;
  877. Proc : TProcedure;
  878. End;
  879. const
  880. ExitProcList: PExitProcInfo = nil;
  881. Procedure DoExitProc;
  882. var
  883. P : PExitProcInfo;
  884. Proc : TProcedure;
  885. Begin
  886. P:=ExitProcList;
  887. ExitProcList:=P^.Next;
  888. ExitProc:=P^.SaveExit;
  889. Proc:=P^.Proc;
  890. DisPose(P);
  891. Proc();
  892. End;
  893. Procedure AddExitProc(Proc: TProcedure);
  894. var
  895. P : PExitProcInfo;
  896. Begin
  897. New(P);
  898. P^.Next:=ExitProcList;
  899. P^.SaveExit:=ExitProc;
  900. P^.Proc:=Proc;
  901. ExitProcList:=P;
  902. ExitProc:=@DoExitProc;
  903. End;
  904. {$endif FPC_HAS_FEATURE_HEAP}
  905. {$ifdef FPC_HAS_FEATURE_HEAP}
  906. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  907. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  908. // Note: for internal use by skilled programmers only
  909. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  910. var p : ppchar;
  911. i : LongInt;
  912. begin
  913. if High(s)<Low(s) Then Exit(NIL);
  914. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  915. // for cmd
  916. if p=nil then
  917. begin
  918. {$ifdef xunix}
  919. fpseterrno(ESysEnomem);
  920. {$endif}
  921. exit(NIL);
  922. end;
  923. for i:=low(s) to high(s) do
  924. p[i+Reserveentries]:=pchar(s[i]);
  925. p[high(s)+1+Reserveentries]:=nil;
  926. ArrayStringToPPchar:=p;
  927. end;
  928. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  929. {
  930. Create a PPChar to structure of pchars which are the arguments specified
  931. in the string S. Especially usefull for creating an ArgV for Exec-calls
  932. }
  933. begin
  934. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  935. end;
  936. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  937. var
  938. i,nr : longint;
  939. Buf : ^char;
  940. p : ppchar;
  941. begin
  942. buf:=s;
  943. nr:=1;
  944. while (buf^<>#0) do // count nr of args
  945. begin
  946. while (buf^ in [' ',#9,#10]) do // Kill separators.
  947. inc(buf);
  948. inc(nr);
  949. if buf^='"' Then // quotes argument?
  950. begin
  951. inc(buf);
  952. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  953. inc(buf);
  954. if buf^='"' then // skip closing quote.
  955. inc(buf);
  956. end
  957. else
  958. begin // else std
  959. while not (buf^ in [' ',#0,#9,#10]) do
  960. inc(buf);
  961. end;
  962. end;
  963. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  964. StringToPPChar:=p;
  965. if p=nil then
  966. begin
  967. {$ifdef xunix}
  968. fpseterrno(ESysEnomem);
  969. {$endif}
  970. exit;
  971. end;
  972. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  973. buf:=s;
  974. while (buf^<>#0) do
  975. begin
  976. while (buf^ in [' ',#9,#10]) do // Kill separators.
  977. begin
  978. buf^:=#0;
  979. inc(buf);
  980. end;
  981. if buf^='"' Then // quotes argument?
  982. begin
  983. inc(buf);
  984. p^:=buf;
  985. inc(p);
  986. p^:=nil;
  987. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  988. inc(buf);
  989. if buf^='"' then // skip closing quote.
  990. begin
  991. buf^:=#0;
  992. inc(buf);
  993. end;
  994. end
  995. else
  996. begin
  997. p^:=buf;
  998. inc(p);
  999. p^:=nil;
  1000. while not (buf^ in [' ',#0,#9,#10]) do
  1001. inc(buf);
  1002. end;
  1003. end;
  1004. end;
  1005. {$endif FPC_HAS_FEATURE_HEAP}
  1006. {*****************************************************************************
  1007. Abstract/Assert support.
  1008. *****************************************************************************}
  1009. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1010. begin
  1011. If pointer(AbstractErrorProc)<>nil then
  1012. AbstractErrorProc();
  1013. HandleErrorFrame(211,get_frame);
  1014. end;
  1015. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1016. begin
  1017. if pointer(AssertErrorProc)<>nil then
  1018. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1019. else
  1020. HandleErrorFrame(227,get_frame);
  1021. end;
  1022. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1023. begin
  1024. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1025. If msg='' then
  1026. write(stderr,'Assertion failed')
  1027. else
  1028. write(stderr,msg);
  1029. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1030. Writeln(stderr,'');
  1031. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  1032. Halt(227);
  1033. {$else FPC_HAS_FEATURE_EXITCODE}
  1034. halt;
  1035. {$endif FPC_HAS_FEATURE_EXITCODE}
  1036. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1037. end;
  1038. {*****************************************************************************
  1039. SetJmp/LongJmp support.
  1040. *****************************************************************************}
  1041. {$i setjump.inc}
  1042. {$ifdef IOCheckWasOn}
  1043. {$I+}
  1044. {$endif}
  1045. {$ifdef RangeCheckWasOn}
  1046. {$R+}
  1047. {$endif}
  1048. {$ifdef OverflowCheckWasOn}
  1049. {$Q+}
  1050. {$endif}
  1051. {*****************************************************************************
  1052. Heap
  1053. *****************************************************************************}
  1054. {$ifdef FPC_HAS_FEATURE_HEAP}
  1055. {$i sysheap.inc}
  1056. {$i heap.inc}
  1057. {$endif FPC_HAS_FEATURE_HEAP}
  1058. {*****************************************************************************
  1059. Thread support
  1060. *****************************************************************************}
  1061. {$ifdef FPC_HAS_FEATURE_THREADING}
  1062. { Generic threadmanager }
  1063. {$i thread.inc}
  1064. { Generic threadvar support }
  1065. {$i threadvr.inc}
  1066. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1067. { OS Dependent implementation }
  1068. {$i systhrd.inc}
  1069. {$endif DISABLE_NO_THREAD_MANAGER}
  1070. {$endif FPC_HAS_FEATURE_THREADING}
  1071. {*****************************************************************************
  1072. File Handling
  1073. *****************************************************************************}
  1074. { Allow slash and backslash as separators }
  1075. procedure DoDirSeparators(p:PRtlChar);
  1076. begin
  1077. while p^ <> #0 do begin
  1078. if AnsiChar(p^) in AllowDirectorySeparators then
  1079. p^:=DirectorySeparator;
  1080. Inc(p);
  1081. end;
  1082. end;
  1083. procedure DoDirSeparators(var p:shortstring);
  1084. var
  1085. i : longint;
  1086. begin
  1087. for i:=1 to length(p) do
  1088. if p[i] in AllowDirectorySeparators then
  1089. p[i]:=DirectorySeparator;
  1090. end;
  1091. { OS dependent low level file functions }
  1092. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1093. {$i sysfile.inc}
  1094. {$endif FPC_HAS_FEATURE_FILEIO}
  1095. { Text file }
  1096. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1097. {$i text.inc}
  1098. {$endif FPC_HAS_FEATURE_TEXTIO}
  1099. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1100. { Untyped file }
  1101. {$i file.inc}
  1102. { Typed file }
  1103. {$i typefile.inc}
  1104. {$endif FPC_HAS_FEATURE_FILEIO}
  1105. {*****************************************************************************
  1106. Directory Handling
  1107. *****************************************************************************}
  1108. { OS dependent dir functions }
  1109. {$i sysdir.inc}
  1110. {*****************************************************************************
  1111. Resources support
  1112. *****************************************************************************}
  1113. {$i sysres.inc}
  1114. const
  1115. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1116. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1117. (* It is possible to provide platform specific implementation performing *)
  1118. (* special initialization; default implementation just sets the procedural *)
  1119. (* variable to make it available for use from the exception handler. *)
  1120. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1121. begin
  1122. (* Return either nil or previous handler *)
  1123. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1124. CtrlBreakHandler := Handler;
  1125. end;
  1126. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}