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