#ifndef GGML_ZDNN_SCRATCH_HPP
#define GGML_ZDNN_SCRATCH_HPP
#include "ggml.h"
#include <cstddef>
#include <vector>
struct ggml_zdnn_pool {
struct buffer {
void * ptr = nullptr;
size_t size = 0;
};
std::vector<buffer> cached;
~ggml_zdnn_pool();
void * alloc(size_t size, size_t * actual_size);
void free(void * ptr, size_t size);
};
template <typename T>
struct ggml_zdnn_pool_alloc {
ggml_zdnn_pool * pool = nullptr;
T * ptr = nullptr;
size_t actual_size = 0;
ggml_zdnn_pool_alloc() = default;
ggml_zdnn_pool_alloc(ggml_zdnn_pool & pool, size_t size) {
alloc(pool, size);
}
~ggml_zdnn_pool_alloc() {
if (ptr != nullptr) {
pool->free(ptr, actual_size);
}
}
T * alloc(ggml_zdnn_pool & pool, size_t size) {
GGML_ASSERT(ptr == nullptr);
this->pool = &pool;
ptr = (T *) pool.alloc(size * sizeof(T), &actual_size);
return ptr;
}
T * get() {
return ptr;
}
ggml_zdnn_pool_alloc(const ggml_zdnn_pool_alloc &) = delete;
ggml_zdnn_pool_alloc(ggml_zdnn_pool_alloc &&) = delete;
ggml_zdnn_pool_alloc & operator=(const ggml_zdnn_pool_alloc &) = delete;
ggml_zdnn_pool_alloc & operator=(ggml_zdnn_pool_alloc &&) = delete;
};
#endif