Skip to content

Commit

Permalink
Remove gc4c because it has some bugs. Using 'Boehm GC' for Garbage co…
Browse files Browse the repository at this point in the history
…llect.
  • Loading branch information
hhuang22 committed Sep 2, 2019
1 parent 344d1b8 commit 504031a
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 1,165 deletions.
28 changes: 7 additions & 21 deletions ArrayList.c
@@ -1,6 +1,6 @@
#include <string.h>
#include "ArrayList.h"
#include "malloc.h"
#include <gc.h>

static inline void PTR_SWAP(void **a, void **b) {
void *t = *a;
Expand All @@ -9,14 +9,14 @@ static inline void PTR_SWAP(void **a, void **b) {
}

ArrayList arrlist_new() {
struct ArrayList *a = gc_malloc(sizeof(struct ArrayList));
struct ArrayList *a = GC_malloc(sizeof(struct ArrayList));
if (a == NULL) {
return NULL;
}

a->data = gc_malloc(sizeof(void *) * ARRAYLIST_INIT_CAPACITY);
a->data = GC_malloc(sizeof(void *) * ARRAYLIST_INIT_CAPACITY);
if (a->data == NULL) {
gc_free(a);
GC_free(a);
return NULL;
}

Expand All @@ -31,26 +31,12 @@ void arrlist_setFreeFunc(ArrayList a, void (*freeItemFunc)(void *)) {
}

static int arrlist_extend(ArrayList a, int newSize) {
#if 0
/* Below will core dump, gc4c's bug??? */
void **tmp = gc_realloc(a->data, sizeof(void *) * newSize);
void **tmp = GC_realloc(a->data, sizeof(void *) * newSize);
if (tmp == NULL) {
return -1;
}

a->data = tmp;
#endif
void **tmp = gc_malloc(sizeof(void *) * newSize);
if (tmp == NULL) {
return -1;
}

for (int i = 0; i < a->length; i++) {
tmp[i] = a->data[i];
}

gc_free(a->data);
a->data = tmp;

return 0;
}
Expand Down Expand Up @@ -148,8 +134,8 @@ int arrlist_destroy(ArrayList a) {
}
}

gc_free(a->data);
gc_free(a);
GC_free(a->data);
GC_free(a);

return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions Makefile
@@ -1,19 +1,19 @@
cc=/usr/local/bin/gcc

SRCS=ArrayList.c linq.c hashmap.c main.c ./gc4c/dataStructure.c ./gc4c/linkedList.c ./gc4c/malloc.c ./gc4c/mark_and_sweep.c
MACRO_SRCS=ArrayList.c linq.c hashmap.c main_macro.c ./gc4c/dataStructure.c ./gc4c/linkedList.c ./gc4c/malloc.c ./gc4c/mark_and_sweep.c
SRCS=ArrayList.c linq.c hashmap.c main.c
MACRO_SRCS=ArrayList.c linq.c hashmap.c main_macro.c

DEBUG_FLAG=-g
INCLUDE=-I. -I./gc4c
INCLUDE=-I.
CFLAGS=-D_GNU_SOURCE $(DEBUG_FLAG) $(INCLUDE)
LD_FLAGS=-lpthread

TARGET_LINQ=linq
TARGET_LINQ_MACRO=linq_macro

all:
$(cc) $(CFLAGS) $(SRCS) $(LD_FLAGS) -o $(TARGET_LINQ)
$(cc) $(CFLAGS) $(MACRO_SRCS) $(LD_FLAGS) -o $(TARGET_LINQ_MACRO)
$(cc) $(CFLAGS) $(SRCS) $(LD_FLAGS) -o $(TARGET_LINQ) -lgc
$(cc) $(CFLAGS) $(MACRO_SRCS) $(LD_FLAGS) -o $(TARGET_LINQ_MACRO) -lgc

clean:
rm -f $(TARGET_LINQ) $(TARGET_LINQ_MACRO)
24 changes: 13 additions & 11 deletions README.md
Expand Up @@ -14,9 +14,9 @@
### Example using callbacks

```c
#include <gc.h>
#include "ArrayList.h"
#include "linq.h"
#include "malloc.h"

bool WhereCallback(void *item) {
char *str= (char *)item;
Expand All @@ -40,7 +40,7 @@ ArrayList testLinq(ArrayList array) {
}

int main() {
gc_init();
GC_INIT();

char *str1 = "huang", *str2 = "hai", *str3 = "feng";

Expand All @@ -54,7 +54,6 @@ int main() {
printf("%s\n", arrlist_get(result, i));
}

gc_destroy();
return 0;
}
```
Expand Down Expand Up @@ -82,7 +81,7 @@ ArrayList testLinq(ArrayList array) {
}
int main() {
gc_init();
GC_INIT();
char *str1 = "huang", *str2 = "hai", *str3 = "feng";
Expand All @@ -96,7 +95,6 @@ int main() {
printf("%s\n", arrlist_get(result, i));
}
gc_destroy();
return 0;
}
```
Expand Down Expand Up @@ -128,7 +126,7 @@ ArrayList testLinq(ArrayList array) {
}

int main(int argc, char **argv) {
gc_init();
GC_INIT();

char *str1 = "huang", *str2 = "hai", *str3 = "feng";

Expand All @@ -142,11 +140,19 @@ int main(int argc, char **argv) {
printf("%s\n", arrlist_get(result, i));
}

gc_destroy();
return 0;
}
```
## Requirement
As for the garbage collect, you need to install `Boehm GC`.
```sh
# yum install gc-devel # RedHat family
# apt-get install libgc-dev # Debian family
```

## What's implemented

-----
Expand Down Expand Up @@ -216,10 +222,6 @@ int main(int argc, char **argv) {
- [x] Print
- [x] Println

## Credits
- faisalabujabal:
For his wonderful Garbage collector for C [gc4c](https://github.com/faisalabujabal/gc4c).

## License

Expand Down

0 comments on commit 504031a

Please sign in to comment.