stdlib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <alloca.h>
  2. #include <priv-vars.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <syscall.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. int atoi(const char* str)
  10. {
  11. int ret = 0;
  12. while (*str) {
  13. ret *= 10;
  14. ret += *str - '0';
  15. }
  16. return ret;
  17. }
  18. void __attribute__((noreturn)) exit(int status)
  19. {
  20. syscall1(SYS_exit, status);
  21. for (;;)
  22. ;
  23. }
  24. #define MINIMUM_ALLOCATION_SIZE (8)
  25. #define MEM_ALLOCATED (1)
  26. static inline int _is_end(struct mem* p)
  27. {
  28. return p->sz == 0;
  29. }
  30. static inline int _is_allocated(struct mem* p)
  31. {
  32. return !!(p->flag & MEM_ALLOCATED);
  33. }
  34. static inline size_t _max(size_t a, size_t b)
  35. {
  36. return a > b ? a : b;
  37. }
  38. static inline size_t _size(struct mem* from)
  39. {
  40. if (!_is_end(from))
  41. return from->sz;
  42. size_t sz = curr_brk - (void*)from;
  43. if (sz < sizeof(struct mem))
  44. return 0;
  45. return sz - sizeof(struct mem);
  46. }
  47. static inline struct mem* _next(struct mem* p, size_t sz)
  48. {
  49. return (void*)p + sizeof(struct mem) + sz;
  50. }
  51. static inline void _union(struct mem* p)
  52. {
  53. if (_is_end(p))
  54. return;
  55. for (struct mem* next = _next(p, p->sz);
  56. !(next->flag & MEM_ALLOCATED);
  57. next = _next(p, p->sz)) {
  58. if (_is_end(next)) {
  59. p->sz = 0;
  60. break;
  61. }
  62. p->sz += sizeof(struct mem);
  63. p->sz += next->sz;
  64. }
  65. }
  66. static inline void _cut_block(struct mem* p, size_t mem_size, size_t block_size)
  67. {
  68. if (block_size >= mem_size + sizeof(struct mem) + MINIMUM_ALLOCATION_SIZE) {
  69. p->sz = mem_size;
  70. struct mem* next = _next(p, mem_size);
  71. next->flag = 0;
  72. next->sz = block_size - mem_size - sizeof(struct mem);
  73. }
  74. }
  75. void* malloc(size_t size)
  76. {
  77. if (size < MINIMUM_ALLOCATION_SIZE)
  78. size = MINIMUM_ALLOCATION_SIZE;
  79. struct mem* p = start_brk;
  80. size_t sz = 0;
  81. for (;; p = _next(p, p->sz)) {
  82. if (_is_allocated(p))
  83. continue;
  84. _union(p);
  85. sz = _size(p);
  86. if (_is_end(p)) {
  87. if (sz < size + sizeof(struct mem))
  88. sbrk(_max(128 * 1024, size + sizeof(struct mem)));
  89. sz = p->sz = size;
  90. struct mem* next = _next(p, size);
  91. next->flag = 0;
  92. next->sz = 0;
  93. break;
  94. }
  95. if (sz >= size)
  96. break;
  97. }
  98. p->flag |= MEM_ALLOCATED;
  99. _cut_block(p, size, sz);
  100. return _next(p, 0);
  101. }
  102. void* realloc(void* ptr, size_t newsize)
  103. {
  104. if (!ptr)
  105. return malloc(newsize);
  106. struct mem* p = ptr - sizeof(struct mem);
  107. size_t oldsize = p->sz;
  108. _union(p);
  109. if (_is_end(p)) {
  110. if (_size(p) < newsize + sizeof(struct mem))
  111. sbrk(_max(128 * 1024, newsize + sizeof(struct mem)));
  112. p->sz = newsize;
  113. struct mem* next = _next(p, newsize);
  114. next->flag = 0;
  115. next->sz = 0;
  116. return ptr;
  117. }
  118. if (p->sz >= newsize) {
  119. _cut_block(p, newsize, p->sz);
  120. return ptr;
  121. }
  122. void* newptr = malloc(newsize);
  123. if (!newptr)
  124. return NULL;
  125. memcpy(newptr, ptr, oldsize);
  126. free(ptr);
  127. return newptr;
  128. }
  129. void free(void* ptr)
  130. {
  131. struct mem* p = ptr - sizeof(struct mem);
  132. p->flag &= ~MEM_ALLOCATED;
  133. _union(p);
  134. }
  135. static inline void _swap(void* a, void* b, size_t sz)
  136. {
  137. void* tmp = alloca(sz);
  138. memcpy(tmp, a, sz);
  139. memcpy(a, b, sz);
  140. memcpy(b, tmp, sz);
  141. }
  142. void qsort(void* arr, size_t len, size_t sz, comparator_t cmp) {
  143. if (len <= 1)
  144. return;
  145. char* pivot = alloca(sz);
  146. memcpy(pivot, arr + sz * (rand() % len), sz);
  147. int i = 0, j = 0, k = len;
  148. while (i < k) {
  149. int res = cmp(arr + sz * i, pivot);
  150. if (res < 0)
  151. _swap(arr + sz * i++, arr + sz * j++, sz);
  152. else if (res > 0)
  153. _swap(arr + sz * i, arr + sz * --k, sz);
  154. else
  155. i++;
  156. }
  157. qsort(arr, j, sz, cmp);
  158. qsort(arr + sz * k, len - k, sz, cmp);
  159. }
  160. static unsigned int __next_rand;
  161. int rand(void)
  162. {
  163. return rand_r(&__next_rand);
  164. }
  165. int rand_r(unsigned int* seedp)
  166. {
  167. *seedp = *seedp * 1103515245 + 12345;
  168. return (unsigned int) (*seedp / 65536) % 32768;
  169. }
  170. void srand(unsigned int seed)
  171. {
  172. __next_rand = seed;
  173. rand();
  174. }
  175. void* bsearch(const void* key, const void* base, size_t num, size_t size, comparator_t cmp)
  176. {
  177. if (num == 0)
  178. return NULL;
  179. size_t mid = num / 2;
  180. int result = cmp(key, base + size * mid);
  181. if (result == 0)
  182. return (void*)base + size * mid;
  183. if (result > 0) {
  184. ++mid;
  185. return bsearch(key, base + size * mid, num - mid, size, cmp);
  186. }
  187. return bsearch(key, base, mid, size, cmp);
  188. }
  189. int setenv(const char* name, const char* value, int overwrite)
  190. {
  191. size_t i = 0;
  192. for (; environ[i]; ++i) {
  193. char* eqpos = strchr(environ[i], '=');
  194. if (strncmp(name, environ[i], eqpos - environ[i]) == 0) {
  195. if (overwrite)
  196. goto fill_p;
  197. return 0;
  198. }
  199. }
  200. if (i + 2 == environ_size) {
  201. environ_size *= 2;
  202. char** newarr = malloc(environ_size * sizeof(char*));
  203. if (!newarr)
  204. return -1;
  205. memcpy(newarr, environ, sizeof(char*) * environ_size / 2);
  206. free(environ);
  207. environ = newarr;
  208. }
  209. environ[i + 1] = NULL;
  210. fill_p:;
  211. char* newenv = NULL;
  212. if (asprintf(&newenv, "%s=%s", name, value) < 0)
  213. return -1;
  214. environ[i] = newenv;
  215. return 0;
  216. }