Skip to content

Commit

Permalink
gc_free
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Jan 9, 2018
1 parent 242a673 commit f1c509a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
16 changes: 15 additions & 1 deletion gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define HASH(ptr) ((uintptr_t)ptr >> 3)
#define SET_BIT(l, i, v) (l[i/8] |= 1 << (i % 8))
#define GET_BIT(l, i) ((l[i/8] >> (i % 8)) & 1)
#define SWAP(type, a,b) { type tmp; tmp = a; a = b; b = tmp; }
#define SWAP(type, a, b) { type tmp; tmp = a; a = b; b = tmp; }

t_gc GC_G = (t_gc) { .ref_count = 0 };

Expand Down Expand Up @@ -40,6 +40,19 @@ void *gc_alloc(size_t size)
return (ptr);
}

void gc_free(void *ptr)
{
t_gc_list *lst;

lst = GC_G.pointer_map[HASH(ptr) % P_MAP_SIZE];
if (lst && gc_list_exist(lst, (uintptr_t)lst))
{
gc_list_rm(&lst, (uintptr_t)lst);
GC_G.pointer_nb--;
free(ptr);
}
}

size_t gc_ptr_index(uintptr_t ptr, t_gc_list **e)
{
int i;
Expand Down Expand Up @@ -106,6 +119,7 @@ void gc_sweep(uint8_t *mark_bits)
free((void *)e->data.start);
e = e->next;
gc_list_rm(&GC_G.pointer_map[i], k);
GC_G.pointer_nb--;
}
else
e = e->next;
Expand Down
2 changes: 2 additions & 0 deletions gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ size_t gc_ptr_index(uintptr_t ptr, t_gc_list **e);
void gc_mark_stack(uint8_t *mark_bits);
void gc_run();
void gc_sweep(uint8_t *mark_bits);
void gc_free(void *ptr);


void gc_list_push(t_gc_list **begin_list, t_gc_ptr data);
void gc_list_rm(t_gc_list **begin_list, size_t index);
int gc_list_exist(t_gc_list *begin_list, uintptr_t ptr);


#endif
11 changes: 11 additions & 0 deletions gc_lst.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#include "gc.h"

int gc_list_exist(t_gc_list *begin_list, uintptr_t ptr)
{
while (begin_list)
{
if (begin_list->data.start == ptr)
return (1);
begin_list = begin_list->next;
}
return (0);
}

void gc_list_push(t_gc_list **begin_list, t_gc_ptr data)
{
t_gc_list *elem;
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
int lol() {
void** hello = gc_alloc(10);
*hello = gc_alloc(11);

gc_free(*hello);
}

int main(int argc, char *argv[])
Expand Down

0 comments on commit f1c509a

Please sign in to comment.