1 module grain.allocator;
2 
3 import grain.tensor : Opt;
4 
5 struct CPUMallocator
6 {
7     Opt opt;
8     alias opt this;
9     
10     enum deviceof = "cpu";
11     enum pinned = false;
12 
13     /**
14     Standard allocator methods per the semantics defined above. The
15     $(D deallocate) method is $(D @system) because it
16     may move memory around, leaving dangling pointers in user code. Somewhat
17     paradoxically, $(D malloc) is $(D @safe) but that's only useful to safe
18     programs that can afford to leak memory allocated.
19     */
20     @trusted @nogc nothrow
21     void[] allocate()(size_t bytes)
22     {
23         if (!bytes) return null;
24 
25         void* p;
26         import core.memory : pureMalloc;
27         p = pureMalloc(bytes);
28         return p ? p[0 .. bytes] : null;
29     }
30 
31     /// Ditto
32     @system @nogc nothrow
33     bool deallocate()(void[] b)
34     {
35         import core.memory : pureFree;
36         pureFree(b.ptr);
37         return true;
38     }
39 
40     enum instance =  typeof(this)();
41 }
42 
43 import grain.storage;
44 alias cpu = RCStorage!CPUMallocator;