]> git.sesse.net Git - nageru/blobdiff - ref_counted_frame.h
Refcount the input frames directly instead of trying to free them after-the-fact...
[nageru] / ref_counted_frame.h
diff --git a/ref_counted_frame.h b/ref_counted_frame.h
new file mode 100644 (file)
index 0000000..5912ce8
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _REF_COUNTED_FRAME_H
+#define _REF_COUNTED_FRAME_H 1
+
+// A wrapper around FrameAllocator::Frame that is automatically refcounted;
+// when the refcount goes to zero, the frame is given back to the allocator.
+//
+// Note that the important point isn't really the pointer to the Frame itself,
+// it's the resources it's representing that need to go back to the allocator.
+
+#include "bmusb.h"
+
+void release_refcounted_frame(FrameAllocator::Frame *frame);
+
+typedef std::shared_ptr<FrameAllocator::Frame> RefCountedFrameBase;
+
+class RefCountedFrame : public RefCountedFrameBase {
+public:
+       RefCountedFrame() {}
+
+       RefCountedFrame(const FrameAllocator::Frame &frame)
+               : RefCountedFrameBase(new FrameAllocator::Frame(frame), release_refcounted_frame) {}
+};
+
+#endif  // !defined(_REF_COUNTED_FRAME_H)