system.inc 38 KB

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