system.inc 35 KB

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