system.inc 37 KB

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