func.c 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdio.h>
  2. #include "table.h"
  3. #include "mem.h"
  4. #include "func.h"
  5. static TFunc *function_root = NULL;
  6. /*
  7. ** Insert function in list for GC
  8. */
  9. void luaI_insertfunction (TFunc *f)
  10. {
  11. lua_pack();
  12. f->next = function_root;
  13. function_root = f;
  14. f->marked = 0;
  15. }
  16. /*
  17. ** Free function
  18. */
  19. static void freefunc (TFunc *f)
  20. {
  21. luaI_free (f->code);
  22. luaI_free (f);
  23. }
  24. /*
  25. ** Garbage collection function.
  26. ** This function traverse the function list freeing unindexed functions
  27. */
  28. Long luaI_funccollector (void)
  29. {
  30. TFunc *curr = function_root;
  31. TFunc *prev = NULL;
  32. Long counter = 0;
  33. while (curr)
  34. {
  35. TFunc *next = curr->next;
  36. if (!curr->marked)
  37. {
  38. if (prev == NULL)
  39. function_root = next;
  40. else
  41. prev->next = next;
  42. freefunc (curr);
  43. ++counter;
  44. }
  45. else
  46. {
  47. curr->marked = 0;
  48. prev = curr;
  49. }
  50. curr = next;
  51. }
  52. return counter;
  53. }