Skip to content

Commit d15b58f

Browse files
committed
Add Hashbag, RCU Cache and README
1 parent 560392e commit d15b58f

File tree

10 files changed

+727
-36
lines changed

10 files changed

+727
-36
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ GTAGS
5959
TAGS
6060
test.bin
6161

62-
tmp_test.c
62+
tmp_test.c
63+
64+
*.bin
65+
tags
66+
tmp_test_dynarr.c

Makefile

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,106 @@ IDIR = .
22
CC = cc
33

44
ifdef MODERN_CC
5-
EXTRA_C_FLAGS = -g -O03 -std=c99 -pedantic -Wall
5+
EXTRA_C_FLAGS = -g -O03 -std=c99 -pedantic -Wall -Wno-unused-function
66
endif
77

88
ifdef CC_32_BIT
9-
EXTRA_C_FLAGS = -m32 -g -O03 -std=c99 -pedantic -Wall
9+
EXTRA_C_FLAGS = -m32 -g -O03 -std=c99 -pedantic -Wall -Wno-unused-function
1010
endif
1111

1212
ifdef ADD_SAN
1313
CC = clang
14-
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=address -fno-omit-frame-pointer
15-
USE_GC_STRING = -use_gc
14+
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=address -fno-omit-frame-pointer -Wno-unused-function
1615
endif
1716

1817
ifdef MEM_SAN
1918
CC = clang
20-
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=memory -fno-omit-frame-pointer
19+
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=memory -fno-omit-frame-pointer -Wno-unused-function
2120
endif
2221

2322
ifdef UB_SAN
2423
CC = clang
25-
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=undefined -fno-omit-frame-pointer
24+
EXTRA_C_FLAGS = -std=c99 -Wall -pedantic -g -O00 -fsanitize=undefined -fno-omit-frame-pointer -Wno-unused-function
2625
endif
2726

28-
CFLAGS = -g -O01 -I$(IDIR) $(EXTRA_C_FLAGS)
27+
CFLAGS = -g -O00 -I$(IDIR) $(EXTRA_C_FLAGS)
2928

3029
ODIR = .
3130
LDIR =
3231

33-
DEPS = dynarr.h test_framework.h
32+
_DEPS = dynarr.h test_framework.h hashbag.h lru.h list.h
3433
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
3534

36-
_OBJ = test.o
35+
_OBJ = test_dynarr.o
3736
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
3837
C_FILES = $(patsubst %.o,%.c,$(_OBJ))
3938

39+
_OBJ2 = test_hashbag.o
40+
OBJ2 = $(patsubst %,$(ODIR)/%,$(_OBJ2))
41+
C_FILES2 = $(patsubst %.o,%.c,$(_OBJ2))
42+
43+
_OBJ3 = test_lru.o
44+
OBJ3 = $(patsubst %,$(ODIR)/%,$(_OBJ3))
45+
C_FILES3 = $(patsubst %.o,%.c,$(_OBJ3))
46+
47+
ALL_CFILES = $(C_FILES) $(C_FILES2) $(C_FILES3)
48+
49+
main: test_dynarr.bin test_hashbag.bin test_lru.bin
50+
echo OK
51+
4052
$(ODIR)/%.o: %.c $(DEPS)
4153
$(CC) -c -o $@ $< $(CFLAGS)
4254

43-
test.bin: $(OBJ)
55+
test_dynarr.bin: $(OBJ)
4456
$(CC) -o $@ $^ $(CFLAGS)
4557

58+
test_hashbag.bin: $(OBJ2)
59+
$(CC) -o $@ $^ $(CFLAGS)
60+
61+
test_lru.bin: $(OBJ3)
62+
$(CC) -o $@ $^ $(CFLAGS)
4663

4764
.PHONY: clean test run_test_continusly CMakeLists.txt cmake_compile clang_format test_add_san test_ub_san test_mem_san test_sanitizers test_modern_cc test_valgrind test_macro_expanded.bin
4865

49-
test_macro_expanded.bin:
50-
gcc -E -C -P test.c > tmp_tmp_test.c
66+
test_macro_expanded_gen:
67+
gcc -E -C -P $(TESTPROG) > tmp_tmp_test.c
5168
clang-format tmp_tmp_test.c > tmp_test.c
5269
rm tmp_tmp_test.c
53-
gcc -g -O01 tmp_test.c
70+
gcc -Wall -Wno-unused-function -g -O01 tmp_test.c
5471
valgrind --leak-check=full ./a.out
5572

56-
test: test.bin
57-
./test.bin ;\
73+
test_macro_expanded_dynarr:
74+
TESTPROG=test_dynarr.c make test_macro_expanded_gen
75+
76+
test_macro_expanded_hashbag:
77+
TESTPROG=test_hashbag.c make test_macro_expanded_gen
78+
79+
test_macro_expanded_lru:
80+
TESTPROG=test_lru.c make test_macro_expanded_gen
81+
82+
test: test_dynarr.bin test_hashbag.bin test_lru.bin
83+
./test_dynarr.bin && ./test_hashbag.bin && ./test_lru.bin;\
5884
RESULT=$$? &&\
5985
(exit $$RESULT) &&\
6086
printf "\n\n\033[0;32mALL TESTS PASSED!\033[0m\n\n\n" ||\
6187
printf "\n\n\033[0;31mTEST FAILED!\033[0m\n\n\n" &&\
6288
exit $$RESULT
6389

