@@ -19,7 +19,6 @@ namespace mlx::core {
1919// Forward declaration
2020class Primitive ;
2121
22- using Deleter = std::function<void (allocator::Buffer)>;
2322using ShapeElem = int32_t ;
2423using Shape = SmallVector<ShapeElem>;
2524using Strides = SmallVector<int64_t >;
@@ -69,12 +68,8 @@ class MLX_API array {
6968 Dtype dtype,
7069 const std::function<void (void *)>& deleter);
7170
72- /* Build an array from a buffer */
73- explicit array (
74- allocator::Buffer data,
75- Shape shape,
76- Dtype dtype,
77- Deleter deleter = allocator::free);
71+ /* Build an array from data */
72+ explicit array (allocator::Data data, Shape shape, Dtype dtype);
7873
7974 /* * Assignment to rvalue does not compile. */
8075 array& operator =(const array& other) && = delete ;
@@ -238,22 +233,7 @@ class MLX_API array {
238233 return reinterpret_cast <std::uintptr_t >(array_desc_->primitive .get ());
239234 }
240235
241- struct Data {
242- allocator::Buffer buffer;
243- Deleter d;
244- Data (allocator::Buffer buffer, Deleter d = allocator::free)
245- : buffer(buffer), d(d) {}
246- // Not copyable
247- Data (const Data& d) = delete ;
248- Data& operator =(const Data& d) = delete ;
249- Data (Data&& o) : buffer(o.buffer), d(o.d) {
250- o.buffer = allocator::Buffer (nullptr );
251- o.d = [](allocator::Buffer) {};
252- }
253- ~Data () {
254- d (buffer);
255- }
256- };
236+ using Data = allocator::Data;
257237
258238 struct Flags {
259239 // True iff there are no gaps in the underlying data. Each item
@@ -302,7 +282,8 @@ class MLX_API array {
302282
303283 /* * True indicates the arrays buffer is safe to reuse */
304284 bool is_donatable () const {
305- return array_desc_.use_count () == 1 && (array_desc_->data .use_count () == 1 );
285+ return array_desc_.use_count () == 1 &&
286+ (array_desc_->data .use_count () == 1 ) && array_desc_->data ->is_owned ();
306287 }
307288
308289 /* * The array's siblings. */
@@ -359,11 +340,8 @@ class MLX_API array {
359340 return array_desc_->data_size ;
360341 }
361342
362- allocator::Buffer& buffer () {
363- return array_desc_->data ->buffer ;
364- }
365- const allocator::Buffer& buffer () const {
366- return array_desc_->data ->buffer ;
343+ allocator::Buffer buffer () const {
344+ return array_desc_->data ->buffer ();
367345 }
368346
369347 size_t buffer_size () const {
@@ -447,24 +425,14 @@ class MLX_API array {
447425 // Check if the array is a tracer array
448426 bool is_tracer () const ;
449427
450- void set_data (allocator::Buffer buffer, Deleter d = allocator::free );
428+ void set_data (allocator::Data data );
451429
452430 void set_data (
453- allocator::Buffer buffer ,
431+ allocator::Data data ,
454432 size_t data_size,
455433 Strides strides,
456434 Flags flags,
457- Deleter d) {
458- set_data (buffer, data_size, std::move (strides), flags, 0 , std::move (d));
459- }
460-
461- void set_data (
462- allocator::Buffer buffer,
463- size_t data_size,
464- Strides strides,
465- Flags flags,
466- int64_t offset = 0 ,
467- Deleter d = allocator::free);
435+ int64_t offset = 0 );
468436
469437 void copy_shared_buffer (
470438 const array& other,
0 commit comments