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/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
);
23 return ERR_PTR(-ENOMEM
);
25 frag
->fragment
= kmalloc(len
, gfp
);
26 if (!frag
->fragment
) {
28 return ERR_PTR(-ENOMEM
);
34 static void string_stream_fragment_destroy(struct string_stream_fragment
*frag
)
36 list_del(&frag
->node
);
37 kfree(frag
->fragment
);
41 int string_stream_vadd(struct string_stream
*stream
,
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
);
60 /* Reserve one extra for possible appended newline. */
61 if (stream
->append_newlines
)
64 /* Need space for null byte. */
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
);
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
);
90 int string_stream_add(struct string_stream
*stream
, const char *fmt
, ...)
96 result
= string_stream_vadd(stream
, fmt
, args
);
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
,
111 string_stream_fragment_destroy(frag_container
);
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. */
123 buf
= kzalloc(buf_len
, stream
->gfp
);
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
);
135 int string_stream_append(struct string_stream
*stream
,
136 struct string_stream
*other
)
138 const char *other_content
;
141 other_content
= string_stream_get_string(other
);
146 ret
= string_stream_add(stream
, other_content
);
147 kfree(other_content
);
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
);
163 return ERR_PTR(-ENOMEM
);
166 INIT_LIST_HEAD(&stream
->fragments
);
167 spin_lock_init(&stream
->lock
);
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
))
179 string_stream_clear(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
);
198 if (kunit_add_action_or_reset(test
, resource_free_string_stream
, stream
) != 0)
199 return ERR_PTR(-ENOMEM
);
204 void kunit_free_string_stream(struct kunit
*test
, struct string_stream
*stream
)
206 kunit_release_action(test
, resource_free_string_stream
, (void *)stream
);