system.inc 51 KB

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