Skip to content

Commit

Permalink
Merge pull request #6238 from dhalbert/ringbuf-free-fix
Browse files Browse the repository at this point in the history
Free ringbuf buffer by relying on gc, not gc_free()
  • Loading branch information
jepler authored Apr 5, 2022
2 parents 8cd09b1 + 70add52 commit e0827eb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
7 changes: 2 additions & 5 deletions py/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "ringbuf.h"

bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity) {
r->heap = false;
r->buf = buf;
r->size = capacity;
r->iget = r->iput = 0;
Expand All @@ -40,17 +39,15 @@ bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity) {
// size of the buffer is one greater than that, due to how the buffer
// handles empty and full statuses.
bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived) {
r->heap = true;
r->buf = gc_alloc(capacity + 1, false, long_lived);
r->size = capacity + 1;
r->iget = r->iput = 0;
return r->buf != NULL;
}

void ringbuf_free(ringbuf_t *r) {
if (r->heap) {
gc_free(r->buf);
}
// Free buf by letting gc take care of it. If the VM has finished already,
// this will be safe.
r->buf = (uint8_t *)NULL;
r->size = 0;
ringbuf_clear(r);
Expand Down
1 change: 0 additions & 1 deletion py/ringbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ typedef struct _ringbuf_t {
uint32_t size;
uint32_t iget;
uint32_t iput;
bool heap;
} ringbuf_t;

// Note that the capacity of the buffer is N-1!
Expand Down

0 comments on commit e0827eb

Please sign in to comment.