1 // SPDX-License-Identifier: GPL-2.0
3 * C++ stream style string builder used in KUnit for building messages.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
9 #include <kunit/test.h>
10 #include <linux/list.h>
11 #include <linux/slab.h>
13 #include "string-stream.h"
16 static struct string_stream_fragment *alloc_string_stream_fragment(
17 struct kunit *test, int len, gfp_t gfp)
19 struct string_stream_fragment *frag;
21 frag = kunit_kzalloc(test, sizeof(*frag), gfp);
23 return ERR_PTR(-ENOMEM);
25 frag->fragment = kunit_kmalloc(test, len, gfp);
26 if (!frag->fragment) {
27 kunit_kfree(test, frag);
28 return ERR_PTR(-ENOMEM);
34 static void string_stream_fragment_destroy(struct kunit *test,
35 struct string_stream_fragment *frag)
37 list_del(&frag->node);
38 kunit_kfree(test, frag->fragment);
39 kunit_kfree(test, frag);
42 int string_stream_vadd(struct string_stream *stream,
46 struct string_stream_fragment *frag_container;
48 va_list args_for_counting;
50 /* Make a copy because `vsnprintf` could change it */
51 va_copy(args_for_counting, args);
53 /* Need space for null byte. */
54 len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1;
56 va_end(args_for_counting);
58 frag_container = alloc_string_stream_fragment(stream->test,
61 if (IS_ERR(frag_container))
62 return PTR_ERR(frag_container);
64 len = vsnprintf(frag_container->fragment, len, fmt, args);
65 spin_lock(&stream->lock);
66 stream->length += len;
67 list_add_tail(&frag_container->node, &stream->fragments);
68 spin_unlock(&stream->lock);
73 int string_stream_add(struct string_stream *stream, const char *fmt, ...)
79 result = string_stream_vadd(stream, fmt, args);
85 static void string_stream_clear(struct string_stream *stream)
87 struct string_stream_fragment *frag_container, *frag_container_safe;
89 spin_lock(&stream->lock);
90 list_for_each_entry_safe(frag_container,
94 string_stream_fragment_destroy(stream->test, frag_container);
97 spin_unlock(&stream->lock);
100 char *string_stream_get_string(struct string_stream *stream)
102 struct string_stream_fragment *frag_container;
103 size_t buf_len = stream->length + 1; /* +1 for null byte. */
106 buf = kunit_kzalloc(stream->test, buf_len, stream->gfp);
110 spin_lock(&stream->lock);
111 list_for_each_entry(frag_container, &stream->fragments, node)
112 strlcat(buf, frag_container->fragment, buf_len);
113 spin_unlock(&stream->lock);
118 int string_stream_append(struct string_stream *stream,
119 struct string_stream *other)
121 const char *other_content;
123 other_content = string_stream_get_string(other);
128 return string_stream_add(stream, other_content);
131 bool string_stream_is_empty(struct string_stream *stream)
133 return list_empty(&stream->fragments);
136 struct string_stream_alloc_context {
141 struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
143 struct string_stream *stream;
145 stream = kunit_kzalloc(test, sizeof(*stream), gfp);
147 return ERR_PTR(-ENOMEM);
151 INIT_LIST_HEAD(&stream->fragments);
152 spin_lock_init(&stream->lock);
157 void string_stream_destroy(struct string_stream *stream)
159 string_stream_clear(stream);