MLX
 
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
1// Copyright © 2023 Apple Inc.
2
3#pragma once
4
5#include <cstdlib>
6
8
9// Simple wrapper around buffer pointers
10// WARNING: Only Buffer objects constructed from and those that wrap
11// raw pointers from mlx::allocator are supported.
12class Buffer {
13 private:
14 void* ptr_;
15
16 public:
17 Buffer(void* ptr) : ptr_(ptr) {};
18
19 // Get the raw data pointer from the buffer
20 void* raw_ptr();
21
22 // Get the buffer pointer from the buffer
23 const void* ptr() const {
24 return ptr_;
25 };
26 void* ptr() {
27 return ptr_;
28 };
29};
30
31Buffer malloc(size_t size);
32
33void free(Buffer buffer);
34
35// Wait for running tasks to finish and free up memory
36// if allocation fails
38
39class Allocator {
41 public:
42 virtual Buffer malloc(size_t size, bool allow_swap = false) = 0;
43 virtual void free(Buffer buffer) = 0;
44 virtual size_t size(Buffer buffer) const = 0;
45
46 Allocator() = default;
47 Allocator(const Allocator& other) = delete;
48 Allocator(Allocator&& other) = delete;
49 Allocator& operator=(const Allocator& other) = delete;
50 Allocator& operator=(Allocator&& other) = delete;
51 virtual ~Allocator() = default;
52};
53
55
56class CommonAllocator : public Allocator {
58 public:
59 virtual Buffer malloc(size_t size, bool allow_swap = false) override;
60 virtual void free(Buffer buffer) override;
61 virtual size_t size(Buffer buffer) const override;
62
63 private:
64 CommonAllocator() = default;
66};
67
68} // namespace mlx::core::allocator
Definition allocator.h:39
Allocator & operator=(const Allocator &other)=delete
virtual size_t size(Buffer buffer) const =0
Allocator & operator=(Allocator &&other)=delete
Allocator(Allocator &&other)=delete
virtual Buffer malloc(size_t size, bool allow_swap=false)=0
Abstract base class for a memory allocator.
Allocator(const Allocator &other)=delete
virtual void free(Buffer buffer)=0
Definition allocator.h:12
const void * ptr() const
Definition allocator.h:23
Buffer(void *ptr)
Definition allocator.h:17
void * ptr()
Definition allocator.h:26
virtual Buffer malloc(size_t size, bool allow_swap=false) override
A general CPU allocator.
virtual void free(Buffer buffer) override
virtual size_t size(Buffer buffer) const override
Definition allocator.h:7
Buffer malloc(size_t size)
void free(Buffer buffer)
Buffer malloc_or_wait(size_t size)
Allocator & allocator()