mirror of
https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git
synced 2024-11-26 12:23:38 +08:00
Remove scratchbuf implementation
All its unique features have been ported to strbuf and users converted. Nuke it. Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com> Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com> Link: https://github.com/kmod-project/kmod/pull/239
This commit is contained in:
parent
3fca291dc5
commit
c6b4652dd9
@ -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)
|
||||
|
||||
|
@ -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',
|
||||
|
@ -1,46 +0,0 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
/*
|
||||
* Copyright (C) 2016 Intel Corporation. All rights reserved.
|
||||
*/
|
||||
#include "scratchbuf.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <shared/macro.h>
|
||||
|
||||
/*
|
||||
* 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, \
|
||||
}
|
3
testsuite/.gitignore
vendored
3
testsuite/.gitignore
vendored
@ -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
|
||||
|
@ -91,7 +91,6 @@ _testsuite = [
|
||||
'test-modinfo',
|
||||
'test-modprobe',
|
||||
'test-new-module',
|
||||
'test-scratchbuf',
|
||||
'test-strbuf',
|
||||
'test-testsuite',
|
||||
'test-util',
|
||||
|
@ -1,76 +0,0 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
/*
|
||||
* Copyright (C) 2016 Intel Corporation. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <shared/scratchbuf.h>
|
||||
|
||||
#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();
|
Loading…
Reference in New Issue
Block a user