at commit afb1ecfe

1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * C++ stream style string builder used in KUnit for building messages.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8
9 #include <kunit/static_stub.h>
10 #include <kunit/test.h>
11 #include <linux/list.h>
12 #include <linux/seq_buf.h>
13 #include <linux/slab.h>
14
15 #include "string-stream.h"
16
17
18 static struct string_stream_fragment *alloc_string_stream_fragment(int len, gfp_t gfp)
19 {
20 struct string_stream_fragment *frag;
21
22 frag = kzalloc_obj(*frag, gfp);
23 if (!frag)
24 return ERR_PTR(-ENOMEM);
25
26 frag->fragment = kmalloc(len, gfp);
27 if (!frag->fragment) {
28 kfree(frag);
29 return ERR_PTR(-ENOMEM);
30 }
31
32 return frag;
33 }
34
35 static void string_stream_fragment_destroy(struct string_stream_fragment *frag)
36 {
37 list_del(&frag->node);
38 kfree(frag->fragment);
39 kfree(frag);
40 }
41
42 int string_stream_vadd(struct string_stream *stream,
43 const char *fmt,
44 va_list args)
45 {
46 struct string_stream_fragment *frag_container;
47 int buf_len, result_len;
48 va_list args_for_counting;
49
50 /* Make a copy because `vsnprintf` could change it */
51 va_copy(args_for_counting, args);
52
53 /* Evaluate length of formatted string */
54 buf_len = vsnprintf(NULL, 0, fmt, args_for_counting);
55
56 va_end(args_for_counting);
57
58 if (buf_len == 0)
59 return 0;
60
61 /* Reserve one extra for possible appended newline. */
62 if (stream->append_newlines)
63 buf_len++;
64
65 /* Need space for null byte. */
66 buf_len++;
67
68 frag_container = alloc_string_stream_fragment(buf_len, stream->gfp);
69 if (IS_ERR(frag_container))
70 return PTR_ERR(frag_container);
71
72 if (stream->append_newlines) {
73 /* Don't include reserved newline byte in writeable length. */
74 result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args);
75
76 /* Append newline if necessary. */
77 if (frag_container->fragment[result_len - 1] != '\n')
78 result_len += strscpy(frag_container->fragment + result_len,
79 "\n", buf_len - result_len);
80 } else {
81 result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args);
82 }
83
84 spin_lock(&stream->lock);
85 stream->length += result_len;
86 list_add_tail(&frag_container->node, &stream->fragments);
87 spin_unlock(&stream->lock);
88
89 return 0;
90 }
91
92 int string_stream_add(struct string_stream *stream, const char *fmt, ...)
93 {
94 va_list args;
95 int result;
96
97 va_start(args, fmt);
98 result = string_stream_vadd(stream, fmt, args);
99 va_end(args);
100
101 return result;
102 }
103
104 void string_stream_clear(struct string_stream *stream)
105 {
106 struct string_stream_fragment *frag_container, *frag_container_safe;
107
108 spin_lock(&stream->lock);
109 list_for_each_entry_safe(frag_container,
110 frag_container_safe,
111 &stream->fragments,
112 node) {
113 string_stream_fragment_destroy(frag_container);
114 }
115 stream->length = 0;
116 spin_unlock(&stream->lock);
117 }
118
119 char *string_stream_get_string(struct string_stream *stream)
120 {
121 struct string_stream_fragment *frag_container;
122 size_t buf_len = stream->length + 1; /* +1 for null byte. */
123 struct seq_buf sb;
124 char *buf;
125
126 buf = kzalloc(buf_len, stream->gfp);
127 if (!buf)
128 return NULL;
129
130 seq_buf_init(&sb, buf, buf_len);
131
132 spin_lock(&stream->lock);
133 list_for_each_entry(frag_container, &stream->fragments, node)
134 seq_buf_puts(&sb, frag_container->fragment);
135 spin_unlock(&stream->lock);
136
137 return buf;
138 }
139
140 int string_stream_append(struct string_stream *stream,
141 struct string_stream *other)
142 {
143 const char *other_content;
144 int ret;
145
146 other_content = string_stream_get_string(other);
147
148 if (!other_content)
149 return -ENOMEM;
150
151 ret = string_stream_add(stream, other_content);
152 kfree(other_content);
153
154 return ret;
155 }
156
157 bool string_stream_is_empty(struct string_stream *stream)
158 {
159 return list_empty(&stream->fragments);
160 }
161
162 struct string_stream *alloc_string_stream(gfp_t gfp)
163 {
164 struct string_stream *stream;
165
166 stream = kzalloc_obj(*stream, gfp);
167 if (!stream)
168 return ERR_PTR(-ENOMEM);
169
170 stream->gfp = gfp;
171 INIT_LIST_HEAD(&stream->fragments);
172 spin_lock_init(&stream->lock);
173
174 return stream;
175 }
176
177 void string_stream_destroy(struct string_stream *stream)
178 {
179 KUNIT_STATIC_STUB_REDIRECT(string_stream_destroy, stream);
180
181 if (IS_ERR_OR_NULL(stream))
182 return;
183
184 string_stream_clear(stream);
185 kfree(stream);
186 }
187
188 static void resource_free_string_stream(void *p)
189 {
190 struct string_stream *stream = p;
191
192 string_stream_destroy(stream);
193 }
194
195 struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
196 {
197 struct string_stream *stream;
198
199 stream = alloc_string_stream(gfp);
200 if (IS_ERR(stream))
201 return stream;
202
203 if (kunit_add_action_or_reset(test, resource_free_string_stream, stream) != 0)
204 return ERR_PTR(-ENOMEM);
205
206 return stream;
207 }
208
209 void kunit_free_string_stream(struct kunit *test, struct string_stream *stream)
210 {
211 kunit_release_action(test, resource_free_string_stream, (void *)stream);
212 }