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"
15 struct string_stream_fragment_alloc_context {
21 static int string_stream_fragment_init(struct kunit_resource *res,
24 struct string_stream_fragment_alloc_context *ctx = context;
25 struct string_stream_fragment *frag;
27 frag = kunit_kzalloc(ctx->test, sizeof(*frag), ctx->gfp);
31 frag->test = ctx->test;
32 frag->fragment = kunit_kmalloc(ctx->test, ctx->len, ctx->gfp);
41 static void string_stream_fragment_free(struct kunit_resource *res)
43 struct string_stream_fragment *frag = res->data;
45 list_del(&frag->node);
46 kunit_kfree(frag->test, frag->fragment);
47 kunit_kfree(frag->test, frag);
50 static struct string_stream_fragment *alloc_string_stream_fragment(
51 struct kunit *test, int len, gfp_t gfp)
53 struct string_stream_fragment_alloc_context context = {
59 return kunit_alloc_resource(test,
60 string_stream_fragment_init,
61 string_stream_fragment_free,
66 static int string_stream_fragment_destroy(struct string_stream_fragment *frag)
68 return kunit_destroy_resource(frag->test,
69 kunit_resource_instance_match,
73 int string_stream_vadd(struct string_stream *stream,
77 struct string_stream_fragment *frag_container;
79 va_list args_for_counting;
81 /* Make a copy because `vsnprintf` could change it */
82 va_copy(args_for_counting, args);
84 /* Need space for null byte. */
85 len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1;
87 va_end(args_for_counting);
89 frag_container = alloc_string_stream_fragment(stream->test,
95 len = vsnprintf(frag_container->fragment, len, fmt, args);
96 spin_lock(&stream->lock);
97 stream->length += len;
98 list_add_tail(&frag_container->node, &stream->fragments);
99 spin_unlock(&stream->lock);
104 int string_stream_add(struct string_stream *stream, const char *fmt, ...)
110 result = string_stream_vadd(stream, fmt, args);
116 static void string_stream_clear(struct string_stream *stream)
118 struct string_stream_fragment *frag_container, *frag_container_safe;
120 spin_lock(&stream->lock);
121 list_for_each_entry_safe(frag_container,
125 string_stream_fragment_destroy(frag_container);
128 spin_unlock(&stream->lock);
131 char *string_stream_get_string(struct string_stream *stream)
133 struct string_stream_fragment *frag_container;
134 size_t buf_len = stream->length + 1; /* +1 for null byte. */
137 buf = kunit_kzalloc(stream->test, buf_len, stream->gfp);
141 spin_lock(&stream->lock);
142 list_for_each_entry(frag_container, &stream->fragments, node)
143 strlcat(buf, frag_container->fragment, buf_len);
144 spin_unlock(&stream->lock);
149 int string_stream_append(struct string_stream *stream,
150 struct string_stream *other)
152 const char *other_content;
154 other_content = string_stream_get_string(other);
159 return string_stream_add(stream, other_content);
162 bool string_stream_is_empty(struct string_stream *stream)
164 return list_empty(&stream->fragments);
167 struct string_stream_alloc_context {
172 static int string_stream_init(struct kunit_resource *res, void *context)
174 struct string_stream *stream;
175 struct string_stream_alloc_context *ctx = context;
177 stream = kunit_kzalloc(ctx->test, sizeof(*stream), ctx->gfp);
182 stream->gfp = ctx->gfp;
183 stream->test = ctx->test;
184 INIT_LIST_HEAD(&stream->fragments);
185 spin_lock_init(&stream->lock);
190 static void string_stream_free(struct kunit_resource *res)
192 struct string_stream *stream = res->data;
194 string_stream_clear(stream);
197 struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
199 struct string_stream_alloc_context context = {
204 return kunit_alloc_resource(test,
211 int string_stream_destroy(struct string_stream *stream)
213 return kunit_destroy_resource(stream->test,
214 kunit_resource_instance_match,