string-stream.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #ifndef _KUNIT_STRING_STREAM_H
  9. #define _KUNIT_STRING_STREAM_H
  10. #include <linux/spinlock.h>
  11. #include <linux/types.h>
  12. #include <linux/stdarg.h>
  13. struct string_stream_fragment {
  14. struct list_head node;
  15. char *fragment;
  16. };
  17. struct string_stream {
  18. size_t length;
  19. struct list_head fragments;
  20. /* length and fragments are protected by this lock */
  21. spinlock_t lock;
  22. struct kunit *test;
  23. gfp_t gfp;
  24. };
  25. struct kunit;
  26. struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp);
  27. int __printf(2, 3) string_stream_add(struct string_stream *stream,
  28. const char *fmt, ...);
  29. int __printf(2, 0) string_stream_vadd(struct string_stream *stream,
  30. const char *fmt,
  31. va_list args);
  32. char *string_stream_get_string(struct string_stream *stream);
  33. int string_stream_append(struct string_stream *stream,
  34. struct string_stream *other);
  35. bool string_stream_is_empty(struct string_stream *stream);
  36. void string_stream_destroy(struct string_stream *stream);
  37. #endif /* _KUNIT_STRING_STREAM_H */