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);
27 return ERR_PTR(-ENOMEM);
32 static void string_stream_fragment_destroy(struct kunit *test,
33 struct string_stream_fragment *frag)
35 list_del(&frag->node);
36 kunit_kfree(test, frag->fragment);
37 kunit_kfree(test, frag);
40 int string_stream_vadd(struct string_stream *stream,
44 struct string_stream_fragment *frag_container;
46 va_list args_for_counting;
48 /* Make a copy because `vsnprintf` could change it */
49 va_copy(args_for_counting, args);
51 /* Need space for null byte. */
52 len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1;
54 va_end(args_for_counting);
56 frag_container = alloc_string_stream_fragment(stream->test,
59 if (IS_ERR(frag_container))
60 return PTR_ERR(frag_container);
62 len = vsnprintf(frag_container->fragment, len, fmt, args);
63 spin_lock(&stream->lock);
64 stream->length += len;
65 list_add_tail(&frag_container->node, &stream->fragments);
66 spin_unlock(&stream->lock);
71 int string_stream_add(struct string_stream *stream, const char *fmt, ...)
77 result = string_stream_vadd(stream, fmt, args);
83 static void string_stream_clear(struct string_stream *stream)
85 struct string_stream_fragment *frag_container, *frag_container_safe;
87 spin_lock(&stream->lock);
88 list_for_each_entry_safe(frag_container,
92 string_stream_fragment_destroy(stream->test, frag_container);
95 spin_unlock(&stream->lock);
98 char *string_stream_get_string(struct string_stream *stream)
100 struct string_stream_fragment *frag_container;
101 size_t buf_len = stream->length + 1; /* +1 for null byte. */
104 buf = kunit_kzalloc(stream->test, buf_len, stream->gfp);
108 spin_lock(&stream->lock);
109 list_for_each_entry(frag_container, &stream->fragments, node)
110 strlcat(buf, frag_container->fragment, buf_len);
111 spin_unlock(&stream->lock);
116 int string_stream_append(struct string_stream *stream,
117 struct string_stream *other)
119 const char *other_content;
121 other_content = string_stream_get_string(other);
126 return string_stream_add(stream, other_content);
129 bool string_stream_is_empty(struct string_stream *stream)
131 return list_empty(&stream->fragments);
134 struct string_stream_alloc_context {
139 struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
141 struct string_stream *stream;
143 stream = kunit_kzalloc(test, sizeof(*stream), gfp);
145 return ERR_PTR(-ENOMEM);
149 INIT_LIST_HEAD(&stream->fragments);
150 spin_lock_init(&stream->lock);
155 void string_stream_destroy(struct string_stream *stream)
157 string_stream_clear(stream);