Skip to content

Commit

Permalink
made data structures immune to call term/destroy twice
Browse files Browse the repository at this point in the history
  • Loading branch information
tezc committed Apr 13, 2021
1 parent 3d11311 commit 57e67dc
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 124 deletions.
8 changes: 4 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
Expand Down Expand Up @@ -100,7 +100,7 @@ PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortIncludes: CaseSensitive
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: true
Expand Down
5 changes: 5 additions & 0 deletions array/array_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ static void test1(void)
{
int *arr, total = 0;

sc_array_create(arr, 10);
assert(arr != NULL);
sc_array_destroy(arr);
assert(arr == NULL);

sc_array_create(arr, 5);
sc_array_add(arr, 3);
sc_array_add(arr, 4);
Expand Down
18 changes: 18 additions & 0 deletions buffer/buf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ void test1()
const char *xstr;
struct sc_buf buf, buf2;

sc_buf_init(&buf, 100);
assert(buf.mem != NULL);
sc_buf_term(&buf);

sc_buf_init(&buf2, 0);
assert(buf.mem == buf2.mem);
assert(buf.limit == buf2.limit);
assert(buf.wpos == buf2.wpos);
assert(buf.rpos == buf2.rpos);
assert(buf.ref == buf2.ref);
assert(buf.cap == buf2.cap);
assert(buf.err == buf2.err);
sc_buf_term(&buf2);

sc_buf_put_64(&buf, 100);
assert(sc_buf_get_64(&buf) == 100);
sc_buf_term(&buf);

sc_buf_init(&buf, 100);
sc_buf_set_rpos(&buf, 1);
assert(sc_buf_valid(&buf) == false);
Expand Down
2 changes: 2 additions & 0 deletions buffer/sc_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ void sc_buf_term(struct sc_buf *b)
if (!b->ref) {
sc_buf_free(b->mem);
}

sc_buf_init(b, 0);
}

void sc_buf_limit(struct sc_buf *b, uint32_t limit)
Expand Down
7 changes: 4 additions & 3 deletions condition/cond_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,17 @@ int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg)

int sc_thread_join(struct sc_thread *thread, void **ret)
{
int rc;
void *val;
int rc = 0;
void *val = NULL;

if (thread->id == 0) {
return -1;
goto out;
}

rc = pthread_join(thread->id, &val);
thread->id = 0;

out:
if (ret != NULL) {
*ret = val;
}
Expand Down
18 changes: 16 additions & 2 deletions heap/heap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

int example(void)
{
Expand Down Expand Up @@ -54,6 +55,19 @@ void test1(void)
void *data;
struct sc_heap heap;

sc_heap_init(&heap, 0);
sc_heap_add(&heap, 100, "test");
sc_heap_pop(&heap, &key, &data);
assert(key == 100);
assert(strcmp("test", data) == 0);
sc_heap_term(&heap);

sc_heap_add(&heap, 100, "test");
sc_heap_pop(&heap, &key, &data);
assert(key == 100);
assert(strcmp("test", data) == 0);
sc_heap_term(&heap);

assert(sc_heap_init(&heap, SIZE_MAX / 2) == false);
assert(sc_heap_init(&heap, 3) == true);

Expand All @@ -67,7 +81,7 @@ void test1(void)

for (int i = 0; i < 10; i++) {
assert(sc_heap_add(&heap, arr[i],
(void *) (uintptr_t)(arr[i] * 2)) == true);
(void *) (uintptr_t) (arr[i] * 2)) == true);
}

for (int i = 0; i < 10; i++) {
Expand Down Expand Up @@ -97,7 +111,7 @@ void test2(void)

for (int i = 0; i < 10; i++) {
assert(sc_heap_add(&heap, -arr[i],
(void *) (uintptr_t)(arr[i] * 2)) == true);
(void *) (uintptr_t) (arr[i] * 2)) == true);
}

for (int i = 0; i < 10; i++) {
Expand Down
4 changes: 4 additions & 0 deletions heap/sc_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ bool sc_heap_init(struct sc_heap *h, size_t cap)
void sc_heap_term(struct sc_heap *h)
{
sc_heap_free(h->elems);

*h = (struct sc_heap){
.elems = NULL,
};
}

size_t sc_heap_size(struct sc_heap *h)
Expand Down
1 change: 0 additions & 1 deletion ini/sc_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "sc_ini.h"

#include <ctype.h>
#include <stdint.h>
#include <string.h>

#if defined(_WIN32) || defined(_WIN64)
Expand Down
5 changes: 4 additions & 1 deletion logger/sc_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ int sc_log_term(void)
if (rc != 0) {
rc = -1;
}
sc_log_mutex_term(&sc_log.mtx);
}

sc_log_mutex_term(&sc_log.mtx);
sc_log = (struct sc_log){
.fp = NULL,
};

