system.inc 36 KB

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