Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/.aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Build on aarch64
steps:
- uses: actions/[email protected]
- uses: uraimo/[email protected].8
- uses: uraimo/[email protected].9
name: Build artifact
id: build
with:
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/.armv6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
name: Build on armv6
steps:
- uses: actions/[email protected]
- uses: uraimo/[email protected].8
- uses: uraimo/[email protected].9
name: Build artifact
id: build
with:
arch: armv6
distro: buster
distro: alpine_latest

# Not required, but speeds up builds
githubToken: ${{ github.token }}
Expand All @@ -28,8 +28,7 @@ jobs:

# Produce a binary artifact and place it in the mounted volume
run: |
apt-get update -q -y
apt-get install -q -y build-essential git gcc valgrind cmake
uname -a;id;uname -m;lscpu | grep Endian
apk update
apk add git build-base gcc make valgrind cmake
mkdir build && cd build
cmake .. && make -j && make check
2 changes: 1 addition & 1 deletion .github/workflows/.armv7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Build on armv7
steps:
- uses: actions/[email protected]
- uses: uraimo/[email protected].8
- uses: uraimo/[email protected].9
name: Build artifact
id: build
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/.ppc64le.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Build on ppc64le
steps:
- uses: actions/[email protected]
- uses: uraimo/[email protected].8
- uses: uraimo/[email protected].9
name: Build artifact
id: build
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/.s390x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Build on s390x
steps:
- uses: actions/[email protected]
- uses: uraimo/[email protected].8
- uses: uraimo/[email protected].9
name: Build artifact
id: build
with:
Expand Down
2 changes: 1 addition & 1 deletion buffer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (SC_BUILD_TEST)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
target_link_options(${PROJECT_NAME}_test PRIVATE
-Wl,--wrap=vsnprintf,--wrap=malloc,--wrap=realloc,--wrap=strlen)
-Wl,--wrap=vsnprintf,--wrap=calloc,--wrap=realloc,--wrap=strlen)
endif ()
endif ()

Expand Down
65 changes: 55 additions & 10 deletions buffer/buf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ void test1()

void test2()
{
unsigned char tmp[32];
struct sc_buf buf;
sc_buf_init(&buf, 100);

Expand Down Expand Up @@ -248,22 +249,57 @@ void test2()
sc_buf_set_32(&buf, 100);
sc_buf_set_64(&buf, 100);
sc_buf_set_data(&buf, 19, "d", 1);
sc_buf_peek_data(&buf, 10, NULL, 0);
sc_buf_peek_data(&buf, 10, tmp, 0);
assert(!sc_buf_valid(&buf));
sc_buf_term(&buf);

sc_buf_init(&buf, 32);
sc_buf_put_64(&buf, 100);
sc_buf_put_64(&buf, 200);
sc_buf_put_64(&buf, 300);
sc_buf_put_64(&buf, 400);
sc_buf_get_64(&buf);
sc_buf_get_64(&buf);
sc_buf_shrink(&buf, 24);
assert(sc_buf_get_64(&buf) == 300);
assert(sc_buf_get_64(&buf) == 400);
assert(sc_buf_size(&buf) == 0);
sc_buf_term(&buf);

sc_buf_init(&buf, 4096);
sc_buf_shrink(&buf, 4096 * 2);
sc_buf_shrink(&buf, 128);

for (int i = 0; i < 4000; i++) {
sc_buf_put_64(&buf, i);
}

sc_buf_shrink(&buf, 0);

for (int i = 0; i < 3700; i++) {
sc_buf_get_64(&buf);
}

sc_buf_shrink(&buf, 4096);

for (int i = 0; i < 300; i++) {
assert(sc_buf_get_64(&buf) == (uint64_t) 3700 + i);
}

sc_buf_term(&buf);
}

#ifdef SC_HAVE_WRAP

bool fail_malloc = false;
void *__real_malloc(size_t n);
void *__wrap_malloc(size_t n)
bool fail_calloc = false;
void *__real_calloc(size_t m, size_t n);
void *__wrap_calloc(size_t m, size_t n)
{
if (fail_malloc) {
if (fail_calloc) {
return NULL;
}

return __real_malloc(n);
return __real_calloc(m, n);
}

bool fail_realloc = false;
Expand Down Expand Up @@ -309,22 +345,22 @@ void fail_test()
unsigned char* p;
struct sc_buf buf;

fail_malloc = true;
fail_calloc = true;
assert(sc_buf_init(&buf, 100) == false);
fail_malloc = false;
fail_calloc = false;

assert(sc_buf_init(&buf, 0) == true);
sc_buf_put_32(&buf, 100);
assert(sc_buf_valid(&buf) == true);
sc_buf_term(&buf);

fail_malloc = true;
fail_calloc = true;
fail_realloc = true;
assert(sc_buf_init(&buf, 0) == true);
sc_buf_put_32(&buf, 100);
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);
fail_malloc = false;
fail_calloc = false;
fail_realloc = false;

