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 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 return ERR_PTR(-ENOMEM); 28 29 return frag; 30 } 31 32 static void string_stream_fragment_destroy(struct kunit *test, 33 struct string_stream_fragment *frag) 34 { 35 list_del(&frag->node); 36 kunit_kfree(test, frag->fragment); 37 kunit_kfree(test, frag); 38 } 39 40 int string_stream_vadd(struct string_stream *stream, 41 const char *fmt, 42 va_list args) 43 { 44 struct string_stream_fragment *frag_container; 45 int len; 46 va_list args_for_counting; 47 48 /* Make a copy because `vsnprintf` could change it */ 49 va_copy(args_for_counting, args); 50 51 /* Need space for null byte. */ 52 len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1; 53 54 va_end(args_for_counting); 55 56 frag_container = alloc_string_stream_fragment(stream->test, 57 len, 58 stream->gfp); 59 if (!frag_container) 60 return -ENOMEM; 61 62 len = vsnprintf(frag_container->fragment, len, fmt, args); 63 spin_lock(&stream->lock); 64 stream->length += len; 65 list_add_tail(&frag_container->node, &stream->fragments); 66 spin_unlock(&stream->lock); 67 68 return 0; 69 } 70 71 int string_stream_add(struct string_stream *stream, const char *fmt, ...) 72 { 73 va_list args; 74 int result; 75 76 va_start(args, fmt); 77 result = string_stream_vadd(stream, fmt, args); 78 va_end(args); 79 80 return result; 81 } 82 83 static void string_stream_clear(struct string_stream *stream) 84 { 85 struct string_stream_fragment *frag_container, *frag_container_safe; 86 87 spin_lock(&stream->lock); 88 list_for_each_entry_safe(frag_container, 89 frag_container_safe, 90 &stream->fragments, 91 node) { 92 string_stream_fragment_destroy(stream->test, frag_container); 93 } 94 stream->length = 0; 95 spin_unlock(&stream->lock); 96 } 97 98 char *string_stream_get_string(struct string_stream *stream) 99 { 100 struct string_stream_fragment *frag_container; 101 size_t buf_len = stream->length + 1; /* +1 for null byte. */ 102 char *buf; 103 104 buf = kunit_kzalloc(stream->test, buf_len, stream->gfp); 105 if (!buf) 106 return NULL; 107 108 spin_lock(&stream->lock); 109 list_for_each_entry(frag_container, &stream->fragments, node) 110 strlcat(buf, frag_container->fragment, buf_len); 111 spin_unlock(&stream->lock); 112 113 return buf; 114 } 115 116 int string_stream_append(struct string_stream *stream, 117 struct string_stream *other) 118 { 119 const char *other_content; 120 121 other_content = string_stream_get_string(other); 122 123 if (!other_content) 124 return -ENOMEM; 125 126 return string_stream_add(stream, other_content); 127 } 128 129 bool string_stream_is_empty(struct string_stream *stream) 130 { 131 return list_empty(&stream->fragments); 132 } 133 134 struct string_stream_alloc_context { 135 struct kunit *test; 136 gfp_t gfp; 137 }; 138 139 struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) 140 { 141 struct string_stream *stream; 142 143 stream = kunit_kzalloc(test, sizeof(*stream), gfp); 144 if (!stream) 145 return ERR_PTR(-ENOMEM); 146 147 stream->gfp = gfp; 148 stream->test = test; 149 INIT_LIST_HEAD(&stream->fragments); 150 spin_lock_init(&stream->lock); 151 152 return stream; 153 } 154 155 void string_stream_destroy(struct string_stream *stream) 156 { 157 string_stream_clear(stream); 158 } 159