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