sc_buf_init(&buf, 10);
Expand Down Expand Up @@ -497,6 +533,15 @@ void fail_test()
assert(sc_buf_valid(&buf) == true);
sc_buf_term(&buf);

sc_buf_init(&buf, 4096 * 8);
fail_realloc = true;
assert(sc_buf_shrink(&buf, 4096) == false);
fail_realloc = false;
assert(sc_buf_shrink(&buf, 4096) == true);
assert(sc_buf_cap(&buf) == 4096);
sc_buf_term(&buf);


}
#else
void fail_test()
Expand Down
26 changes: 25 additions & 1 deletion buffer/sc_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool sc_buf_init(struct sc_buf *buf, uint32_t cap)
*buf = (struct sc_buf){0};

if (cap > 0) {
mem = sc_buf_malloc(cap);
mem = sc_buf_calloc(1, cap);
if (mem == NULL) {
return false;
}
Expand Down Expand Up @@ -119,6 +119,30 @@ bool sc_buf_reserve(struct sc_buf *buf, uint32_t len)
return true;
}

bool sc_buf_shrink(struct sc_buf *buf, uint32_t len)
{
void *tmp;

sc_buf_compact(buf);

if (len > buf->cap || buf->wpos >= len) {
return true;
}

len = ((len + 4095) / 4096) * 4096;

tmp = sc_buf_realloc(buf->mem, len);
if (tmp == NULL) {
buf->error |= SC_BUF_OOM;
return false;
}

buf->cap = len;
buf->mem = tmp;

return true;
}

bool sc_buf_valid(struct sc_buf *buf)
{
return buf->error == 0;
Expand Down
4 changes: 3 additions & 1 deletion buffer/sc_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_buf_malloc malloc
#define sc_buf_calloc calloc
#define sc_buf_realloc realloc
#define sc_buf_free free
#endif
Expand Down Expand Up @@ -123,6 +123,8 @@ uint32_t sc_buf_cap(struct sc_buf *buf);
*/
bool sc_buf_reserve(struct sc_buf *buf, uint32_t len);


bool sc_buf_shrink(struct sc_buf *buf, uint32_t len);
/**
* @param buf buf
* @return 'true' if buffer is valid. Buffer becomes invalid on out of
Expand Down
2 changes: 2 additions & 0 deletions linked-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ int main()
struct sc_list list;

sc_list_init(&list);


for (int i = 0; i < 5; i++) {
sc_list_init(&users[i].next);
sc_list_add_tail(&list, &users[i].next);
}

Expand Down
2 changes: 2 additions & 0 deletions linked-list/list_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ int main()

sc_list_init(&list);


for (int i = 0; i < 5; i++) {
sc_list_init(&users[i].next);
sc_list_add_tail(&list, &users[i].next);
}

Expand Down
36 changes: 36 additions & 0 deletions linked-list/list_test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "sc_list.h"

#include <assert.h>
#include <stdio.h>

struct elem
{
Expand All @@ -14,6 +15,12 @@ static void test1(void)
struct elem a, b, c, d, e, *elem;
struct sc_list list, *item, *tmp;

sc_list_init(&a.list);
sc_list_init(&b.list);
sc_list_init(&c.list);
sc_list_init(&d.list);
sc_list_init(&e.list);

a.id = 1;
b.id = 2;
c.id = 3;
Expand Down Expand Up @@ -97,12 +104,41 @@ static void test1(void)
sc_list_add_tail(&list, &a.list);
sc_list_add_tail(&list, &b.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &b.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_head(&list, &c.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_head(&list, &e.list);
sc_list_add_tail(&list, &e.list);
sc_list_add_head(&list, &e.list);
sc_list_add_tail(&list, &e.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_tail(&list, &e.list);
sc_list_add_head(&list, &e.list);
sc_list_add_tail(&list, &e.list);

assert(sc_list_head(&list) != NULL);
assert(sc_list_tail(&list) != NULL);
assert(sc_list_is_empty(&list) == false);
assert(sc_list_count(&list) == 5);

sc_list_clear(&list);

sc_list_add_tail(&list, &a.list);
sc_list_add_tail(&list, &b.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &c.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_tail(&list, &d.list);
sc_list_add_tail(&list, &e.list);
sc_list_add_tail(&list, &e.list);

k = 0;
sc_list_foreach_safe (&list, tmp, item) {
Expand Down
Loading