system.inc 36 KB

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