Skip to content

Commit 20631e1

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: string-stream: Add kunit_alloc_string_stream()
Add function kunit_alloc_string_stream() to do a resource-managed allocation of a string stream, and corresponding kunit_free_string_stream() to free the resource-managed stream. This is preparing for decoupling the string_stream implementation from struct kunit, to reduce the amount of code churn when that happens. Currently: - kunit_alloc_string_stream() only calls alloc_string_stream(). - kunit_free_string_stream() takes a struct kunit* which isn't used yet. Callers of the old alloc_string_stream() and string_stream_destroy() are all requesting a managed allocation so have been changed to use the new functions. alloc_string_stream() has been temporarily made static because its current behavior has been replaced with kunit_alloc_string_stream(). Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 7b4481c commit 20631e1

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

lib/kunit/string-stream-test.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void string_stream_init_test(struct kunit *test)
2525
{
2626
struct string_stream *stream;
2727

28-
stream = alloc_string_stream(test, GFP_KERNEL);
28+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
2929
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
3030

3131
KUNIT_EXPECT_EQ(test, stream->length, 0);
@@ -48,7 +48,7 @@ static void string_stream_line_add_test(struct kunit *test)
4848
size_t len, total_len;
4949
int num_lines, i;
5050

51-
stream = alloc_string_stream(test, GFP_KERNEL);
51+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
5252
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
5353

5454
/* Add series of sequence numbered lines */
@@ -104,7 +104,7 @@ static void string_stream_variable_length_line_test(struct kunit *test)
104104
size_t offset, total_len;
105105
int num_lines, i;
106106

107-
stream = alloc_string_stream(test, GFP_KERNEL);
107+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
108108
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
109109

110110
/*
@@ -164,10 +164,10 @@ static void string_stream_append_test(struct kunit *test)
164164
size_t combined_length;
165165
int i;
166166

167-
stream_1 = alloc_string_stream(test, GFP_KERNEL);
167+
stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
168168
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
169169

170-
stream_2 = alloc_string_stream(test, GFP_KERNEL);
170+
stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
171171
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
172172

173173
/* Append content of empty stream to empty stream */
@@ -207,9 +207,9 @@ static void string_stream_append_test(struct kunit *test)
207207
KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), combined_content);
208208

209209
/* Append content of non-empty stream to empty stream */
210-
string_stream_destroy(stream_1);
210+
kunit_free_string_stream(test, stream_1);
211211

212-
stream_1 = alloc_string_stream(test, GFP_KERNEL);
212+
stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
213213
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
214214

215215
string_stream_append(stream_1, stream_2);
@@ -222,13 +222,13 @@ static void string_stream_append_auto_newline_test(struct kunit *test)
222222
struct string_stream *stream_1, *stream_2;
223223

224224
/* Stream 1 has newline appending enabled */
225-
stream_1 = alloc_string_stream(test, GFP_KERNEL);
225+
stream_1 = kunit_alloc_string_stream(test, GFP_KERNEL);
226226
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
227227
string_stream_set_append_newlines(stream_1, true);
228228
KUNIT_EXPECT_TRUE(test, stream_1->append_newlines);
229229

230230
/* Stream 2 does not append newlines */
231-
stream_2 = alloc_string_stream(test, GFP_KERNEL);
231+
stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
232232
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
233233

234234
/* Appending a stream with a newline should not add another newline */
@@ -239,8 +239,8 @@ static void string_stream_append_auto_newline_test(struct kunit *test)
239239
KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
240240
"Original string\nAppended content\nMore stuff\n");
241241

242-
string_stream_destroy(stream_2);
243-
stream_2 = alloc_string_stream(test, GFP_KERNEL);
242+
kunit_free_string_stream(test, stream_2);
243+
stream_2 = kunit_alloc_string_stream(test, GFP_KERNEL);
244244
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
245245

246246
/*
@@ -261,7 +261,7 @@ static void string_stream_append_empty_string_test(struct kunit *test)
261261
struct string_stream *stream;
262262
int original_frag_count;
263263

264-
stream = alloc_string_stream(test, GFP_KERNEL);
264+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
265265
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
266266

267267
/* Formatted empty string */
@@ -283,7 +283,7 @@ static void string_stream_no_auto_newline_test(struct kunit *test)
283283
{
284284
struct string_stream *stream;
285285

286-
stream = alloc_string_stream(test, GFP_KERNEL);
286+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
287287
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
288288

289289
/*
@@ -306,7 +306,7 @@ static void string_stream_auto_newline_test(struct kunit *test)
306306
{
307307
struct string_stream *stream;
308308

309-
stream = alloc_string_stream(test, GFP_KERNEL);
309+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
310310
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
311311

312312
string_stream_set_append_newlines(stream, true);

lib/kunit/string-stream.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool string_stream_is_empty(struct string_stream *stream)
153153
return list_empty(&stream->fragments);
154154
}
155155

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

@@ -173,3 +173,13 @@ void string_stream_destroy(struct string_stream *stream)
173173
{
174174
string_stream_clear(stream);
175175
}
176+
177+
struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
178+
{
179+
return alloc_string_stream(test, gfp);
180+
}
181+
182+
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream)
183+
{
184+
string_stream_destroy(stream);
185+
}

lib/kunit/string-stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ struct string_stream {
3030

3131
struct kunit;
3232

33-
struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp);
33+
struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp);
34+
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream);
3435

3536
int __printf(2, 3) string_stream_add(struct string_stream *stream,
3637
const char *fmt, ...);

lib/kunit/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
308308

309309
kunit_set_failure(test);
310310

311-
stream = alloc_string_stream(test, GFP_KERNEL);
311+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
312312
if (IS_ERR(stream)) {
313313
WARN(true,
314314
"Could not allocate stream to print failed assertion in %s:%d\n",
@@ -322,7 +322,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
322322

323323
kunit_print_string_stream(test, stream);
324324

325-
string_stream_destroy(stream);
325+
kunit_free_string_stream(test, stream);
326326
}
327327

328328
void __noreturn __kunit_abort(struct kunit *test)

0 commit comments

Comments
 (0)