string-stream.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <kunit/static_stub.h>
  9. #include <kunit/test.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include "string-stream.h"
  13. static struct string_stream_fragment *alloc_string_stream_fragment(int len, gfp_t gfp)
  14. {
  15. struct string_stream_fragment *frag;
  16. frag = kzalloc_obj(*frag, gfp);
  17. if (!frag)
  18. return ERR_PTR(-ENOMEM);
  19. frag->fragment = kmalloc(len, gfp);
  20. if (!frag->fragment) {
  21. kfree(frag);
  22. return ERR_PTR(-ENOMEM);
  23. }
  24. return frag;
  25. }
  26. static void string_stream_fragment_destroy(struct string_stream_fragment *frag)
  27. {
  28. list_del(&frag->node);
  29. kfree(frag->fragment);
  30. kfree(frag);
  31. }
  32. int string_stream_vadd(struct string_stream *stream,
  33. const char *fmt,
  34. va_list args)
  35. {
  36. struct string_stream_fragment *frag_container;
  37. int buf_len, result_len;
  38. va_list args_for_counting;
  39. /* Make a copy because `vsnprintf` could change it */
  40. va_copy(args_for_counting, args);
  41. /* Evaluate length of formatted string */
  42. buf_len = vsnprintf(NULL, 0, fmt, args_for_counting);
  43. va_end(args_for_counting);
  44. if (buf_len == 0)
  45. return 0;
  46. /* Reserve one extra for possible appended newline. */
  47. if (stream->append_newlines)
  48. buf_len++;
  49. /* Need space for null byte. */
  50. buf_len++;
  51. frag_container = alloc_string_stream_fragment(buf_len, stream->gfp);
  52. if (IS_ERR(frag_container))
  53. return PTR_ERR(frag_container);
  54. if (stream->append_newlines) {
  55. /* Don't include reserved newline byte in writeable length. */
  56. result_len = vsnprintf(frag_container->fragment, buf_len - 1, fmt, args);
  57. /* Append newline if necessary. */
  58. if (frag_container->fragment[result_len - 1] != '\n')
  59. result_len = strlcat(frag_container->fragment, "\n", buf_len);
  60. } else {
  61. result_len = vsnprintf(frag_container->fragment, buf_len, fmt, args);
  62. }
  63. spin_lock(&stream->lock);
  64. stream->length += result_len;
  65. list_add_tail(&frag_container->node, &stream->fragments);
  66. spin_unlock(&stream->lock);
  67. return 0;
  68. }
  69. int string_stream_add(struct string_stream *stream, const char *fmt, ...)
  70. {
  71. va_list args;
  72. int result;
  73. va_start(args, fmt);
  74. result = string_stream_vadd(stream, fmt, args);
  75. va_end(args);
  76. return result;
  77. }
  78. void string_stream_clear(struct string_stream *stream)
  79. {
  80. struct string_stream_fragment *frag_container, *frag_container_safe;
  81. spin_lock(&stream->lock);
  82. list_for_each_entry_safe(frag_container,
  83. frag_container_safe,
  84. &stream->fragments,
  85. node) {
  86. string_stream_fragment_destroy(frag_container);
  87. }
  88. stream->length = 0;
  89. spin_unlock(&stream->lock);
  90. }
  91. char *string_stream_get_string(struct string_stream *stream)
  92. {
  93. struct string_stream_fragment *frag_container;
  94. size_t buf_len = stream->length + 1; /* +1 for null byte. */
  95. char *buf;
  96. buf = kzalloc(buf_len, stream->gfp);
  97. if (!buf)
  98. return NULL;
  99. spin_lock(&stream->lock);
  100. list_for_each_entry(frag_container, &stream->fragments, node)
  101. strlcat(buf, frag_container->fragment, buf_len);
  102. spin_unlock(&stream->lock);
  103. return buf;
  104. }
  105. int string_stream_append(struct string_stream *stream,
  106. struct string_stream *other)
  107. {
  108. const char *other_content;
  109. int ret;
  110. other_content = string_stream_get_string(other);
  111. if (!other_content)
  112. return -ENOMEM;
  113. ret = string_stream_add(stream, other_content);
  114. kfree(other_content);
  115. return ret;
  116. }
  117. bool string_stream_is_empty(struct string_stream *stream)
  118. {
  119. return list_empty(&stream->fragments);
  120. }
  121. struct string_stream *alloc_string_stream(gfp_t gfp)
  122. {
  123. struct string_stream *stream;
  124. stream = kzalloc_obj(*stream, gfp);
  125. if (!stream)
  126. return ERR_PTR(-ENOMEM);
  127. stream->gfp = gfp;
  128. INIT_LIST_HEAD(&stream->fragments);
  129. spin_lock_init(&stream->lock);
  130. return stream;
  131. }
  132. void string_stream_destroy(struct string_stream *stream)
  133. {
  134. KUNIT_STATIC_STUB_REDIRECT(string_stream_destroy, stream);
  135. if (IS_ERR_OR_NULL(stream))
  136. return;
  137. string_stream_clear(stream);
  138. kfree(stream);
  139. }
  140. static void resource_free_string_stream(void *p)
  141. {
  142. struct string_stream *stream = p;
  143. string_stream_destroy(stream);
  144. }
  145. struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
  146. {
  147. struct string_stream *stream;
  148. stream = alloc_string_stream(gfp);
  149. if (IS_ERR(stream))
  150. return stream;
  151. if (kunit_add_action_or_reset(test, resource_free_string_stream, stream) != 0)
  152. return ERR_PTR(-ENOMEM);
  153. return stream;
  154. }
  155. void kunit_free_string_stream(struct kunit *test, struct string_stream *stream)
  156. {
  157. kunit_release_action(test, resource_free_string_stream, (void *)stream);
  158. }