From: Lucas De Marchi Date: Tue, 12 Nov 2024 20:31:36 +0000 (-0600) Subject: Remove scratchbuf implementation X-Git-Tag: v34~82 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c6b4652dd95e8aa9c37c5d19f765ce693bb042be;p=thirdparty%2Fkmod.git Remove scratchbuf implementation All its unique features have been ported to strbuf and users converted. Nuke it. Signed-off-by: Lucas De Marchi Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/239 --- diff --git a/Makefile.am b/Makefile.am index a7eb1b20..e8ff6318 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,8 +62,6 @@ shared_libshared_la_SOURCES = \ shared/hash.h \ shared/macro.h \ shared/missing.h \ - shared/scratchbuf.c \ - shared/scratchbuf.h \ shared/strbuf.c \ shared/strbuf.h \ shared/util.c \ @@ -295,7 +293,6 @@ TESTSUITE = \ testsuite/test-modinfo \ testsuite/test-modprobe \ testsuite/test-new-module \ - testsuite/test-scratchbuf \ testsuite/test-strbuf \ testsuite/test-testsuite \ testsuite/test-util \ @@ -314,9 +311,6 @@ testsuite_test_hash_CPPFLAGS = $(TESTSUITE_CPPFLAGS) testsuite_test_array_LDADD = $(TESTSUITE_LDADD) testsuite_test_array_CPPFLAGS = $(TESTSUITE_CPPFLAGS) -testsuite_test_scratchbuf_LDADD = $(TESTSUITE_LDADD) -testsuite_test_scratchbuf_CPPFLAGS = $(TESTSUITE_CPPFLAGS) - testsuite_test_strbuf_LDADD = $(TESTSUITE_LDADD) testsuite_test_strbuf_CPPFLAGS = $(TESTSUITE_CPPFLAGS) diff --git a/meson.build b/meson.build index 905342c2..3631dd2a 100644 --- a/meson.build +++ b/meson.build @@ -343,8 +343,6 @@ libshared = static_library( 'shared/hash.h', 'shared/macro.h', 'shared/missing.h', - 'shared/scratchbuf.c', - 'shared/scratchbuf.h', 'shared/strbuf.c', 'shared/strbuf.h', 'shared/util.c', diff --git a/shared/scratchbuf.c b/shared/scratchbuf.c deleted file mode 100644 index 5c51e4b4..00000000 --- a/shared/scratchbuf.c +++ /dev/null @@ -1,46 +0,0 @@ -// SPDX-License-Identifier: LGPL-2.1-or-later -/* - * Copyright (C) 2016 Intel Corporation. All rights reserved. - */ -#include "scratchbuf.h" - -#include -#include - -void scratchbuf_init(struct scratchbuf *buf, char *stackbuf, size_t size) -{ - buf->bytes = stackbuf; - buf->size = size; - buf->need_free = false; -} - -int scratchbuf_alloc(struct scratchbuf *buf, size_t size) -{ - char *tmp; - - if (size <= buf->size) - return 0; - - if (buf->need_free) { - tmp = realloc(buf->bytes, size); - if (tmp == NULL) - return -ENOMEM; - } else { - tmp = malloc(size); - if (tmp == NULL) - return -ENOMEM; - memcpy(tmp, buf->bytes, buf->size); - } - - buf->size = size; - buf->bytes = tmp; - buf->need_free = true; - - return 0; -} - -void scratchbuf_release(struct scratchbuf *buf) -{ - if (buf->need_free) - free(buf->bytes); -} diff --git a/shared/scratchbuf.h b/shared/scratchbuf.h deleted file mode 100644 index 7ba84373..00000000 --- a/shared/scratchbuf.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include - -#include - -/* - * Buffer abstract data type - */ -struct scratchbuf { - char *bytes; - size_t size; - bool need_free; -}; - -void scratchbuf_init(struct scratchbuf *buf, char *stackbuf, size_t size); -int scratchbuf_alloc(struct scratchbuf *buf, size_t sz); -void scratchbuf_release(struct scratchbuf *buf); - -/* Return a C string */ -static inline char *scratchbuf_str(struct scratchbuf *buf) -{ - return buf->bytes; -} - -#define SCRATCHBUF_INITIALIZER(buf_) \ - { \ - .bytes = buf_, \ - .size = sizeof(buf_) + _array_size_chk(buf_), \ - .need_free = false, \ - } diff --git a/testsuite/.gitignore b/testsuite/.gitignore index ce51cc20..f0289d47 100644 --- a/testsuite/.gitignore +++ b/testsuite/.gitignore @@ -2,7 +2,6 @@ *.la *.so /.dirstamp -/test-scratchbuf /test-strbuf /test-array /test-util @@ -21,8 +20,6 @@ /test-weakdep /rootfs /stamp-rootfs -/test-scratchbuf.log -/test-scratchbuf.trs /test-strbuf.log /test-strbuf.trs /test-array.log diff --git a/testsuite/meson.build b/testsuite/meson.build index fdfa6eb1..1f49f698 100644 --- a/testsuite/meson.build +++ b/testsuite/meson.build @@ -91,7 +91,6 @@ _testsuite = [ 'test-modinfo', 'test-modprobe', 'test-new-module', - 'test-scratchbuf', 'test-strbuf', 'test-testsuite', 'test-util', diff --git a/testsuite/test-scratchbuf.c b/testsuite/test-scratchbuf.c deleted file mode 100644 index b5fdd7f4..00000000 --- a/testsuite/test-scratchbuf.c +++ /dev/null @@ -1,76 +0,0 @@ -// SPDX-License-Identifier: LGPL-2.1-or-later -/* - * Copyright (C) 2016 Intel Corporation. All rights reserved. - */ - -#include -#include -#include - -#include - -#include "testsuite.h" - -static int test_scratchbuf_onlystack(const struct test *t) -{ - struct scratchbuf sbuf; - const char *smallstr = "xyz"; - char buf[3 + 2]; - char buf2[3 + 1]; - - scratchbuf_init(&sbuf, buf, sizeof(buf)); - assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); - assert_return(sbuf.need_free == false, EXIT_FAILURE); - scratchbuf_release(&sbuf); - - scratchbuf_init(&sbuf, buf2, sizeof(buf2)); - assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); - assert_return(sbuf.need_free == false, EXIT_FAILURE); - scratchbuf_release(&sbuf); - - memcpy(scratchbuf_str(&sbuf), smallstr, strlen(smallstr) + 1); - assert_return(strcmp(scratchbuf_str(&sbuf), smallstr) == 0, EXIT_FAILURE); - - return 0; -} -DEFINE_TEST(test_scratchbuf_onlystack, - .description = "test scratchbuf for buffer on stack only"); - -static int test_scratchbuf_heap(const struct test *t) -{ - struct scratchbuf sbuf; - const char *smallstr = "xyz"; - const char *largestr = "xyzxyzxyz"; - const char *largestr2 = "xyzxyzxyzxyzxyz"; - char buf[3 + 1]; - - scratchbuf_init(&sbuf, buf, sizeof(buf)); - - /* Initially only on stack */ - assert_return(scratchbuf_alloc(&sbuf, strlen(smallstr) + 1) == 0, EXIT_FAILURE); - assert_return(sbuf.need_free == false, EXIT_FAILURE); - memcpy(scratchbuf_str(&sbuf), smallstr, strlen(smallstr) + 1); - - /* Grow once to heap */ - assert_return(scratchbuf_alloc(&sbuf, strlen(largestr) + 1) == 0, EXIT_FAILURE); - assert_return(sbuf.need_free == true, EXIT_FAILURE); - assert_return(sbuf.size == strlen(largestr) + 1, EXIT_FAILURE); - assert_return(strcmp(scratchbuf_str(&sbuf), smallstr) == 0, EXIT_FAILURE); - memcpy(scratchbuf_str(&sbuf), largestr, strlen(largestr) + 1); - assert_return(strcmp(scratchbuf_str(&sbuf), largestr) == 0, EXIT_FAILURE); - - /* Grow again - realloc should take place */ - assert_return(scratchbuf_alloc(&sbuf, strlen(largestr2) + 1) == 0, EXIT_FAILURE); - assert_return(sbuf.need_free == true, EXIT_FAILURE); - assert_return(sbuf.size == strlen(largestr2) + 1, EXIT_FAILURE); - memcpy(scratchbuf_str(&sbuf), largestr2, strlen(largestr2) + 1); - assert_return(strcmp(scratchbuf_str(&sbuf), largestr2) == 0, EXIT_FAILURE); - - scratchbuf_release(&sbuf); - - return 0; -} -DEFINE_TEST(test_scratchbuf_heap, - .description = "test scratchbuf for buffer on that grows to heap"); - -TESTSUITE_MAIN();