system.inc 51 KB

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