alloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include "alloc.h"
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include "stream.h"
  5. void* byte_heap;
  6. Cell* cell_heap;
  7. uint16_t cells_used;
  8. uint16_t byte_heap_used;
  9. Cell** free_list;
  10. uint16_t free_list_avail;
  11. uint16_t free_list_consumed;
  12. Cell oom_cell;
  13. //#define DEBUG_GC
  14. // TODO define in machine specs
  15. #define MAX_CELLS 100000
  16. #define MAX_BYTE_HEAP 8*1024
  17. static struct MemStats mem_stats;
  18. void init_allocator() {
  19. unsigned int cell_mem_reserved = MAX_CELLS*sizeof(Cell);
  20. oom_cell.tag = TAG_ERROR;
  21. oom_cell.ar.value = ERR_OUT_OF_MEMORY;
  22. byte_heap_used = 0;
  23. cells_used = 0;
  24. free_list_avail = 0;
  25. free_list_consumed = 0;
  26. byte_heap = NULL;
  27. cell_heap = malloc(cell_mem_reserved);
  28. printf("\r\n++ cell heap at %p, %d bytes reserved\r\n",cell_heap,cell_mem_reserved);
  29. memset(cell_heap,0,cell_mem_reserved);
  30. free_list = malloc(MAX_CELLS*sizeof(Cell*));
  31. printf("[allocator] initialized.\r\n");
  32. //byte_heap = malloc(MAX_BYTE_HEAP);
  33. }
  34. Cell* get_cell_heap() {
  35. return cell_heap;
  36. }
  37. // FIXME header?
  38. env_t* get_global_env();
  39. Cell* cell_alloc() {
  40. /*if (free_list_avail<free_list_consumed) {
  41. // try gc
  42. // FIXME need access to current frame
  43. collect_garbage(get_global_env());
  44. }*/
  45. if (free_list_avail>free_list_consumed) {
  46. // serve from free list
  47. int idx = free_list_consumed;
  48. Cell* res = free_list[idx];
  49. free_list_consumed++;
  50. //printf("++ cell_alloc: recycled %d (%p)\r\n",idx,res);
  51. return res;
  52. } else {
  53. Cell* res = &cell_heap[cells_used];
  54. cells_used++;
  55. if (cells_used>MAX_CELLS) {
  56. printf("!! cell_alloc failed, MAX_CELLS used.\n");
  57. exit(1);
  58. }
  59. //printf("++ cell_alloc: %d \r\n",cells_used);
  60. return res;
  61. }
  62. }
  63. void* bytes_alloc(int num_bytes) {
  64. //#ifdef SLEDGE_MALLOC
  65. void* new_mem = malloc(num_bytes);
  66. //printf("bytes_alloc: %p +%d\r\n",new_mem,num_bytes);
  67. memset(new_mem, 0, num_bytes);
  68. return new_mem;
  69. //#endif
  70. /*void* new_mem = byte_heap + byte_heap_used;
  71. if (byte_heap_used + num_bytes < MAX_BYTE_HEAP) {
  72. byte_heap_used += num_bytes;
  73. //printf("++ byte_alloc: %d (+%d) \r\n",byte_heap_used,num_bytes);
  74. return new_mem;
  75. } else {
  76. printf("~~ bytes_alloc: out of memory: %d (%d)\r\n",byte_heap,byte_heap_used);
  77. exit(1);
  78. return NULL;
  79. }*/
  80. }
  81. void mark_tree(Cell* c) {
  82. if (!c) {
  83. //printf("~! warning: mark_tree encountered NULL cell.\n");
  84. return;
  85. }
  86. if (!(c->tag & TAG_MARK)) {
  87. /*char buf[80];
  88. lisp_write(c, buf, 79);
  89. printf("~~ marking live: %p %s\n",c,buf);*/
  90. c->tag |= TAG_MARK;
  91. if (c->tag & TAG_CONS) {
  92. if (c->ar.addr) mark_tree((Cell*)c->ar.addr);
  93. if (c->dr.next) mark_tree((Cell*)c->dr.next);
  94. }
  95. else if (c->tag & TAG_SYM) {
  96. // TODO: mark bytes in heap
  97. // also for STR, BYTES
  98. }
  99. else if (c->tag & TAG_LAMBDA) {
  100. /*static char buf[512];
  101. lisp_write((Cell*)c->ar.addr, buf, 511);
  102. printf("~~ mark lambda args: %s\n",buf);*/
  103. mark_tree((Cell*)c->ar.addr); // function arguments
  104. //mark_tree((Cell*)c->dr.next); // function body
  105. // TODO: mark compiled code / free unused compiled code
  106. // -- keep all compiled blobs in a list
  107. }
  108. else if (c->tag & TAG_BUILTIN) {
  109. mark_tree((Cell*)c->dr.next); // builtin signature
  110. }
  111. else if (c->tag & TAG_STREAM) {
  112. Stream* s = (Stream*)c->ar.addr;
  113. mark_tree(s->path);
  114. }
  115. else if (c->tag & TAG_FS) {
  116. Filesystem* fs = (Filesystem*)c->dr.next;
  117. mark_tree(fs->mount_point);
  118. mark_tree(fs->open_fn);
  119. mark_tree(fs->close_fn);
  120. mark_tree(fs->read_fn);
  121. mark_tree(fs->write_fn);
  122. mark_tree(fs->delete_fn);
  123. mark_tree(fs->mmap_fn);
  124. }
  125. }
  126. }
  127. static Cell* _symbols_list;
  128. void list_symbols_iter(const char *key, void *value, const void *obj)
  129. {
  130. env_entry* e = (env_entry*)value;
  131. _symbols_list = alloc_cons(alloc_sym(e->name), _symbols_list);
  132. }
  133. Cell* list_symbols(env_t* global_env) {
  134. _symbols_list = alloc_nil();
  135. sm_enum(global_env, list_symbols_iter, NULL);
  136. return _symbols_list;
  137. }
  138. void collect_garbage_iter(const char *key, void *value, const void *obj)
  139. {
  140. env_entry* e = (env_entry*)value;
  141. //printf("key: %s value: %s\n", key, value);
  142. mark_tree(e->cell);
  143. }
  144. Cell* collect_garbage(env_t* global_env, void* stack_end, void* stack_pointer) {
  145. // mark
  146. // check all symbols in the environment
  147. // and look where they lead (cons trees, bytes, strings)
  148. // mark all of them as usable
  149. // (def foo (fn (do (let a 1) (let b 2) (+ a b) (gc))))
  150. //printf("[gc] stack at: %p, stack end: %p\r\n",stack_pointer,stack_end);
  151. int gc, highwater, i;
  152. //char buf[300];
  153. int sw_state = 0;
  154. jit_word_t* a;
  155. for (a=(jit_word_t*)stack_end; a>=(jit_word_t*)stack_pointer; a--) {
  156. jit_word_t item = *a;
  157. jit_word_t next_item = *(a-1);
  158. if (next_item==STACK_FRAME_MARKER) {
  159. sw_state=2;
  160. } else {
  161. if (sw_state==2) {
  162. sw_state=1;
  163. } else if (sw_state==1) {
  164. // FIXME total hack, need type information for stack
  165. // maybe type/signature byte frame header?
  166. if ((Cell*)item>cell_heap) {
  167. mark_tree((Cell*)item);
  168. }
  169. }
  170. }
  171. /*if (sw_state==2) {
  172. printf(KMAG "%p: 0x%08lx\r\n" KWHT,a,item);
  173. }
  174. else if (sw_state==1) {
  175. printf(KCYN "%p: 0x%08lx\r\n" KWHT,a,item);
  176. }
  177. else {
  178. printf(KWHT "%p: 0x%08lx\r\n" KWHT,a,item);
  179. }*/
  180. }
  181. //printf("[gc] stack walk complete -------------------------------\r\n");
  182. sm_enum(global_env, collect_garbage_iter, NULL);
  183. mark_tree(get_fs_list());
  184. /*for (env_entry* e=global_env; e != NULL; e=e->hh.next) {
  185. //printf("env entry: %s pointing to %p\n",e->name,e->cell);
  186. if (!e->cell) {
  187. //printf("~! warning: NULL env entry %s.\n",e->name);
  188. }
  189. mark_tree(e->cell);
  190. }*/
  191. // sweep -- free all things that are not marked
  192. free_list_avail = 0;
  193. free_list_consumed = 0;
  194. #ifdef DEBUG_GC
  195. printf("\e[1;1H\e[2J");
  196. printf("~~ cell memory: ");
  197. #endif
  198. for (i=0; i<cells_used; i++) {
  199. Cell* c = &cell_heap[i];
  200. // FIXME: we cannot free LAMBDAS currently
  201. // because nobody points to anonymous closures.
  202. // this has to be fixed by introducing metadata to their callers. (?)
  203. if (!(c->tag & TAG_MARK) && c->tag!=TAG_LAMBDA) {
  204. #ifdef DEBUG_GC
  205. printf(".");
  206. #endif
  207. if (c->tag == TAG_BYTES || c->tag == TAG_STR) {
  208. free(c->ar.addr);
  209. }
  210. c->tag = TAG_FREED;
  211. free_list[free_list_avail] = c;
  212. free_list_avail++;
  213. gc++;
  214. } else {
  215. highwater = i;
  216. #ifdef DEBUG_GC
  217. printf("o");
  218. #endif
  219. }
  220. // unset mark bit
  221. cell_heap[i].tag &= ~TAG_MARK;
  222. }
  223. //printf("[gc] highwater %d fl_avail %d \r\n",highwater,free_list_avail);
  224. // FIXME on x64, this line causes corruption over time
  225. //cells_used = highwater+1;
  226. #ifdef DEBUG_GC
  227. printf("\n\n");
  228. printf("~~ %d of %d cells were garbage.\r\n",gc,cells_used);
  229. #endif
  230. //printf("-- %d high water mark.\n\n",cells_used);
  231. return alloc_int(gc);
  232. }
  233. void* cell_realloc(void* old_addr, unsigned int old_size, unsigned int num_bytes) {
  234. // FIXME
  235. //cell_free(old_addr, old_size);
  236. void* new = bytes_alloc(num_bytes+1);
  237. memcpy(new, old_addr, old_size);
  238. return new;
  239. }
  240. MemStats* alloc_stats(void) {
  241. mem_stats.byte_heap_used = byte_heap_used;
  242. mem_stats.byte_heap_max = MAX_BYTE_HEAP;
  243. mem_stats.cells_used = cells_used;
  244. mem_stats.cells_max = MAX_CELLS;
  245. return &mem_stats;
  246. }
  247. Cell* alloc_cons(Cell* ar, Cell* dr) {
  248. //printf("alloc_cons: ar %p dr %p\n",ar,dr);
  249. Cell* cons = cell_alloc();
  250. cons->tag = TAG_CONS;
  251. cons->ar.addr = ar; //?alloc_clone(ar):ar;
  252. cons->dr.next = dr; //?alloc_clone(dr):dr;
  253. return cons;
  254. }
  255. Cell* alloc_list(Cell** items, int num) {
  256. Cell* list = alloc_nil();
  257. int i;
  258. for (i=num-1; i>=0; i--) {
  259. list = alloc_cons(items[i], list);
  260. }
  261. return list;
  262. }
  263. //extern void uart_puts(char* str);
  264. extern void memdump(jit_word_t start,uint16_t len,int raw);
  265. Cell* alloc_sym(char* str) {
  266. Cell* sym = cell_alloc();
  267. //printf("++ alloc sym at %p %p %d\r\n",sym,sym->ar.addr,sym->size);
  268. sym->tag = TAG_SYM;
  269. if (str) {
  270. int sz = strlen(str)+1;
  271. sym->dr.size = sz;
  272. //printf("alloc_sym: %s (%d)\r\n",str,sz);
  273. //memdump(sym,sizeof(Cell),0);
  274. sym->ar.addr = bytes_alloc(sz+1);
  275. //memdump(sym,sizeof(Cell),0);
  276. memcpy(sym->ar.addr, str, sz);
  277. //memdump(sym,sizeof(Cell),0);
  278. } else {
  279. sym->ar.addr = 0;
  280. sym->dr.size = 0;
  281. }
  282. return sym;
  283. }
  284. Cell* alloc_int(int i) {
  285. //printf("++ alloc_int %d\r\n",i);
  286. Cell* num = cell_alloc();
  287. num->tag = TAG_INT;
  288. num->ar.value = i;
  289. return num;
  290. }
  291. Cell* alloc_num_bytes(unsigned int num_bytes) {
  292. Cell* cell = cell_alloc();
  293. cell->ar.addr = bytes_alloc(num_bytes+1); // 1 zeroed byte more to defeat clib-str overflows
  294. cell->tag = TAG_BYTES;
  295. cell->dr.size = num_bytes;
  296. return cell;
  297. }
  298. Cell* alloc_bytes(void) {
  299. return alloc_num_bytes(SYM_INIT_BUFFER_SIZE);
  300. }
  301. Cell* alloc_num_string(unsigned int num_bytes) {
  302. Cell* cell = cell_alloc();
  303. cell->ar.addr = bytes_alloc(num_bytes+1); // 1 zeroed byte more to defeat clib-str overflows
  304. cell->tag = TAG_STR;
  305. cell->dr.size = num_bytes;
  306. return cell;
  307. }
  308. Cell* alloc_substr(Cell* str, unsigned int from, unsigned int len) {
  309. Cell* cell;
  310. if (!str) return alloc_string_copy("");
  311. if (str->tag!=TAG_BYTES && str->tag!=TAG_STR) return alloc_string_copy("");
  312. //printf("substr %s %d %d\n",str->ar.addr,from,len);
  313. if (from>=str->dr.size) from=str->dr.size-1;
  314. if (len+from>str->dr.size) len=str->dr.size-from; // FIXME TEST
  315. cell = cell_alloc();
  316. cell->ar.addr = bytes_alloc(len+1); // 1 zeroed byte more to defeat clib-str overflows
  317. cell->tag = TAG_STR;
  318. cell->dr.size = len;
  319. memcpy(cell->ar.addr, (uint8_t*)str->ar.addr+from, len);
  320. return cell;
  321. }
  322. Cell* alloc_string(void) {
  323. return alloc_num_string(SYM_INIT_BUFFER_SIZE);
  324. }
  325. Cell* alloc_string_copy(char* str) {
  326. Cell* cell = cell_alloc();
  327. cell->ar.addr = bytes_alloc(strlen(str)+1);
  328. strcpy(cell->ar.addr, str);
  329. cell->tag = TAG_STR;
  330. cell->dr.size = strlen(str)+1;
  331. return cell;
  332. }
  333. Cell* alloc_string_from_bytes(Cell* bytes) {
  334. Cell* cell;
  335. if (!bytes) return alloc_string_copy("");
  336. if (bytes->tag!=TAG_BYTES && bytes->tag!=TAG_STR) return alloc_string_copy("");
  337. if (bytes->dr.size<1) return alloc_string_copy("");
  338. cell = cell_alloc();
  339. cell->ar.addr = bytes_alloc(bytes->dr.size+1);
  340. memcpy(cell->ar.addr, bytes->ar.addr, bytes->dr.size);
  341. ((char*)cell->ar.addr)[bytes->dr.size]=0;
  342. cell->tag = TAG_STR;
  343. cell->dr.size = bytes->dr.size+1;
  344. return cell;
  345. }
  346. Cell* alloc_concat(Cell* str1, Cell* str2) {
  347. Cell* cell;
  348. int size1, size2, newsize;
  349. if (!str1 || !str2) return alloc_string_copy("");
  350. if (str1->tag!=TAG_BYTES && str1->tag!=TAG_STR) return alloc_string_copy("");
  351. if (str2->tag!=TAG_BYTES && str2->tag!=TAG_STR) return alloc_string_copy("");
  352. cell = cell_alloc();
  353. size1 = strlen(str1->ar.addr); // ,str2->size
  354. size2 = strlen(str2->ar.addr);
  355. newsize = size1+size2+1;
  356. cell->ar.addr = bytes_alloc(newsize+1);
  357. cell->dr.size = newsize;
  358. strncpy(cell->ar.addr, str1->ar.addr, size1);
  359. strncpy(cell->ar.addr+size1, str2->ar.addr, 1+cell->dr.size-size1);
  360. ((char*)cell->ar.addr)[newsize]=0;
  361. cell->tag = TAG_STR;
  362. cell->dr.size = newsize;
  363. return cell;
  364. }
  365. Cell* alloc_builtin(unsigned int b, Cell* signature) {
  366. Cell* num = cell_alloc();
  367. num->tag = TAG_BUILTIN;
  368. num->ar.value = b;
  369. num->dr.next = signature;
  370. return num;
  371. }
  372. Cell* alloc_lambda(Cell* args) {
  373. Cell* l = cell_alloc();
  374. l->tag = TAG_LAMBDA;
  375. l->ar.addr = args; // arguments
  376. //l->dr.next = cdr(def); // body
  377. return l;
  378. }
  379. Cell* alloc_nil(void) {
  380. return alloc_cons(0,0);
  381. }
  382. int is_nil(Cell* c) {
  383. return (c==NULL || (c->ar.addr==NULL && c->dr.next==NULL));
  384. }
  385. Cell* alloc_error(unsigned int code) {
  386. Cell* c = cell_alloc();
  387. c->tag = TAG_ERROR;
  388. c->ar.value = code;
  389. c->dr.next = 0;
  390. return c;
  391. }
  392. Cell* alloc_clone(Cell* orig) {
  393. Cell* clone;
  394. if (!orig) return 0;
  395. clone = cell_alloc();
  396. clone->tag = orig->tag;
  397. clone->ar.addr = 0;
  398. clone->dr.next = 0;
  399. clone->dr.size = orig->dr.size;
  400. //printf("cloning a %d (value %d)\n",orig->tag,orig->ar.value);
  401. if (orig->tag == TAG_SYM || orig->tag == TAG_STR || orig->tag == TAG_BYTES) {
  402. clone->ar.addr = bytes_alloc(orig->dr.size+1);
  403. memcpy(clone->ar.addr, orig->ar.addr, orig->dr.size);
  404. /*} else if (orig->tag == TAG_BYTES) {
  405. clone->ar.addr = bytes_alloc(orig->dr.size);
  406. memcpy(clone->ar.addr, orig->ar.addr, orig->dr.size);*/
  407. } else if (orig->tag == TAG_CONS) {
  408. if (orig->ar.addr) {
  409. clone->ar.addr = alloc_clone(orig->ar.addr);
  410. }
  411. if (orig->dr.next) {
  412. clone->dr.next = alloc_clone(orig->dr.next);
  413. }
  414. } else {
  415. clone->ar.addr = orig->ar.addr;
  416. clone->dr.next = orig->dr.next;
  417. }
  418. return clone;
  419. }