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
45 Allocator() = default;
46 Allocator(const Allocator& other) = delete;
47 Allocator(Allocator&& other) = delete;
48 Allocator& operator=(const Allocator& other) = delete;
49 Allocator& operator=(Allocator&& other) = delete;
50 virtual ~Allocator() = default;
51};
52
54
55class CommonAllocator : public Allocator {
57 public:
58 virtual Buffer malloc(size_t size, bool allow_swap = false) override;
59 virtual void free(Buffer buffer) override;
60
61 private:
62 CommonAllocator() = default;
64};
65
66} // namespace mlx::core::allocator
Definition allocator.h:39
Allocator & operator=(const Allocator &other)=delete
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
Definition allocator.h:55
virtual Buffer malloc(size_t size, bool allow_swap=false) override
A general CPU allocator.
virtual void free(Buffer buffer) override
Definition allocator.h:7
Buffer malloc(size_t size)
void free(Buffer buffer)
Buffer malloc_or_wait(size_t size)
Allocator & allocator()