Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Last active December 26, 2023 02:36
Show Gist options
  • Save maxgoren/8f53f98ccad28a45174ca565044e18ac to your computer and use it in GitHub Desktop.
Save maxgoren/8f53f98ccad28a45174ca565044e18ac to your computer and use it in GitHub Desktop.
Mark/Sweep GC example with BST in C
/*
Example of how mark/sweep garbage collection works.
MIT License
Copyright (c) 2023 Max Goren - http://www.maxgcoding.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int value;
struct node* left;
struct node* right;
bool mark;
struct node* next;
};
typedef struct node Node;
struct root {
Node *start;
struct root* next;
};
typedef struct root Root;
struct alloc {
Node *nodes;
Root *roots;
};
typedef struct alloc Alloc;
Alloc *make_allocator(void);
Node *make_node(int value, Node *l, Node *r, Alloc *a);
Root *make_root(Node *n, Alloc *a);
void gc(Alloc *a);
Alloc *make_allocator(void) {
Alloc *a = malloc(sizeof(Alloc));
a->nodes = NULL;
a->roots = NULL;
return a;
}
Node *make_node(int value, Node* l, Node* r, Alloc *a) {
Node* n = malloc(sizeof(Node));
n->value = value;
n->left = l;
n->right = r;
n->mark = false;
n->next = a->nodes;
a->nodes = n;
return n;
}
Root *make_root(Node* n, Alloc *a) {
Root *g = malloc(sizeof(Root));
g->start = n;
g->next = a->roots;
a->roots = g;
return g;
}
void mark_node(Node *node) {
if (node != NULL && !node->mark) {
printf("Marking %d as Reached.\n", node->value);
node->mark = true;
mark_node(node->left);
mark_node(node->right);
}
}
void mark(Alloc* a) {
Root *g = a->roots;
while (g != NULL) {
mark_node(g->start);
g = g->next;
}
}
void sweep(Alloc *a) {
Node *n = a->nodes;
Node *live = NULL;
while (n != NULL) {
Node *tl = n->next;
if (n->mark == false) {
printf("%d is unreachable, cleaning up.\n", n->value);
free(n);
} else {
printf("%d is not garbage, adding to live list\n", n->value);
n->mark = false;
n->next = live;
live = n;
}
n = tl;
}
a->nodes = live;
}
void gc(Alloc *a) {
puts("GC Started.");
mark(a);
sweep(a);
puts("GC Finished.");
}
Node* put(Node* h, int val, Alloc* a) {
if (h == NULL)
return make_node(val, NULL, NULL, a);
if (val < h->value) h->left = put(h->left, val, a);
else h->right = put(h->right, val, a);
return h;
}
Node* erasemin(Node* h) {
if (h->left == NULL)
return h->right;
h->left = erasemin(h->left);
return h;
}
Node* min(Node* h) {
Node* x = h;
while (x->left) x = x->left;
return x;
}
Node* erase(Node *h, int key) {
if (h == NULL)
return h;
if (key < h->value)
h->left = erase(h->left, key);
else if (key > h->value)
h->right = erase(h->right, key);
else {
if (h->left == NULL)
return h->right;
if (h->right == NULL)
return h->left;
Node* t = h;
h = min(t->right);
h->right = erasemin(t->right);
h->left = t->left;
}
return h;
}
void show(Node* h) {
if (h != NULL) {
printf("%d ", h->value);
show(h->left);
show(h->right);
}
}
//Create random bst, delete 2 random values from it.
//run GC
//traverse tree post GC
void testGCbst() {
Node* head = NULL;
Alloc* a = make_allocator();
int n[10];
for (int i = 0; i < 10; i++) {
int p = rand() % 100;
head = put(head, p, a);
n[i] = p;
}
a->roots = make_root(head, a);
int te1 = 3;
int te2 = 7;
show(head); puts(" ");
printf("Erase: %d\n", n[te1]);
head = erase(head, n[te1]);
show(head); puts(" ");
printf("Erase: %d\n", n[te2]);
head = erase(head, n[te2]);
show(head);
puts(" ");
gc(a);
show(head);
}
int main() {
testGCbst();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment