25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

core.hpp 4.0 KiB

3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
3 hafta önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef CORE_HPP
  2. #define CORE_HPP
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <sys/stat.h>
  7. #include <sys/mman.h>
  8. // ### Types ###
  9. typedef int8_t int8;
  10. typedef int16_t int16;
  11. typedef int32_t int32;
  12. typedef int64_t int64;
  13. typedef uint8_t uint8;
  14. typedef uint16_t uint16;
  15. typedef uint32_t uint32;
  16. typedef uint64_t uint64;
  17. typedef uint8_t byte;
  18. typedef float real32;
  19. typedef double real64;
  20. // ### Sizes and Numbers ###
  21. #define Bytes(n) (n)
  22. #define Kilobytes(n) (n << 10)
  23. #define Megabytes(n) (n << 20)
  24. #define Gigabytes(n) (((uint64)n) << 30)
  25. #define Terabytes(n) (((uint64)n) << 40)
  26. #define Thousand(n) ((n)*1000)
  27. #define Million(n) ((n)*1000000)
  28. #define Billion(n) ((n)*1000000000LL)
  29. #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
  30. // ### Arenas ###
  31. struct Arena {
  32. void *memory;
  33. size_t capacity;
  34. size_t head;
  35. };
  36. void *pushSize(Arena *arena, size_t bytes) {
  37. if (arena->capacity - arena->head >= bytes) {
  38. void *ptr = (char *)arena->memory + arena->head;
  39. arena->head += bytes;
  40. return ptr;
  41. }
  42. return 0;
  43. }
  44. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  45. #define PushStruct(arena, type, size) (type *)pushSize(arena, sizeof(type))
  46. Arena createArena(size_t capacity) {
  47. Arena result = {};
  48. result.memory = mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  49. result.capacity = capacity;
  50. result.head = 0;
  51. return result;
  52. }
  53. // ### Lists ###
  54. template <typename T>
  55. struct list {
  56. T* data;
  57. size_t capacity;
  58. size_t length;
  59. };
  60. #define PushList(arena, type, size) ((list<type>){ PushArray(arena, type, size), size, 0 })
  61. template <typename T>
  62. T *appendList(list<T> *list, T element) {
  63. if (list->length < list->capacity) {
  64. list->data[list->length] = element;
  65. list->length++;
  66. return &(list->data[list->length - 1]);
  67. } else {
  68. return 0;
  69. }
  70. }
  71. template <typename T>
  72. void zeroList(list<T> *list) {
  73. list->length = 0;
  74. memset(list->data, 0, list->capacity * sizeof(T));
  75. }
  76. #define foreach(it, list, type) for (size_t __i = 0; __i < list.length; __i++) { type it = list.data[__i];
  77. #define foreach_end() }
  78. // ### Strings ###
  79. #define strlit(lit) ((string){(char *)(lit), sizeof(lit) - 1})
  80. struct string {
  81. char *str;
  82. size_t len;
  83. };
  84. #define PushString(arena, length) ((string){ (char *)pushSize(arena, length), (length) })
  85. const char *cstring(Arena *arena, list<char> buf) {
  86. char *arr = PushArray(arena, char, buf.length + 1);
  87. memmove(arr, buf.data, buf.length);
  88. arr[buf.length] = '\0';
  89. return arr;
  90. }
  91. const char *cstring(Arena *arena, string str) {
  92. char *arr = PushArray(arena, char, str.len + 1);
  93. memmove(arr, str.str, str.len);
  94. arr[str.len] = '\0';
  95. return arr;
  96. }
  97. bool strEql(string s1, string s2) {
  98. if (s1.len != s2.len) {
  99. return false;
  100. }
  101. for (size_t i = 0; i < s1.len; i++) {
  102. if (s1.str[i] != s2.str[i]) {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. string strReverse(Arena *arena, string str) {
  109. string reversed = PushString(arena, str.len);
  110. for (
  111. int mainIndex = str.len - 1, reversedIndex = 0;
  112. mainIndex < str.len;
  113. mainIndex--, reversedIndex++
  114. ) {
  115. reversed.str[reversedIndex] = str.str[mainIndex];
  116. }
  117. return reversed;
  118. }
  119. string strSlice(char *data, size_t start, size_t stop) {
  120. return {
  121. data + start,
  122. stop - start,
  123. };
  124. }
  125. // ### File IO ###
  126. size_t getFileSize(string file) {
  127. struct stat st;
  128. stat((char *)file.str, &st);
  129. return st.st_size;
  130. }
  131. list<char> readEntireFile(Arena *arena, string file) {
  132. FILE *input = fopen((char *)file.str, "r");
  133. size_t filesize = getFileSize(file);
  134. list<char> readBuffer = PushList(arena, char, filesize);
  135. readBuffer.length = filesize;
  136. fread(readBuffer.data, sizeof(byte), filesize, input);
  137. fclose(input);
  138. return readBuffer;
  139. }
  140. int cmpint(const void *a, const void *b) {
  141. int *x = (int *)a;
  142. int *y = (int *)b;
  143. return (*x > *y) - (*x < *y);
  144. }
  145. #endif