Skip to content

Commit a3fdf78

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: string-stream: Decouple string_stream from kunit
Re-work string_stream so that it is not tied to a struct kunit. This is to allow using it for the log of struct kunit_suite. Instead of resource-managing individual allocations the whole string_stream can be resource-managed, if required. alloc_string_stream() now allocates a string stream that is not resource-managed. string_stream_destroy() now works on an unmanaged string_stream allocated by alloc_string_stream() and frees the entire string_stream (previously it only freed the fragments). string_stream_clear() has been made public for callers that want to free the fragments without destroying the string_stream. For resource-managed allocations use kunit_alloc_string_stream() and kunit_free_string_stream(). In addition to this, string_stream_get_string() now returns an unmanaged buffer that the caller must kfree(). Signed-off-by: Richard Fitzgerald <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 20631e1 commit a3fdf78

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

lib/kunit/string-stream-test.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111

1212
#include "string-stream.h"
1313

14+
/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
15+
static void kfree_wrapper(void *p)
16+
{
17+
kfree(p);
18+
}
19+
1420
static char *get_concatenated_string(struct kunit *test, struct string_stream *stream)
1521
{
1622
char *str = string_stream_get_string(stream);
1723

1824
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str);
25+
kunit_add_action(test, kfree_wrapper, (void *)str);
1926

2027
return str;
2128
}
@@ -30,7 +37,6 @@ static void string_stream_init_test(struct kunit *test)
3037

3138
KUNIT_EXPECT_EQ(test, stream->length, 0);
3239
KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
33-
KUNIT_EXPECT_PTR_EQ(test, stream->test, test);
3440
KUNIT_EXPECT_TRUE(test, (stream->gfp == GFP_KERNEL));
3541
KUNIT_EXPECT_FALSE(test, stream->append_newlines);
3642
KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));

lib/kunit/string-stream.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,28 @@
1313
#include "string-stream.h"
1414

1515

16-
static struct string_stream_fragment *alloc_string_stream_fragment(
17-
struct kunit *test, int len, gfp_t gfp)
16+
static struct string_stream_fragment *alloc_string_stream_fragment(int len, gfp_t gfp)
1817
{
1918
struct string_stream_fragment *frag;
2019

21-
frag = kunit_kzalloc(test, sizeof(*frag), gfp);
20+
frag = kzalloc(sizeof(*frag), gfp);
2221
if (!frag)
2322
return ERR_PTR(-ENOMEM);
2423

25-
frag->fragment = kunit_kmalloc(test, len, gfp);
24+
frag->fragment = kmalloc(len, gfp);
2625
if (!frag->fragment) {
27-
kunit_kfree(test, frag);
26+
kfree(frag);
2827
return ERR_PTR(-ENOMEM);
2928
}
3029

3130
return frag;
3231
}
3332

34-
static void string_stream_fragment_destroy(struct kunit *test,
35-
struct string_stream_fragment *frag)
33+
static void string_stream_fragment_destroy(struct string_stream_fragment *frag)
3634
{
3735
list_del(&frag->node);
38-
kunit_kfree(test, frag->fragment);
39-
kunit_kfree(test, frag);
36+
kfree(frag->fragment);
37+
kfree(frag);
4038
}
4139

4240
int string_stream_vadd(struct string_stream *stream,
@@ -65,9 +63,7 @@ int string_stream_vadd(struct string_stream *stream,
6563
/* Need space for null byte. */
6664
buf_len++;
6765

68-
frag_container = alloc_string_stream_fragment(stream->test,
69-
buf_len,
70-
stream->gfp);
66+
frag_container = alloc_string_stream_fragment(buf_len, stream->gfp);
7167
if (IS_ERR(frag_container))
7268
return PTR_ERR(frag_container);
7369

@@ -102,7 +98,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...)
10298
return result;
10399
}
104100

