Skip to content

Commit c0149e9

Browse files
authored
sc_buf_shrink, sc_list del before add. (#62)
sc_buf_shrink, sc_list del before add.
1 parent 8c5b16e commit c0149e9

File tree

16 files changed

+168
-51
lines changed

16 files changed

+168
-51
lines changed

.github/workflows/.aarch64.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Build on aarch64
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: uraimo/[email protected].8
16+
- uses: uraimo/[email protected].9
1717
name: Build artifact
1818
id: build
1919
with:

.github/workflows/.armv6.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
name: Build on armv6
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: uraimo/[email protected].8
16+
- uses: uraimo/[email protected].9
1717
name: Build artifact
1818
id: build
1919
with:
2020
arch: armv6
21-
distro: buster
21+
distro: alpine_latest
2222

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

2929
# Produce a binary artifact and place it in the mounted volume
3030
run: |
31-
apt-get update -q -y
32-
apt-get install -q -y build-essential git gcc valgrind cmake
33-
uname -a;id;uname -m;lscpu | grep Endian
31+
apk update
32+
apk add git build-base gcc make valgrind cmake
3433
mkdir build && cd build
3534
cmake .. && make -j && make check

.github/workflows/.armv7.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Build on armv7
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: uraimo/[email protected].8
16+
- uses: uraimo/[email protected].9
1717
name: Build artifact
1818
id: build
1919
with:

.github/workflows/.ppc64le.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Build on ppc64le
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: uraimo/[email protected].8
16+
- uses: uraimo/[email protected].9
1717
name: Build artifact
1818
id: build
1919
with:

.github/workflows/.s390x.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Build on s390x
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: uraimo/[email protected].8
16+
- uses: uraimo/[email protected].9
1717
name: Build artifact
1818
id: build
1919
with:

buffer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if (SC_BUILD_TEST)
4747
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP)
4848
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
4949
target_link_options(${PROJECT_NAME}_test PRIVATE
50-
-Wl,--wrap=vsnprintf,--wrap=malloc,--wrap=realloc,--wrap=strlen)
50+
-Wl,--wrap=vsnprintf,--wrap=calloc,--wrap=realloc,--wrap=strlen)
5151
endif ()
5252
endif ()
5353

buffer/buf_test.c

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ void test1()
188188

189189
void test2()
190190
{
191+
unsigned char tmp[32];
191192
struct sc_buf buf;
192193
sc_buf_init(&buf, 100);
193194

@@ -248,22 +249,57 @@ void test2()
248249
sc_buf_set_32(&buf, 100);
249250
sc_buf_set_64(&buf, 100);
250251
sc_buf_set_data(&buf, 19, "d", 1);
251-
sc_buf_peek_data(&buf, 10, NULL, 0);
252+
sc_buf_peek_data(&buf, 10, tmp, 0);
252253
assert(!sc_buf_valid(&buf));
253254
sc_buf_term(&buf);
255+
256+
sc_buf_init(&buf, 32);
257+
sc_buf_put_64(&buf, 100);
258+
sc_buf_put_64(&buf, 200);
259+
sc_buf_put_64(&buf, 300);
260+
sc_buf_put_64(&buf, 400);
261+
sc_buf_get_64(&buf);
262+
sc_buf_get_64(&buf);
263+
sc_buf_shrink(&buf, 24);
264+
assert(sc_buf_get_64(&buf) == 300);
265+
assert(sc_buf_get_64(&buf) == 400);
266+
assert(sc_buf_size(&buf) == 0);
267+
sc_buf_term(&buf);
268+
269+
sc_buf_init(&buf, 4096);
270+
sc_buf_shrink(&buf, 4096 * 2);
271+
sc_buf_shrink(&buf, 128);
272+
273+
for (int i = 0; i < 4000; i++) {
274+
sc_buf_put_64(&buf, i);
275+
}
276+
277+
sc_buf_shrink(&buf, 0);
278+
279+
for (int i = 0; i < 3700; i++) {
280+
sc_buf_get_64(&buf);
281+
}
282+
283+
sc_buf_shrink(&buf, 4096);
284+
285+
for (int i = 0; i < 300; i++) {
286+
assert(sc_buf_get_64(&buf) == (uint64_t) 3700 + i);
287+
}
288+
289+
sc_buf_term(&buf);
254290
}
255291

256292
#ifdef SC_HAVE_WRAP
257293

