2023-12-01 03:12:53 +08:00
|
|
|
// Copyright © 2023 Apple Inc.
|
|
|
|
|
2023-11-30 02:52:08 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
namespace mlx::core::allocator {
|
|
|
|
|
|
|
|
// Simple wrapper around buffer pointers
|
|
|
|
// WARNING: Only Buffer objects constructed from and those that wrap
|
|
|
|
// raw pointers from mlx::allocator are supported.
|
|
|
|
class Buffer {
|
|
|
|
private:
|
|
|
|
void* ptr_;
|
|
|
|
|
|
|
|
public:
|
2024-04-30 22:18:09 +08:00
|
|
|
Buffer(void* ptr) : ptr_(ptr) {};
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
// Get the raw data pointer from the buffer
|
|
|
|
void* raw_ptr();
|
|
|
|
|
|
|
|
// Get the buffer pointer from the buffer
|
|
|
|
const void* ptr() const {
|
|
|
|
return ptr_;
|
|
|
|
};
|
|
|
|
void* ptr() {
|
|
|
|
return ptr_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Buffer malloc(size_t size);
|
|
|
|
|
|
|
|
void free(Buffer buffer);
|
|
|
|
|
|
|
|
class Allocator {
|
2024-01-02 13:08:17 +08:00
|
|
|
/** Abstract base class for a memory allocator. */
|
2023-11-30 02:52:08 +08:00
|
|
|
public:
|
2025-03-21 07:48:43 +08:00
|
|
|
virtual Buffer malloc(size_t size) = 0;
|
2023-11-30 02:52:08 +08:00
|
|
|
virtual void free(Buffer buffer) = 0;
|
2024-09-12 12:02:16 +08:00
|
|
|
virtual size_t size(Buffer buffer) const = 0;
|
2023-11-30 02:52:08 +08:00
|
|
|
|
|
|
|
Allocator() = default;
|
|
|
|
Allocator(const Allocator& other) = delete;
|
|
|
|
Allocator(Allocator&& other) = delete;
|
|
|
|
Allocator& operator=(const Allocator& other) = delete;
|
|
|
|
Allocator& operator=(Allocator&& other) = delete;
|
|
|
|
virtual ~Allocator() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
Allocator& allocator();
|
|
|
|
|
|
|
|
} // namespace mlx::core::allocator
|