CRuntime.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Hebron.Runtime
  4. {
  5. internal static unsafe class CRuntime
  6. {
  7. private static readonly string numbers = "0123456789";
  8. public static void* malloc(ulong size)
  9. {
  10. return malloc((long)size);
  11. }
  12. public static void* malloc(long size)
  13. {
  14. var ptr = Marshal.AllocHGlobal((int)size);
  15. MemoryStats.Allocated();
  16. return ptr.ToPointer();
  17. }
  18. public static void free(void* a)
  19. {
  20. if (a == null)
  21. return;
  22. var ptr = new IntPtr(a);
  23. Marshal.FreeHGlobal(ptr);
  24. MemoryStats.Freed();
  25. }
  26. public static void memcpy(void* a, void* b, long size)
  27. {
  28. var ap = (byte*)a;
  29. var bp = (byte*)b;
  30. for (long i = 0; i < size; ++i)
  31. *ap++ = *bp++;
  32. }
  33. public static void memcpy(void* a, void* b, ulong size)
  34. {
  35. memcpy(a, b, (long)size);
  36. }
  37. public static void memmove(void* a, void* b, long size)
  38. {
  39. void* temp = null;
  40. try
  41. {
  42. temp = malloc(size);
  43. memcpy(temp, b, size);
  44. memcpy(a, temp, size);
  45. }
  46. finally
  47. {
  48. if (temp != null)
  49. free(temp);
  50. }
  51. }
  52. public static void memmove(void* a, void* b, ulong size)
  53. {
  54. memmove(a, b, (long)size);
  55. }
  56. public static int memcmp(void* a, void* b, long size)
  57. {
  58. var result = 0;
  59. var ap = (byte*)a;
  60. var bp = (byte*)b;
  61. for (long i = 0; i < size; ++i)
  62. {
  63. if (*ap != *bp)
  64. result += 1;
  65. ap++;
  66. bp++;
  67. }
  68. return result;
  69. }
  70. public static int memcmp(void* a, void* b, ulong size)
  71. {
  72. return memcmp(a, b, (long)size);
  73. }
  74. public static int memcmp(byte* a, byte[] b, ulong size)
  75. {
  76. fixed (void* bptr = b)
  77. {
  78. return memcmp(a, bptr, (long)size);
  79. }
  80. }
  81. public static void memset(void* ptr, int value, long size)
  82. {
  83. var bptr = (byte*)ptr;
  84. var bval = (byte)value;
  85. for (long i = 0; i < size; ++i)
  86. *bptr++ = bval;
  87. }
  88. public static void memset(void* ptr, int value, ulong size)
  89. {
  90. memset(ptr, value, (long)size);
  91. }
  92. public static uint _lrotl(uint x, int y)
  93. {
  94. return (x << y) | (x >> (32 - y));
  95. }
  96. public static void* realloc(void* a, long newSize)
  97. {
  98. if (a == null)
  99. return malloc(newSize);
  100. var ptr = new IntPtr(a);
  101. var result = Marshal.ReAllocHGlobal(ptr, new IntPtr(newSize));
  102. return result.ToPointer();
  103. }
  104. public static void* realloc(void* a, ulong newSize)
  105. {
  106. return realloc(a, (long)newSize);
  107. }
  108. public static int abs(int v)
  109. {
  110. return Math.Abs(v);
  111. }
  112. public static double pow(double a, double b)
  113. {
  114. return Math.Pow(a, b);
  115. }
  116. public static void SetArray<T>(T[] data, T value)
  117. {
  118. for (var i = 0; i < data.Length; ++i)
  119. data[i] = value;
  120. }
  121. public static double ldexp(double number, int exponent)
  122. {
  123. return number * Math.Pow(2, exponent);
  124. }
  125. public static int strcmp(sbyte* src, string token)
  126. {
  127. var result = 0;
  128. for (var i = 0; i < token.Length; ++i)
  129. if (src[i] != token[i])
  130. ++result;
  131. return result;
  132. }
  133. public static int strncmp(sbyte* src, string token, ulong size)
  134. {
  135. var result = 0;
  136. for (var i = 0; i < Math.Min(token.Length, (int)size); ++i)
  137. if (src[i] != token[i])
  138. ++result;
  139. return result;
  140. }
  141. public static long strtol(sbyte* start, sbyte** end, int radix)
  142. {
  143. // First step - determine length
  144. var length = 0;
  145. var ptr = start;
  146. while (numbers.IndexOf((char)*ptr) != -1)
  147. {
  148. ++ptr;
  149. ++length;
  150. }
  151. long result = 0;
  152. // Now build up the number
  153. ptr = start;
  154. while (length > 0)
  155. {
  156. long num = numbers.IndexOf((char)*ptr);
  157. var pow = (long)Math.Pow(10, length - 1);
  158. result += num * pow;
  159. ++ptr;
  160. --length;
  161. }
  162. if (end != null) *end = ptr;
  163. return result;
  164. }
  165. public static float fabs(double a)
  166. {
  167. return (float)Math.Abs(a);
  168. }
  169. public static double ceil(double a)
  170. {
  171. return Math.Ceiling(a);
  172. }
  173. public static double floor(double a)
  174. {
  175. return Math.Floor(a);
  176. }
  177. public static double cos(double value)
  178. {
  179. return Math.Cos(value);
  180. }
  181. public static double acos(double value)
  182. {
  183. return Math.Acos(value);
  184. }
  185. public static double sin(double value)
  186. {
  187. return Math.Sin(value);
  188. }
  189. public static double sqrt(double val)
  190. {
  191. return Math.Sqrt(val);
  192. }
  193. public static double fmod(double x, double y)
  194. {
  195. return x % y;
  196. }
  197. public static ulong strlen(sbyte* str)
  198. {
  199. var ptr = str;
  200. while (*ptr != '\0')
  201. ptr++;
  202. return (ulong)ptr - (ulong)str - 1;
  203. }
  204. }
  205. }