64-
test_valgrind:
90+
test_valgrind_gen:
6591
make clean && \
6692
make EXTRA_C_FLAGS="-g -O01" && \
67-
valgrind --undef-value-errors=no ./test.bin ;\
93+
valgrind --undef-value-errors=no --leak-check=full $(BIN) ;\
6894
RESULT=$$? &&\
6995
(exit $$RESULT) &&\
7096
printf "\n\n\033[0;32mALL TESTS PASSED!\033[0m\n\n\n" ||\
7197
printf "\n\n\033[0;31mTEST FAILED!\033[0m\n\n\n" &&\
7298
exit $$RESULT
7399

100+
test_valgrind:
101+
BIN=./test_dynarr.bin make test_valgrind_gen
102+
BIN=./test_hashbag.bin make test_valgrind_gen
103+
BIN=./test_lru.bin make test_valgrind_gen
104+
74105
test_add_san:
75106
make clean && \
76107
make ADD_SAN=1 test
@@ -100,18 +131,24 @@ test_32_bit:
100131
test_all:
101132
make test_valgrind && \
102133
make test_sanitizers && \
103-
make test_modern_cc && \
104-
make test_32_bit
134+
make test_modern_cc
135+
# make test_32_bit
105136

106137
run_test_continusly:
107138
inotifywait -e close_write,moved_to,create -m ./*.c ./*.h | while read -r directory events filename; do gtags ; make test ; done
108139

109-
CMakeLists.txt: $(C_FILES)
140+
CMakeLists.txt: $(C_FILES) $(C_FILES2) $(C_FILES3)
110141
echo "cmake_minimum_required (VERSION 2.6)" > CMakeLists.txt
111-
echo "project (SIMPLE_C_GC)" >> CMakeLists.txt
112-
echo "add_executable(cmake.out" >> CMakeLists.txt
142+
echo "project (DS)" >> CMakeLists.txt
143+
echo "add_executable(dynarr.out" >> CMakeLists.txt
113144
echo $(C_FILES) >> CMakeLists.txt
114145
echo ")" >> CMakeLists.txt
146+
echo "add_executable(hashbag.out" >> CMakeLists.txt
147+
echo $(C_FILES2) >> CMakeLists.txt
148+
echo ")" >> CMakeLists.txt
149+
echo "add_executable(lru.out" >> CMakeLists.txt
150+
echo $(C_FILES3> CMakeLists.txt
151+
echo ")" >> CMakeLists.txt
115152

116153

117154
cmake_compile: CMakeLists.txt
@@ -125,4 +162,4 @@ clang_format:
125162
clang-format -style="{BasedOnStyle: LLVM}" -i *.c *.h
126163

127164
clean:
128-
rm -f $(ODIR)/*.o *~ core $(IDIR)/*~ test.bin CMakeLists.txt
165+
rm -f $(ODIR)/*.o *~ core $(IDIR)/*~ test_dynarr.bin CMakeLists.txt

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,55 @@
1-
# c_reusable_data_structures
2-
Some resusable data structures implemented in C
1+
# C Reusable Data Structures
2+
3+
This repository contains reusable generic data structures for C.
4+
The data structures are generated by macros that generate structs
5+
and functions for a specific type. For example, one can generate
6+
one dynamic array (`dynarr.h`) holding objects of type int and another
7+
for holding objects of type double or any other type you like.
8+
9+
## Currently Available Data Structures
10+
11+
The following data structures are currently available in the
12+
library.
13+
14+
### Dynamic Array (`dynarr.h`)
15+
16+
See `test_dynarr.c` for how to use the dynamic arrays. Notice that the test
17+
cases do not cover all available functions yet.
18+
19+
20+
### Hash Bag (`hashbag.h`)
21+
22+
See `test_harhbag.c` for how to use hash bags. The same data structure can be
23+
used for hash maps and hash sets with some creativity.
24+
25+
26+
### LRU Cache (`hashbag.h`)
27+
28+
See `test_lru.c` for how to use lru caches.
29+
30+
31+
# Testing
32+
33+
make test
34+
35+
To run valgrind tests etc:
36+
37+
make test_all
38+
39+
# Contribute
40+
41+
Send pull requests to:
42+
43+
https://github.com/kjellwinblad/c_reusable_data_structures
44+
45+
46+
# License
47+
48+
Apache License 2.0
49+
50+
See license file for detail.
51+
52+
# Contact
53+
54+
The original author (Kjell Winblad, https://dupwin.se) can be contacted at
55+

0 commit comments

Comments
 (0)