kunit: added reference counting to string-stream
Change-Id: If8f85562681c0b7a70661ce95d281a3525fba87f
Signed-off-by: Felix Guo <felixguo@google.com>
diff --git a/include/test/string-stream.h b/include/test/string-stream.h
index db46e33..814f0bb 100644
--- a/include/test/string-stream.h
+++ b/include/test/string-stream.h
@@ -3,6 +3,7 @@
#include <linux/types.h>
#include <linux/spinlock.h>
+#include <linux/kref.h>
#include <stdarg.h>
struct string_stream_fragment {
@@ -16,6 +17,7 @@
/* length and fragments are protected by this lock */
struct spinlock lock;
+ struct kref refcount;
int (*add)(struct string_stream *this, const char *fmt, ...);
int (*vadd)(struct string_stream *this, const char *fmt, va_list args);
char *(*get_string)(struct string_stream *this);
@@ -27,4 +29,8 @@
void destroy_string_stream(struct string_stream *stream);
+void string_stream_get(struct string_stream *stream);
+
+int string_stream_put(struct string_stream *stream);
+
#endif /* _TEST_STRING_STREAM_H */
diff --git a/test/string-stream.c b/test/string-stream.c
index 993d95d..f91bb60 100644
--- a/test/string-stream.c
+++ b/test/string-stream.c
@@ -102,6 +102,14 @@
kfree(stream);
}
+static void string_stream_destroy(struct kref *kref)
+{
+ struct string_stream *stream = container_of(kref,
+ struct string_stream,
+ refcount);
+ destroy_string_stream(stream);
+}
+
struct string_stream *new_string_stream(void)
{
struct string_stream *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
@@ -111,6 +119,7 @@
INIT_LIST_HEAD(&stream->fragments);
spin_lock_init(&stream->lock);
+ kref_init(&stream->refcount);
stream->add = string_stream_add;
stream->vadd = string_stream_vadd;
stream->get_string = string_stream_get_string;
@@ -118,3 +127,13 @@
stream->is_empty = string_stream_is_empty;
return stream;
}
+
+void string_stream_get(struct string_stream *stream)
+{
+ kref_get(&stream->refcount);
+}
+
+int string_stream_put(struct string_stream *stream)
+{
+ return kref_put(&stream->refcount, &string_stream_destroy);
+}
\ No newline at end of file