Adding utility for reusing the subspan buffer implementation.
This allows for wrapper-hosted storage (with caveats).
diff --git a/iree/hal/buffer.c b/iree/hal/buffer.c
index ceb0b2f..3af50a6 100644
--- a/iree/hal/buffer.c
+++ b/iree/hal/buffer.c
@@ -75,6 +75,27 @@
static const iree_hal_buffer_vtable_t iree_hal_subspan_buffer_vtable;
+IREE_API_EXPORT void iree_hal_subspan_buffer_initialize(
+ iree_hal_buffer_t* allocated_buffer, iree_device_size_t byte_offset,
+ iree_device_size_t byte_length, iree_hal_allocator_t* device_allocator,
+ iree_allocator_t host_allocator, iree_hal_buffer_t* out_buffer) {
+ IREE_ASSERT_ARGUMENT(allocated_buffer);
+ IREE_ASSERT_ARGUMENT(out_buffer);
+ iree_hal_buffer_initialize(host_allocator, device_allocator, allocated_buffer,
+ allocated_buffer->allocation_size, byte_offset,
+ byte_length, allocated_buffer->memory_type,
+ allocated_buffer->allowed_access,
+ allocated_buffer->allowed_usage,
+ &iree_hal_subspan_buffer_vtable, out_buffer);
+}
+
+IREE_API_EXPORT void iree_hal_subspan_buffer_deinitialize(
+ iree_hal_buffer_t* buffer) {
+ IREE_ASSERT_ARGUMENT(buffer);
+ iree_hal_buffer_release(buffer->allocated_buffer);
+ buffer->allocated_buffer = NULL;
+}
+
IREE_API_EXPORT iree_status_t iree_hal_subspan_buffer_create(
iree_hal_buffer_t* allocated_buffer, iree_device_size_t byte_offset,
iree_device_size_t byte_length, iree_hal_allocator_t* device_allocator,
diff --git a/iree/hal/buffer.h b/iree/hal/buffer.h
index 7057ecb..2f2a16f 100644
--- a/iree/hal/buffer.h
+++ b/iree/hal/buffer.h
@@ -503,6 +503,25 @@
// iree_hal_subspan_buffer_t
//===----------------------------------------------------------------------===//
+// Initializes in-place a subspan buffer stored in |out_buffer|.
+// The reference count of the buffer will be set to 1.
+//
+// This is intended to be used for provably on-stack transient subspans or
+// buffer wrapping where ownership is controlled externally. If the lifetime of
+// the subspan may extend beyond the lifetime of the |out_buffer| storage then
+// iree_hal_subspan_buffer_create must be used instead.
+//
+// iree_hal_subspan_buffer_deinitialize must be used to deinitialize the buffer.
+IREE_API_EXPORT void iree_hal_subspan_buffer_initialize(
+ iree_hal_buffer_t* allocated_buffer, iree_device_size_t byte_offset,
+ iree_device_size_t byte_length, iree_hal_allocator_t* device_allocator,
+ iree_allocator_t host_allocator, iree_hal_buffer_t* out_buffer);
+
+// Deinitializes a subspan buffer that was initialized with
+// iree_hal_subspan_buffer_initialize.
+IREE_API_EXPORT void iree_hal_subspan_buffer_deinitialize(
+ iree_hal_buffer_t* buffer);
+
// Creates a buffer referencing a subspan of some base allocation.
// Optionally |device_allocator| can be provided if this subspan references
// managed buffers that need deallocation callbacks.