system.inc 37 KB

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