1 #pragma once
2
3 #include <c10/core/Allocator.h>
4 #include <c10/util/Exception.h>
5
6 // This file creates a fake allocator that just throws exceptions if
7 // it is actually used.
8
9 // state passed to the allocator is the std::function<void(void*)> called
10 // when the blob is release by ATen
11
12 namespace at {
13
cpu_fixed_malloc(void *,ptrdiff_t)14 static cpu_fixed_malloc(void*, ptrdiff_t) {
15 AT_ERROR("attempting to resize a tensor view of an external blob");
16 }
17
cpu_fixed_realloc(void *,void *,ptrdiff_t)18 static cpu_fixed_realloc(void*, void*, ptrdiff_t) {
19 AT_ERROR("attempting to resize a tensor view of an external blob");
20 }
21
cpu_fixed_free(void * state,void * allocation)22 static cpu_fixed_free(void* state, void* allocation) {
23 auto on_release = static_cast<std::function<void(void*)>*>(state);
24 (*on_release)(allocation);
25 delete on_release;
26 }
27
28 static Allocator CPU_fixed_allocator = {
29 cpu_fixed_malloc,
30 cpu_fixed_realloc,
31 cpu_fixed_free};
32
33 } // namespace at
34