system.inc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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. { contains the definition of the TExceptAddr type, which is required
  11. by the compiler to generate code for any routine containing
  12. implicit or explicit exceptions }
  13. {$i excepth.inc}
  14. { ObjpasInt is the integer type, equivalent to Objpas.Integer (the Integer
  15. type in ObjFpc and Delphi modes). It is defined here for use in the
  16. implementation part of the System unit. }
  17. {$ifdef CPU16}
  18. type
  19. ObjpasInt = SmallInt;
  20. {$else CPU16}
  21. type
  22. ObjpasInt = LongInt;
  23. {$endif CPU16}
  24. { The RTTI is implemented through a series of constants : }
  25. Const
  26. // please update tkManagedTypes below if you add new
  27. // values
  28. tkUnknown = 0;
  29. tkInteger = 1;
  30. tkChar = 2;
  31. tkEnumeration = 3;
  32. tkFloat = 4;
  33. tkSet = 5;
  34. tkMethod = 6;
  35. tkSString = 7;
  36. tkString = tkSString;
  37. tkLString = 8;
  38. tkAString = 9;
  39. tkWString = 10;
  40. tkVariant = 11;
  41. tkArray = 12;
  42. tkRecord = 13;
  43. tkInterface = 14;
  44. tkClass = 15;
  45. tkObject = 16;
  46. tkWChar = 17;
  47. tkBool = 18;
  48. tkInt64 = 19;
  49. tkQWord = 20;
  50. tkDynArray = 21;
  51. tkInterfaceCorba = 22;
  52. tkProcVar = 23;
  53. tkUString = 24;
  54. tkHelper = 26;
  55. tkFile = 27;
  56. tkClassRef = 28;
  57. tkPointer = 29;
  58. // all potentially managed types
  59. tkManagedTypes = [tkAstring,tkWstring,tkUstring,tkArray,
  60. tkObject,tkRecord,tkDynArray,tkInterface,tkVariant];
  61. {****************************************************************************
  62. Local types
  63. ****************************************************************************}
  64. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  65. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  66. {$if High(errorcode)<>maxExitCode}
  67. {$define FPC_LIMITED_EXITCODE}
  68. {$endif}
  69. {$else}
  70. {$define FPC_LIMITED_EXITCODE}
  71. {$endif FPC_OBJFPC_EXTENDED_IF}
  72. {$endif FPC_HAS_FEATURE_EXITCODE}
  73. Procedure HandleError (Errno : Longint); external name 'FPC_HANDLEERROR';
  74. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  75. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer); forward;
  76. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer); forward;
  77. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  78. type
  79. FileFunc = Procedure(var t : TextRec);
  80. {$endif FPC_HAS_FEATURE_TEXTIO}
  81. const
  82. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  83. { Random / Randomize constants }
  84. OldRandSeed : Cardinal = 0;
  85. { For Error Handling.}
  86. ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE';
  87. { Used by the ansi/widestrings and maybe also other things in the future }
  88. var
  89. { widechar, because also used by widestring -> pwidechar conversions }
  90. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  91. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  92. { if the OS does the stack checking, we don't need any stklen from the
  93. main program }
  94. initialstklen : SizeUint;external name '__stklen';
  95. {$endif FPC_NO_GENERIC_STACK_CHECK}
  96. { checks whether the given suggested size for the stack of the current
  97. thread is acceptable. If this is the case, returns it unaltered.
  98. Otherwise it should return an acceptable value.
  99. Operating systems that automatically expand their stack on demand, should
  100. simply return a very large value.
  101. Operating systems which do not have a possibility to retrieve stack size
  102. information, should simply return the given stklen value (This is the default
  103. implementation).
  104. }
  105. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  106. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  107. {$endif FPC_HAS_FEATURE_STACKCHECK}
  108. {*****************************************************************************
  109. OS dependent Helpers/Syscalls
  110. *****************************************************************************}
  111. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  112. it isn't, and is used before the actual definition is encountered }
  113. {$ifdef FPC_HAS_FEATURE_FILEIO}
  114. function do_isdevice(handle:thandle):boolean;forward;
  115. {$endif FPC_HAS_FEATURE_FILEIO}
  116. {$i sysos.inc}
  117. {****************************************************************************
  118. Include processor specific routines
  119. ****************************************************************************}
  120. {$ifdef FPC_USE_LIBC}
  121. { Under Haiku, bcopy cause a problem when searching for include file
  122. in the compiler. So, we use the internal implementation for now
  123. under BeOS and Haiku. }
  124. {$ifndef BEOS}
  125. { prefer libc implementations over our own, as they're most likely faster }
  126. {$i cgeneric.inc}
  127. { is now declared as external reference to another routine in the interface }
  128. {$i cgenstr.inc}
  129. {$endif}
  130. {$endif FPC_USE_LIBC}
  131. {$ifdef cpui386}
  132. {$ifdef SYSPROCDEFINED}
  133. {$Error Can't determine processor type !}
  134. {$endif}
  135. {$i i386.inc} { Case dependent, don't change }
  136. {$define SYSPROCDEFINED}
  137. {$endif cpui386}
  138. {$ifdef cpui8086}
  139. {$ifdef SYSPROCDEFINED}
  140. {$Error Can't determine processor type !}
  141. {$endif}
  142. {$i i8086.inc} { Case dependent, don't change }
  143. {$define SYSPROCDEFINED}
  144. {$endif cpui8086}
  145. {$ifdef cpum68k}
  146. {$ifdef SYSPROCDEFINED}
  147. {$Error Can't determine processor type !}
  148. {$endif}
  149. {$i m68k.inc} { Case dependent, don't change }
  150. {$define SYSPROCDEFINED}
  151. {$endif cpum68k}
  152. {$ifdef cpux86_64}
  153. {$ifdef SYSPROCDEFINED}
  154. {$Error Can't determine processor type !}
  155. {$endif}
  156. {$i x86_64.inc} { Case dependent, don't change }
  157. {$define SYSPROCDEFINED}
  158. {$endif cpux86_64}
  159. {$ifdef cpupowerpc32}
  160. {$ifdef SYSPROCDEFINED}
  161. {$Error Can't determine processor type !}
  162. {$endif}
  163. {$i powerpc.inc} { Case dependent, don't change }
  164. {$define SYSPROCDEFINED}
  165. {$endif cpupowerpc32}
  166. {$ifdef cpupowerpc64}
  167. {$ifdef SYSPROCDEFINED}
  168. {$Error Can't determine processor type !}
  169. {$endif}
  170. {$i powerpc64.inc} { Case dependent, don't change }
  171. {$define SYSPROCDEFINED}
  172. {$endif cpupowerpc64}
  173. {$ifdef cpualpha}
  174. {$ifdef SYSPROCDEFINED}
  175. {$Error Can't determine processor type !}
  176. {$endif}
  177. {$i alpha.inc} { Case dependent, don't change }
  178. {$define SYSPROCDEFINED}
  179. {$endif cpualpha}
  180. {$ifdef cpuiA64}
  181. {$ifdef SYSPROCDEFINED}
  182. {$Error Can't determine processor type !}
  183. {$endif}
  184. {$i ia64.inc} { Case dependent, don't change }
  185. {$define SYSPROCDEFINED}
  186. {$endif cpuiA64}
  187. {$ifdef cpusparc}
  188. {$ifdef SYSPROCDEFINED}
  189. {$Error Can't determine processor type !}
  190. {$endif}
  191. {$i sparc.inc} { Case dependent, don't change }
  192. {$define SYSPROCDEFINED}
  193. {$endif cpusparc}
  194. {$ifdef cpuarm}
  195. {$ifdef SYSPROCDEFINED}
  196. {$Error Can't determine processor type !}
  197. {$endif}
  198. {$i armdefines.inc}
  199. {$if defined(CPUTHUMB2)}
  200. {$i thumb2.inc} { Case dependent, don't change }
  201. {$else}
  202. {$if defined(CPUTHUMB)}
  203. {$i thumb.inc} { Case dependent, don't change }
  204. {$else}
  205. {$i arm.inc} { Case dependent, don't change }
  206. {$endif}
  207. {$endif}
  208. {$define SYSPROCDEFINED}
  209. {$endif cpuarm}
  210. {$ifdef cpuavr}
  211. {$ifdef SYSPROCDEFINED}
  212. {$Error Can't determine processor type !}
  213. {$endif}
  214. {$i avr.inc} { Case dependent, don't change }
  215. {$define SYSPROCDEFINED}
  216. {$endif cpuavr}
  217. {$ifdef cpumipsel}
  218. {$ifdef SYSPROCDEFINED}
  219. {$Error Can't determine processor type !}
  220. {$endif}
  221. { there is no mipsel.inc, we use mips.inc instead }
  222. {$i mips.inc} { Case dependent, don't change }
  223. {$define SYSPROCDEFINED}
  224. {$else not cpumipsel}
  225. {$ifdef cpumips}
  226. {$ifdef SYSPROCDEFINED}
  227. {$Error Can't determine processor type !}
  228. {$endif}
  229. {$i mips.inc} { Case dependent, don't change }
  230. {$define SYSPROCDEFINED}
  231. {$endif cpumips}
  232. {$endif not cpumipsel}
  233. {$ifndef SYSPROCDEFINED}
  234. {$Error Can't determine processor type !}
  235. {$endif}
  236. procedure fillchar(var x;count : SizeInt;value : boolean);
  237. begin
  238. fillchar(x,count,byte(value));
  239. end;
  240. procedure fillchar(var x;count : SizeInt;value : char);
  241. begin
  242. fillchar(x,count,byte(value));
  243. end;
  244. procedure FillByte (var x;count : SizeInt;value : byte );
  245. begin
  246. FillChar (X,Count,VALUE);
  247. end;
  248. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  249. begin
  250. IndexChar:=IndexByte(Buf,Len,byte(B));
  251. end;
  252. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  253. begin
  254. CompareChar:=CompareByte(buf1,buf2,len);
  255. end;
  256. procedure fpc_zeromem(p:pointer;len:ptruint);
  257. begin
  258. FillChar(p^,len,0);
  259. end;
  260. procedure fpc_fillmem(out data;len:ptruint;b : byte);
  261. begin
  262. FillByte(data,len,b);
  263. end;
  264. { Include generic pascal only routines which are not defined in the processor
  265. specific include file }
  266. {$I generic.inc}
  267. {****************************************************************************
  268. Set Handling
  269. ****************************************************************************}
  270. { Include set support which is processor specific}
  271. {$i set.inc}
  272. { Include generic pascal routines for sets if the processor }
  273. { specific routines are not available. }
  274. {$i genset.inc}
  275. {****************************************************************************
  276. Math Routines
  277. ****************************************************************************}
  278. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  279. begin
  280. Hi := b shr 4
  281. end;
  282. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  283. begin
  284. Lo := b and $0f
  285. end;
  286. Function Swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  287. Begin
  288. Swap := SwapEndian(X);
  289. End;
  290. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  291. Begin
  292. Swap := SwapEndian(X);
  293. End;
  294. Function Swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  295. Begin
  296. Swap:=(X and $ffff) shl 16 + (X shr 16)
  297. End;
  298. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  299. Begin
  300. Swap:=(X and $ffff) shl 16 + (X shr 16)
  301. End;
  302. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  303. Begin
  304. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  305. End;
  306. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  307. Begin
  308. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  309. End;
  310. {$ifdef SUPPORT_DOUBLE}
  311. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  312. begin
  313. D:=real2double(b);
  314. end;
  315. {$endif SUPPORT_DOUBLE}
  316. {$ifdef SUPPORT_EXTENDED}
  317. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  318. begin
  319. e:=real2double(b);
  320. end;
  321. {$endif SUPPORT_EXTENDED}
  322. {$ifndef FPUNONE}
  323. {$ifdef FPC_USE_LIBC}
  324. { Include libc versions }
  325. {$i cgenmath.inc}
  326. {$endif FPC_USE_LIBC}
  327. { Include processor specific routines }
  328. {$I math.inc}
  329. { Include generic version }
  330. {$I genmath.inc}
  331. {$endif}
  332. {$i gencurr.inc}
  333. function aligntoptr(p : pointer) : pointer;inline;
  334. begin
  335. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  336. result:=align(p,sizeof(p));
  337. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  338. result:=p;
  339. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  340. end;
  341. {****************************************************************************
  342. Subroutines for String handling
  343. ****************************************************************************}
  344. { Needs to be before RTTI handling }
  345. {$i sstrings.inc}
  346. { requires sstrings.inc for initval }
  347. {$I int64p.inc}
  348. {$I int64.inc}
  349. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  350. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  351. {$i astrings.inc}
  352. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  353. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  354. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  355. {$i wstrings.inc}
  356. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  357. {$i ustrings.inc}
  358. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  359. {$i aliases.inc}
  360. {*****************************************************************************
  361. Dynamic Array support
  362. *****************************************************************************}
  363. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  364. {$i dynarr.inc}
  365. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  366. {*****************************************************************************
  367. Object Pascal support
  368. *****************************************************************************}
  369. {$ifdef FPC_HAS_FEATURE_CLASSES}
  370. {$i objpas.inc}
  371. {$endif FPC_HAS_FEATURE_CLASSES}
  372. {*****************************************************************************
  373. Variant support
  374. *****************************************************************************}
  375. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  376. {$i variant.inc}
  377. {$endif FPC_HAS_FEATURE_VARIANTS}
  378. {****************************************************************************
  379. Run-Time Type Information (RTTI)
  380. ****************************************************************************}
  381. {$ifdef FPC_HAS_FEATURE_RTTI}
  382. {$i rtti.inc}
  383. {$endif FPC_HAS_FEATURE_RTTI}
  384. {$if defined(FPC_HAS_FEATURE_RANDOM)}
  385. {----------------------------------------------------------------------
  386. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  387. Pseudo-Random Number Generator.
  388. What is Mersenne Twister?
  389. Mersenne Twister(MT) is a pseudorandom number generator developped by
  390. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  391. 1996-1997. MT has the following merits:
  392. It is designed with consideration on the flaws of various existing
  393. generators.
  394. Far longer period and far higher order of equidistribution than any
  395. other implemented generators. (It is proved that the period is 2^19937-1,
  396. and 623-dimensional equidistribution property is assured.)
  397. Fast generation. (Although it depends on the system, it is reported that
  398. MT is sometimes faster than the standard ANSI-C library in a system
  399. with pipeline and cache memory.)
  400. Efficient use of the memory. (The implemented C-code mt19937.c
  401. consumes only 624 words of working area.)
  402. home page
  403. http://www.math.keio.ac.jp/~matumoto/emt.html
  404. original c source
  405. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  406. Coded by Takuji Nishimura, considering the suggestions by
  407. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  408. This library is free software; you can redistribute it and/or
  409. modify it under the terms of the GNU Library General Public
  410. License as published by the Free Software Foundation; either
  411. version 2 of the License, or (at your option) any later
  412. version.
  413. This library is distributed in the hope that it will be useful,
  414. but WITHOUT ANY WARRANTY; without even the implied warranty of
  415. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  416. See the GNU Library General Public License for more details.
  417. You should have received a copy of the GNU Library General
  418. Public License along with this library; if not, write to the
  419. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  420. 02111-1307 USA
  421. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  422. When you use this, send an email to: [email protected]
  423. with an appropriate reference to your work.
  424. REFERENCE
  425. M. Matsumoto and T. Nishimura,
  426. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  427. Pseudo-Random Number Generator",
  428. ACM Transactions on Modeling and Computer Simulation,
  429. Vol. 8, No. 1, January 1998, pp 3--30.
  430. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  431. http://www.rksolution.cz/delphi/tips.htm
  432. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  433. 2003/10/26: adapted to use the improved intialisation mentioned at
  434. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  435. removed the assembler code
  436. ----------------------------------------------------------------------}
  437. {$R-} {range checking off}
  438. {$Q-} {overflow checking off}
  439. { Period parameter }
  440. Const
  441. MT19937N=624;
  442. Type
  443. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  444. { Period parameters }
  445. const
  446. MT19937M=397;
  447. MT19937MATRIX_A =$9908b0df; // constant vector a
  448. MT19937UPPER_MASK=longint($80000000); // most significant w-r bits
  449. MT19937LOWER_MASK=longint($7fffffff); // least significant r bits
  450. { Tempering parameters }
  451. TEMPERING_MASK_B=longint($9d2c5680);
  452. TEMPERING_MASK_C=longint($efc60000);
  453. VAR
  454. mt : tMT19937StateArray;
  455. const
  456. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  457. { Initializing the array with a seed }
  458. procedure sgenrand_MT19937(seed: longint);
  459. var
  460. i: longint;
  461. begin
  462. mt[0] := seed;
  463. for i := 1 to MT19937N-1 do
  464. begin
  465. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  466. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  467. { In the previous versions, MSBs of the seed affect }
  468. { only MSBs of the array mt[]. }
  469. { 2002/01/09 modified by Makoto Matsumoto }
  470. end;
  471. mti := MT19937N;
  472. end;
  473. function genrand_MT19937: longint;
  474. const
  475. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  476. var
  477. y: longint;
  478. kk: longint;
  479. begin
  480. if RandSeed<>OldRandSeed then
  481. mti:=MT19937N+1;
  482. if (mti >= MT19937N) { generate MT19937N longints at one time }
  483. then begin
  484. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  485. begin
  486. sgenrand_MT19937(randseed); // default initial seed is used
  487. { hack: randseed is not used more than once in this algorithm. Most }
  488. { user changes are re-initialising reandseed with the value it had }
  489. { at the start -> with the "not", we will detect this change. }
  490. { Detecting other changes is not useful, since the generated }
  491. { numbers will be different anyway. }
  492. randseed := not(randseed);
  493. oldrandseed := randseed;
  494. end;
  495. for kk:=0 to MT19937N-MT19937M-1 do begin
  496. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  497. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  498. end;
  499. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  500. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  501. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  502. end;
  503. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  504. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  505. mti := 0;
  506. end;
  507. y := mt[mti]; inc(mti);
  508. y := y xor (y shr 11);
  509. y := y xor (y shl 7) and TEMPERING_MASK_B;
  510. y := y xor (y shl 15) and TEMPERING_MASK_C;
  511. y := y xor (y shr 18);
  512. Result := y;
  513. end;
  514. function random(l:longint): longint;
  515. begin
  516. { otherwise we can return values = l (JM) }
  517. if (l < 0) then
  518. inc(l);
  519. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  520. end;
  521. function random(l:int64): int64;
  522. begin
  523. { always call random, so the random generator cycles (TP-compatible) (JM) }
  524. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  525. if (l<>0) then
  526. random := random mod l
  527. else
  528. random := 0;
  529. end;
  530. {$ifndef FPUNONE}
  531. function random: extended;
  532. begin
  533. random := cardinal(genrand_MT19937) * (extended(1.0)/(int64(1) shl 32));
  534. end;
  535. {$endif}
  536. {$endif FPC_HAS_FEATURE_RANDOM}
  537. {****************************************************************************
  538. Memory Management
  539. ****************************************************************************}
  540. {$ifndef FPC_SYSTEM_HAS_PTR}
  541. Function Ptr(sel,off : {$ifdef CPU16}Word{$else}Longint{$endif}) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  542. Begin
  543. ptr:=farpointer((sel shl 4)+off);
  544. End;
  545. {$endif not FPC_SYSTEM_HAS_PTR}
  546. {$ifndef FPC_SYSTEM_HAS_CSEG}
  547. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  548. Begin
  549. Cseg:=0;
  550. End;
  551. {$endif not FPC_SYSTEM_HAS_CSEG}
  552. {$ifndef FPC_SYSTEM_HAS_DSEG}
  553. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  554. Begin
  555. Dseg:=0;
  556. End;
  557. {$endif not FPC_SYSTEM_HAS_DSEG}
  558. {$ifndef FPC_SYSTEM_HAS_SSEG}
  559. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  560. Begin
  561. Sseg:=0;
  562. End;
  563. {$endif not FPC_SYSTEM_HAS_SSEG}
  564. {$push}
  565. {$R-}
  566. {$I-}
  567. {$Q-}
  568. {*****************************************************************************
  569. Miscellaneous
  570. *****************************************************************************}
  571. {$ifndef FPC_SYSTEM_HAS_STACKTOP}
  572. function StackTop: pointer;
  573. begin
  574. result:=StackBottom+StackLength;
  575. end;
  576. {$endif FPC_SYSTEM_HAS_STACKTOP}
  577. {$ifndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  578. { This provides a dummy implementation
  579. of get_pc_addr function, for CPU's that don't need
  580. the instruction address to walk the stack. }
  581. function get_pc_addr : codepointer;
  582. begin
  583. get_pc_addr:=nil;
  584. end;
  585. {$endif ndef FPC_SYSTEM_HAS_GET_PC_ADDR}
  586. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  587. { This provides a simple implementation
  588. of get_caller_stackinfo procedure,
  589. using get_caller_addr and get_caller_frame
  590. functions. }
  591. procedure get_caller_stackinfo(var framebp : pointer; var addr : codepointer);
  592. var
  593. nextbp : pointer;
  594. nextaddr : codepointer;
  595. begin
  596. nextbp:=get_caller_frame(framebp,addr);
  597. nextaddr:=get_caller_addr(framebp,addr);
  598. framebp:=nextbp;
  599. addr:=nextaddr;
  600. end;
  601. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_STACKINFO}
  602. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  603. begin
  604. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  605. end;
  606. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  607. begin
  608. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  609. end;
  610. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  611. begin
  612. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  613. end;
  614. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  615. begin
  616. HandleErrorAddrFrameInd(6,get_pc_addr,get_frame);
  617. end;
  618. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  619. begin
  620. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  621. end;
  622. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  623. var
  624. l : longint;
  625. HInoutRes : PWord;
  626. begin
  627. HInOutRes:=@InoutRes;
  628. if HInOutRes^<>0 then
  629. begin
  630. l:=HInOutRes^;
  631. HInOutRes^:=0;
  632. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  633. end;
  634. end;
  635. Function IOResult:Word;
  636. var
  637. HInoutRes : PWord;
  638. Begin
  639. HInoutRes:=@InoutRes;
  640. IOResult:=HInOutRes^;
  641. HInOutRes^:=0;
  642. End;
  643. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  644. begin
  645. (* ThreadID is stored in a threadvar and made available in interface *)
  646. (* to allow setup of this value during thread initialization. *)
  647. GetThreadID := ThreadID;
  648. end;
  649. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  650. begin
  651. if res<0 then
  652. begin
  653. if assigned(SafeCallErrorProc) then
  654. SafeCallErrorProc(res,get_frame);
  655. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  656. end;
  657. result:=res;
  658. end;
  659. {*****************************************************************************
  660. Stack check code
  661. *****************************************************************************}
  662. { be compatible with old code }
  663. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  664. {$define NO_GENERIC_STACK_CHECK}
  665. {$endif FPC_NO_GENERIC_STACK_CHECK}
  666. {$IFNDEF NO_GENERIC_STACK_CHECK}
  667. {$PUSH}
  668. {$S-}
  669. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  670. var
  671. c : Pointer;
  672. begin
  673. { Avoid recursive calls when called from the exit routines }
  674. if StackError then
  675. exit;
  676. { don't use sack_size, since the stack pointer has already been
  677. decreased when this routine is called
  678. }
  679. c := Sptr - STACK_MARGIN;
  680. if (c <= StackBottom) then
  681. begin
  682. StackError:=true;
  683. HandleError(202);
  684. end;
  685. end;
  686. {$POP}
  687. {$ENDIF NO_GENERIC_STACK_CHECK}
  688. {*****************************************************************************
  689. Initialization / Finalization
  690. *****************************************************************************}
  691. const
  692. maxunits=1024; { See also files.pas of the compiler source }
  693. type
  694. TInitFinalRec=record
  695. InitProc,
  696. FinalProc : TProcedure;
  697. end;
  698. TInitFinalTable = record
  699. TableCount,
  700. InitCount : {$ifdef VER2_6}longint{$else}sizeint{$endif};
  701. Procs : array[1..maxunits] of TInitFinalRec;
  702. end;
  703. PInitFinalTable = ^TInitFinalTable;
  704. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  705. var
  706. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  707. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  708. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  709. var
  710. i : ObjpasInt;
  711. {$ifdef DEBUG}
  712. pt : PInitFinalTable;
  713. {$endif}
  714. begin
  715. { call cpu/fpu initialisation routine }
  716. fpc_cpuinit;
  717. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  718. {$ifdef DEBUG}
  719. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  720. {$endif}
  721. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  722. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  723. {$ifdef DEBUG}
  724. pt := @InitFinalTable;
  725. {$endif}
  726. with InitFinalTable do
  727. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  728. begin
  729. for i:=1 to TableCount do
  730. begin
  731. if assigned(Procs[i].InitProc) then
  732. Procs[i].InitProc();
  733. InitCount:=i;
  734. end;
  735. end;
  736. if assigned(InitProc) then
  737. TProcedure(InitProc)();
  738. end;
  739. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  740. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  741. begin
  742. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  743. IsLibrary:=true;
  744. { must also be set to true for packages when implemented }
  745. ModuleIsLib:=true;
  746. internal_initializeunits;
  747. {$endif FPC_HAS_FEATURE_DYNLIBS}
  748. end;
  749. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  750. begin
  751. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  752. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  753. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  754. with InitFinalTable do
  755. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  756. begin
  757. while (InitCount>0) do
  758. begin
  759. // we've to decrement the cound before calling the final. code
  760. // else a halt in the final. code leads to a endless loop
  761. dec(InitCount);
  762. if assigned(Procs[InitCount+1].FinalProc) then
  763. Procs[InitCount+1].FinalProc();
  764. end;
  765. end;
  766. end;
  767. {*****************************************************************************
  768. Error / Exit / ExitProc
  769. *****************************************************************************}
  770. Procedure system_exit;forward;
  771. {$ifdef FPC_HAS_FEATURE_HEAP}
  772. {$ifndef HAS_MEMORYMANAGER}
  773. //not needed if independant memory manager
  774. Procedure FinalizeHeap;forward;
  775. {$endif HAS_MEMORYMANAGER}
  776. {$endif FPC_HAS_FEATURE_HEAP}
  777. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  778. procedure SysFlushStdIO;
  779. begin
  780. { Make sure that all output is written to the redirected file }
  781. if Textrec(Output).Mode=fmOutput then
  782. Flush(Output);
  783. if Textrec(ErrOutput).Mode=fmOutput then
  784. Flush(ErrOutput);
  785. if Textrec(stdout).Mode=fmOutput then
  786. Flush(stdout);
  787. if Textrec(StdErr).Mode=fmOutput then
  788. Flush(StdErr);
  789. end;
  790. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  791. Procedure InternalExit;
  792. var
  793. current_exit : Procedure;
  794. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  795. pstdout : ^Text;
  796. {$endif}
  797. {$if defined(MSWINDOWS) or defined(OS2)}
  798. i : longint;
  799. {$endif}
  800. Begin
  801. {$ifdef SYSTEMDEBUG}
  802. writeln('InternalExit');
  803. {$endif SYSTEMDEBUG}
  804. while exitProc<>nil Do
  805. Begin
  806. InOutRes:=0;
  807. current_exit:=tProcedure(exitProc);
  808. exitProc:=nil;
  809. current_exit();
  810. End;
  811. { Finalize units }
  812. FinalizeUnits;
  813. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  814. { the embedded system unit itself contains no routines for console i/o
  815. console i/o is done by the Consoleio unit which can do things like
  816. redirection to seriell i/o }
  817. {$ifndef EMBEDDED}
  818. { Show runtime error and exit }
  819. pstdout:=@stdout;
  820. If erroraddr<>nil Then
  821. Begin
  822. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  823. { to get a nice symify }
  824. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  825. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  826. Writeln(pstdout^,'');
  827. End;
  828. SysFlushStdIO;
  829. {$endif EMBEDDED}
  830. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  831. {$if defined(MSWINDOWS) or defined(OS2)}
  832. { finally release the heap if possible, especially
  833. important for DLLs.
  834. Reset the array to nil, and finally also argv itself to
  835. avoid double freeing problem in case this function gets called twice. }
  836. if assigned(argv) then
  837. begin
  838. for i:=0 to argc-1 do
  839. if assigned(argv[i]) then
  840. begin
  841. sysfreemem(argv[i]);
  842. argv[i]:=nil;
  843. end;
  844. sysfreemem(argv);
  845. argv:=nil;
  846. end;
  847. {$endif}
  848. {$ifdef LINUX}
  849. {sysfreemem already checks for nil}
  850. { Do not try to do anything if the heap manager already reported an error }
  851. if (errorcode<>203) and (errorcode<>204) then
  852. sysfreemem(calculated_cmdline);
  853. {$endif}
  854. {$ifdef BSD}
  855. { Do not try to do anything if the heap manager already reported an error }
  856. if (errorcode<>203) and (errorcode<>204) then
  857. sysfreemem(cmdline);
  858. {$endif}
  859. {$ifdef FPC_HAS_FEATURE_HEAP}
  860. {$ifndef HAS_MEMORYMANAGER}
  861. {$ifndef FPC_NO_DEFAULT_HEAP}
  862. FinalizeHeap;
  863. {$endif not FPC_NO_DEFAULT_HEAP}
  864. {$endif not HAS_MEMORYMANAGER}
  865. {$endif FPC_HAS_FEATURE_HEAP}
  866. End;
  867. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  868. begin
  869. InternalExit;
  870. System_exit;
  871. end;
  872. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  873. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  874. begin
  875. InternalExit;
  876. end;
  877. Procedure Halt(ErrNum: Longint);
  878. Begin
  879. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  880. {$ifdef FPC_LIMITED_EXITCODE}
  881. if ErrNum > maxExitCode then
  882. ExitCode:=255
  883. else
  884. {$endif FPC_LIMITED_EXITCODE}
  885. ExitCode:=ErrNum;
  886. {$endif FPC_HAS_FEATURE_EXITCODE}
  887. internal_do_exit;
  888. end;
  889. function SysBackTraceStr (Addr: CodePointer): ShortString;
  890. begin
  891. SysBackTraceStr:=' $'+hexstr(addr);
  892. end;
  893. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  894. var
  895. curr_frame,prev_frame: pointer;
  896. curr_addr: codepointer;
  897. i: sizeint;
  898. begin
  899. curr_frame:=get_frame;
  900. curr_addr:=get_pc_addr;
  901. prev_frame:=curr_frame;
  902. get_caller_stackinfo(curr_frame,curr_addr);
  903. i:=-skipframes;
  904. while (i<count) and (curr_frame>prev_frame) and
  905. (curr_frame<StackTop) do
  906. begin
  907. prev_frame:=curr_frame;
  908. get_caller_stackinfo(curr_frame,curr_addr);
  909. if (curr_addr=nil) or
  910. (curr_frame=nil) then
  911. break;
  912. if (i>=0) then
  913. frames[i]:=curr_addr;
  914. inc(i);
  915. end;
  916. if i<0 then
  917. result:=0
  918. else
  919. result:=i;
  920. end;
  921. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  922. begin
  923. If pointer(ErrorProc)<>Nil then
  924. ErrorProc(Errno,addr,frame);
  925. errorcode:=word(Errno);
  926. erroraddr:=addr;
  927. errorbase:=frame;
  928. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  929. if ExceptAddrStack <> nil then
  930. raise TObject(nil) at addr,frame;
  931. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  932. Halt(errorcode);
  933. end;
  934. { This is used internally by system skip first level,
  935. and generated the same output as before, when
  936. HandleErrorFrame function was used internally. }
  937. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer);
  938. begin
  939. get_caller_stackinfo (frame, addr);
  940. HandleErrorAddrFrame (Errno,addr,frame);
  941. end;
  942. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  943. {
  944. Procedure to handle internal errors, i.e. not user-invoked errors
  945. Internal function should ALWAYS call HandleError instead of RunError.
  946. Can be used for exception handlers to specify the frame
  947. }
  948. begin
  949. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  950. end;
  951. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  952. {
  953. Procedure to handle internal errors, i.e. not user-invoked errors
  954. Internal function should ALWAYS call HandleError instead of RunError.
  955. }
  956. begin
  957. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  958. end;
  959. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  960. var
  961. bp : pointer;
  962. pcaddr : codepointer;
  963. begin
  964. errorcode:=w;
  965. pcaddr:=get_pc_addr;
  966. bp:=get_frame;
  967. get_caller_stackinfo(bp,pcaddr);
  968. erroraddr:=pcaddr;
  969. errorbase:=bp;
  970. Halt(errorcode);
  971. end;
  972. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  973. Begin
  974. RunError (0);
  975. End;
  976. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  977. Begin
  978. Halt(0);
  979. End;
  980. Procedure Error(RunTimeError : TRunTimeError);
  981. begin
  982. RunError(RuntimeErrorExitCodes[RunTimeError]);
  983. end;
  984. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  985. var
  986. i : Longint;
  987. prevfp : Pointer;
  988. is_dev : boolean;
  989. Begin
  990. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  991. try
  992. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  993. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  994. prevfp:=get_frame;
  995. i:=0;
  996. is_dev:=do_isdevice(textrec(f).Handle);
  997. { sanity checks, new frame pointer must be always greater than the old one, further
  998. it must point into the stack area, else something went wrong }
  999. while (fp>prevfp) and (fp<StackTop) do
  1000. Begin
  1001. prevfp:=fp;
  1002. get_caller_stackinfo(fp,addr);
  1003. if (addr=nil) then
  1004. break;
  1005. Writeln(f,BackTraceStrFunc(addr));
  1006. if (fp=nil) then
  1007. break;
  1008. Inc(i);
  1009. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1010. break;
  1011. End;
  1012. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1013. except
  1014. { prevent endless dump if an exception occured }
  1015. end;
  1016. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1017. End;
  1018. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1019. procedure DumpExceptionBackTrace(var f:text);
  1020. var
  1021. FrameNumber,
  1022. FrameCount : longint;
  1023. Frames : PCodePointer;
  1024. begin
  1025. if RaiseList=nil then
  1026. exit;
  1027. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1028. FrameCount:=RaiseList^.Framecount;
  1029. Frames:=RaiseList^.Frames;
  1030. for FrameNumber := 0 to FrameCount-1 do
  1031. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1032. end;
  1033. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1034. {$ifdef FPC_HAS_FEATURE_HEAP}
  1035. Type
  1036. PExitProcInfo = ^TExitProcInfo;
  1037. TExitProcInfo = Record
  1038. Next : PExitProcInfo;
  1039. SaveExit : CodePointer;
  1040. Proc : TProcedure;
  1041. End;
  1042. const
  1043. ExitProcList: PExitProcInfo = nil;
  1044. Procedure DoExitProc;
  1045. var
  1046. P : PExitProcInfo;
  1047. Proc : TProcedure;
  1048. Begin
  1049. P:=ExitProcList;
  1050. ExitProcList:=P^.Next;
  1051. ExitProc:=P^.SaveExit;
  1052. Proc:=P^.Proc;
  1053. DisPose(P);
  1054. Proc();
  1055. End;
  1056. Procedure AddExitProc(Proc: TProcedure);
  1057. var
  1058. P : PExitProcInfo;
  1059. Begin
  1060. New(P);
  1061. P^.Next:=ExitProcList;
  1062. P^.SaveExit:=ExitProc;
  1063. P^.Proc:=Proc;
  1064. ExitProcList:=P;
  1065. ExitProc:=@DoExitProc;
  1066. End;
  1067. {$endif FPC_HAS_FEATURE_HEAP}
  1068. {$ifdef FPC_HAS_FEATURE_HEAP}
  1069. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  1070. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  1071. // Note: for internal use by skilled programmers only
  1072. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1073. var p : ppchar;
  1074. i : LongInt;
  1075. begin
  1076. if High(s)<Low(s) Then Exit(NIL);
  1077. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1078. // for cmd
  1079. if p=nil then
  1080. begin
  1081. {$ifdef xunix}
  1082. fpseterrno(ESysEnomem);
  1083. {$endif}
  1084. exit(NIL);
  1085. end;
  1086. for i:=low(s) to high(s) do
  1087. p[i+Reserveentries]:=pchar(s[i]);
  1088. p[high(s)+1+Reserveentries]:=nil;
  1089. ArrayStringToPPchar:=p;
  1090. end;
  1091. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  1092. {
  1093. Create a PPChar to structure of pchars which are the arguments specified
  1094. in the string S. Especially useful for creating an ArgV for Exec-calls
  1095. }
  1096. begin
  1097. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1098. end;
  1099. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1100. var
  1101. i,nr : longint;
  1102. Buf : ^char;
  1103. p : ppchar;
  1104. begin
  1105. buf:=s;
  1106. nr:=1;
  1107. while (buf^<>#0) do // count nr of args
  1108. begin
  1109. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1110. inc(buf);
  1111. inc(nr);
  1112. if buf^='"' Then // quotes argument?
  1113. begin
  1114. inc(buf);
  1115. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1116. inc(buf);
  1117. if buf^='"' then // skip closing quote.
  1118. inc(buf);
  1119. end
  1120. else
  1121. begin // else std
  1122. while not (buf^ in [' ',#0,#9,#10]) do
  1123. inc(buf);
  1124. end;
  1125. end;
  1126. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1127. StringToPPChar:=p;
  1128. if p=nil then
  1129. begin
  1130. {$ifdef xunix}
  1131. fpseterrno(ESysEnomem);
  1132. {$endif}
  1133. exit;
  1134. end;
  1135. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1136. buf:=s;
  1137. while (buf^<>#0) do
  1138. begin
  1139. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1140. begin
  1141. buf^:=#0;
  1142. inc(buf);
  1143. end;
  1144. if buf^='"' Then // quotes argument?
  1145. begin
  1146. inc(buf);
  1147. p^:=buf;
  1148. inc(p);
  1149. p^:=nil;
  1150. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1151. inc(buf);
  1152. if buf^='"' then // skip closing quote.
  1153. begin
  1154. buf^:=#0;
  1155. inc(buf);
  1156. end;
  1157. end
  1158. else
  1159. begin
  1160. p^:=buf;
  1161. inc(p);
  1162. p^:=nil;
  1163. while not (buf^ in [' ',#0,#9,#10]) do
  1164. inc(buf);
  1165. end;
  1166. end;
  1167. end;
  1168. {$endif FPC_HAS_FEATURE_HEAP}
  1169. {*****************************************************************************
  1170. Abstract/Assert support.
  1171. *****************************************************************************}
  1172. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1173. begin
  1174. end;
  1175. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1176. begin
  1177. If pointer(AbstractErrorProc)<>nil then
  1178. AbstractErrorProc();
  1179. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1180. end;
  1181. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1182. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1183. begin
  1184. if pointer(AssertErrorProc)<>nil then
  1185. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1186. else
  1187. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1188. end;
  1189. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1190. begin
  1191. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1192. If msg='' then
  1193. write(stderr,'Assertion failed')
  1194. else
  1195. write(stderr,msg);
  1196. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1197. Writeln(stderr,'');
  1198. Halt(227);
  1199. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1200. end;
  1201. {*****************************************************************************
  1202. SetJmp/LongJmp support.
  1203. *****************************************************************************}
  1204. {$i setjump.inc}
  1205. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1206. {*****************************************************************************
  1207. Heap
  1208. *****************************************************************************}
  1209. {$i sysheap.inc}
  1210. {$i heap.inc}
  1211. {*****************************************************************************
  1212. Thread support
  1213. *****************************************************************************}
  1214. {$ifdef FPC_HAS_FEATURE_THREADING}
  1215. { Generic threadmanager }
  1216. {$i thread.inc}
  1217. {$ifndef FPC_SECTION_THREADVARS}
  1218. { Generic threadvar support }
  1219. {$i threadvr.inc}
  1220. {$endif FPC_SECTION_THREADVARS}
  1221. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1222. { OS Dependent implementation }
  1223. {$i systhrd.inc}
  1224. {$endif DISABLE_NO_THREAD_MANAGER}
  1225. {$endif FPC_HAS_FEATURE_THREADING}
  1226. {*****************************************************************************
  1227. File Handling
  1228. *****************************************************************************}
  1229. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1230. { Allow slash and backslash as separators }
  1231. procedure DoDirSeparators(var p: pchar; inplace: boolean = true);
  1232. var
  1233. i : longint;
  1234. len : sizeint;
  1235. newp : pchar;
  1236. begin
  1237. len:=length(p);
  1238. newp:=nil;
  1239. for i:=0 to len do
  1240. if p[i] in AllowDirectorySeparators then
  1241. begin
  1242. if not inplace and
  1243. not assigned(newp) then
  1244. begin
  1245. getmem(newp,len+1);
  1246. move(p^,newp^,len+1);
  1247. p:=newp;
  1248. end;
  1249. p[i]:=DirectorySeparator;
  1250. end;
  1251. end;
  1252. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1253. var
  1254. i : longint;
  1255. len : sizeint;
  1256. newp : pwidechar;
  1257. begin
  1258. len:=length(p);
  1259. newp:=nil;
  1260. for i:=0 to len do
  1261. if (ord(p[i])<255) and
  1262. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1263. begin
  1264. if not inplace and
  1265. not assigned(newp) then
  1266. begin
  1267. getmem(newp,(len+1)*2);
  1268. move(p^,newp^,(len+1)*2);
  1269. p:=newp;
  1270. end;
  1271. p[i]:=DirectorySeparator;
  1272. end;
  1273. end;
  1274. procedure DoDirSeparators(var p:shortstring);
  1275. var
  1276. i : longint;
  1277. begin
  1278. for i:=1 to length(p) do
  1279. if p[i] in AllowDirectorySeparators then
  1280. p[i]:=DirectorySeparator;
  1281. end;
  1282. procedure DoDirSeparators(var ps:RawByteString);
  1283. var
  1284. i : longint;
  1285. p : pchar;
  1286. unique : boolean;
  1287. begin
  1288. unique:=false;
  1289. for i:=1 to length(ps) do
  1290. if ps[i] in AllowDirectorySeparators then
  1291. begin
  1292. if not unique then
  1293. begin
  1294. uniquestring(ps);
  1295. p:=pchar(ps);
  1296. unique:=true;
  1297. end;
  1298. p[i-1]:=DirectorySeparator;
  1299. end;
  1300. end;
  1301. procedure DoDirSeparators(var ps:UnicodeString);
  1302. var
  1303. i : longint;
  1304. p : pwidechar;
  1305. unique : boolean;
  1306. begin
  1307. unique:=false;
  1308. for i:=1 to length(ps) do
  1309. if ps[i] in AllowDirectorySeparators then
  1310. begin
  1311. if not unique then
  1312. begin
  1313. uniquestring(ps);
  1314. p:=pwidechar(ps);
  1315. unique:=true;
  1316. end;
  1317. p[i-1]:=DirectorySeparator;
  1318. end;
  1319. end;
  1320. {$endif FPC_HAS_FEATURE_FILEIO}
  1321. { OS dependent low level file functions }
  1322. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1323. {$i sysfile.inc}
  1324. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1325. {$ifdef FPC_ANSI_TEXTFILEREC}
  1326. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1327. var
  1328. u: UnicodeString;
  1329. begin
  1330. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1331. do_open(f,pwidechar(u),flags,true);
  1332. end;
  1333. procedure do_erase(p: pansichar; pchangeable: boolean);
  1334. var
  1335. u: UnicodeString;
  1336. begin
  1337. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1338. do_erase(pwidechar(u),true);
  1339. end;
  1340. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1341. var
  1342. usrc, udst: UnicodeString;
  1343. begin
  1344. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1345. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1346. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1347. end;
  1348. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1349. var
  1350. usrc: UnicodeString;
  1351. begin
  1352. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1353. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1354. end;
  1355. {$endif FPC_ANSI_TEXTFILEREC}
  1356. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1357. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1358. {$ifndef FPC_ANSI_TEXTFILEREC}
  1359. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1360. var
  1361. s: RawByteString;
  1362. begin
  1363. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1364. do_open(f,pansichar(s),flags,true);
  1365. end;
  1366. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1367. var
  1368. s: RawByteString;
  1369. begin
  1370. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1371. do_erase(pansichar(s),true);
  1372. end;
  1373. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1374. var
  1375. rsrc, rdst: RawByteString;
  1376. begin
  1377. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1378. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1379. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1380. end;
  1381. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1382. var
  1383. rsrc: RawByteString;
  1384. begin
  1385. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1386. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1387. end;
  1388. {$endif not FPC_ANSI_TEXTFILEREC}
  1389. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1390. {$endif FPC_HAS_FEATURE_FILEIO}
  1391. { Text file }
  1392. {$i text.inc}
  1393. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1394. { Untyped file }
  1395. {$i file.inc}
  1396. { Typed file }
  1397. {$i typefile.inc}
  1398. {$endif FPC_HAS_FEATURE_FILEIO}
  1399. {*****************************************************************************
  1400. Directory Handling
  1401. *****************************************************************************}
  1402. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1403. { OS dependent dir functions }
  1404. {$i sysdir.inc}
  1405. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1406. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1407. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1408. var
  1409. u: unicodestring;
  1410. begin
  1411. Do_getdir(drivenr,u);
  1412. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1413. end;
  1414. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1415. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1416. Begin
  1417. If (s='') or (InOutRes <> 0) then
  1418. exit;
  1419. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1420. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1421. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1422. Do_mkdir(S);
  1423. {$endif}
  1424. end;
  1425. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1426. Begin
  1427. If (s='') or (InOutRes <> 0) then
  1428. exit;
  1429. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1430. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1431. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1432. Do_rmdir(S);
  1433. {$endif}
  1434. End;
  1435. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1436. Begin
  1437. If (s='') or (InOutRes <> 0) then
  1438. exit;
  1439. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1440. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1441. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1442. Do_chdir(S);
  1443. {$endif}
  1444. End;
  1445. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1446. begin
  1447. Do_getdir(drivenr,dir);
  1448. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1449. necessary }
  1450. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1451. end;
  1452. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1453. supporting ansi/unicodestrings; for now assume there are no systems that
  1454. support unicodestrings but not ansistrings }
  1455. { avoid double string conversions }
  1456. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1457. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1458. begin
  1459. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1460. end;
  1461. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1462. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1463. begin
  1464. GetDirStrFromShortstring:=s;
  1465. end;
  1466. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1467. Procedure MkDir(Const s: shortstring);[IOCheck];
  1468. Begin
  1469. If (s='') or (InOutRes <> 0) then
  1470. exit;
  1471. Do_mkdir(GetDirStrFromShortstring(S));
  1472. End;
  1473. Procedure RmDir(Const s: shortstring);[IOCheck];
  1474. Begin
  1475. If (s='') or (InOutRes <> 0) then
  1476. exit;
  1477. Do_rmdir(GetDirStrFromShortstring(S));
  1478. End;
  1479. Procedure ChDir(Const s: shortstring);[IOCheck];
  1480. Begin
  1481. If (s='') or (InOutRes <> 0) then
  1482. exit;
  1483. Do_chdir(GetDirStrFromShortstring(S));
  1484. End;
  1485. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1486. var
  1487. s: rawbytestring;
  1488. begin
  1489. Do_getdir(drivenr,s);
  1490. if length(s)<=high(dir) then
  1491. dir:=s
  1492. else
  1493. inoutres:=3;
  1494. end;
  1495. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1496. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1497. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1498. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1499. converted to the right code page }
  1500. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1501. begin
  1502. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1503. end;
  1504. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1505. begin
  1506. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1507. end;
  1508. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1509. begin
  1510. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1511. end;
  1512. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1513. var
  1514. s: rawbytestring;
  1515. begin
  1516. Do_getdir(drivenr,s);
  1517. dir:=s;
  1518. end;
  1519. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1520. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1521. Begin
  1522. if (s='') or (InOutRes <> 0) then
  1523. exit;
  1524. Do_mkdir(S);
  1525. End;
  1526. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1527. Begin
  1528. if (s='') or (InOutRes <> 0) then
  1529. exit;
  1530. Do_rmdir(S);
  1531. End;
  1532. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1533. Begin
  1534. if (s='') or (InOutRes <> 0) then
  1535. exit;
  1536. Do_chdir(S);
  1537. End;
  1538. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1539. begin
  1540. Do_getdir(drivenr,dir);
  1541. end;
  1542. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1543. {$endif FPC_HAS_FEATURE_FILEIO}
  1544. {*****************************************************************************
  1545. Resources support
  1546. *****************************************************************************}
  1547. {$i sysres.inc}
  1548. const
  1549. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1550. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1551. (* It is possible to provide platform specific implementation performing *)
  1552. (* special initialization; default implementation just sets the procedural *)
  1553. (* variable to make it available for use from the exception handler. *)
  1554. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1555. begin
  1556. (* Return either nil or previous handler *)
  1557. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1558. CtrlBreakHandler := Handler;
  1559. end;
  1560. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}