cgeneric.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Processor independent implementation for the system unit
  5. (based on libc)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. Primitives
  14. ****************************************************************************}
  15. {$ifndef FPC_SYSTEM_HAS_MOVE}
  16. {$define FPC_SYSTEM_HAS_MOVE}
  17. procedure bcopy(const source;var dest;count:size_t); cdecl; external clib name 'bcopy';
  18. { we need this separate move declaration because we can't add a "public, alias" to the above }
  19. procedure Move(const source;var dest;count:sizeint); [public, alias: 'FPC_MOVE'];{$ifdef SYSTEMINLINE}inline;{$endif}
  20. begin
  21. if count <= 0 then
  22. exit;
  23. bcopy(source,dest,count);
  24. end;
  25. {$endif not FPC_SYSTEM_HAS_MOVE}
  26. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  27. {$define FPC_SYSTEM_HAS_FILLCHAR}
  28. procedure memset(var x; value: byte; count: size_t); cdecl; external clib;
  29. Procedure FillChar(var x;count: sizeint;value:byte);{$ifdef SYSTEMINLINE}inline;{$endif}
  30. begin
  31. if count <= 0 then
  32. exit;
  33. memset(x,value,count);
  34. end;
  35. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  36. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  37. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  38. {$ifdef LINUX}
  39. {$define BUGGYMEMCHR}
  40. {$endif}
  41. function memchr(const buf; b: cint; len: size_t): pointer; cdecl; external clib;
  42. {$ifdef BUGGYMEMCHR}
  43. function rawmemchr(const buf; b: cint): pointer; cdecl; external clib;
  44. {$endif BUGGYMEMCHR}
  45. function IndexByte(Const buf;len:sizeint;b:byte):sizeint;{$ifdef SYSTEMINLINE}inline;{$endif}
  46. var
  47. res: pointer;
  48. begin
  49. if len = 0 then
  50. exit(-1);
  51. { simulate assembler implementations behaviour, which is expected }
  52. { fpc_pchar_to_ansistr in astrings.inc (interpret values < 0 as }
  53. { unsigned) }
  54. {$ifdef BUGGYMEMCHR}
  55. if len = -1 then
  56. res := rawmemchr(buf,cint(b))
  57. else
  58. {$endif BUGGYMEMCHR}
  59. res := memchr(buf,cint(b),size_t(sizeuint(len)));
  60. if (res <> nil) then
  61. IndexByte := SizeInt(res-@buf)
  62. else
  63. IndexByte := -1;
  64. end;
  65. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  66. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  67. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  68. function memcmp_comparechar(Const buf1,buf2;len:size_t):cint; cdecl; external clib name 'memcmp';
  69. function CompareByte(Const buf1,buf2;len:sizeint):sizeint;{$ifdef SYSTEMINLINE}inline;{$endif}
  70. var
  71. res: longint;
  72. begin
  73. if len <= 0 then
  74. exit(0);
  75. res := memcmp_comparechar(buf1,buf2,len);
  76. if res < 0 then
  77. CompareByte := -1
  78. else if res > 0 then
  79. CompareByte := 1
  80. else
  81. CompareByte := 0;
  82. end;
  83. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  84. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  85. {$define FPC_SYSTEM_HAS_COMPARECHAR0}
  86. function strncmp_comparechar0(Const buf1,buf2;len:size_t):longint; cdecl; external clib name 'strncmp';
  87. function CompareChar0(Const buf1,buf2;len:sizeint):sizeint;{$ifdef SYSTEMINLINE}inline;{$endif}
  88. begin
  89. if len <= 0 then
  90. exit(0);
  91. CompareChar0:=strncmp_comparechar0(buf1,buf2,len);
  92. end;
  93. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  94. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  95. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  96. function libc_pchar_length(p:pchar):size_t; cdecl; external clib name 'strlen';
  97. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  98. begin
  99. if assigned(p) then
  100. fpc_pchar_length:=libc_pchar_length(p)
  101. else
  102. fpc_pchar_length:=0;
  103. end;
  104. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}