system.inc 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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_objecterror; compilerproc;
  609. begin
  610. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  611. end;
  612. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  613. begin
  614. HandleErrorAddrFrameInd(201,get_pc_addr,get_frame);
  615. end;
  616. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  617. begin
  618. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  619. end;
  620. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  621. begin
  622. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  623. end;
  624. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  625. begin
  626. HandleErrorAddrFrameInd(6,get_pc_addr,get_frame);
  627. end;
  628. procedure fpc_invalidpointer; [public,alias:'FPC_INVALIDPOINTER'];
  629. begin
  630. HandleErrorAddrFrameInd(216,get_pc_addr,get_frame);
  631. end;
  632. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  633. var
  634. l : longint;
  635. HInoutRes : PWord;
  636. begin
  637. HInOutRes:=@InoutRes;
  638. if HInOutRes^<>0 then
  639. begin
  640. l:=HInOutRes^;
  641. HInOutRes^:=0;
  642. HandleErrorAddrFrameInd(l,get_pc_addr,get_frame);
  643. end;
  644. end;
  645. Function IOResult:Word;
  646. var
  647. HInoutRes : PWord;
  648. Begin
  649. HInoutRes:=@InoutRes;
  650. IOResult:=HInOutRes^;
  651. HInOutRes^:=0;
  652. End;
  653. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  654. begin
  655. (* ThreadID is stored in a threadvar and made available in interface *)
  656. (* to allow setup of this value during thread initialization. *)
  657. GetThreadID := ThreadID;
  658. end;
  659. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc; {$ifdef CPU86} register; {$endif}
  660. begin
  661. if res<0 then
  662. begin
  663. if assigned(SafeCallErrorProc) then
  664. SafeCallErrorProc(res,get_frame);
  665. HandleErrorAddrFrameInd(229,get_pc_addr,get_frame);
  666. end;
  667. result:=res;
  668. end;
  669. {*****************************************************************************
  670. Stack check code
  671. *****************************************************************************}
  672. { be compatible with old code }
  673. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  674. {$define NO_GENERIC_STACK_CHECK}
  675. {$endif FPC_NO_GENERIC_STACK_CHECK}
  676. {$IFNDEF NO_GENERIC_STACK_CHECK}
  677. {$PUSH}
  678. {$S-}
  679. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK']; compilerproc;
  680. var
  681. c : Pointer;
  682. begin
  683. { Avoid recursive calls when called from the exit routines }
  684. if StackError then
  685. exit;
  686. { don't use sack_size, since the stack pointer has already been
  687. decreased when this routine is called
  688. }
  689. c := Sptr - STACK_MARGIN;
  690. if (c <= StackBottom) then
  691. begin
  692. StackError:=true;
  693. HandleError(202);
  694. end;
  695. end;
  696. {$POP}
  697. {$ENDIF NO_GENERIC_STACK_CHECK}
  698. {*****************************************************************************
  699. Initialization / Finalization
  700. *****************************************************************************}
  701. const
  702. maxunits=1024; { See also files.pas of the compiler source }
  703. type
  704. TInitFinalRec=record
  705. InitProc,
  706. FinalProc : TProcedure;
  707. end;
  708. TInitFinalTable = record
  709. TableCount,
  710. InitCount : {$if defined(VER2)}longint
  711. {$elseif defined(VER3_0)}sizeint
  712. {$else}ALUUInt{$endif}
  713. ;
  714. Procs : array[1..maxunits] of TInitFinalRec;
  715. end;
  716. PInitFinalTable = ^TInitFinalTable;
  717. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  718. var
  719. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  720. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  721. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  722. var
  723. i : ALUUInt;
  724. {$ifdef DEBUG}
  725. pt : PInitFinalTable;
  726. {$endif}
  727. begin
  728. { call cpu/fpu initialisation routine }
  729. fpc_cpuinit;
  730. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  731. {$ifdef DEBUG}
  732. pt := PInitFinalTable(EntryInformation.InitFinalTable);
  733. {$endif}
  734. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  735. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  736. {$ifdef DEBUG}
  737. pt := @InitFinalTable;
  738. {$endif}
  739. with InitFinalTable do
  740. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  741. begin
  742. for i:=1 to ALUUInt(TableCount) do
  743. begin
  744. if assigned(Procs[i].InitProc) then
  745. Procs[i].InitProc();
  746. InitCount:=i;
  747. end;
  748. end;
  749. if assigned(InitProc) then
  750. TProcedure(InitProc)();
  751. end;
  752. procedure internal_initializeunits; external name 'FPC_INITIALIZEUNITS';
  753. procedure fpc_LibInitializeUnits;[public,alias:'FPC_LIBINITIALIZEUNITS'];compilerproc;
  754. begin
  755. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  756. IsLibrary:=true;
  757. { must also be set to true for packages when implemented }
  758. ModuleIsLib:=true;
  759. internal_initializeunits;
  760. {$endif FPC_HAS_FEATURE_DYNLIBS}
  761. end;
  762. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  763. begin
  764. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  765. with PInitFinalTable(EntryInformation.InitFinalTable)^ do
  766. {$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
  767. with InitFinalTable do
  768. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  769. begin
  770. while (InitCount>0) do
  771. begin
  772. // we've to decrement the cound before calling the final. code
  773. // else a halt in the final. code leads to a endless loop
  774. dec(InitCount);
  775. if assigned(Procs[InitCount+1].FinalProc) then
  776. Procs[InitCount+1].FinalProc();
  777. end;
  778. end;
  779. end;
  780. {*****************************************************************************
  781. Error / Exit / ExitProc
  782. *****************************************************************************}
  783. Procedure system_exit;forward;
  784. {$ifdef FPC_HAS_FEATURE_HEAP}
  785. {$ifndef HAS_MEMORYMANAGER}
  786. //not needed if independant memory manager
  787. Procedure FinalizeHeap;forward;
  788. {$endif HAS_MEMORYMANAGER}
  789. {$endif FPC_HAS_FEATURE_HEAP}
  790. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  791. procedure SysFlushStdIO;
  792. begin
  793. { Make sure that all output is written to the redirected file }
  794. if Textrec(Output).Mode=fmOutput then
  795. Flush(Output);
  796. if Textrec(ErrOutput).Mode=fmOutput then
  797. Flush(ErrOutput);
  798. if Textrec(stdout).Mode=fmOutput then
  799. Flush(stdout);
  800. if Textrec(StdErr).Mode=fmOutput then
  801. Flush(StdErr);
  802. end;
  803. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  804. Procedure InternalExit;
  805. var
  806. current_exit : Procedure;
  807. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  808. pstdout : ^Text;
  809. {$endif}
  810. {$if defined(MSWINDOWS) or defined(OS2)}
  811. i : longint;
  812. {$endif}
  813. Begin
  814. {$ifdef SYSTEMDEBUG}
  815. writeln('InternalExit');
  816. {$endif SYSTEMDEBUG}
  817. while exitProc<>nil Do
  818. Begin
  819. InOutRes:=0;
  820. current_exit:=tProcedure(exitProc);
  821. exitProc:=nil;
  822. current_exit();
  823. End;
  824. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  825. { the embedded system unit itself contains no routines for console i/o
  826. console i/o is done by the Consoleio unit which can do things like
  827. redirection to seriell i/o }
  828. {$ifndef EMBEDDED}
  829. { Show runtime error and exit }
  830. if WriteErrorsToStdErr then
  831. pstdout:=@stderr
  832. else
  833. pstdout:=@stdout;
  834. If erroraddr<>nil Then
  835. Begin
  836. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  837. { to get a nice symify }
  838. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  839. dump_stack(pstdout^,ErrorBase,ErrorAddr);
  840. Writeln(pstdout^,'');
  841. End;
  842. SysFlushStdIO;
  843. {$endif EMBEDDED}
  844. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  845. { Finalize units }
  846. FinalizeUnits;
  847. {$if defined(MSWINDOWS) or defined(OS2)}
  848. { finally release the heap if possible, especially
  849. important for DLLs.
  850. Reset the array to nil, and finally also argv itself to
  851. avoid double freeing problem in case this function gets called twice. }
  852. if assigned(argv) then
  853. begin
  854. for i:=0 to argc-1 do
  855. if assigned(argv[i]) then
  856. begin
  857. sysfreemem(argv[i]);
  858. argv[i]:=nil;
  859. end;
  860. sysfreemem(argv);
  861. argv:=nil;
  862. end;
  863. {$endif}
  864. {$ifdef LINUX}
  865. {sysfreemem already checks for nil}
  866. { Do not try to do anything if the heap manager already reported an error }
  867. if (errorcode<>203) and (errorcode<>204) then
  868. sysfreemem(calculated_cmdline);
  869. {$endif}
  870. {$ifdef BSD}
  871. { Do not try to do anything if the heap manager already reported an error }
  872. if (errorcode<>203) and (errorcode<>204) then
  873. sysfreemem(cmdline);
  874. {$endif}
  875. {$ifdef FPC_HAS_FEATURE_HEAP}
  876. {$ifndef HAS_MEMORYMANAGER}
  877. {$ifndef FPC_NO_DEFAULT_HEAP}
  878. FinalizeHeap;
  879. {$endif not FPC_NO_DEFAULT_HEAP}
  880. {$endif not HAS_MEMORYMANAGER}
  881. {$endif FPC_HAS_FEATURE_HEAP}
  882. End;
  883. Procedure fpc_do_exit;[Public,Alias:'FPC_DO_EXIT']; compilerproc;
  884. begin
  885. InternalExit;
  886. System_exit;
  887. end;
  888. procedure internal_do_exit; external name 'FPC_DO_EXIT';
  889. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  890. begin
  891. InternalExit;
  892. end;
  893. Procedure Halt(ErrNum: Longint);
  894. Begin
  895. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  896. {$ifdef FPC_LIMITED_EXITCODE}
  897. if ErrNum > maxExitCode then
  898. ExitCode:=255
  899. else
  900. {$endif FPC_LIMITED_EXITCODE}
  901. ExitCode:=ErrNum;
  902. {$endif FPC_HAS_FEATURE_EXITCODE}
  903. internal_do_exit;
  904. end;
  905. function SysBackTraceStr (Addr: CodePointer): ShortString;
  906. begin
  907. SysBackTraceStr:=' $'+hexstr(addr);
  908. end;
  909. {$ifndef FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  910. function CaptureBacktrace(skipframes,count:sizeint;frames:PCodePointer):sizeint;
  911. var
  912. curr_frame,prev_frame: pointer;
  913. curr_addr: codepointer;
  914. i: sizeint;
  915. begin
  916. curr_frame:=get_frame;
  917. curr_addr:=get_pc_addr;
  918. prev_frame:=curr_frame;
  919. get_caller_stackinfo(curr_frame,curr_addr);
  920. i:=-skipframes;
  921. while (i<count) and (curr_frame>prev_frame) and
  922. (curr_frame<StackTop) do
  923. begin
  924. if (curr_addr=nil) or
  925. (curr_frame=nil) then
  926. break;
  927. if (i>=0) then
  928. frames[i]:=curr_addr;
  929. inc(i);
  930. prev_frame:=curr_frame;
  931. get_caller_stackinfo(curr_frame,curr_addr);
  932. end;
  933. if i<0 then
  934. result:=0
  935. else
  936. result:=i;
  937. end;
  938. {$endif FPC_SYSTEM_HAS_CAPTUREBACKTRACE}
  939. Procedure HandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer);[public,alias:'FPC_BREAK_ERROR']; {$ifdef CPUI386} register; {$endif}
  940. begin
  941. If pointer(ErrorProc)<>Nil then
  942. ErrorProc(Errno,addr,frame);
  943. errorcode:=word(Errno);
  944. erroraddr:=addr;
  945. errorbase:=frame;
  946. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  947. if ExceptAddrStack <> nil then
  948. raise TObject(nil) at addr,frame;
  949. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  950. Halt(errorcode);
  951. end;
  952. { This is used internally by system skip first level,
  953. and generated the same output as before, when
  954. HandleErrorFrame function was used internally. }
  955. Procedure HandleErrorAddrFrameInd (Errno : longint;addr : CodePointer; frame : Pointer);
  956. begin
  957. get_caller_stackinfo (frame, addr);
  958. HandleErrorAddrFrame (Errno,addr,frame);
  959. end;
  960. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  961. {
  962. Procedure to handle internal errors, i.e. not user-invoked errors
  963. Internal function should ALWAYS call HandleError instead of RunError.
  964. Can be used for exception handlers to specify the frame
  965. }
  966. begin
  967. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  968. end;
  969. procedure fpc_handleerror (Errno : longint); compilerproc; [public,alias : 'FPC_HANDLEERROR'];
  970. {
  971. Procedure to handle internal errors, i.e. not user-invoked errors
  972. Internal function should ALWAYS call HandleError instead of RunError.
  973. }
  974. begin
  975. HandleErrorAddrFrameInd(Errno,get_pc_addr,get_frame);
  976. end;
  977. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  978. var
  979. bp : pointer;
  980. pcaddr : codepointer;
  981. begin
  982. errorcode:=w;
  983. pcaddr:=get_pc_addr;
  984. bp:=get_frame;
  985. get_caller_stackinfo(bp,pcaddr);
  986. erroraddr:=pcaddr;
  987. errorbase:=bp;
  988. Halt(errorcode);
  989. end;
  990. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  991. Begin
  992. RunError (0);
  993. End;
  994. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  995. Begin
  996. Halt(0);
  997. End;
  998. Procedure Error(RunTimeError : TRunTimeError);
  999. begin
  1000. RunError(RuntimeErrorExitCodes[RunTimeError]);
  1001. end;
  1002. Procedure dump_stack(var f : text;fp : Pointer; addr : CodePointer);
  1003. var
  1004. i : Longint;
  1005. prevfp : Pointer;
  1006. is_dev : boolean;
  1007. Begin
  1008. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1009. try
  1010. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1011. { Frame of this procedure acts as StackBottom, fp values below that are invalid. }
  1012. prevfp:=get_frame;
  1013. i:=0;
  1014. is_dev:=do_isdevice(textrec(f).Handle);
  1015. { sanity checks, new frame pointer must be always greater than the old one, further
  1016. it must point into the stack area, else something went wrong }
  1017. while (fp>prevfp) and (fp<StackTop) do
  1018. Begin
  1019. prevfp:=fp;
  1020. get_caller_stackinfo(fp,addr);
  1021. if (addr=nil) then
  1022. break;
  1023. Writeln(f,BackTraceStrFunc(addr));
  1024. if (fp=nil) then
  1025. break;
  1026. Inc(i);
  1027. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  1028. break;
  1029. End;
  1030. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1031. except
  1032. { prevent endless dump if an exception occured }
  1033. end;
  1034. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1035. End;
  1036. procedure dump_stack(var f: text; skipframes: longint);
  1037. var
  1038. i,count: longint;
  1039. frames: array [0..255] of codepointer;
  1040. begin
  1041. if do_isdevice(textrec(f).handle) then
  1042. count:=max_frame_dump
  1043. else
  1044. count:=255;
  1045. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1046. try
  1047. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1048. count:=CaptureBacktrace(skipframes+1,count,@frames[0]);
  1049. for i:=0 to count-1 do
  1050. writeln(f,BackTraceStrFunc(frames[i]));
  1051. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1052. except
  1053. end;
  1054. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1055. end;
  1056. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  1057. procedure DumpExceptionBackTrace(var f:text);
  1058. var
  1059. FrameNumber,
  1060. FrameCount : longint;
  1061. Frames : PCodePointer;
  1062. begin
  1063. if RaiseList=nil then
  1064. exit;
  1065. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  1066. FrameCount:=RaiseList^.Framecount;
  1067. Frames:=RaiseList^.Frames;
  1068. for FrameNumber := 0 to FrameCount-1 do
  1069. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  1070. end;
  1071. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  1072. {$ifdef FPC_HAS_FEATURE_HEAP}
  1073. Type
  1074. PExitProcInfo = ^TExitProcInfo;
  1075. TExitProcInfo = Record
  1076. Next : PExitProcInfo;
  1077. SaveExit : CodePointer;
  1078. Proc : TProcedure;
  1079. End;
  1080. const
  1081. ExitProcList: PExitProcInfo = nil;
  1082. Procedure DoExitProc;
  1083. var
  1084. P : PExitProcInfo;
  1085. Proc : TProcedure;
  1086. Begin
  1087. P:=ExitProcList;
  1088. ExitProcList:=P^.Next;
  1089. ExitProc:=P^.SaveExit;
  1090. Proc:=P^.Proc;
  1091. DisPose(P);
  1092. Proc();
  1093. End;
  1094. Procedure AddExitProc(Proc: TProcedure);
  1095. var
  1096. P : PExitProcInfo;
  1097. Begin
  1098. New(P);
  1099. P^.Next:=ExitProcList;
  1100. P^.SaveExit:=ExitProc;
  1101. P^.Proc:=Proc;
  1102. ExitProcList:=P;
  1103. ExitProc:=@DoExitProc;
  1104. End;
  1105. {$endif FPC_HAS_FEATURE_HEAP}
  1106. {$ifdef FPC_HAS_FEATURE_HEAP}
  1107. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1108. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  1109. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  1110. // Note: for internal use by skilled programmers only
  1111. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  1112. var p : ppchar;
  1113. i : LongInt;
  1114. begin
  1115. if High(s)<Low(s) Then Exit(NIL);
  1116. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  1117. // for cmd
  1118. if p=nil then
  1119. begin
  1120. {$ifdef xunix}
  1121. fpseterrno(ESysEnomem);
  1122. {$endif}
  1123. exit(NIL);
  1124. end;
  1125. for i:=low(s) to high(s) do
  1126. p[i+Reserveentries]:=pchar(s[i]);
  1127. p[high(s)+1+Reserveentries]:=nil;
  1128. ArrayStringToPPchar:=p;
  1129. end;
  1130. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  1131. {
  1132. Create a PPChar to structure of pchars which are the arguments specified
  1133. in the string S. Especially useful for creating an ArgV for Exec-calls
  1134. }
  1135. begin
  1136. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  1137. end;
  1138. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1139. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  1140. var
  1141. i,nr : longint;
  1142. Buf : ^char;
  1143. p : ppchar;
  1144. begin
  1145. buf:=s;
  1146. nr:=1;
  1147. while (buf^<>#0) do // count nr of args
  1148. begin
  1149. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1150. inc(buf);
  1151. inc(nr);
  1152. if buf^='"' Then // quotes argument?
  1153. begin
  1154. inc(buf);
  1155. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1156. inc(buf);
  1157. if buf^='"' then // skip closing quote.
  1158. inc(buf);
  1159. end
  1160. else
  1161. begin // else std
  1162. while not (buf^ in [' ',#0,#9,#10]) do
  1163. inc(buf);
  1164. end;
  1165. end;
  1166. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  1167. StringToPPChar:=p;
  1168. if p=nil then
  1169. begin
  1170. {$ifdef xunix}
  1171. fpseterrno(ESysEnomem);
  1172. {$endif}
  1173. exit;
  1174. end;
  1175. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  1176. buf:=s;
  1177. while (buf^<>#0) do
  1178. begin
  1179. while (buf^ in [' ',#9,#10]) do // Kill separators.
  1180. begin
  1181. buf^:=#0;
  1182. inc(buf);
  1183. end;
  1184. if buf^='"' Then // quotes argument?
  1185. begin
  1186. inc(buf);
  1187. p^:=buf;
  1188. inc(p);
  1189. p^:=nil;
  1190. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  1191. inc(buf);
  1192. if buf^='"' then // skip closing quote.
  1193. begin
  1194. buf^:=#0;
  1195. inc(buf);
  1196. end;
  1197. end
  1198. else
  1199. begin
  1200. p^:=buf;
  1201. inc(p);
  1202. p^:=nil;
  1203. while not (buf^ in [' ',#0,#9,#10]) do
  1204. inc(buf);
  1205. end;
  1206. end;
  1207. end;
  1208. {$endif FPC_HAS_FEATURE_HEAP}
  1209. {*****************************************************************************
  1210. Abstract/Assert support.
  1211. *****************************************************************************}
  1212. procedure fpc_emptymethod;[public,alias : 'FPC_EMPTYMETHOD'];
  1213. begin
  1214. end;
  1215. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  1216. begin
  1217. If pointer(AbstractErrorProc)<>nil then
  1218. AbstractErrorProc();
  1219. HandleErrorAddrFrameInd(211,get_pc_addr,get_frame);
  1220. end;
  1221. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;
  1222. ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  1223. begin
  1224. if pointer(AssertErrorProc)<>nil then
  1225. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  1226. else
  1227. HandleErrorAddrFrameInd(227,get_pc_addr,get_frame);
  1228. end;
  1229. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  1230. begin
  1231. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1232. If msg='' then
  1233. write(stderr,'Assertion failed')
  1234. else
  1235. write(stderr,msg);
  1236. Writeln(stderr,' (',FName,', line ',LineNo,').');
  1237. Writeln(stderr,'');
  1238. Halt(227);
  1239. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1240. end;
  1241. {*****************************************************************************
  1242. SetJmp/LongJmp support.
  1243. *****************************************************************************}
  1244. {$i setjump.inc}
  1245. {$pop} //{$I-,R-,Q-} before 'procedure fpc_rangeerror'
  1246. {*****************************************************************************
  1247. Heap
  1248. *****************************************************************************}
  1249. {$i sysheap.inc}
  1250. {$i heap.inc}
  1251. {*****************************************************************************
  1252. Thread support
  1253. *****************************************************************************}
  1254. {$ifdef FPC_HAS_FEATURE_THREADING}
  1255. { Generic threadmanager }
  1256. {$i thread.inc}
  1257. {$ifndef FPC_SECTION_THREADVARS}
  1258. { Generic threadvar support }
  1259. {$i threadvr.inc}
  1260. {$endif FPC_SECTION_THREADVARS}
  1261. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1262. { OS Dependent implementation }
  1263. {$i systhrd.inc}
  1264. {$endif DISABLE_NO_THREAD_MANAGER}
  1265. {$endif FPC_HAS_FEATURE_THREADING}
  1266. {*****************************************************************************
  1267. Dynamic library support
  1268. *****************************************************************************}
  1269. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  1270. {$i dynlib.inc}
  1271. {$ifdef DISABLE_NO_DYNLIBS_MANAGER}
  1272. { OS Dependant implementation }
  1273. {$i sysdl.inc}
  1274. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  1275. {$endif FPC_HAS_FEATURE_DYNLIBS}
  1276. {*****************************************************************************
  1277. File Handling
  1278. *****************************************************************************}
  1279. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1280. { Allow slash and backslash as separators }
  1281. procedure DoDirSeparators(var p: pchar; inplace: boolean = true);
  1282. var
  1283. i : longint;
  1284. len : sizeint;
  1285. newp : pchar;
  1286. begin
  1287. len:=length(p);
  1288. newp:=nil;
  1289. for i:=0 to len do
  1290. if p[i] in AllowDirectorySeparators then
  1291. begin
  1292. if not inplace and
  1293. not assigned(newp) then
  1294. begin
  1295. getmem(newp,len+1);
  1296. move(p^,newp^,len+1);
  1297. p:=newp;
  1298. end;
  1299. p[i]:=DirectorySeparator;
  1300. end;
  1301. end;
  1302. procedure DoDirSeparators(var p: pwidechar; inplace: boolean = true);
  1303. var
  1304. i : longint;
  1305. len : sizeint;
  1306. newp : pwidechar;
  1307. begin
  1308. len:=length(p);
  1309. newp:=nil;
  1310. for i:=0 to len do
  1311. if (ord(p[i])<255) and
  1312. (ansichar(ord(p[i])) in AllowDirectorySeparators) then
  1313. begin
  1314. if not inplace and
  1315. not assigned(newp) then
  1316. begin
  1317. getmem(newp,(len+1)*2);
  1318. move(p^,newp^,(len+1)*2);
  1319. p:=newp;
  1320. end;
  1321. p[i]:=DirectorySeparator;
  1322. end;
  1323. end;
  1324. procedure DoDirSeparators(var p:shortstring);
  1325. var
  1326. i : longint;
  1327. begin
  1328. for i:=1 to length(p) do
  1329. if p[i] in AllowDirectorySeparators then
  1330. p[i]:=DirectorySeparator;
  1331. end;
  1332. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1333. procedure DoDirSeparators(var ps:RawByteString);
  1334. var
  1335. i : longint;
  1336. p : pchar;
  1337. unique : boolean;
  1338. begin
  1339. unique:=false;
  1340. for i:=1 to length(ps) do
  1341. if ps[i] in AllowDirectorySeparators then
  1342. begin
  1343. if not unique then
  1344. begin
  1345. uniquestring(ps);
  1346. p:=pchar(ps);
  1347. unique:=true;
  1348. end;
  1349. p[i-1]:=DirectorySeparator;
  1350. end;
  1351. end;
  1352. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1353. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  1354. procedure DoDirSeparators(var ps:UnicodeString);
  1355. var
  1356. i : longint;
  1357. p : pwidechar;
  1358. unique : boolean;
  1359. begin
  1360. unique:=false;
  1361. for i:=1 to length(ps) do
  1362. if ps[i] in AllowDirectorySeparators then
  1363. begin
  1364. if not unique then
  1365. begin
  1366. uniquestring(ps);
  1367. p:=pwidechar(ps);
  1368. unique:=true;
  1369. end;
  1370. p[i-1]:=DirectorySeparator;
  1371. end;
  1372. end;
  1373. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  1374. {$endif FPC_HAS_FEATURE_FILEIO}
  1375. { OS dependent low level file functions }
  1376. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1377. {$i sysfile.inc}
  1378. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1379. {$ifdef FPC_ANSI_TEXTFILEREC}
  1380. procedure do_open(var f; p: pansichar; flags: longint; pchangeable: boolean);
  1381. var
  1382. u: UnicodeString;
  1383. begin
  1384. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1385. do_open(f,pwidechar(u),flags,true);
  1386. end;
  1387. procedure do_erase(p: pansichar; pchangeable: boolean);
  1388. var
  1389. u: UnicodeString;
  1390. begin
  1391. widestringmanager.Ansi2UnicodeMoveProc(p,DefaultFileSystemCodePage,u,length(p));
  1392. do_erase(pwidechar(u),true);
  1393. end;
  1394. procedure do_rename(src, dst: pansichar; srcchangeable, dstchangeable: boolean);
  1395. var
  1396. usrc, udst: UnicodeString;
  1397. begin
  1398. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1399. widestringmanager.Ansi2UnicodeMoveProc(dst,DefaultFileSystemCodePage,udst,length(dst));
  1400. do_rename(pwidechar(usrc),pwidechar(udst),true,true);
  1401. end;
  1402. procedure do_rename(src: pansichar; dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1403. var
  1404. usrc: UnicodeString;
  1405. begin
  1406. widestringmanager.Ansi2UnicodeMoveProc(src,DefaultFileSystemCodePage,usrc,length(src));
  1407. do_rename(pwidechar(usrc),dst,true,dstchangeable);
  1408. end;
  1409. {$endif FPC_ANSI_TEXTFILEREC}
  1410. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1411. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1412. {$ifndef FPC_ANSI_TEXTFILEREC}
  1413. procedure do_open(var f; p: pwidechar; flags: longint; pchangeable: boolean);
  1414. var
  1415. s: RawByteString;
  1416. begin
  1417. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1418. do_open(f,pansichar(s),flags,true);
  1419. end;
  1420. procedure do_erase(p: pwidechar; pchangeable: boolean);
  1421. var
  1422. s: RawByteString;
  1423. begin
  1424. widestringmanager.Unicode2AnsiMoveProc(p,s,DefaultFileSystemCodePage,length(p));
  1425. do_erase(pansichar(s),true);
  1426. end;
  1427. procedure do_rename(src, dst: pwidechar; srcchangeable, dstchangeable: boolean);
  1428. var
  1429. rsrc, rdst: RawByteString;
  1430. begin
  1431. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1432. widestringmanager.Unicode2AnsiMoveProc(dst,rdst,DefaultFileSystemCodePage,length(dst));
  1433. do_rename(pansichar(rsrc),pansichar(rdst),true,true);
  1434. end;
  1435. procedure do_rename(src: pwidechar; dst: pansichar; srcchangeable, dstchangeable: boolean);
  1436. var
  1437. rsrc: RawByteString;
  1438. begin
  1439. widestringmanager.Unicode2AnsiMoveProc(src,rsrc,DefaultFileSystemCodePage,length(src));
  1440. do_rename(pansichar(rsrc),dst,true,dstchangeable);
  1441. end;
  1442. {$endif not FPC_ANSI_TEXTFILEREC}
  1443. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1444. {$endif FPC_HAS_FEATURE_FILEIO}
  1445. { helper for targets supporting no ansistrings, it is used
  1446. by non-ansistring code }
  1447. function min(v1,v2 : SizeInt) : SizeInt;
  1448. begin
  1449. if v1<v2 then
  1450. result:=v1
  1451. else
  1452. result:=v2;
  1453. end;
  1454. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1455. { Text file }
  1456. {$i text.inc}
  1457. {$endif FPC_HAS_FEATURE_TEXTIO}
  1458. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1459. { Untyped file }
  1460. {$i file.inc}
  1461. { Typed file }
  1462. {$i typefile.inc}
  1463. {$endif FPC_HAS_FEATURE_FILEIO}
  1464. {*****************************************************************************
  1465. Directory Handling
  1466. *****************************************************************************}
  1467. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1468. { OS dependent dir functions }
  1469. {$i sysdir.inc}
  1470. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1471. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1472. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  1473. var
  1474. u: unicodestring;
  1475. begin
  1476. Do_getdir(drivenr,u);
  1477. widestringmanager.Unicode2AnsiMoveProc(pwidechar(u),dir,DefaultRTLFileSystemCodePage,length(u));
  1478. end;
  1479. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1480. Procedure MkDir(Const s: RawByteString);[IOCheck];
  1481. Begin
  1482. If (s='') or (InOutRes <> 0) then
  1483. exit;
  1484. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1485. Do_mkdir(ToSingleByteFileSystemEncodedFileName(S));
  1486. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1487. Do_mkdir(S);
  1488. {$endif}
  1489. end;
  1490. Procedure RmDir(Const s: RawByteString);[IOCheck];
  1491. Begin
  1492. If (s='') or (InOutRes <> 0) then
  1493. exit;
  1494. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1495. Do_rmdir(ToSingleByteFileSystemEncodedFileName(S));
  1496. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1497. Do_rmdir(S);
  1498. {$endif}
  1499. End;
  1500. Procedure ChDir(Const s: RawByteString);[IOCheck];
  1501. Begin
  1502. If (s='') or (InOutRes <> 0) then
  1503. exit;
  1504. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1505. Do_chdir(ToSingleByteFileSystemEncodedFileName(S));
  1506. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1507. Do_chdir(S);
  1508. {$endif}
  1509. End;
  1510. Procedure getdir(drivenr:byte;Var dir:rawbytestring);
  1511. begin
  1512. Do_getdir(drivenr,dir);
  1513. { we should return results in the DefaultRTLFileSystemCodePage -> convert if
  1514. necessary }
  1515. setcodepage(dir,DefaultRTLFileSystemCodePage,true);
  1516. end;
  1517. { the generic shortstring ones are only implemented elsewhere for systems *not*
  1518. supporting ansi/unicodestrings; for now assume there are no systems that
  1519. support unicodestrings but not ansistrings }
  1520. { avoid double string conversions }
  1521. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1522. function GetDirStrFromShortstring(const s: shortstring): RawByteString;
  1523. begin
  1524. GetDirStrFromShortstring:=ToSingleByteFileSystemEncodedFileName(ansistring(s));
  1525. end;
  1526. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1527. function GetDirStrFromShortstring(const s: shortstring): UnicodeString;
  1528. begin
  1529. GetDirStrFromShortstring:=s;
  1530. end;
  1531. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  1532. Procedure MkDir(Const s: shortstring);[IOCheck];
  1533. Begin
  1534. If (s='') or (InOutRes <> 0) then
  1535. exit;
  1536. Do_mkdir(GetDirStrFromShortstring(S));
  1537. End;
  1538. Procedure RmDir(Const s: shortstring);[IOCheck];
  1539. Begin
  1540. If (s='') or (InOutRes <> 0) then
  1541. exit;
  1542. Do_rmdir(GetDirStrFromShortstring(S));
  1543. End;
  1544. Procedure ChDir(Const s: shortstring);[IOCheck];
  1545. Begin
  1546. If (s='') or (InOutRes <> 0) then
  1547. exit;
  1548. Do_chdir(GetDirStrFromShortstring(S));
  1549. End;
  1550. Procedure getdir(drivenr:byte;Var dir:shortstring);
  1551. var
  1552. s: rawbytestring;
  1553. begin
  1554. Do_getdir(drivenr,s);
  1555. if length(s)<=high(dir) then
  1556. dir:=s
  1557. else
  1558. inoutres:=3;
  1559. end;
  1560. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1561. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1562. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1563. { overloads required for mkdir/rmdir/chdir to ensure that the string is
  1564. converted to the right code page }
  1565. procedure do_mkdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1566. begin
  1567. do_mkdir(ToSingleByteFileSystemEncodedFileName(s));
  1568. end;
  1569. procedure do_rmdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1570. begin
  1571. do_rmdir(ToSingleByteFileSystemEncodedFileName(s));
  1572. end;
  1573. procedure do_chdir(const s: unicodestring); {$ifdef SYSTEMINLINE}inline;{$endif}
  1574. begin
  1575. do_chdir(ToSingleByteFileSystemEncodedFileName(s));
  1576. end;
  1577. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  1578. var
  1579. s: rawbytestring;
  1580. begin
  1581. Do_getdir(drivenr,s);
  1582. dir:=s;
  1583. end;
  1584. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  1585. Procedure MkDir(Const s: UnicodeString);[IOCheck];
  1586. Begin
  1587. if (s='') or (InOutRes <> 0) then
  1588. exit;
  1589. Do_mkdir(S);
  1590. End;
  1591. Procedure RmDir(Const s: UnicodeString);[IOCheck];
  1592. Begin
  1593. if (s='') or (InOutRes <> 0) then
  1594. exit;
  1595. Do_rmdir(S);
  1596. End;
  1597. Procedure ChDir(Const s: UnicodeString);[IOCheck];
  1598. Begin
  1599. if (s='') or (InOutRes <> 0) then
  1600. exit;
  1601. Do_chdir(S);
  1602. End;
  1603. Procedure getdir(drivenr:byte;Var dir:unicodestring);
  1604. begin
  1605. Do_getdir(drivenr,dir);
  1606. end;
  1607. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1608. {$endif FPC_HAS_FEATURE_FILEIO}
  1609. {*****************************************************************************
  1610. Resources support
  1611. *****************************************************************************}
  1612. {$i sysres.inc}
  1613. const
  1614. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1615. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1616. (* It is possible to provide platform specific implementation performing *)
  1617. (* special initialization; default implementation just sets the procedural *)
  1618. (* variable to make it available for use from the exception handler. *)
  1619. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1620. begin
  1621. (* Return either nil or previous handler *)
  1622. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1623. CtrlBreakHandler := Handler;
  1624. end;
  1625. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}