sc_buf_shrink, sc_list del before add. (#62)

sc_buf_shrink, sc_list del before add.
This commit is contained in:
Ozan Tezcan 2021-03-24 01:37:41 +03:00 committed by GitHub
parent 8c5b16ecd2
commit c0149e90d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 168 additions and 51 deletions

View File

@ -13,7 +13,7 @@ jobs:
name: Build on aarch64
steps:
- uses: actions/checkout@v2.1.0
- uses: uraimo/run-on-arch-action@v2.0.8
- uses: uraimo/run-on-arch-action@v2.0.9
name: Build artifact
id: build
with:

View File

@ -13,12 +13,12 @@ jobs:
name: Build on armv6
steps:
- uses: actions/checkout@v2.1.0
- uses: uraimo/run-on-arch-action@v2.0.8
- uses: uraimo/run-on-arch-action@v2.0.9
name: Build artifact
id: build
with:
arch: armv6
distro: buster
distro: alpine_latest
# Not required, but speeds up builds
githubToken: ${{ github.token }}
@ -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

View File

@ -13,7 +13,7 @@ jobs:
name: Build on armv7
steps:
- uses: actions/checkout@v2.1.0
- uses: uraimo/run-on-arch-action@v2.0.8
- uses: uraimo/run-on-arch-action@v2.0.9
name: Build artifact
id: build
with:

View File

@ -13,7 +13,7 @@ jobs:
name: Build on ppc64le
steps:
- uses: actions/checkout@v2.1.0
- uses: uraimo/run-on-arch-action@v2.0.8
- uses: uraimo/run-on-arch-action@v2.0.9
name: Build artifact
id: build
with:

View File

@ -13,7 +13,7 @@ jobs:
name: Build on s390x
steps:
- uses: actions/checkout@v2.1.0
- uses: uraimo/run-on-arch-action@v2.0.8
- uses: uraimo/run-on-arch-action@v2.0.9
name: Build artifact
id: build
with:

View File

@ -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 ()

View File

@ -188,6 +188,7 @@ void test1()
void test2()
{
unsigned char tmp[32];
struct sc_buf buf;
sc_buf_init(&buf, 100);
@ -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;
@ -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);
@ -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()

View File

@ -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;
}
@ -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;

View File

@ -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
@ -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

View File

@ -34,7 +34,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);
}

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
#include "sc_list.h"
#include <assert.h>
#include <stdio.h>
struct elem
{
@ -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;
@ -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) {

View File

@ -32,8 +32,11 @@ void sc_list_init(struct sc_list *list)
void sc_list_clear(struct sc_list *list)
{
list->next = list;
list->prev = list;
struct sc_list *tmp, *elem;
sc_list_foreach_safe (list, tmp, elem) {
sc_list_del(NULL, elem);
}
}
bool sc_list_is_empty(struct sc_list *list)
@ -55,32 +58,21 @@ size_t sc_list_count(struct sc_list *list)
struct sc_list *sc_list_head(struct sc_list *list)
{
struct sc_list *elem;
elem = list->next;
if (elem == list) {
return NULL;
}
return elem;
return list->next != list ? list->next : NULL;
}
struct sc_list *sc_list_tail(struct sc_list *list)
{
struct sc_list *elem;
elem = list->prev;
if (elem == list) {
return NULL;
}
return elem;
return list->prev != list ? list->prev : NULL;
}
void sc_list_add_tail(struct sc_list *list, struct sc_list *elem)
{
struct sc_list *prev;
// Delete if exists to prevent adding same item twice
sc_list_del(list, elem);
prev = list->prev;
list->prev = elem;
elem->next = list;
@ -90,13 +82,12 @@ void sc_list_add_tail(struct sc_list *list, struct sc_list *elem)
struct sc_list *sc_list_pop_tail(struct sc_list *list)
{
struct sc_list *tail;
struct sc_list *tail = list->prev;
if (sc_list_is_empty(list)) {
return NULL;
}
tail = list->prev;
sc_list_del(list, list->prev);
return tail;
@ -104,18 +95,26 @@ struct sc_list *sc_list_pop_tail(struct sc_list *list)
void sc_list_add_head(struct sc_list *list, struct sc_list *elem)
{
sc_list_add_before(list, list->next, elem);
struct sc_list *next;
// Delete if exists to prevent adding same item twice
sc_list_del(list, elem);
next = list->next;
list->next = elem;
elem->prev = list;
elem->next = next;
next->prev = elem;
}
struct sc_list *sc_list_pop_head(struct sc_list *list)
{
struct sc_list *head;
struct sc_list *head = list->next;
if (sc_list_is_empty(list)) {
return NULL;
}
head = list->next;
sc_list_del(list, list->next);
return head;
@ -127,6 +126,9 @@ void sc_list_add_after(struct sc_list *list, struct sc_list *prev,
(void) list;
struct sc_list *next;
// Delete if exists to prevent adding same item twice
sc_list_del(list, elem);
next = prev->next;
prev->next = elem;
elem->next = next;
@ -140,6 +142,9 @@ void sc_list_add_before(struct sc_list *list, struct sc_list *next,
(void) list;
struct sc_list *prev;
// Delete if exists to prevent adding same item twice
sc_list_del(list, elem);
prev = next->prev;
next->prev = elem;
elem->next = next;

View File

@ -166,9 +166,10 @@ void sc_list_del(struct sc_list *list, struct sc_list *elem);
*
* struct container *container; // User object
* struct sc_list *list; // List pointer, should already be initialized.
* struct sc_list *tmp; // Variable for loop, user should not use this.
* struct sc_list *it; // Iterator
*
* sc_list_foreach(list, it) {
* sc_list_foreach_safe (list, it) {
* container = sc_list_entry(it, struct container, others);
* sc_list_del(list, &container->others);
* }
@ -188,11 +189,10 @@ void sc_list_del(struct sc_list *list, struct sc_list *elem);
* struct sc_list *list; // List pointer, should already be initialized.
* struct sc_list *it; // Iterator
*
* sc_list_foreach(list, it) {
* sc_list_foreach_r (list, it) {
* container = sc_list_entry(it, struct container, others);
* }
*/
#define sc_list_foreach_r(list, elem) \
for ((elem) = (list)->prev; (elem) != (list); (elem) = (elem)->prev)
@ -209,9 +209,10 @@ void sc_list_del(struct sc_list *list, struct sc_list *elem);
*
* struct container *container; // User object
* struct sc_list *list; // List pointer, should already be initialized.
* struct sc_list *tmp; // Variable for loop, user should not use this.
* struct sc_list *it; // Iterator
*
* sc_list_foreach(list, it) {
* sc_list_foreach_safe_r (list, tmp, it) {
* container = sc_list_entry(it, struct container, others);
* sc_list_del(list, &container->others);
* }

View File

@ -57,6 +57,7 @@ void test1()
#ifdef SC_HAVE_WRAP
#include <errno.h>
#include <stdint.h>
#include <sys/stat.h>
bool fail_open;

View File

@ -52,7 +52,7 @@ void test2()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/wait.h>
bool fail_signal;
extern void (*__real_signal(int sig, void (*func)(int)))(int);