memory.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "memory.h"
  2. #include "vm.h"
  3. #ifdef DEBUG_LOG_GC
  4. #include "debug.h"
  5. #include <stdlib.h>
  6. #endif
  7. #define GC_HEAP_GROW_FACTOR 2
  8. static void markArray(ValueArray *);
  9. // oldSize newSize Operation
  10. // 0 Non‑zero Allocate new block.
  11. // Non‑zero 0 Free allocation.
  12. // Non‑zero Smaller than oldSize Shrink existing allocation.
  13. // Non‑zero Larger than oldSize Grow existing allocation.
  14. void *reallocate(void *pointer, size_t oldSize, size_t newSize) {
  15. vm.bytesAllocated += newSize - oldSize;
  16. if (newSize > oldSize) {
  17. #ifdef DEBUG_STRESS_GC
  18. collectGarbage();
  19. #endif
  20. if (vm.bytesAllocated > vm.nextGC) {
  21. collectGarbage();
  22. }
  23. }
  24. if (newSize == 0) {
  25. free(pointer);
  26. return NULL;
  27. }
  28. /*
  29. The realloc() function tries to change the size of the allocation pointed
  30. to by ptr to size, and returns ptr. If there is not enough room to
  31. enlarge the memory allocation pointed to by ptr, realloc() creates a new
  32. allocation, copies as much of the old data pointed to by ptr as will fit
  33. to the new allocation, frees the old allocation, and returns a pointer to
  34. the allocated memory. If ptr is NULL, realloc() is identical to a call
  35. to malloc() for size bytes. If size is zero and ptr is not NULL, a new,
  36. minimum sized object is allocated and the original object is freed. When
  37. extending a region allocated with calloc(3), realloc(3) does not guaran-
  38. tee that the additional memory is also zero-filled
  39. */
  40. void *result = realloc(pointer, newSize);
  41. if (result == NULL) exit(1);
  42. return result;
  43. }
  44. static void freeObject(Obj *object) {
  45. #ifdef DEBUG_LOG_GC
  46. PRINTLNF("%p free type %d\n", (void *) object, object->type);
  47. #endif
  48. switch (object->type) {
  49. case OBJ_NATIVE:
  50. FREE(ObjNative, object);
  51. break;
  52. case OBJ_FUNCTION: {
  53. ObjFunction *function = (ObjFunction *) object;
  54. freeChunk(&function->chunk);
  55. FREE(ObjFunction, object);
  56. break;
  57. }
  58. case OBJ_STRING:
  59. FREE_ARRAY(char, ((ObjString *) object)->chars, ((ObjString *) object)->length + 1);
  60. FREE(ObjString, object);
  61. break;
  62. case OBJ_CLOSURE: {
  63. ObjClosure *closure = (ObjClosure *) object;
  64. FREE_ARRAY(ObjUpvalue *, closure->upvalues, closure->upvalueCount);
  65. FREE(ObjClosure, object);
  66. break;
  67. }
  68. case OBJ_UPVALUE:
  69. FREE(ObjUpvalue, object);
  70. break;
  71. case OBJ_CLASS: {
  72. ObjClass *klass = (ObjClass *) object;
  73. freeTable(&klass->methods);
  74. FREE(ObjClass, object);
  75. break;
  76. }
  77. case OBJ_INSTANCE: {
  78. ObjInstance *instance = (ObjInstance *) object;
  79. freeTable(&instance->fields);
  80. FREE(ObjInstance, object);
  81. break;
  82. }
  83. case OBJ_BOUND_METHOD:
  84. FREE(ObjBoundMethod, object);
  85. break;
  86. }
  87. }
  88. void freeObjects() {
  89. Obj *object = vm.objects;
  90. while (object != NULL) {
  91. Obj *next = object->next;
  92. freeObject(object);
  93. object = next;
  94. }
  95. free(vm.grayStack);
  96. }
  97. static void markRoots() {
  98. for (Value *slot = vm.stack; slot < vm.stackTop; slot++) {
  99. markValue(*slot);
  100. }
  101. for (int i = 0; i < vm.frameCount; i++) {
  102. markObject((Obj *) vm.frames[i].closure);
  103. }
  104. for (ObjUpvalue *upvalue = vm.openUpvalues;
  105. upvalue != NULL; upvalue = upvalue->next) {
  106. markObject((Obj *) upvalue);
  107. }
  108. markTable(&vm.globals);
  109. markCompilerRoots();
  110. }
  111. void blackenObject(Obj *object) {
  112. #ifdef DEBUG_LOG_GC
  113. printf("%p blacken ", (void *) object);
  114. printValue(OBJ_VAL(object));
  115. printf("\n");
  116. #endif
  117. switch (object->type) {
  118. case OBJ_INSTANCE: {
  119. ObjInstance *instance = (ObjInstance *) object;
  120. markObject((Obj *) instance->klass);
  121. markTable(&instance->fields);
  122. break;
  123. }
  124. case OBJ_CLASS: {
  125. ObjClass *klass = (ObjClass *) object;
  126. markObject((Obj *) klass->name);
  127. markTable(&klass->methods);
  128. break;
  129. }
  130. case OBJ_CLOSURE: {
  131. ObjClosure *closure = (ObjClosure *) object;
  132. markObject((Obj *) closure->function);
  133. for (int i = 0; i < closure->upvalueCount; i++) {
  134. markObject((Obj *) closure->upvalues[i]);
  135. }
  136. break;
  137. }
  138. case OBJ_FUNCTION: {
  139. ObjFunction *function = (ObjFunction *) object;
  140. markObject((Obj *) function->name);
  141. markArray(&function->chunk.constants);
  142. break;
  143. }
  144. case OBJ_UPVALUE:
  145. markValue(((ObjUpvalue *) object)->closed);
  146. break;
  147. case OBJ_NATIVE:
  148. case OBJ_STRING:
  149. break;
  150. case OBJ_BOUND_METHOD: {
  151. ObjBoundMethod *bound = (ObjBoundMethod *) object;
  152. markValue(bound->receiver);
  153. markObject((Obj *) bound->method);
  154. break;
  155. }
  156. }
  157. }
  158. void traceReferences() {
  159. while (vm.grayCount > 0) {
  160. Obj *object = vm.grayStack[--vm.grayCount];
  161. blackenObject(object);
  162. }
  163. }
  164. void sweep() {
  165. Obj *previous = NULL;
  166. Obj *object = vm.objects;
  167. while (object != NULL) {
  168. if (object->isMarked) {
  169. object->isMarked = false;
  170. previous = object;
  171. object = object->next;
  172. } else {
  173. Obj *unreached = object;
  174. object = object->next;
  175. if (previous != NULL) {
  176. previous->next = object;
  177. } else {
  178. vm.objects = object;
  179. }
  180. freeObject(unreached);
  181. }
  182. }
  183. }
  184. void collectGarbage() {
  185. #ifdef DEBUG_LOG_GC
  186. printf("-- gc begin\n");
  187. size_t before = vm.bytesAllocated;
  188. #endif
  189. markRoots();
  190. traceReferences();
  191. tableRemoveWhite(&vm.strings);
  192. sweep();// sweeping unused objects
  193. vm.nextGC = vm.bytesAllocated * GC_HEAP_GROW_FACTOR;
  194. #ifdef DEBUG_LOG_GC
  195. printf("-- gc end\n");
  196. printf(" collected %zu bytes (from %zu to %zu) next at %zu\n",
  197. before - vm.bytesAllocated, before, vm.bytesAllocated, vm.nextGC);
  198. #endif
  199. }
  200. void markValue(Value value) {
  201. if (IS_OBJ(value)) markObject(AS_OBJ(value));
  202. }
  203. static void markArray(ValueArray *array) {
  204. for (int i = 0; i < array->count; i++) {
  205. markValue(array->values[i]);
  206. }
  207. }
  208. void markObject(Obj *object) {
  209. if (object == NULL) return;
  210. if (object->isMarked) return;
  211. #ifdef DEBUG_LOG_GC
  212. printf("%p mark ", (void *) object);
  213. printValue(OBJ_VAL(object));
  214. printf("\n");
  215. #endif
  216. object->isMarked = true;
  217. if (vm.grayCapacity < vm.grayCount + 1) {
  218. vm.grayCapacity = GROW_CAPACITY(vm.grayCapacity);
  219. vm.grayStack = (Obj **) realloc(vm.grayStack, sizeof(Obj *) * vm.grayCapacity);
  220. if (vm.grayStack == NULL) exit(1);
  221. }
  222. vm.grayStack[vm.grayCount++] = object;
  223. }