system.inc 51 KB

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