alloc.c 14 KB

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