orangeduck / tgc

A Tiny Garbage Collector for C
Other
968 stars 64 forks source link

Sweep not work correctly on ESP8266 #23

Open elct9620 opened 2 years ago

elct9620 commented 2 years ago

Thanks for this tiny GC implementation that I can use as an example in my book about implementing a mruby virtual machine.

For the demo of my example, I run it on my macOS and ESP8266 chip. But the tgc_sweep never runs because the gc->nfrees always be 0 in ESP8266.

The patch has removed this line to force it run sweep event nothing to be free. https://github.com/orangeduck/tgc/blob/35207051557c79ea25942c021fb18856c72af8e3/tgc.c#L249

Does anyone have any idea about the root cause to let the GC never work?

orangeduck commented 2 years ago

If the number of frees was zero it means it probably didn't mark anything during the marking phase. Perhaps for some reason it is not finding any pointers on the stack? This is hard to debug without knowing more of the code.

elct9620 commented 2 years ago

Thanks for your reply.

Currently, it marks correctly in my macOS but does not work when I upload it to the ESP8266 chip.

I am not sure the setjmp does have any relation to this issue.

The implementation which I used. The tgc_stop isn't free any memory after the program stopped.

// Enum
enum mrb_type {
  MRB_TYPE_OBJECT,
}

// Structures
typedef struct mrb_state {
  // ...
  tgc_t gc;
} mrb_state;

typedef struct mb_value {
  union {
    int i;
    void* p;
  } value;
  mrb_type type;
} mrb_value;

typedef struct RObject {
  // ...
  kh_iv iv;
} RObject;
// Functions

mrb_state* mrb_open() {
  mrb_value mrb;
  // ...

  void* stack;
  tgc_start(&mrb->gc, &stack);
  return mrb;
}

void mrb_close(mrb_state* mrb) {
  // ...
  tgc_stop(&mrb->gc);
  free(mrb);
}

void mrb_new_object(mrb_state* mrb) {
  mrb_value value;
  RObject* object = (RObject*)tgc_alloc_opt(&mrb->gc, sizeof(RObject), 0, mrb_free_object);
  kh_init(iv, object->iv);
  // ...
  value.type = MRB_TYPE_OBJECT;
  value.value.p = (void*)object;

  return value;
}
// Usage

int main(int argc, char** argv) {
  mrb_state* mrb = mrb_open();
  // Inside VM - Begin
  mrb_value regs[3];
  while(true) {
      // ...
      case(OP_SEND) {
        regs[0] = mrb_new_object(mrb);
        continue;
      }
      // ...
  }
  // Inside VM - End
  mrb_close(mrb);
}
orangeduck commented 2 years ago

To me it looks like maybe you need to call tgc_start(&mrb->gc, &stack); and tgc_stop(&mrb->gc); in the main function otherwise it might not be starting marking the stack from deep enough.

elct9620 commented 2 years ago

I have tried to call tgc_start(&mrb->gc, &stack) and tgc_stop(&mrb->gc) in the main function but not work, too.

Does any way to change the stack deep threshold to make it start marking early? The ESP8266 design may be different from the PC cause the stack is not deep enough.

orangeduck commented 2 years ago

You can try this tip: https://github.com/orangeduck/tgc#tgc-isnt-working-when-optimisations-are-enabled

elct9620 commented 2 years ago

Thanks, I will try it and report the result.