Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / lib / kunit / string-stream.c
blob54f4fdcbfac87dcfc30b8c7da7cde62ff945ea02
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <kunit/static_stub.h>
10 #include <kunit/test.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
14 #include "string-stream.h"
17 static struct string_stream_fragment *alloc_string_stream_fragment(int len, gfp_t gfp)
19 struct string_stream_fragment *frag;
21 frag = kzalloc(sizeof(*frag), gfp);
22 if (!frag)
23 return ERR_PTR(-ENOMEM);
25 frag->fragment = kmalloc(len, gfp);
26 if (!frag->fragment) {
27 kfree(frag);
28 return ERR_PTR(-ENOMEM);
31 return frag;
34 static void string_stream_fragment_destroy(struct string_stream_fragment *frag)
36 list_del(&frag->node);
37 kfree(frag->fragment);
38 kfree(frag);
41 int string_stream_vadd(struct string_stream *stream,
42 const char *fmt,
43 va_list args)
45 struct string_stream_fragment *frag_container;
46 int buf_len, result_len;
47 va_list args_for_counting;
49 /* Make a copy because `vsnprintf` could change it */
50 va_copy(args_for_counting, args);
52 /* Evaluate length of formatted string */
53 buf_len = vsnprintf(NULL, 0, fmt, args_for_counting);
55 va_end(args_for_counting);
57 if (buf_len == 0)
58 return 0;
60 /* Reserve one extra for possible appended newline. */
61 if (stream->append_newlines)
62 buf_len++;
64 /* Need space for null byte. */
65 buf_len++;
67 frag_container = alloc_string_stream_fragment(buf_len, stream->gfp);
68 if (IS_ERR(frag_container))
69 return PTR_ERR(frag_container);
71 if (stream->append_newlines) {
72 /* Don't include reserved newline byte in writeable length. */
73 result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args);
75 /* Append newline if necessary. */
76 if (frag_container->fragment[result_len - 1] != '\n')
77 result_len = strlcat(frag_container->fragment, "\n", buf_len);
78 } else {
79 result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args);
82 spin_lock(&stream->lock);
83 stream->length += result_len;
84 list_add_tail(&frag_container->node, &stream->fragments);
85 spin_unlock(&stream->lock);
87 return 0;
90 int string_stream_add(struct string_stream *stream, const char *fmt, ...)
92 va_list args;
93 int result;
95 va_start(args, fmt);
96 result = string_stream_vadd(stream, fmt, args);
97 va_end(args);
99 return result;
102 void string_stream_clear(struct string_stream *stream)
104 struct string_stream_fragment *frag_container, *frag_container_safe;
106 spin_lock(&stream->lock);
107 list_for_each_entry_safe(frag_container,
108 frag_container_safe,
109 &stream->fragments,
110 node) {
111 string_stream_fragment_destroy(frag_container);
113 stream->length = 0;
114 spin_unlock(&stream->lock);
117 char *string_stream_get_string(struct string_stream *stream)
119 struct string_stream_fragment *frag_container;
120 size_t buf_len = stream->length + 1; /* +1 for null byte. */
121 char *buf;
123 buf = kzalloc(buf_len, stream->gfp);
124 if (!buf)
125 return NULL;
127 spin_lock(&stream->lock);
128 list_for_each_entry(frag_container, &stream->fragments, node)
129 strlcat(buf, frag_container->fragment, buf_len);
130 spin_unlock(&stream->lock);
132 return buf;
135 int string_stream_append(struct string_stream *stream,
136 struct string_stream *other)
138 const char *other_content;
139 int ret;
141 other_content = string_stream_get_string(other);
143 if (!other_content)
144 return -ENOMEM;
146 ret = string_stream_add(stream, other_content);
147 kfree(other_content);
149 return ret;
152 bool string_stream_is_empty(struct string_stream *stream)
154 return list_empty(&stream->fragments);
157 struct string_stream *alloc_string_stream(gfp_t gfp)
159 struct string_stream *stream;
161 stream = kzalloc(sizeof(*stream), gfp);
162 if (!stream)
163 return ERR_PTR(-ENOMEM);
165 stream->gfp = gfp;
166 INIT_LIST_HEAD(&stream->fragments);
167 spin_lock_init(&stream->lock);
169 return stream;
172 void string_stream_destroy(struct string_stream *stream)
174 KUNIT_STATIC_STUB_REDIRECT(string_stream_destroy, stream);
176 if (IS_ERR_OR_NULL(stream))
177 return;
179 string_stream_clear(stream);
180 kfree(stream);
183 static void resource_free_string_stream(void *p)
185 struct string_stream *stream = p;
187 string_stream_destroy(stream);
190 struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
192 struct string_stream *stream;
194 stream = alloc_string_stream(gfp);
195 if (IS_ERR(stream))
196 return stream;
198 if (kunit_add_action_or_reset(test, resource_free_string_stream, stream) != 0)
199 return ERR_PTR(-ENOMEM);
201 return stream;
204 void kunit_free_string_stream(struct kunit *test, struct string_stream *stream)
206 kunit_release_action(test, resource_free_string_stream, (void *)stream);