return rc;
}
Expand Down
11 changes: 11 additions & 0 deletions map/map_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ void test1()
values[i] = str_random((rand() % 64) + 32);
}

sc_map_init_str(&map, 0, 0);
sc_map_put_str(&map, "100", "200");
sc_map_get_str(&map, "100", &value);
assert(strcmp(value, "200") == 0);
sc_map_term_str(&map);
sc_map_put_str(&map, "100", "200");
sc_map_get_str(&map, "100", &value);
assert(strcmp(value, "200") == 0);
sc_map_term_str(&map);


assert(!sc_map_init_str(&map, 0, -1));
assert(!sc_map_init_str(&map, 0, 24));
assert(!sc_map_init_str(&map, 0, 96));
Expand Down
7 changes: 4 additions & 3 deletions map/sc_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
m->used = false; \
m->cap = cap; \
m->load_fac = f; \
m->remap = (uint32_t)(m->cap * ((double) m->load_fac / 100)); \
m->remap = (uint32_t) (m->cap * ((double) m->load_fac / 100)); \
\
return true; \
} \
Expand All @@ -140,6 +140,7 @@
{ \
if (m->mem != sc_map_empty_##name.mem) { \
sc_map_free(&m->mem[-1]); \
*m = sc_map_empty_##name; \
} \
} \
\
Expand Down Expand Up @@ -199,7 +200,7 @@
\
m->mem = new; \
m->cap = cap; \
m->remap = (uint32_t)(m->cap * ((double) m->load_fac / 100)); \
m->remap = (uint32_t) (m->cap * ((double) m->load_fac / 100)); \
\
return true; \
} \
Expand Down Expand Up @@ -330,7 +331,7 @@ static uint32_t sc_map_hash_32(uint32_t a)

static uint32_t sc_map_hash_64(uint64_t a)
{
return ((uint32_t) a) ^ (uint32_t)(a >> 32u);
return ((uint32_t) a) ^ (uint32_t) (a >> 32u);
}

// clang-format off
Expand Down
2 changes: 2 additions & 0 deletions memory-map/mmap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void test1()
assert(rc == 0);
rc = sc_mmap_term(&mmap);
assert(rc == 0);
rc = sc_mmap_term(&mmap);
assert(rc == 0);

rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC,
PROT_READ | PROT_WRITE, MAP_SHARED, 0, 8192);
Expand Down
10 changes: 10 additions & 0 deletions memory-map/sc_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,23 @@ int sc_mmap_term(struct sc_mmap *m)
{
int rc;

if (m->fd == -1) {
return 0;
}

close(m->fd);

rc = munmap(m->ptr, m->len);
if (rc != 0) {
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
}

*m = (struct sc_mmap) {
.ptr = NULL,
.fd = -1,
.len = 0,
};

return rc;
}

Expand Down
17 changes: 9 additions & 8 deletions string/sc_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@ char *sc_str_create_fmt(const char *fmt, ...)
return str;
}

void sc_str_destroy(char *str)
void sc_str_destroy(char **str)
{
if (str == NULL) {
if (str == NULL || *str == NULL) {
return;
}

sc_str_free(sc_str_meta(str));
sc_str_free(sc_str_meta(*str));
*str = NULL;
}

int64_t sc_str_len(const char *str)
Expand Down Expand Up @@ -174,7 +175,7 @@ bool sc_str_set(char **str, const char *param)
return false;
}

sc_str_destroy(*str);
sc_str_destroy(str);
*str = c;

return true;
Expand All @@ -190,7 +191,7 @@ bool sc_str_set_fmt(char **str, const char *fmt, ...)
va_end(args);

if (ret != NULL) {
sc_str_destroy(*str);
sc_str_destroy(str);
*str = ret;
}

Expand Down Expand Up @@ -310,7 +311,7 @@ bool sc_str_trim(char **str, const char *list)
return false;
}

sc_str_destroy(*str);
sc_str_destroy(str);
*str = head;
}

Expand All @@ -336,7 +337,7 @@ bool sc_str_substring(char **str, uint32_t start, uint32_t end)
return false;
}

sc_str_destroy(*str);
sc_str_destroy(str);
*str = c;

return true;
Expand Down Expand Up @@ -416,7 +417,7 @@ bool sc_str_replace(char **str, const char *replace, const char *with)

memcpy(tmp, orig, orig_end - orig + 1);

sc_str_destroy(*str);
sc_str_destroy(str);
*str = dest->buf;

return true;
Expand Down
2 changes: 1 addition & 1 deletion string/sc_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ char *sc_str_create_va(const char *fmt, va_list va);
* Deallocate length prefixed string.
* @param str length prefixed string. str may be NULL.
*/
void sc_str_destroy(char *str);
void sc_str_destroy(char **str);

/**
* @param str length prefixed string. NULL values are accepted.
Expand Down
Loading

0 comments on commit 57e67dc

Please sign in to comment.