105-
static void string_stream_clear(struct string_stream *stream)
101+
void string_stream_clear(struct string_stream *stream)
106102
{
107103
struct string_stream_fragment *frag_container, *frag_container_safe;
108104

@@ -111,7 +107,7 @@ static void string_stream_clear(struct string_stream *stream)
111107
frag_container_safe,
112108
&stream->fragments,
113109
node) {
114-
string_stream_fragment_destroy(stream->test, frag_container);
110+
string_stream_fragment_destroy(frag_container);
115111
}
116112
stream->length = 0;
117113
spin_unlock(&stream->lock);
@@ -123,7 +119,7 @@ char *string_stream_get_string(struct string_stream *stream)
123119
size_t buf_len = stream->length + 1; /* +1 for null byte. */
124120
char *buf;
125121

126-
buf = kunit_kzalloc(stream->test, buf_len, stream->gfp);
122+
buf = kzalloc(buf_len, stream->gfp);
127123
if (!buf)
128124
return NULL;
129125

@@ -139,30 +135,33 @@ int string_stream_append(struct string_stream *stream,
139135
struct string_stream *other)
140136
{
141137
const char *other_content;
138+
int ret;
142139

143140
other_content = string_stream_get_string(other);
144141

145142
if (!other_content)
146143
return -ENOMEM;
147144

148-
return string_stream_add(stream, other_content);
145+
ret = string_stream_add(stream, other_content);
146+
kfree(other_content);
147+
148+
return ret;
149149
}
150150

151151
bool string_stream_is_empty(struct string_stream *stream)
152152
{
153153
return list_empty(&stream->fragments);
154154
}
155155

156-
static struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
156+
struct string_stream *alloc_string_stream(gfp_t gfp)
157157
{
158158
struct string_stream *stream;
159159

160-
stream = kunit_kzalloc(test, sizeof(*stream), gfp);
160+
stream = kzalloc(sizeof(*stream), gfp);
161161
if (!stream)
162162
return ERR_PTR(-ENOMEM);
163163

164164
stream->gfp = gfp;
165-
stream->test = test;
166165
INIT_LIST_HEAD(&stream->fragments);
167166
spin_lock_init(&stream->lock);
168167

@@ -171,15 +170,35 @@ static struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
171170

172171
void string_stream_destroy(struct string_stream *stream)
173172
{
173+
if (!stream)
174+
return;
175+
174176
string_stream_clear(stream);
177+
kfree(stream);
178+
}
179+
180+
static void resource_free_string_stream(void *p)
181+
{
182+
struct string_stream *stream = p;
183+
184+
string_stream_destroy(stream);
175185
}
176186

177187
struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
178188
{
179-
return alloc_string_stream(test, gfp);
189+
struct string_stream *stream;
190+
191+
stream = alloc_string_stream(gfp);
192+
if (IS_ERR(stream))
193+
return stream;
194+
195+
if (kunit_add_action_or_reset(test, resource_free_string_stream, stream) != 0)
196+
return ERR_PTR(-ENOMEM);
197+
198+
return stream;
180199
}
181200

182201
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream)
183202
{
184-
string_stream_destroy(stream);
203+
kunit_release_action(test, resource_free_string_stream, (void *)stream);
185204
}

lib/kunit/string-stream.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ struct string_stream {
2323
struct list_head fragments;
2424
/* length and fragments are protected by this lock */
2525
spinlock_t lock;
26-
struct kunit *test;
2726
gfp_t gfp;
2827
bool append_newlines;
2928
};
@@ -33,13 +32,18 @@ struct kunit;
3332
struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp);
3433
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream);
3534

35+
struct string_stream *alloc_string_stream(gfp_t gfp);
36+
void free_string_stream(struct string_stream *stream);
37+
3638
int __printf(2, 3) string_stream_add(struct string_stream *stream,
3739
const char *fmt, ...);
3840

3941
int __printf(2, 0) string_stream_vadd(struct string_stream *stream,
4042
const char *fmt,
4143
va_list args);
4244

45+
void string_stream_clear(struct string_stream *stream);
46+
4347
char *string_stream_get_string(struct string_stream *stream);
4448

4549
int string_stream_append(struct string_stream *stream,

lib/kunit/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static void kunit_print_string_stream(struct kunit *test,
296296
kunit_err(test, "\n");
297297
} else {
298298
kunit_err(test, "%s", buf);
299-
kunit_kfree(test, buf);
299+
kfree(buf);
300300
}
301301
}
302302

0 commit comments

Comments
 (0)