The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gc.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include "sol.h"
  5. /** Allocates and returns a new reference to a typeless object.
  6. *
  7. * This is an internal function. Users should use `sol_alloc_object` instead.
  8. */
  9. sol_object_t *_sol_gc_alloc_object(sol_state_t *state) {
  10. sol_object_t *res = malloc(sizeof(sol_object_t));
  11. if(!res) {
  12. sol_set_error(state, state->OutOfMemory);
  13. return sol_incref(state->None);
  14. }
  15. res->refcnt = 0;
  16. res->ops = &(state->NullOps);
  17. return sol_incref(res);
  18. }
  19. /** Frees a reference to an object.
  20. *
  21. * This is an internal function. Users should use `sol_obj_free` instead.
  22. */
  23. void _sol_gc_obj_free(sol_object_t *obj) {
  24. if(!obj) {
  25. /*printf("WARNING: Attempt to free NULL\n");*/
  26. return;
  27. }
  28. if(sol_decref(obj) <= 0) {
  29. if(obj->refcnt < 0) {
  30. printf("WARNING: Encountered refcnt < 0!\nObject %p type %d ref %d\n", obj, obj->type, obj->refcnt);
  31. } else {
  32. sol_obj_release(obj);
  33. }
  34. }
  35. }
  36. /** Increments the reference count of an object, and return it.
  37. *
  38. * This function is exactly an identity function, but it increments the
  39. * object's reference count.
  40. *
  41. * It is intended for use in places where a function is required, such as
  42. * assigning to a function pointer.
  43. *
  44. * Users with the ability to should use `sol_incref` instead.
  45. */
  46. sol_object_t *sol_obj_acquire(sol_object_t *obj) {
  47. return sol_incref(obj);
  48. }
  49. #ifdef DEBUG_GC
  50. static FILE *gclog = NULL;
  51. static int gcrefcnt = 0;
  52. static char gctime[64];
  53. /*
  54. char *prtime() {
  55. time_t t;
  56. struct tm t2;
  57. time(&t);
  58. localtime_r(&t, &t2);
  59. strftime(gctime, 64, "%Y/%m/%d %T", &t2);
  60. return gctime;
  61. }
  62. */
  63. char *prtime() {return "";}
  64. void sol_mm_initialize(sol_state_t *state) {
  65. if(gclog) {
  66. fprintf(gclog, " === Reopened at %s ===\n", prtime());
  67. } else {
  68. gclog = fopen("gclog.txt", "a");
  69. fprintf(gclog, "=== Opened at %s ===\n", prtime());
  70. }
  71. gcrefcnt++;
  72. }
  73. void sol_mm_finalize(sol_state_t *state) {
  74. gcrefcnt--;
  75. fprintf(gclog, "=== Closed at %s ===\n", prtime());
  76. if(gcrefcnt <= 0) {
  77. fflush(gclog);
  78. fclose(gclog);
  79. gclog = NULL;
  80. }
  81. }
  82. sol_object_t *_int_sol_alloc_object(const char *func, sol_state_t *state) {
  83. fprintf(gclog, "%s\tA\n", func);
  84. return _sol_gc_alloc_object(state);
  85. }
  86. sol_object_t *_int_sol_incref(const char *func, sol_object_t *obj) {
  87. int oldref = obj->refcnt++;
  88. fprintf(gclog, "%s\tI\t%s\t%p\t%d\t->\t%d\n", func, obj->ops->tname, obj, oldref, obj->refcnt);
  89. return obj;
  90. }
  91. void _int_sol_obj_free(const char *func, sol_object_t *obj) {
  92. fprintf(gclog, "%s\tD\t%s\t%p\t%d\t->\t%d\n", func, obj->ops->tname, obj, obj->refcnt, obj->refcnt - 1);
  93. _sol_gc_obj_free(obj);
  94. }
  95. void sol_obj_release(sol_object_t *obj) {
  96. fprintf(gclog, "\tF\t%s\t%p\n", obj->ops->tname, obj);
  97. if(obj->ops->free) obj->ops->free(NULL, obj);
  98. free(obj);
  99. }
  100. sol_object_t *_sol_gc_dsl_copier(sol_object_t *obj) {
  101. fprintf(gclog, "<dsl>\tI\t%s\t%p\t%d\t->\t%d\n", obj->ops->tname, obj, obj->refcnt, ++obj->refcnt);
  102. return obj;
  103. }
  104. void _sol_gc_dsl_destructor(sol_object_t *obj) {
  105. fprintf(gclog, "<dsl>\tD\t%s\t%p\t%d\t->\t%d\n", obj->ops->tname, obj, obj->refcnt, obj->refcnt - 1);
  106. _sol_gc_obj_free(obj);
  107. }
  108. #else
  109. /** Allocates and returns a new reference to a typeless object.
  110. *
  111. * This function is intended to be called from object constructors that will
  112. * ultimately set the type and operations on the object.
  113. *
  114. * The returned reference is initially the only reference to this object.
  115. */
  116. sol_object_t *sol_alloc_object(sol_state_t *state) {
  117. return _sol_gc_alloc_object(state);
  118. }
  119. /** Frees a reference to an object.
  120. *
  121. * If the given reference is the last reference to this object, the memory is
  122. * freed, after any type-specific destructors are called.
  123. */
  124. void sol_obj_free(sol_object_t *obj) {
  125. _sol_gc_obj_free(obj);
  126. }
  127. /** Destroys an object.
  128. *
  129. * This function is called on an object whose last reference has been freed; it
  130. * is responsible for implementing the destructor protocol (and so calling the
  131. * necessary methods). In general, it should not be used by user code, as it
  132. * may introduce use-after-free.
  133. */
  134. void sol_obj_release(sol_object_t *obj) {
  135. if(obj->ops->free) {
  136. obj->ops->free(NULL, obj);
  137. }
  138. free(obj);
  139. }
  140. /** Initialize the memory manager for a state.
  141. *
  142. * You normally do not need to call this; it is also done in `sol_state_init`.
  143. */
  144. void sol_mm_initialize(sol_state_t *state) {}
  145. /** Finalize the memory manager for a state.
  146. *
  147. * You normally do not need to call this; it is also done in
  148. * `sol_state_cleanup`.
  149. */
  150. void sol_mm_finalize(sol_state_t *state) {}
  151. #endif