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 <[email protected]> 7 */ 8 9 #include <kunit/test.h> 10 #include <linux/list.h> 11 #include <linux/slab.h> 12 13 #include "string-stream.h" 14 15 16 static struct string_stream_fragment *alloc_string_stream_fragment( 17 struct kunit *test, int len, gfp_t gfp) 18 { 19 struct string_stream_fragment *frag; 20 21 frag = kunit_kzalloc(test, sizeof(*frag), gfp); 22 if (!frag) 23 return ERR_PTR(-ENOMEM); 24 25 frag->fragment = kunit_kmalloc(test, len, gfp); 26 if (!frag->fragment) { 27 kunit_kfree(test, frag); 28 return ERR_PTR(-ENOMEM); 29 } 30 31 return frag; 32 } 33 34 static void string_stream_fragment_destroy(struct kunit *test, 35 struct string_stream_fragment *frag) 36 { 37 list_del(&frag->node); 38 kunit_kfree(test, frag->fragment); 39 kunit_kfree(test, 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(stream->test, 69 buf_len, 70 stream->gfp); 71 if (IS_ERR(frag_container)) 72 return PTR_ERR(frag_container); 73 74 if (stream->append_newlines) { 75 /* Don't include reserved newline byte in writeable length. */ 76 result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args); 77 78 /* Append newline if necessary. */ 79 if (frag_container->fragment[result_len - 1] != '\n') 80 result_len = strlcat(frag_container->fragment, "\n", buf_len); 81 } else { 82 result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args); 83 } 84 85 spin_lock(&stream->lock); 86 stream->length += result_len; 87 list_add_tail(&frag_container->node, &stream->fragments); 88 spin_unlock(&stream->lock); 89 90 return 0; 91 } 92 93 int string_stream_add(struct string_stream *stream, const char *fmt, ...) 94 { 95 va_list args; 96 int result; 97 98 va_start(args, fmt); 99 result = string_stream_vadd(stream, fmt, args); 100 va_end(args); 101 102 return result; 103 } 104 105 static void string_stream_clear(struct string_stream *stream) 106 { 107 struct string_stream_fragment *frag_container, *frag_container_safe; 108 109 spin_lock(&stream->lock); 110 list_for_each_entry_safe(frag_container, 111 frag_container_safe, 112 &stream->fragments, 113 node) { 114 string_stream_fragment_destroy(stream->test, frag_container); 115 } 116 stream->length = 0; 117 spin_unlock(&stream->lock); 118 } 119 120 char *string_stream_get_string(struct string_stream *stream) 121 { 122 struct string_stream_fragment *frag_container; 123 size_t buf_len = stream->length + 1; /* +1 for null byte. */ 124 char *buf; 125 126 buf = kunit_kzalloc(stream->test, buf_len, stream->gfp); 127 if (!buf) 128 return NULL; 129 130 spin_lock(&stream->lock); 131 list_for_each_entry(frag_container, &stream->fragments, node) 132 strlcat(buf, frag_container->fragment, buf_len); 133 spin_unlock(&stream->lock); 134 135 return buf; 136 } 137 138 int string_stream_append(struct string_stream *stream, 139 struct string_stream *other) 140 { 141 const char *other_content; 142 143 other_content = string_stream_get_string(other); 144 145 if (!other_content) 146 return -ENOMEM; 147 148 return string_stream_add(stream, other_content); 149 } 150 151 bool string_stream_is_empty(struct string_stream *stream) 152 { 153 return list_empty(&stream->fragments); 154 } 155 156 struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) 157 { 158 struct string_stream *stream; 159 160 stream = kunit_kzalloc(test, sizeof(*stream), gfp); 161 if (!stream) 162 return ERR_PTR(-ENOMEM); 163 164 stream->gfp = gfp; 165 stream->test = test; 166 INIT_LIST_HEAD(&stream->fragments); 167 spin_lock_init(&stream->lock); 168 169 return stream; 170 } 171 172 void string_stream_destroy(struct string_stream *stream) 173 { 174 string_stream_clear(stream); 175 } 176