You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by pa...@apache.org on 2024/01/08 15:11:01 UTC

(arrow-nanoarrow) branch main updated: feat(r): Provide LinkingTo headers for extension packages (#332)

This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 34b0ca5  feat(r): Provide LinkingTo headers for extension packages (#332)
34b0ca5 is described below

commit 34b0ca57c42f7414495a0946d6bbceb768ef7673
Author: Dewey Dunnington <de...@dunnington.ca>
AuthorDate: Mon Jan 8 15:10:55 2024 +0000

    feat(r): Provide LinkingTo headers for extension packages (#332)
    
    In developing geoarrow, an R extension that imports/exports a number of
    Arrow C data structures wrapped by nanoarrow S3 objects, it has become
    apparent that the sanitize and allocate operations are non-trivial and
    basically have to be copied in every package that wants to import/export
    nanoarrow things. @eddelbuettel has run up against some valid use-cases
    as well! https://github.com/eddelbuettel/linesplitter/issues/1 ,
    https://github.com/apache/arrow-nanoarrow/issues/187
    
    This PR makes the definition of how Arrow C Data interface objects are
    encoded as R external pointers available as a public header (such that
    downstream packages can `LinkingTo: nanoarrow` and `#include
    <nanoarrow/r.h>`. I think the initial target will just be allocate an
    owning external pointer and sanitize an input SEXP.
    
    @eddelbuettel Are there any other operations that are blocking any of
    your projects that would be must-haves in this header?
    
    (I know it's missing array_stream...I forgot about the ability to supply
    R finalizers and so it's a slightly more complicated change)
---
 r/R/array-stream.R                   |  13 +-
 r/inst/include/nanoarrow/r.h         | 431 +++++++++++++++++++++++++++++++++++
 r/inst/include/r_nanoarrow.h         | 118 ----------
 r/man/array_stream_set_finalizer.Rd  |   9 +-
 r/src/Makevars                       |  18 ++
 r/src/array.c                        |  51 ++---
 r/src/array.h                        |  47 +---
 r/src/array_stream.c                 |  45 ++--
 r/src/array_stream.h                 |  39 +---
 r/src/array_view.c                   |   4 +-
 r/src/as_array.c                     |  10 +-
 r/src/convert.c                      |   8 +-
 r/src/convert_array.c                |   2 +-
 r/src/convert_array_stream.c         |  12 +-
 r/src/infer_ptype.c                  |   4 +-
 r/src/pointers.c                     |  11 +-
 r/src/schema.c                       |  49 ++--
 r/src/schema.h                       |  44 +---
 r/tests/testthat/test-array-stream.R |   8 +-
 19 files changed, 563 insertions(+), 360 deletions(-)

diff --git a/r/R/array-stream.R b/r/R/array-stream.R
index c2fb2e5..eefa6ae 100644
--- a/r/R/array-stream.R
+++ b/r/R/array-stream.R
@@ -65,12 +65,15 @@ basic_array_stream <- function(batches, schema = NULL, validate = TRUE) {
 #' @param array_stream A [nanoarrow_array_stream][as_nanoarrow_array_stream]
 #' @param finalizer A function that will be called with zero arguments.
 #'
-#' @return `array_stream`, invisibly
+#' @return A newly allocated `array_stream` whose release callback will call
+#'   the supplied finalizer.
 #' @export
 #'
 #' @examples
-#' stream <- basic_array_stream(list(1:5))
-#' array_stream_set_finalizer(stream, function() message("All done!"))
+#' stream <- array_stream_set_finalizer(
+#'   basic_array_stream(list(1:5)),
+#'   function() message("All done!")
+#' )
 #' stream$release()
 #'
 array_stream_set_finalizer <- function(array_stream, finalizer) {
@@ -81,7 +84,9 @@ array_stream_set_finalizer <- function(array_stream, finalizer) {
   class(prot) <- "nanoarrow_array_stream_finalizer"
 
   nanoarrow_pointer_set_protected(array_stream, prot)
-  invisible(array_stream)
+  out <- nanoarrow_allocate_array_stream()
+  nanoarrow_pointer_export(array_stream, out)
+  out
 }
 
 #' Convert an object to a nanoarrow array_stream
diff --git a/r/inst/include/nanoarrow/r.h b/r/inst/include/nanoarrow/r.h
new file mode 100644
index 0000000..c6b655d
--- /dev/null
+++ b/r/inst/include/nanoarrow/r.h
@@ -0,0 +1,431 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef NANOARROW_R_H_INCLUDED
+#define NANOARROW_R_H_INCLUDED
+
+#include <R.h>
+#include <Rinternals.h>
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/// \defgroup nanoarrow-r Utilities for Arrow R extensions
+///
+/// EXPERIMENTAL: The interface and lifecycle semantics described in this header
+/// should be considered experimental and may change in a future version based on
+/// user feedback.
+///
+/// In the nanoarrow R package, an external pointer to an ArrowSchema, ArrowArray, or
+/// ArrowArrayStream carries the class "nanoarrow_schema", "nanoarrow_array", or
+/// "nanoarrow_array_stream" (respectively). The pointer must point to valid memory
+/// or be NULL until the R external pointer object is finalized.
+///
+/// nanoarrow_schema_owning_xptr(), nanoarrow_array_owning_xptr(), and
+/// nanoarrow_array_stream_owning_xptr() initialize such an external pointer using
+/// malloc() and a NULL initial release() callback such that it can be distinguished from
+/// a pointer to an initialized value according to the Arrow C Data/Stream interface
+/// documentation. This structure is intended to have a valid value initialized into it
+/// using ArrowXXXMove() or by passing the pointer to a suitable exporting function.
+///
+/// External pointers allocated by nanoarrow_xxxx_owning_xptr() register a finalizer
+/// that will call the release() callback when its value is non-NULL and points to
+/// a structure whose release() callback is also non-NULL. External pointers may also
+/// manage lifecycle by declaring a strong reference to a single R object via
+/// R_SetExternalPtrProtected(); however, when passing the address of an R external
+/// pointer to a non-R library, the ownership of the structure must *not* have such SEXP
+/// dependencies. The nanoarrow R package can wrap such an SEXP dependency into a
+/// self-contained thread-safe release callback via nanoarrow_pointer_export() that
+/// manages the SEXP dependency using a preserve/release mechanism similar to
+/// R_PreserveObject()/ R_ReleaseObject().
+///
+/// The "tag" of an external pointer to an ArrowArray must be R_NilValue or an external
+/// pointer to an ArrowSchema that may be used to interpret the pointed-to ArrowArray. The
+/// "tag" of a nanoarrow external pointer to an ArrowSchema or ArrowArrayStream is
+/// reserved for future use and must be R_NilValue.
+///
+/// @{
+
+// Extra guard for versions of Arrow without the canonical guard
+#ifndef ARROW_FLAG_DICTIONARY_ORDERED
+
+#ifndef ARROW_C_DATA_INTERFACE
+#define ARROW_C_DATA_INTERFACE
+
+#include <stdint.h>
+
+#define ARROW_FLAG_DICTIONARY_ORDERED 1
+#define ARROW_FLAG_NULLABLE 2
+#define ARROW_FLAG_MAP_KEYS_SORTED 4
+
+struct ArrowSchema {
+  // Array type description
+  const char* format;
+  const char* name;
+  const char* metadata;
+  int64_t flags;
+  int64_t n_children;
+  struct ArrowSchema** children;
+  struct ArrowSchema* dictionary;
+
+  // Release callback
+  void (*release)(struct ArrowSchema*);
+  // Opaque producer-specific data
+  void* private_data;
+};
+
+struct ArrowArray {
+  // Array data description
+  int64_t length;
+  int64_t null_count;
+  int64_t offset;
+  int64_t n_buffers;
+  int64_t n_children;
+  const void** buffers;
+  struct ArrowArray** children;
+  struct ArrowArray* dictionary;
+
+  // Release callback
+  void (*release)(struct ArrowArray*);
+  // Opaque producer-specific data
+  void* private_data;
+};
+
+#endif  // ARROW_C_DATA_INTERFACE
+
+#ifndef ARROW_C_STREAM_INTERFACE
+#define ARROW_C_STREAM_INTERFACE
+
+struct ArrowArrayStream {
+  // Callback to get the stream type
+  // (will be the same for all arrays in the stream).
+  //
+  // Return value: 0 if successful, an `errno`-compatible error code otherwise.
+  //
+  // If successful, the ArrowSchema must be released independently from the stream.
+  int (*get_schema)(struct ArrowArrayStream*, struct ArrowSchema* out);
+
+  // Callback to get the next array
+  // (if no error and the array is released, the stream has ended)
+  //
+  // Return value: 0 if successful, an `errno`-compatible error code otherwise.
+  //
+  // If successful, the ArrowArray must be released independently from the stream.
+  int (*get_next)(struct ArrowArrayStream*, struct ArrowArray* out);
+
+  // Callback to get optional detailed error information.
+  // This must only be called if the last stream operation failed
+  // with a non-0 return code.
+  //
+  // Return value: pointer to a null-terminated character array describing
+  // the last error, or NULL if no description is available.
+  //
+  // The returned pointer is only valid until the next operation on this stream
+  // (including release).
+  const char* (*get_last_error)(struct ArrowArrayStream*);
+
+  // Release callback: release the stream's own resources.
+  // Note that arrays returned by `get_next` must be individually released.
+  void (*release)(struct ArrowArrayStream*);
+
+  // Opaque producer-specific data
+  void* private_data;
+};
+
+#endif  // ARROW_C_STREAM_INTERFACE
+#endif  // ARROW_FLAG_DICTIONARY_ORDERED
+
+/// \brief Allocate an external pointer to an ArrowSchema
+///
+/// Allocate an external pointer to an uninitialized ArrowSchema with a finalizer that
+/// ensures that any non-null release callback in a pointed-to structure will be called
+/// when the external pointer is garbage collected.
+static inline SEXP nanoarrow_schema_owning_xptr(void);
+
+/// \brief Allocate an external pointer to an ArrowArray
+///
+/// Allocate an external pointer to an uninitialized ArrowArray with a finalizer that
+/// ensures that any non-null release callback in a pointed-to structure will be called
+/// when the external pointer is garbage collected.
+static inline SEXP nanoarrow_array_owning_xptr(void);
+
+/// \brief Allocate an external pointer to an ArrowArrayStream
+///
+/// Allocate an external pointer to an uninitialized ArrowArrayStream with a finalizer
+/// that ensures that any non-null release callback in a pointed-to structure will be
+/// called when the external pointer is garbage collected.
+static inline SEXP nanoarow_array_stream_owning_xptr(void);
+
+/// \brief Ensure an input SEXP points to an initialized ArrowSchema
+///
+/// This function will always return an ArrowSchema pointer that can be safely
+/// consumed or raise an error via Rf_error(). This is intended to be used to
+/// sanitize an *input* ArrowSchema.
+static inline struct ArrowSchema* nanoarrow_schema_from_xptr(SEXP schema_xptr);
+
+/// \brief Ensure an output SEXP points to an uninitialized ArrowSchema
+///
+/// This function will always return an ArrowSchema pointer that can be safely
+/// used as an output argument or raise an error via Rf_error(). This is intended
+/// to be used to sanitize an *output* ArrowSchema allocated from R or elsewhere.
+static inline struct ArrowSchema* nanoarrow_output_schema_from_xptr(SEXP schema_xptr);
+
+/// \brief Ensure an input SEXP points to an initialized ArrowArray
+///
+/// This function will always return an ArrowArray pointer that can be safely
+/// consumed or raise an error via Rf_error(). This is intended to be used to
+/// sanitize an *input* ArrowArray.
+static inline struct ArrowArray* nanoarrow_array_from_xptr(SEXP array_xptr);
+
+/// \brief Ensure an output SEXP points to an uninitialized ArrowArray
+///
+/// This function will always return an ArrowArray pointer that can be safely
+/// used as an output argument or raise an error via Rf_error(). This is intended
+/// to be used to sanitize an *output* ArrowArray allocated from R or elsewhere.
+static inline struct ArrowArray* nanoarrow_output_array_from_xptr(SEXP array_xptr);
+
+/// \brief Ensure an input SEXP points to an initialized ArrowArrayStream
+///
+/// This function will always return an ArrowArrayStream pointer that can be safely
+/// consumed or raise an error via Rf_error(). This is intended to be used to
+/// sanitize an *input* ArrowArrayStream.
+static inline struct ArrowArrayStream* nanoarrow_array_stream_from_xptr(
+    SEXP array_stream_xptr);
+
+/// \brief Ensure an output SEXP points to an uninitialized ArrowArrayStream
+///
+/// This function will always return an ArrowArrayStream pointer that can be safely
+/// used as an output argument or raise an error via Rf_error(). This is intended
+/// to be used to sanitize an *output* ArrowArrayStream allocated from R or elsewhere.
+static inline struct ArrowArrayStream* nanoarrow_output_array_stream_from_xptr(
+    SEXP array_stream_xptr);
+
+/// \brief Finalize an external pointer to an ArrowSchema
+///
+/// This function is provided for internal use by nanoarrow_schema_owning_xptr()
+/// and should not be called directly.
+static void nanoarrow_finalize_schema_xptr(SEXP schema_xptr);
+
+/// \brief Finalize an external pointer to an ArrowArray
+///
+/// This function is provided for internal use by nanoarrow_array_owning_xptr()
+/// and should not be called directly.
+static void nanoarrow_finalize_array_xptr(SEXP array_xptr);
+
+/// \brief Finalize an external pointer to an ArrowArrayStream
+///
+/// This function is provided for internal use by nanoarrow_array_stream_owning_xptr()
+/// and should not be called directly.
+static void nanoarrow_finalize_array_stream_xptr(SEXP array_stream_xptr);
+
+/// @}
+
+// Implementations follow
+
+static void nanoarrow_finalize_schema_xptr(SEXP schema_xptr) {
+  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
+  if (schema != NULL && schema->release != NULL) {
+    schema->release(schema);
+  }
+
+  if (schema != NULL) {
+    free(schema);
+    R_ClearExternalPtr(schema_xptr);
+  }
+}
+
+static void nanoarrow_finalize_array_xptr(SEXP array_xptr) {
+  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  if (array != NULL && array->release != NULL) {
+    array->release(array);
+  }
+
+  if (array != NULL) {
+    free(array);
+    R_ClearExternalPtr(array_xptr);
+  }
+}
+
+static void nanoarrow_finalize_array_stream_xptr(SEXP array_stream_xptr) {
+  struct ArrowArrayStream* array_stream =
+      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
+  if (array_stream != NULL && array_stream->release != NULL) {
+    array_stream->release(array_stream);
+  }
+
+  if (array_stream != NULL) {
+    free(array_stream);
+    R_ClearExternalPtr(array_stream_xptr);
+  }
+}
+
+static inline SEXP nanoarrow_schema_owning_xptr(void) {
+  struct ArrowSchema* schema = (struct ArrowSchema*)malloc(sizeof(struct ArrowSchema));
+  if (schema == NULL) {
+    Rf_error("Failed to allocate ArrowSchema");
+  }
+
+  schema->release = NULL;
+
+  SEXP schema_xptr = PROTECT(R_MakeExternalPtr(schema, R_NilValue, R_NilValue));
+  SEXP schema_cls = PROTECT(Rf_mkString("nanoarrow_schema"));
+  Rf_setAttrib(schema_xptr, R_ClassSymbol, schema_cls);
+  R_RegisterCFinalizer(schema_xptr, &nanoarrow_finalize_schema_xptr);
+  UNPROTECT(2);
+  return schema_xptr;
+}
+
+static inline SEXP nanoarrow_array_owning_xptr(void) {
+  struct ArrowArray* array = (struct ArrowArray*)malloc(sizeof(struct ArrowArray));
+  array->release = NULL;
+
+  SEXP array_xptr = PROTECT(R_MakeExternalPtr(array, R_NilValue, R_NilValue));
+  SEXP array_cls = PROTECT(Rf_mkString("nanoarrow_array"));
+  Rf_setAttrib(array_xptr, R_ClassSymbol, array_cls);
+  R_RegisterCFinalizer(array_xptr, &nanoarrow_finalize_array_xptr);
+  UNPROTECT(2);
+  return array_xptr;
+}
+
+static inline SEXP nanoarow_array_stream_owning_xptr(void) {
+  struct ArrowArrayStream* array_stream =
+      (struct ArrowArrayStream*)malloc(sizeof(struct ArrowArrayStream));
+  array_stream->release = NULL;
+
+  SEXP array_stream_xptr =
+      PROTECT(R_MakeExternalPtr(array_stream, R_NilValue, R_NilValue));
+  SEXP array_stream_cls = PROTECT(Rf_mkString("nanoarrow_array_stream"));
+  Rf_setAttrib(array_stream_xptr, R_ClassSymbol, array_stream_cls);
+  R_RegisterCFinalizer(array_stream_xptr, &nanoarrow_finalize_array_stream_xptr);
+  UNPROTECT(2);
+  return array_stream_xptr;
+}
+
+static inline struct ArrowSchema* nanoarrow_schema_from_xptr(SEXP schema_xptr) {
+  if (!Rf_inherits(schema_xptr, "nanoarrow_schema")) {
+    Rf_error("`schema` argument that does not inherit from 'nanoarrow_schema'");
+  }
+
+  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
+  if (schema == NULL) {
+    Rf_error("nanoarrow_schema() is an external pointer to NULL");
+  }
+
+  if (schema->release == NULL) {
+    Rf_error("nanoarrow_schema() has already been released");
+  }
+
+  return schema;
+}
+
+static inline struct ArrowSchema* nanoarrow_output_schema_from_xptr(SEXP schema_xptr) {
+  if (!Rf_inherits(schema_xptr, "nanoarrow_schema")) {
+    Rf_error("`schema` argument that does not inherit from 'nanoarrow_schema'");
+  }
+
+  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
+  if (schema == NULL) {
+    Rf_error("nanoarrow_schema() is an external pointer to NULL");
+  }
+
+  if (schema->release != NULL) {
+    Rf_error("nanoarrow_schema() output has already been initialized");
+  }
+
+  return schema;
+}
+
+static inline struct ArrowArray* nanoarrow_array_from_xptr(SEXP array_xptr) {
+  if (!Rf_inherits(array_xptr, "nanoarrow_array")) {
+    Rf_error("`array` argument that is not a nanoarrow_array()");
+  }
+
+  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  if (array == NULL) {
+    Rf_error("nanoarrow_array() is an external pointer to NULL");
+  }
+
+  if (array->release == NULL) {
+    Rf_error("nanoarrow_array() has already been released");
+  }
+
+  return array;
+}
+
+static inline struct ArrowArray* nanoarrow_output_array_from_xptr(SEXP array_xptr) {
+  if (!Rf_inherits(array_xptr, "nanoarrow_array")) {
+    Rf_error("`array` argument that is not a nanoarrow_array()");
+  }
+
+  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  if (array == NULL) {
+    Rf_error("nanoarrow_array() is an external pointer to NULL");
+  }
+
+  if (array->release != NULL) {
+    Rf_error("nanoarrow_array() output has already been initialized");
+  }
+
+  return array;
+}
+
+static inline struct ArrowArrayStream* nanoarrow_array_stream_from_xptr(
+    SEXP array_stream_xptr) {
+  if (!Rf_inherits(array_stream_xptr, "nanoarrow_array_stream")) {
+    Rf_error("`array_stream` argument that is not a nanoarrow_array_stream()");
+  }
+
+  struct ArrowArrayStream* array_stream =
+      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
+  if (array_stream == NULL) {
+    Rf_error("nanoarrow_array_stream() is an external pointer to NULL");
+  }
+
+  if (array_stream->release == NULL) {
+    Rf_error("nanoarrow_array_stream() has already been released");
+  }
+
+  return array_stream;
+}
+
+static inline struct ArrowArrayStream* nanoarrow_output_array_stream_from_xptr(
+    SEXP array_stream_xptr) {
+  if (!Rf_inherits(array_stream_xptr, "nanoarrow_array_stream")) {
+    Rf_error("`array_stream` argument that is not a nanoarrow_array_stream()");
+  }
+
+  struct ArrowArrayStream* array_stream =
+      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
+  if (array_stream == NULL) {
+    Rf_error("nanoarrow_array_stream() is an external pointer to NULL");
+  }
+
+  if (array_stream->release != NULL) {
+    Rf_error("nanoarrow_array_stream() output has already been initialized");
+  }
+
+  return array_stream;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/r/inst/include/r_nanoarrow.h b/r/inst/include/r_nanoarrow.h
deleted file mode 100644
index 3481fc0..0000000
--- a/r/inst/include/r_nanoarrow.h
+++ /dev/null
@@ -1,118 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-#ifndef R_NANOARROW_H_INCLUDED
-#define R_NANOARROW_H_INCLUDED
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-// Extra guard for versions of Arrow without the canonical guard
-#ifndef ARROW_FLAG_DICTIONARY_ORDERED
-
-#ifndef ARROW_C_DATA_INTERFACE
-#define ARROW_C_DATA_INTERFACE
-
-#include <stdint.h>
-
-#define ARROW_FLAG_DICTIONARY_ORDERED 1
-#define ARROW_FLAG_NULLABLE 2
-#define ARROW_FLAG_MAP_KEYS_SORTED 4
-
-struct ArrowSchema {
-  // Array type description
-  const char* format;
-  const char* name;
-  const char* metadata;
-  int64_t flags;
-  int64_t n_children;
-  struct ArrowSchema** children;
-  struct ArrowSchema* dictionary;
-
-  // Release callback
-  void (*release)(struct ArrowSchema*);
-  // Opaque producer-specific data
-  void* private_data;
-};
-
-struct ArrowArray {
-  // Array data description
-  int64_t length;
-  int64_t null_count;
-  int64_t offset;
-  int64_t n_buffers;
-  int64_t n_children;
-  const void** buffers;
-  struct ArrowArray** children;
-  struct ArrowArray* dictionary;
-
-  // Release callback
-  void (*release)(struct ArrowArray*);
-  // Opaque producer-specific data
-  void* private_data;
-};
-
-#endif  // ARROW_C_DATA_INTERFACE
-
-#ifndef ARROW_C_STREAM_INTERFACE
-#define ARROW_C_STREAM_INTERFACE
-
-struct ArrowArrayStream {
-  // Callback to get the stream type
-  // (will be the same for all arrays in the stream).
-  //
-  // Return value: 0 if successful, an `errno`-compatible error code otherwise.
-  //
-  // If successful, the ArrowSchema must be released independently from the stream.
-  int (*get_schema)(struct ArrowArrayStream*, struct ArrowSchema* out);
-
-  // Callback to get the next array
-  // (if no error and the array is released, the stream has ended)
-  //
-  // Return value: 0 if successful, an `errno`-compatible error code otherwise.
-  //
-  // If successful, the ArrowArray must be released independently from the stream.
-  int (*get_next)(struct ArrowArrayStream*, struct ArrowArray* out);
-
-  // Callback to get optional detailed error information.
-  // This must only be called if the last stream operation failed
-  // with a non-0 return code.
-  //
-  // Return value: pointer to a null-terminated character array describing
-  // the last error, or NULL if no description is available.
-  //
-  // The returned pointer is only valid until the next operation on this stream
-  // (including release).
-  const char* (*get_last_error)(struct ArrowArrayStream*);
-
-  // Release callback: release the stream's own resources.
-  // Note that arrays returned by `get_next` must be individually released.
-  void (*release)(struct ArrowArrayStream*);
-
-  // Opaque producer-specific data
-  void* private_data;
-};
-
-#endif  // ARROW_C_STREAM_INTERFACE
-#endif  // ARROW_FLAG_DICTIONARY_ORDERED
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/r/man/array_stream_set_finalizer.Rd b/r/man/array_stream_set_finalizer.Rd
index 0ecc878..c22a86d 100644
--- a/r/man/array_stream_set_finalizer.Rd
+++ b/r/man/array_stream_set_finalizer.Rd
@@ -12,7 +12,8 @@ array_stream_set_finalizer(array_stream, finalizer)
 \item{finalizer}{A function that will be called with zero arguments.}
 }
 \value{
-\code{array_stream}, invisibly
+A newly allocated \code{array_stream} whose release callback will call
+the supplied finalizer.
 }
 \description{
 In some cases, R functions that return a \link[=as_nanoarrow_array_stream]{nanoarrow_array_stream}
@@ -26,8 +27,10 @@ environments will be garbage-collected when \code{nanoarrow:::preserved_empty()}
 is run.
 }
 \examples{
-stream <- basic_array_stream(list(1:5))
-array_stream_set_finalizer(stream, function() message("All done!"))
+stream <- array_stream_set_finalizer(
+  basic_array_stream(list(1:5)),
+  function() message("All done!")
+)
 stream$release()
 
 }
diff --git a/r/src/Makevars b/r/src/Makevars
new file mode 100644
index 0000000..de5c307
--- /dev/null
+++ b/r/src/Makevars
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+PKG_CPPFLAGS=-I../inst/include
diff --git a/r/src/array.c b/r/src/array.c
index 6a15f4a..14e4ca6 100644
--- a/r/src/array.c
+++ b/r/src/array.c
@@ -27,21 +27,11 @@
 #include "schema.h"
 #include "util.h"
 
-void finalize_array_xptr(SEXP array_xptr) {
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
-  if (array != NULL && array->release != NULL) {
-    array->release(array);
-  }
-
-  if (array != NULL) {
-    ArrowFree(array);
-  }
-}
-
 SEXP nanoarrow_c_array_init(SEXP schema_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
-  SEXP array_xptr = PROTECT(array_owning_xptr());
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
+
+  SEXP array_xptr = PROTECT(nanoarrow_array_owning_xptr());
+  struct ArrowArray* array = nanoarrow_output_array_from_xptr(array_xptr);
 
   struct ArrowError error;
   int result = ArrowArrayInitFromSchema(array, schema, &error);
@@ -55,7 +45,7 @@ SEXP nanoarrow_c_array_init(SEXP schema_xptr) {
 }
 
 SEXP nanoarrow_c_array_set_length(SEXP array_xptr, SEXP length_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   if (TYPEOF(length_sexp) != REALSXP || Rf_length(length_sexp) != 1) {
     Rf_error("array$length must be double(1)");
   }
@@ -70,7 +60,7 @@ SEXP nanoarrow_c_array_set_length(SEXP array_xptr, SEXP length_sexp) {
 }
 
 SEXP nanoarrow_c_array_set_null_count(SEXP array_xptr, SEXP null_count_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   if (TYPEOF(null_count_sexp) != REALSXP || Rf_length(null_count_sexp) != 1) {
     Rf_error("array$null_count must be double(1)");
   }
@@ -85,7 +75,7 @@ SEXP nanoarrow_c_array_set_null_count(SEXP array_xptr, SEXP null_count_sexp) {
 }
 
 SEXP nanoarrow_c_array_set_offset(SEXP array_xptr, SEXP offset_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   if (TYPEOF(offset_sexp) != REALSXP || Rf_length(offset_sexp) != 1) {
     Rf_error("array$offset must be double(1)");
   }
@@ -100,7 +90,7 @@ SEXP nanoarrow_c_array_set_offset(SEXP array_xptr, SEXP offset_sexp) {
 }
 
 SEXP nanoarrow_c_array_set_buffers(SEXP array_xptr, SEXP buffers_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
 
   int64_t n_buffers = Rf_xlength(buffers_sexp);
   if (n_buffers > 3) {
@@ -163,7 +153,7 @@ static void free_all_children(struct ArrowArray* array) {
 }
 
 SEXP nanoarrow_c_array_set_children(SEXP array_xptr, SEXP children_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
 
   release_all_children(array);
 
@@ -185,7 +175,7 @@ SEXP nanoarrow_c_array_set_children(SEXP array_xptr, SEXP children_sexp) {
     // The arrays here will be moved, invalidating the arrays in the passed
     // list (the export step is handled in R)
     SEXP child_xptr = VECTOR_ELT(children_sexp, i);
-    struct ArrowArray* child = array_from_xptr(child_xptr);
+    struct ArrowArray* child = nanoarrow_array_from_xptr(child_xptr);
     ArrowArrayMove(child, array->children[i]);
   }
 
@@ -193,7 +183,7 @@ SEXP nanoarrow_c_array_set_children(SEXP array_xptr, SEXP children_sexp) {
 }
 
 SEXP nanoarrow_c_array_set_dictionary(SEXP array_xptr, SEXP dictionary_xptr) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
 
   // If there's already a dictionary, make sure we release it
   if (array->dictionary != NULL) {
@@ -215,7 +205,7 @@ SEXP nanoarrow_c_array_set_dictionary(SEXP array_xptr, SEXP dictionary_xptr) {
       }
     }
 
-    struct ArrowArray* dictionary = array_from_xptr(dictionary_xptr);
+    struct ArrowArray* dictionary = nanoarrow_array_from_xptr(dictionary_xptr);
     ArrowArrayMove(dictionary, array->dictionary);
   }
 
@@ -265,16 +255,17 @@ SEXP nanoarrow_c_array_validate_after_modify(SEXP array_xptr, SEXP schema_xptr)
   // but after we send the array into the wild, that information is lost.
   // This operation will invalidate array_xptr (but this is OK since we very
   // specifically just allocated it).
-  struct ArrowArray* array = array_from_xptr(array_xptr);
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
   struct ArrowError error;
 
   // Even though array was initialized using ArrowArrayInit(), it doesn't have
   // all the information about storage types since it didn't necessarily know
   // what the storage type would be when it was being constructed. Here we create
   // a version that does and move buffers recursively into it.
-  SEXP array_dst_xptr = PROTECT(array_owning_xptr());
-  struct ArrowArray* array_dst = (struct ArrowArray*)R_ExternalPtrAddr(array_dst_xptr);
+  SEXP array_dst_xptr = PROTECT(nanoarrow_array_owning_xptr());
+  struct ArrowArray* array_dst = nanoarrow_output_array_from_xptr(array_dst_xptr);
+
   int result = ArrowArrayInitFromSchema(array_dst, schema, &error);
   if (result != NANOARROW_OK) {
     Rf_error("ArrowArrayInitFromSchema(): %s", error.message);
@@ -304,8 +295,8 @@ SEXP nanoarrow_c_array_set_schema(SEXP array_xptr, SEXP schema_xptr, SEXP valida
   int validate = LOGICAL(validate_sexp)[0];
   if (validate) {
     // If adding a schema, validate the schema and the pair
-    struct ArrowArray* array = array_from_xptr(array_xptr);
-    struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+    struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
+    struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
     struct ArrowArrayView array_view;
     struct ArrowError error;
@@ -343,7 +334,7 @@ static SEXP borrow_array_xptr(struct ArrowArray* array, SEXP shelter) {
 }
 
 SEXP borrow_array_child_xptr(SEXP array_xptr, int64_t i) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   SEXP schema_xptr = R_ExternalPtrTag(array_xptr);
   SEXP child_xptr = PROTECT(borrow_array_xptr(array->children[i], array_xptr));
   if (schema_xptr != R_NilValue) {
@@ -391,7 +382,7 @@ static SEXP borrow_buffer(struct ArrowArrayView* array_view, int64_t i, SEXP she
 }
 
 SEXP nanoarrow_c_array_proxy(SEXP array_xptr, SEXP array_view_xptr, SEXP recursive_sexp) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   int recursive = LOGICAL(recursive_sexp)[0];
   struct ArrowArrayView* array_view = NULL;
   if (array_view_xptr != R_NilValue) {
diff --git a/r/src/array.h b/r/src/array.h
index f45d634..ebe965e 100644
--- a/r/src/array.h
+++ b/r/src/array.h
@@ -21,12 +21,11 @@
 #include <R.h>
 #include <Rinternals.h>
 
+#include <nanoarrow/r.h>
 #include "buffer.h"
 #include "nanoarrow.h"
 #include "util.h"
 
-void finalize_array_xptr(SEXP array_xptr);
-
 // Returns an external pointer to an array child with a schema attached.
 // The returned pointer will keep its parent alive unless passed through
 // array_xptr_ensure_independent(). This is typically what you want when
@@ -34,49 +33,17 @@ void finalize_array_xptr(SEXP array_xptr);
 // is ephemeral.
 SEXP borrow_array_child_xptr(SEXP array_xptr, int64_t i);
 
-// Returns the underlying struct ArrowArray* from an external pointer,
-// checking and erroring for invalid objects, pointers, and arrays.
-static inline struct ArrowArray* array_from_xptr(SEXP array_xptr) {
-  if (!Rf_inherits(array_xptr, "nanoarrow_array")) {
-    Rf_error("`array` argument that is not a nanoarrow_array()");
-  }
-
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
-  if (array == NULL) {
-    Rf_error("nanoarrow_array() is an external pointer to NULL");
-  }
-
-  if (array->release == NULL) {
-    Rf_error("nanoarrow_array() has already been released");
-  }
-
-  return array;
-}
-
 // Returns the underlying struct ArrowArray* from an external pointer,
 // checking and erroring for invalid objects, pointers, and arrays, but
 // allowing for R_NilValue to signify a NULL return.
-static inline struct ArrowArray* nullable_array_from_xptr(SEXP array_xptr) {
+static inline struct ArrowArray* nullable_nanoarrow_array_from_xptr(SEXP array_xptr) {
   if (array_xptr == R_NilValue) {
     return NULL;
   } else {
-    return array_from_xptr(array_xptr);
+    return nanoarrow_array_from_xptr(array_xptr);
   }
 }
 
-// Create an external pointer with the proper class and that will release any
-// non-null, non-released pointer when garbage collected.
-static inline SEXP array_owning_xptr(void) {
-  struct ArrowArray* array = (struct ArrowArray*)ArrowMalloc(sizeof(struct ArrowArray));
-  array->release = NULL;
-
-  SEXP array_xptr = PROTECT(R_MakeExternalPtr(array, R_NilValue, R_NilValue));
-  Rf_setAttrib(array_xptr, R_ClassSymbol, nanoarrow_cls_array);
-  R_RegisterCFinalizer(array_xptr, &finalize_array_xptr);
-  UNPROTECT(1);
-  return array_xptr;
-}
-
 // Attaches a schema to an array external pointer. The nanoarrow R package
 // attempts to do this whenever possible to avoid misinterpreting arrays.
 static inline void array_xptr_set_schema(SEXP array_xptr, SEXP schema_xptr) {
@@ -115,7 +82,7 @@ static inline void array_export(SEXP array_xptr, struct ArrowArray* array_copy)
   // a borrowed child of a struct array), this will ensure a version that can be
   // released independently of its parent.
   SEXP independent_array_xptr = PROTECT(array_xptr_ensure_independent(array_xptr));
-  struct ArrowArray* array = array_from_xptr(independent_array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(independent_array_xptr);
 
   int result = ArrowArrayInitFromType(array_copy, NANOARROW_TYPE_UNINITIALIZED);
   if (result != NANOARROW_OK) {
@@ -180,11 +147,11 @@ static inline void array_export(SEXP array_xptr, struct ArrowArray* array_copy)
 // applies if the array_xptr has the external pointer 'prot' field
 // set (if it doesn't have that set, it is already independent).
 static inline SEXP array_ensure_independent(struct ArrowArray* array) {
-  SEXP original_array_xptr = PROTECT(array_owning_xptr());
+  SEXP original_array_xptr = PROTECT(nanoarrow_array_owning_xptr());
 
   // Move array to the newly created owner
   struct ArrowArray* original_array =
-      (struct ArrowArray*)R_ExternalPtrAddr(original_array_xptr);
+      nanoarrow_output_array_from_xptr(original_array_xptr);
   memcpy(original_array, array, sizeof(struct ArrowArray));
   array->release = NULL;
 
@@ -202,7 +169,7 @@ static inline SEXP array_ensure_independent(struct ArrowArray* array) {
 // field of the external pointer: if it that field is R_NilValue, it is already
 // independent.
 static inline SEXP array_xptr_ensure_independent(SEXP array_xptr) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   if (R_ExternalPtrProtected(array_xptr) == R_NilValue) {
     return array_xptr;
   }
diff --git a/r/src/array_stream.c b/r/src/array_stream.c
index 5b232a9..2580d97 100644
--- a/r/src/array_stream.c
+++ b/r/src/array_stream.c
@@ -41,7 +41,7 @@ static SEXP run_finalizer_error_handler(SEXP cond, void* hdata) {
   return R_NilValue;
 }
 
-void run_user_array_stream_finalizer(SEXP array_stream_xptr) {
+static void run_user_array_stream_finalizer(SEXP array_stream_xptr) {
   SEXP protected = PROTECT(R_ExternalPtrProtected(array_stream_xptr));
   R_SetExternalPtrProtected(array_stream_xptr, R_NilValue);
 
@@ -53,25 +53,13 @@ void run_user_array_stream_finalizer(SEXP array_stream_xptr) {
   UNPROTECT(1);
 }
 
-void finalize_array_stream_xptr(SEXP array_stream_xptr) {
+SEXP nanoarrow_c_array_stream_get_schema(SEXP array_stream_xptr) {
   struct ArrowArrayStream* array_stream =
-      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
-  if (array_stream != NULL && array_stream->release != NULL) {
-    array_stream->release(array_stream);
-  }
+      nanoarrow_array_stream_from_xptr(array_stream_xptr);
 
-  if (array_stream != NULL) {
-    ArrowFree(array_stream);
-  }
-
-  run_user_array_stream_finalizer(array_stream_xptr);
-}
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
 
-SEXP nanoarrow_c_array_stream_get_schema(SEXP array_stream_xptr) {
-  struct ArrowArrayStream* array_stream = array_stream_from_xptr(array_stream_xptr);
-
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
   int result = ArrowArrayStreamGetSchema(array_stream, schema, NULL);
   if (result != 0) {
     Rf_error("array_stream->get_schema(): [%d] %s", result,
@@ -83,10 +71,12 @@ SEXP nanoarrow_c_array_stream_get_schema(SEXP array_stream_xptr) {
 }
 
 SEXP nanoarrow_c_array_stream_get_next(SEXP array_stream_xptr) {
-  struct ArrowArrayStream* array_stream = array_stream_from_xptr(array_stream_xptr);
+  struct ArrowArrayStream* array_stream =
+      nanoarrow_array_stream_from_xptr(array_stream_xptr);
+
+  SEXP array_xptr = PROTECT(nanoarrow_array_owning_xptr());
+  struct ArrowArray* array = nanoarrow_output_array_from_xptr(array_xptr);
 
-  SEXP array_xptr = PROTECT(array_owning_xptr());
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
   int result = ArrowArrayStreamGetNext(array_stream, array, NULL);
   if (result != NANOARROW_OK) {
     Rf_error("array_stream->get_next(): [%d] %s", result,
@@ -102,14 +92,13 @@ SEXP nanoarrow_c_basic_array_stream(SEXP batches_sexp, SEXP schema_xptr,
   int validate = LOGICAL(validate_sexp)[0];
 
   // Schema needs a copy here because ArrowBasicArrayStreamInit() takes ownership
-  SEXP schema_copy_xptr = PROTECT(schema_owning_xptr());
-  struct ArrowSchema* schema_copy =
-      (struct ArrowSchema*)R_ExternalPtrAddr(schema_copy_xptr);
+  SEXP schema_copy_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema_copy = nanoarrow_output_schema_from_xptr(schema_copy_xptr);
   schema_export(schema_xptr, schema_copy);
 
-  SEXP array_stream_xptr = PROTECT(array_stream_owning_xptr());
+  SEXP array_stream_xptr = PROTECT(nanoarow_array_stream_owning_xptr());
   struct ArrowArrayStream* array_stream =
-      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
+      nanoarrow_output_array_stream_from_xptr(array_stream_xptr);
 
   int64_t n_arrays = Rf_xlength(batches_sexp);
   if (ArrowBasicArrayStreamInit(array_stream, schema_copy, n_arrays) != NANOARROW_OK) {
@@ -156,7 +145,7 @@ static void finalize_wrapper_array_stream(struct ArrowArrayStream* array_stream)
     struct WrapperArrayStreamData* data =
         (struct WrapperArrayStreamData*)array_stream->private_data;
 
-    // Release the parent
+    // Run the parent array stream release callback
     data->parent_array_stream->release(data->parent_array_stream);
 
     // If safe to do so, attempt to do an eager evaluation of a release
@@ -198,7 +187,7 @@ static int wrapper_array_stream_get_next(struct ArrowArrayStream* array_stream,
 void array_stream_export(SEXP parent_array_stream_xptr,
                          struct ArrowArrayStream* array_stream_copy) {
   struct ArrowArrayStream* parent_array_stream =
-      array_stream_from_xptr(parent_array_stream_xptr);
+      nanoarrow_array_stream_from_xptr(parent_array_stream_xptr);
 
   // If there is no dependent object, don't bother with this wrapper
   SEXP dependent_sexp = R_ExternalPtrProtected(parent_array_stream_xptr);
@@ -209,7 +198,7 @@ void array_stream_export(SEXP parent_array_stream_xptr,
 
   // Allocate a new external pointer for an array stream (for consistency:
   // we always move an array stream when exporting)
-  SEXP parent_array_stream_xptr_new = PROTECT(array_stream_owning_xptr());
+  SEXP parent_array_stream_xptr_new = PROTECT(nanoarow_array_stream_owning_xptr());
   struct ArrowArrayStream* parent_array_stream_new =
       (struct ArrowArrayStream*)R_ExternalPtrAddr(parent_array_stream_xptr_new);
   ArrowArrayStreamMove(parent_array_stream, parent_array_stream_new);
diff --git a/r/src/array_stream.h b/r/src/array_stream.h
index 823e718..16daa89 100644
--- a/r/src/array_stream.h
+++ b/r/src/array_stream.h
@@ -21,47 +21,10 @@
 #include <R.h>
 #include <Rinternals.h>
 
+#include <nanoarrow/r.h>
 #include "nanoarrow.h"
 #include "util.h"
 
-void run_user_array_stream_finalizer(SEXP array_stream_xptr);
-void finalize_array_stream_xptr(SEXP array_stream_xptr);
-
-// Returns the underlying struct ArrowSchema* from an external pointer,
-// checking and erroring for invalid objects, pointers, and arrays.
-static inline struct ArrowArrayStream* array_stream_from_xptr(SEXP array_stream_xptr) {
-  if (!Rf_inherits(array_stream_xptr, "nanoarrow_array_stream")) {
-    Rf_error("`array_stream` argument that is not a nanoarrow_array_stream()");
-  }
-
-  struct ArrowArrayStream* array_stream =
-      (struct ArrowArrayStream*)R_ExternalPtrAddr(array_stream_xptr);
-  if (array_stream == NULL) {
-    Rf_error("nanoarrow_array_stream() is an external pointer to NULL");
-  }
-
-  if (array_stream->release == NULL) {
-    Rf_error("nanoarrow_array_stream() has already been released");
-  }
-
-  return array_stream;
-}
-
-// Create an external pointer with the proper class and that will release any
-// non-null, non-released pointer when garbage collected.
-static inline SEXP array_stream_owning_xptr(void) {
-  struct ArrowArrayStream* array_stream =
-      (struct ArrowArrayStream*)ArrowMalloc(sizeof(struct ArrowArrayStream));
-  array_stream->release = NULL;
-
-  SEXP array_stream_xptr =
-      PROTECT(R_MakeExternalPtr(array_stream, R_NilValue, R_NilValue));
-  Rf_setAttrib(array_stream_xptr, R_ClassSymbol, nanoarrow_cls_array_stream);
-  R_RegisterCFinalizer(array_stream_xptr, &finalize_array_stream_xptr);
-  UNPROTECT(1);
-  return array_stream_xptr;
-}
-
 void array_stream_export(SEXP array_stream_xptr,
                          struct ArrowArrayStream* array_stream_copy);
 
diff --git a/r/src/array_view.c b/r/src/array_view.c
index 38b6a72..031e06b 100644
--- a/r/src/array_view.c
+++ b/r/src/array_view.c
@@ -35,8 +35,8 @@ static void finalize_array_view_xptr(SEXP array_view_xptr) {
 }
 
 SEXP nanoarrow_c_array_view(SEXP array_xptr, SEXP schema_xptr) {
-  struct ArrowArray* array = array_from_xptr(array_xptr);
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   struct ArrowError error;
   ArrowErrorSet(&error, "");
diff --git a/r/src/as_array.c b/r/src/as_array.c
index a69aa26..0f7563e 100644
--- a/r/src/as_array.c
+++ b/r/src/as_array.c
@@ -37,7 +37,7 @@ static void call_as_nanoarrow_array(SEXP x_sexp, struct ArrowArray* array,
   // In many cases we can skip the array_export() step (which adds some complexity
   // and an additional R object to the mix)
   if (Rf_inherits(result, "nanoarrow_array_dont_export")) {
-    struct ArrowArray* array_result = array_from_xptr(result);
+    struct ArrowArray* array_result = nanoarrow_array_from_xptr(result);
     ArrowArrayMove(array_result, array);
   } else {
     array_export(result, array);
@@ -380,7 +380,7 @@ static void as_array_default(SEXP x_sexp, struct ArrowArray* array, SEXP schema_
 static void as_array_data_frame(SEXP x_sexp, struct ArrowArray* array, SEXP schema_xptr,
                                 struct ArrowSchemaView* schema_view,
                                 struct ArrowError* error) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   switch (schema_view->type) {
     case NANOARROW_TYPE_SPARSE_UNION:
@@ -504,7 +504,7 @@ static void as_array_list(SEXP x_sexp, struct ArrowArray* array, SEXP schema_xpt
 
 static void as_array_default(SEXP x_sexp, struct ArrowArray* array, SEXP schema_xptr,
                              struct ArrowError* error) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   struct ArrowSchemaView schema_view;
   int result = ArrowSchemaViewInit(&schema_view, schema, error);
@@ -551,8 +551,8 @@ static void as_array_default(SEXP x_sexp, struct ArrowArray* array, SEXP schema_
 }
 
 SEXP nanoarrow_c_as_array_default(SEXP x_sexp, SEXP schema_xptr) {
-  SEXP array_xptr = PROTECT(array_owning_xptr());
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  SEXP array_xptr = PROTECT(nanoarrow_array_owning_xptr());
+  struct ArrowArray* array = nanoarrow_output_array_from_xptr(array_xptr);
   struct ArrowError error;
 
   as_array_default(x_sexp, array, schema_xptr, &error);
diff --git a/r/src/convert.c b/r/src/convert.c
index c0054d7..2148421 100644
--- a/r/src/convert.c
+++ b/r/src/convert.c
@@ -174,7 +174,7 @@ static void set_converter_list_of(SEXP converter_xptr, struct RConverter* conver
 static int set_converter_children_schema(SEXP converter_xptr, SEXP schema_xptr) {
   struct RConverter* converter = (struct RConverter*)R_ExternalPtrAddr(converter_xptr);
   SEXP converter_shelter = R_ExternalPtrProtected(converter_xptr);
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   if (schema->n_children != converter->n_children) {
     ArrowErrorSet(&converter->error,
@@ -201,7 +201,7 @@ static int set_converter_children_schema(SEXP converter_xptr, SEXP schema_xptr)
 static int set_converter_children_array(SEXP converter_xptr, SEXP array_xptr) {
   struct RConverter* converter = (struct RConverter*)R_ExternalPtrAddr(converter_xptr);
   SEXP converter_shelter = R_ExternalPtrProtected(converter_xptr);
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
 
   if (array->n_children != converter->n_children) {
     ArrowErrorSet(&converter->error,
@@ -286,7 +286,7 @@ SEXP nanoarrow_converter_from_ptype(SEXP ptype) {
 int nanoarrow_converter_set_schema(SEXP converter_xptr, SEXP schema_xptr) {
   struct RConverter* converter = (struct RConverter*)R_ExternalPtrAddr(converter_xptr);
   SEXP converter_shelter = R_ExternalPtrProtected(converter_xptr);
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
   NANOARROW_RETURN_NOT_OK(
       ArrowSchemaViewInit(&converter->schema_view, schema, &converter->error));
 
@@ -310,7 +310,7 @@ int nanoarrow_converter_set_schema(SEXP converter_xptr, SEXP schema_xptr) {
 int nanoarrow_converter_set_array(SEXP converter_xptr, SEXP array_xptr) {
   struct RConverter* converter = (struct RConverter*)R_ExternalPtrAddr(converter_xptr);
   SEXP converter_shelter = R_ExternalPtrProtected(converter_xptr);
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   NANOARROW_RETURN_NOT_OK(
       ArrowArrayViewSetArray(&converter->array_view, array, &converter->error));
   SET_VECTOR_ELT(converter_shelter, 2, array_xptr);
diff --git a/r/src/convert_array.c b/r/src/convert_array.c
index ac5326a..8e6d0fd 100644
--- a/r/src/convert_array.c
+++ b/r/src/convert_array.c
@@ -170,7 +170,7 @@ static SEXP convert_array_data_frame(SEXP array_xptr, SEXP ptype_sexp) {
     }
   }
 
-  struct ArrowArray* array = array_from_xptr(array_xptr);
+  struct ArrowArray* array = nanoarrow_array_from_xptr(array_xptr);
   R_xlen_t n_col = array->n_children;
   SEXP result = PROTECT(Rf_allocVector(VECSXP, n_col));
 
diff --git a/r/src/convert_array_stream.c b/r/src/convert_array_stream.c
index be1a5ee..55d68a2 100644
--- a/r/src/convert_array_stream.c
+++ b/r/src/convert_array_stream.c
@@ -28,12 +28,14 @@
 
 SEXP nanoarrow_c_convert_array_stream(SEXP array_stream_xptr, SEXP ptype_sexp,
                                       SEXP size_sexp, SEXP n_sexp) {
-  struct ArrowArrayStream* array_stream = array_stream_from_xptr(array_stream_xptr);
+  struct ArrowArrayStream* array_stream =
+      nanoarrow_array_stream_from_xptr(array_stream_xptr);
   double size = REAL(size_sexp)[0];
   double n = REAL(n_sexp)[0];
 
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
+
   int result = ArrowArrayStreamGetSchema(array_stream, schema, NULL);
   if (result != NANOARROW_OK) {
     Rf_error("ArrowArrayStream::get_schema(): %s",
@@ -49,8 +51,8 @@ SEXP nanoarrow_c_convert_array_stream(SEXP array_stream_xptr, SEXP ptype_sexp,
     nanoarrow_converter_stop(converter_xptr);
   }
 
-  SEXP array_xptr = PROTECT(array_owning_xptr());
-  struct ArrowArray* array = (struct ArrowArray*)R_ExternalPtrAddr(array_xptr);
+  SEXP array_xptr = PROTECT(nanoarrow_array_owning_xptr());
+  struct ArrowArray* array = nanoarrow_output_array_from_xptr(array_xptr);
 
   int64_t n_batches = 0;
   int64_t n_materialized = 0;
diff --git a/r/src/infer_ptype.c b/r/src/infer_ptype.c
index 2e8007b..1f5f8e0 100644
--- a/r/src/infer_ptype.c
+++ b/r/src/infer_ptype.c
@@ -70,7 +70,7 @@ enum VectorType nanoarrow_infer_vector_type(enum ArrowType type) {
 
 // The same as the above, but from a nanoarrow_schema()
 enum VectorType nanoarrow_infer_vector_type_schema(SEXP schema_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   struct ArrowSchemaView schema_view;
   struct ArrowError error;
@@ -104,7 +104,7 @@ static SEXP call_infer_ptype_other(SEXP schema_xptr) {
 SEXP nanoarrow_c_infer_ptype(SEXP schema_xptr);
 
 static SEXP infer_ptype_data_frame(SEXP schema_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
   SEXP result = PROTECT(Rf_allocVector(VECSXP, schema->n_children));
   SEXP result_names = PROTECT(Rf_allocVector(STRSXP, schema->n_children));
 
diff --git a/r/src/pointers.c b/r/src/pointers.c
index 23b27f6..4b04452 100644
--- a/r/src/pointers.c
+++ b/r/src/pointers.c
@@ -26,11 +26,13 @@
 // More reliable way to stringify intptr_t on Windows using C++
 void intptr_as_string(intptr_t ptr_int, char* buf);
 
-SEXP nanoarrow_c_allocate_schema(void) { return schema_owning_xptr(); }
+SEXP nanoarrow_c_allocate_schema(void) { return nanoarrow_schema_owning_xptr(); }
 
-SEXP nanoarrow_c_allocate_array(void) { return array_owning_xptr(); }
+SEXP nanoarrow_c_allocate_array(void) { return nanoarrow_array_owning_xptr(); }
 
-SEXP nanoarrow_c_allocate_array_stream(void) { return array_stream_owning_xptr(); }
+SEXP nanoarrow_c_allocate_array_stream(void) {
+  return nanoarow_array_stream_owning_xptr();
+}
 
 SEXP nanoarrow_c_pointer(SEXP obj_sexp) {
   if (TYPEOF(obj_sexp) == EXTPTRSXP) {
@@ -111,7 +113,6 @@ SEXP nanoarrow_c_pointer_release(SEXP ptr) {
     if (obj != NULL && obj->release != NULL) {
       obj->release(obj);
       obj->release = NULL;
-      run_user_array_stream_finalizer(ptr);
     }
   } else {
     Rf_error(
@@ -204,7 +205,7 @@ SEXP nanoarrow_c_pointer_move(SEXP ptr_src, SEXP ptr_dst) {
 // keep all the object dependencies alive and/or risk moving a dependency
 // of some other R object.
 SEXP nanoarrow_c_export_schema(SEXP schema_xptr, SEXP ptr_dst) {
-  struct ArrowSchema* obj_src = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* obj_src = nanoarrow_schema_from_xptr(schema_xptr);
   SEXP xptr_dst = PROTECT(nanoarrow_c_pointer(ptr_dst));
 
   struct ArrowSchema* obj_dst = (struct ArrowSchema*)R_ExternalPtrAddr(xptr_dst);
diff --git a/r/src/schema.c b/r/src/schema.c
index ab94d86..fb00435 100644
--- a/r/src/schema.c
+++ b/r/src/schema.c
@@ -23,22 +23,11 @@
 #include "schema.h"
 #include "util.h"
 
-void finalize_schema_xptr(SEXP schema_xptr) {
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
-  if (schema != NULL && schema->release != NULL) {
-    schema->release(schema);
-  }
-
-  if (schema != NULL) {
-    ArrowFree(schema);
-  }
-}
-
 SEXP nanoarrow_c_schema_init(SEXP type_id_sexp, SEXP nullable_sexp) {
   int type_id = INTEGER(type_id_sexp)[0];
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
 
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
   int result = ArrowSchemaInitFromType(schema, type_id);
   if (result != NANOARROW_OK) {
     Rf_error("ArrowSchemaInitFromType() failed");
@@ -69,9 +58,9 @@ SEXP nanoarrow_c_schema_init_date_time(SEXP type_id_sexp, SEXP time_unit_sexp,
     timezone = NULL;
   }
 
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
 
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
   ArrowSchemaInit(schema);
   int result = ArrowSchemaSetTypeDateTime(schema, type_id, time_unit, timezone);
   if (result != NANOARROW_OK) {
@@ -97,9 +86,9 @@ SEXP nanoarrow_c_schema_init_decimal(SEXP type_id_sexp, SEXP precision_sexp,
   int precision = INTEGER(precision_sexp)[0];
   int scale = INTEGER(scale_sexp)[0];
 
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
 
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
   ArrowSchemaInit(schema);
   int result = ArrowSchemaSetTypeDecimal(schema, type_id, precision, scale);
   if (result != NANOARROW_OK) {
@@ -124,9 +113,9 @@ SEXP nanoarrow_c_schema_init_fixed_size(SEXP type_id_sexp, SEXP fixed_size_sexp,
   int type_id = INTEGER(type_id_sexp)[0];
   int fixed_size = INTEGER(fixed_size_sexp)[0];
 
-  SEXP schema_xptr = PROTECT(schema_owning_xptr());
+  SEXP schema_xptr = PROTECT(nanoarrow_schema_owning_xptr());
+  struct ArrowSchema* schema = nanoarrow_output_schema_from_xptr(schema_xptr);
 
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
   ArrowSchemaInit(schema);
   int result = ArrowSchemaSetTypeFixedSize(schema, type_id, fixed_size);
   if (result != NANOARROW_OK) {
@@ -182,12 +171,12 @@ static SEXP borrow_schema_xptr(struct ArrowSchema* schema, SEXP shelter) {
 }
 
 SEXP borrow_schema_child_xptr(SEXP schema_xptr, int64_t i) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
   return borrow_schema_xptr(schema->children[i], schema_xptr);
 }
 
 SEXP nanoarrow_c_schema_to_list(SEXP schema_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   const char* names[] = {"format",   "name",       "metadata", "flags",
                          "children", "dictionary", ""};
@@ -256,7 +245,7 @@ static SEXP mkStringView(struct ArrowStringView* view) {
 }
 
 SEXP nanoarrow_c_schema_parse(SEXP schema_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_xptr);
 
   struct ArrowSchemaView schema_view;
   struct ArrowError error;
@@ -356,7 +345,7 @@ SEXP nanoarrow_c_schema_format(SEXP schema_xptr, SEXP recursive_sexp) {
 }
 
 SEXP nanoarrow_c_schema_set_format(SEXP schema_mut_xptr, SEXP format_sexp) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
 
   if (TYPEOF(format_sexp) != STRSXP || Rf_length(format_sexp) != 1) {
     Rf_error("schema$format must be character(1)");
@@ -371,7 +360,7 @@ SEXP nanoarrow_c_schema_set_format(SEXP schema_mut_xptr, SEXP format_sexp) {
 }
 
 SEXP nanoarrow_c_schema_set_name(SEXP schema_mut_xptr, SEXP name_sexp) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
   int result;
 
   if (name_sexp == R_NilValue) {
@@ -414,7 +403,7 @@ static SEXP buffer_owning_xptr(void) {
 }
 
 SEXP nanoarrow_c_schema_set_metadata(SEXP schema_mut_xptr, SEXP metadata_sexp) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
   int result;
 
   if (Rf_xlength(metadata_sexp) == 0) {
@@ -492,7 +481,7 @@ SEXP nanoarrow_c_schema_set_metadata(SEXP schema_mut_xptr, SEXP metadata_sexp) {
 }
 
 SEXP nanoarrow_c_schema_set_flags(SEXP schema_mut_xptr, SEXP flags_sexp) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
 
   if (TYPEOF(flags_sexp) != INTSXP || Rf_length(flags_sexp) != 1) {
     Rf_error("schema$flags must be integer(1)");
@@ -529,7 +518,7 @@ static void free_all_children(struct ArrowSchema* schema) {
 }
 
 SEXP nanoarrow_c_schema_set_children(SEXP schema_mut_xptr, SEXP children_sexp) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
 
   release_all_children(schema);
 
@@ -556,7 +545,7 @@ SEXP nanoarrow_c_schema_set_children(SEXP schema_mut_xptr, SEXP children_sexp) {
   SEXP children_names = PROTECT(Rf_getAttrib(children_sexp, R_NamesSymbol));
 
   for (int64_t i = 0; i < schema->n_children; i++) {
-    struct ArrowSchema* child = schema_from_xptr(VECTOR_ELT(children_sexp, i));
+    struct ArrowSchema* child = nanoarrow_schema_from_xptr(VECTOR_ELT(children_sexp, i));
     result = ArrowSchemaDeepCopy(child, schema->children[i]);
     if (result != NANOARROW_OK) {
       Rf_error("Error copying new_values$children[[%ld]]", (long)i);
@@ -588,7 +577,7 @@ SEXP nanoarrow_c_schema_set_children(SEXP schema_mut_xptr, SEXP children_sexp) {
 }
 
 SEXP nanoarrow_c_schema_set_dictionary(SEXP schema_mut_xptr, SEXP dictionary_xptr) {
-  struct ArrowSchema* schema = schema_from_xptr(schema_mut_xptr);
+  struct ArrowSchema* schema = nanoarrow_schema_from_xptr(schema_mut_xptr);
 
   // If there's already a dictionary, make sure we release it
   if (schema->dictionary != NULL) {
@@ -612,7 +601,7 @@ SEXP nanoarrow_c_schema_set_dictionary(SEXP schema_mut_xptr, SEXP dictionary_xpt
       }
     }
 
-    struct ArrowSchema* dictionary = schema_from_xptr(dictionary_xptr);
+    struct ArrowSchema* dictionary = nanoarrow_schema_from_xptr(dictionary_xptr);
     result = ArrowSchemaDeepCopy(dictionary, schema->dictionary);
     if (result != NANOARROW_OK) {
       Rf_error("Error copying schema$dictionary");
diff --git a/r/src/schema.h b/r/src/schema.h
index 5f252ab..0227a97 100644
--- a/r/src/schema.h
+++ b/r/src/schema.h
@@ -21,35 +21,15 @@
 #include <R.h>
 #include <Rinternals.h>
 
+#include <nanoarrow/r.h>
 #include "nanoarrow.h"
 #include "util.h"
 
-void finalize_schema_xptr(SEXP schema_xptr);
-
 // Returns an external pointer to a schema child. The returned pointer will keep its
 // parent alive: this is typically what you want when printing or performing a conversion,
 // where the borrowed external pointer is ephemeral.
 SEXP borrow_schema_child_xptr(SEXP schema_xptr, int64_t i);
 
-// Returns the underlying struct ArrowSchema* from an external pointer,
-// checking and erroring for invalid objects, pointers, and arrays.
-static inline struct ArrowSchema* schema_from_xptr(SEXP schema_xptr) {
-  if (!Rf_inherits(schema_xptr, "nanoarrow_schema")) {
-    Rf_error("`schema` argument that does not inherit from 'nanoarrow_schema'");
-  }
-
-  struct ArrowSchema* schema = (struct ArrowSchema*)R_ExternalPtrAddr(schema_xptr);
-  if (schema == NULL) {
-    Rf_error("nanoarrow_schema() is an external pointer to NULL");
-  }
-
-  if (schema->release == NULL) {
-    Rf_error("nanoarrow_schema() has already been released");
-  }
-
-  return schema;
-}
-
 // Returns the underlying struct ArrowSchema* from an external pointer,
 // checking and erroring for invalid objects, pointers, and arrays, but
 // allowing for R_NilValue to signify a NULL return.
@@ -57,30 +37,12 @@ static inline struct ArrowSchema* nullable_schema_from_xptr(SEXP schema_xptr) {
   if (schema_xptr == R_NilValue) {
     return NULL;
   } else {
-    return schema_from_xptr(schema_xptr);
+    return nanoarrow_schema_from_xptr(schema_xptr);
   }
 }
 
-// Create an external pointer with the proper class and that will release any
-// non-null, non-released pointer when garbage collected.
-static inline SEXP schema_owning_xptr(void) {
-  struct ArrowSchema* schema =
-      (struct ArrowSchema*)ArrowMalloc(sizeof(struct ArrowSchema));
-  if (schema == NULL) {
-    Rf_error("Failed to allocate ArrowSchema");
-  }
-
-  schema->release = NULL;
-
-  SEXP schema_xptr = PROTECT(R_MakeExternalPtr(schema, R_NilValue, R_NilValue));
-  Rf_setAttrib(schema_xptr, R_ClassSymbol, nanoarrow_cls_schema);
-  R_RegisterCFinalizer(schema_xptr, &finalize_schema_xptr);
-  UNPROTECT(1);
-  return schema_xptr;
-}
-
 static inline void schema_export(SEXP schema_xptr, struct ArrowSchema* schema_copy) {
-  int result = ArrowSchemaDeepCopy(schema_from_xptr(schema_xptr), schema_copy);
+  int result = ArrowSchemaDeepCopy(nanoarrow_schema_from_xptr(schema_xptr), schema_copy);
   if (result != NANOARROW_OK) {
     Rf_error("ArrowSchemaDeepCopy() failed");
   }
diff --git a/r/tests/testthat/test-array-stream.R b/r/tests/testthat/test-array-stream.R
index 3032272..c4aeaad 100644
--- a/r/tests/testthat/test-array-stream.R
+++ b/r/tests/testthat/test-array-stream.R
@@ -195,14 +195,14 @@ test_that("nanoarrow_array_stream get_next() with schema = NULL", {
 
 test_that("User array stream finalizers are run on explicit release", {
   stream <- basic_array_stream(list(1:5))
-  array_stream_set_finalizer(stream, function() cat("All done!"))
+  stream <- array_stream_set_finalizer(stream, function() cat("All done!"))
   expect_output(stream$release(), "All done!")
   expect_silent(stream$release())
 })
 
 test_that("User array stream finalizers are run on explicit release even when moved", {
   stream <- basic_array_stream(list(1:5))
-  array_stream_set_finalizer(stream, function() cat("All done!"))
+  stream <- array_stream_set_finalizer(stream, function() cat("All done!"))
 
   stream2 <- nanoarrow_allocate_array_stream()
   nanoarrow_pointer_move(stream, stream2)
@@ -214,7 +214,7 @@ test_that("User array stream finalizers are run on explicit release even when mo
 
 test_that("User array stream finalizers are run on explicit release even when exported", {
   stream <- basic_array_stream(list(1:5))
-  array_stream_set_finalizer(stream, function() cat("All done!"))
+  stream <- array_stream_set_finalizer(stream, function() cat("All done!"))
 
   stream2 <- nanoarrow_allocate_array_stream()
   nanoarrow_pointer_export(stream, stream2)
@@ -226,7 +226,7 @@ test_that("User array stream finalizers are run on explicit release even when ex
 
 test_that("Errors from user array stream finalizer are ignored", {
   stream <- basic_array_stream(list(1:5))
-  array_stream_set_finalizer(stream, function() stop("Error that will be ignored"))
+  stream <- array_stream_set_finalizer(stream, function() stop("Error that will be ignored"))
   # Because this comes from REprintf(), it's not a message and not "output"
   # according to testthat, so we use capture.output()
   expect_identical(