system.inc 51 KB

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