system.inc 50 KB

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