Skip to content

Commit

Permalink
Add destroy at node, hash table
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 22, 2017
1 parent 086259b commit a870d60
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 7 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ add_compile_options(-std=c11)
include_directories(include)

file(GLOB sources "src/*.c")
list(REMOVE_ITEM sources "src/node.c" "src/hashtable.c" "src/thpool.c")
list(REMOVE_ITEM sources "${CMAKE_CURRENT_SOURCE_DIR}/src/node.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/hashtable.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/thpool.c")

add_library(node "src/node.c")
add_library(hashtable "src/hashtable.c")
Expand All @@ -13,12 +15,12 @@ add_library(thpool "src/thpool.c")

add_executable(main ${sources})
add_executable(node_test "test/node_test.c")
add_executable(hash_test "test/hash_test.c")
add_executable(hashtable_test "test/hash_test.c")
add_executable(thpool_rwlock_test "test/thpool_rwlock_test.c")

target_link_libraries(hashtable node)
target_link_libraries(thpool pthread)
target_link_libraries(main hashtable thpool)
target_link_libraries(node_test node)
target_link_libraries(hash_test hashtable)
target_link_libraries(hashtable_test hashtable)
target_link_libraries(thpool_rwlock_test thpool)
1 change: 1 addition & 0 deletions include/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ int ht_hash(hashtable_t *, unsigned int);
int ht_set(hashtable_t *, unsigned int, char *);
int ht_remove(hashtable_t *, unsigned int);
node_t *ht_get(hashtable_t *, unsigned int);
int ht_destroy(hashtable_t *);

#endif
1 change: 1 addition & 0 deletions include/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ node_t *ll_create();
int ll_insert(node_t *, unsigned int, char *);
int ll_remove(node_t *, unsigned int);
node_t *ll_get(node_t *, unsigned int);
int ll_destroy(node_t *);

#endif
15 changes: 14 additions & 1 deletion src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hashtable_t *ht_create(int size) {
return NULL;
}

if ((hashtable->table = malloc(sizeof(node_t *) * size)) == NULL) {
if ((hashtable->table = calloc(size, sizeof(node_t *))) == NULL) {
return NULL;
}

Expand Down Expand Up @@ -95,3 +95,16 @@ int ht_remove(hashtable_t *hashtable, unsigned int key) {

return 0;
}

int ht_destroy(hashtable_t *hashtable) {
if (hashtable == NULL)
return -1;

int i;

for (i = 0; i < hashtable->size; i++) {
ll_destroy(hashtable->table[i]);
}

return 0;
}
14 changes: 14 additions & 0 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ node_t *ll_get(node_t *head, unsigned int key) {

return iter;
}

int ll_destroy(node_t *head) {
if (head == NULL)
return -1;

node_t *iter = head;
while (iter != NULL) {
node_t *temp = iter;
iter = iter->next;
free(temp);
}

return 0;
}
2 changes: 2 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ void server() {
case TYPE_QUIT:
thpool_destroy(thpool);
pthread_rwlock_destroy(&rwlock);
ht_destroy(hashtable);

int_handler(2);
exit(0);
break;
Expand Down
2 changes: 2 additions & 0 deletions test/hash_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int main() {
printf("remove failed\n");
}
print_node(ht_get(hashtable, 2880));

ht_destroy(hashtable);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions test/node_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int main() {
if (temp == NULL) {
printf("ll_get is null\n");
}

ll_destroy(list);
return 0;
}

Expand Down
7 changes: 4 additions & 3 deletions test/thpool_rwlock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void read_task() {
pthread_rwlock_rdlock(&rwlock);
int i;
for (i = 0; i < 20; i++) {
printf("read_task_%d in %u\n", i, (int) pthread_self());
printf("read_task_%d in %u\n", i, (unsigned int) pthread_self());
}
pthread_rwlock_unlock(&rwlock);
}
Expand All @@ -21,7 +21,7 @@ void write_task() {
pthread_rwlock_wrlock(&rwlock);
int i;
for (i = 0; i < 20; i++) {
printf("write_task_%d in %u\n", i, (int) pthread_self());
printf("write_task_%d in %u\n", i, (unsigned int) pthread_self());
}
pthread_rwlock_unlock(&rwlock);
}
Expand All @@ -35,8 +35,9 @@ int main() {
for (i = 0; i < 4; i++) {
if (i == 2) {
thpool_add_work(thpool, (void *) write_task, NULL);
} else {
thpool_add_work(thpool, (void *) read_task, NULL);
}
thpool_add_work(thpool, (void *) read_task, NULL);
}

sleep(1);
Expand Down

0 comments on commit a870d60

Please sign in to comment.