258-
bool fail_malloc = false;
259-
void *__real_malloc(size_t n);
260-
void *__wrap_malloc(size_t n)
294+
bool fail_calloc = false;
295+
void *__real_calloc(size_t m, size_t n);
296+
void *__wrap_calloc(size_t m, size_t n)
261297
{
262-
if (fail_malloc) {
298+
if (fail_calloc) {
263299
return NULL;
264300
}
265301

266-
return __real_malloc(n);
302+
return __real_calloc(m, n);
267303
}
268304

269305
bool fail_realloc = false;
@@ -309,22 +345,22 @@ void fail_test()
309345
unsigned char* p;
310346
struct sc_buf buf;
311347

312-
fail_malloc = true;
348+
fail_calloc = true;
313349
assert(sc_buf_init(&buf, 100) == false);
314-
fail_malloc = false;
350+
fail_calloc = false;
315351

316352
assert(sc_buf_init(&buf, 0) == true);
317353
sc_buf_put_32(&buf, 100);
318354
assert(sc_buf_valid(&buf) == true);
319355
sc_buf_term(&buf);
320356

321-
fail_malloc = true;
357+
fail_calloc = true;
322358
fail_realloc = true;
323359
assert(sc_buf_init(&buf, 0) == true);
324360
sc_buf_put_32(&buf, 100);
325361
assert(sc_buf_valid(&buf) == false);
326362
sc_buf_term(&buf);
327-
fail_malloc = false;
363+
fail_calloc = false;
328364
fail_realloc = false;
329365

330366
sc_buf_init(&buf, 10);
@@ -497,6 +533,15 @@ void fail_test()
497533
assert(sc_buf_valid(&buf) == true);
498534
sc_buf_term(&buf);
499535

536+
sc_buf_init(&buf, 4096 * 8);
537+
fail_realloc = true;
538+
assert(sc_buf_shrink(&buf, 4096) == false);
539+
fail_realloc = false;
540+
assert(sc_buf_shrink(&buf, 4096) == true);
541+
assert(sc_buf_cap(&buf) == 4096);
542+
sc_buf_term(&buf);
543+
544+
500545
}
501546
#else
502547
void fail_test()

buffer/sc_buf.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool sc_buf_init(struct sc_buf *buf, uint32_t cap)
4040
*buf = (struct sc_buf){0};
4141

4242
if (cap > 0) {
43-
mem = sc_buf_malloc(cap);
43+
mem = sc_buf_calloc(1, cap);
4444
if (mem == NULL) {
4545
return false;
4646
}
@@ -119,6 +119,30 @@ bool sc_buf_reserve(struct sc_buf *buf, uint32_t len)
119119
return true;
120120
}
121121

122+
bool sc_buf_shrink(struct sc_buf *buf, uint32_t len)
123+
{
124+
void *tmp;
125+
126+
sc_buf_compact(buf);
127+
128+
if (len > buf->cap || buf->wpos >= len) {
129+
return true;
130+
}
131+
132+
len = ((len + 4095) / 4096) * 4096;
133+
134+
tmp = sc_buf_realloc(buf->mem, len);
135+
if (tmp == NULL) {
136+
buf->error |= SC_BUF_OOM;
137+
return false;
138+
}
139+
140+
buf->cap = len;
141+
buf->mem = tmp;
142+
143+
return true;
144+
}
145+
122146
bool sc_buf_valid(struct sc_buf *buf)
123147
{
124148
return buf->error == 0;

buffer/sc_buf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#ifdef SC_HAVE_CONFIG_H
3737
#include "config.h"
3838
#else
39-
#define sc_buf_malloc malloc
39+
#define sc_buf_calloc calloc
4040
#define sc_buf_realloc realloc
4141
#define sc_buf_free free
4242
#endif
@@ -123,6 +123,8 @@ uint32_t sc_buf_cap(struct sc_buf *buf);
123123
*/
124124
bool sc_buf_reserve(struct sc_buf *buf, uint32_t len);
125125

126+
127+
bool sc_buf_shrink(struct sc_buf *buf, uint32_t len);
126128
/**
127129
* @param buf buf
128130
* @return 'true' if buffer is valid. Buffer becomes invalid on out of

linked-list/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ int main()
3333
struct sc_list list;
3434

3535
sc_list_init(&list);
36+
3637

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

0 commit comments

Comments
 (0)