18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * C++ stream style string builder used in KUnit for building messages.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC.
68c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _KUNIT_STRING_STREAM_H
108c2ecf20Sopenharmony_ci#define _KUNIT_STRING_STREAM_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <stdarg.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct string_stream_fragment {
178c2ecf20Sopenharmony_ci	struct kunit *test;
188c2ecf20Sopenharmony_ci	struct list_head node;
198c2ecf20Sopenharmony_ci	char *fragment;
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct string_stream {
238c2ecf20Sopenharmony_ci	size_t length;
248c2ecf20Sopenharmony_ci	struct list_head fragments;
258c2ecf20Sopenharmony_ci	/* length and fragments are protected by this lock */
268c2ecf20Sopenharmony_ci	spinlock_t lock;
278c2ecf20Sopenharmony_ci	struct kunit *test;
288c2ecf20Sopenharmony_ci	gfp_t gfp;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct kunit;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciint __printf(2, 3) string_stream_add(struct string_stream *stream,
368c2ecf20Sopenharmony_ci				     const char *fmt, ...);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint string_stream_vadd(struct string_stream *stream,
398c2ecf20Sopenharmony_ci		       const char *fmt,
408c2ecf20Sopenharmony_ci		       va_list args);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cichar *string_stream_get_string(struct string_stream *stream);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciint string_stream_append(struct string_stream *stream,
458c2ecf20Sopenharmony_ci			 struct string_stream *other);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cibool string_stream_is_empty(struct string_stream *stream);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciint string_stream_destroy(struct string_stream *stream);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#endif /* _KUNIT_STRING_STREAM_H */
52