^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) #ifndef _KUNIT_STRING_STREAM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define _KUNIT_STRING_STREAM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct string_stream_fragment {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct kunit *test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) char *fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct string_stream {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) size_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct list_head fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* length and fragments are protected by this lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct kunit *test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct kunit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int __printf(2, 3) string_stream_add(struct string_stream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const char *fmt, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int string_stream_vadd(struct string_stream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) va_list args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) char *string_stream_get_string(struct string_stream *stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int string_stream_append(struct string_stream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct string_stream *other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bool string_stream_is_empty(struct string_stream *stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int string_stream_destroy(struct string_stream *stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #endif /* _KUNIT_STRING_STREAM_H */