system.inc 38 KB

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