string-stream.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #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. gfp_t gfp;
  23. bool append_newlines;
  24. };
  25. struct kunit;
  26. struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp);
  27. void kunit_free_string_stream(struct kunit *test, struct string_stream *stream);
  28. struct string_stream *alloc_string_stream(gfp_t gfp);
  29. void free_string_stream(struct string_stream *stream);
  30. int __printf(2, 3) string_stream_add(struct string_stream *stream,
  31. const char *fmt, ...);
  32. int __printf(2, 0) string_stream_vadd(struct string_stream *stream,
  33. const char *fmt,
  34. va_list args);
  35. void string_stream_clear(struct string_stream *stream);
  36. char *string_stream_get_string(struct string_stream *stream);
  37. int string_stream_append(struct string_stream *stream,
  38. struct string_stream *other);
  39. bool string_stream_is_empty(struct string_stream *stream);
  40. void string_stream_destroy(struct string_stream *stream);
  41. static inline void string_stream_set_append_newlines(struct string_stream *stream,
  42. bool append_newlines)
  43. {
  44. stream->append_newlines = append_newlines;
  45. }
  46. #endif /* _KUNIT_STRING_STREAM_H */