@@ -100,20 +100,35 @@ impl HardwareBuffer {
100100 }
101101 }
102102
103- /// Adopts the raw pointer and wraps it in a Rust AHardwareBuffer.
104- ///
105- /// # Errors
106- ///
107- /// Will panic if buffer_ptr is null.
103+ /// Adopts the given raw pointer and wraps it in a Rust HardwareBuffer.
108104 ///
109105 /// # Safety
110106 ///
111- /// This function adopts the pointer but does NOT increment the refcount on the buffer. If the
112- /// caller uses the pointer after the created object is dropped it will cause a memory leak.
107+ /// This function takes ownership of the pointer and does NOT increment the refcount on the
108+ /// buffer. If the caller uses the pointer after the created object is dropped it will cause
109+ /// undefined behaviour. If the caller wants to continue using the pointer after calling this
110+ /// then use [`clone_from_raw`](Self::clone_from_raw) instead.
113111 pub unsafe fn from_raw ( buffer_ptr : NonNull < AHardwareBuffer > ) -> Self {
114112 Self ( buffer_ptr)
115113 }
116114
115+ /// Creates a new Rust HardwareBuffer to wrap the given AHardwareBuffer without taking ownership
116+ /// of it.
117+ ///
118+ /// Unlike [`from_raw`](Self::from_raw) this method will increment the refcount on the buffer.
119+ /// This means that the caller can continue to use the raw buffer it passed in, and must call
120+ /// [`AHardwareBuffer_release`](ffi::AHardwareBuffer_release) when it is finished with it to
121+ /// avoid a memory leak.
122+ ///
123+ /// # Safety
124+ ///
125+ /// The buffer pointer must point to a valid `AHardwareBuffer`.
126+ pub unsafe fn clone_from_raw ( buffer : NonNull < AHardwareBuffer > ) -> Self {
127+ // SAFETY: The caller guarantees that the AHardwareBuffer pointer is valid.
128+ unsafe { ffi:: AHardwareBuffer_acquire ( buffer. as_ptr ( ) ) } ;
129+ Self ( buffer)
130+ }
131+
117132 /// Get the internal |AHardwareBuffer| pointer without decrementing the refcount. This can
118133 /// be used to provide a pointer to the AHB for a C/C++ API over the FFI.
119134 pub fn into_raw ( self ) -> NonNull < AHardwareBuffer > {
0 commit comments