2
0

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