]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_stb_heap.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_stb_heap.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_stb_heap.h
28 #ifndef VOGL_STB_HEAP_H
29 #define VOGL_STB_HEAP_H
30
31 #include "vogl_core.h"
32
33 #include <sys/types.h>
34 #include <sys/mman.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "stb_malloc.h"
40
41 VOGL_NAMESPACE_BEGIN(vogl)
42
43 // A really basic STB heap that uses mmap() for sys allocs. Uses its own locking for max safety until I can be 100% sure that stb's locking is robust.
44 // Note, this class is not actually used for anything right now (vogl_mem.cpp just calls stb_malloc directory).
45 template <uint initialize_size, bool locking, bool destroy>
46 class stb_heap
47 {
48     VOGL_NO_COPY_OR_ASSIGNMENT_OP(stb_heap);
49
50 public:
51     stb_heap()
52         : m_page_size(sysconf(_SC_PAGE_SIZE))
53     {
54         stbm_heap_config config;
55         memset(&config, 0, sizeof(config));
56         config.system_alloc = sys_alloc;
57         config.system_free = sys_free;
58         config.user_context = this;
59
60         m_pHeap = stbm_heap_init(m_initial_heap_storage, sizeof(m_initial_heap_storage), &config);
61     }
62
63     ~stb_heap()
64     {
65         if (destroy)
66         {
67             stbm_heap_free(m_pHeap);
68         }
69     }
70
71     stbm_heap *get_stb_heap()
72     {
73         return m_pHeap;
74     }
75
76     void lock()
77     {
78         if (locking)
79             m_mutex.lock();
80     }
81
82     void unlock()
83     {
84         if (locking)
85             m_mutex.unlock();
86     }
87
88     // Purposely not named malloc, etc. in case they are macros.
89     void *malloc_block(size_t size)
90     {
91         lock();
92         void *p = stbm_alloc(NULL, m_pHeap, size, 0);
93         unlock();
94         return p;
95     }
96
97     void *realloc_block(void *p, size_t size)
98     {
99         lock();
100         void *q = stbm_realloc(NULL, m_pHeap, p, size, 0);
101         unlock();
102         return q;
103     }
104
105     void free_block(void *p)
106     {
107         lock();
108         stbm_free(NULL, m_pHeap, p);
109         unlock();
110     }
111
112     size_t msize_block(void *p)
113     {
114         lock();
115         size_t n = stbm_get_allocation_size(p);
116         unlock();
117         return n;
118     }
119
120 private:
121     stbm_heap *m_pHeap;
122     vogl::mutex m_mutex;
123     size_t m_page_size;
124     uint64_t m_initial_heap_storage[(STBM_HEAP_SIZEOF + initialize_size + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
125
126     static void *sys_alloc(void *user_context, size_t size_requested, size_t *size_provided)
127     {
128         stb_heap *pHeap = static_cast<stb_heap *>(user_context);
129
130         size_requested = (size_requested + pHeap->m_page_size - 1) & (~(pHeap->m_page_size - 1));
131
132         void *p = mmap(NULL, size_requested, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
133
134         *size_provided = 0;
135
136         if (p)
137         {
138             *size_provided = size_requested;
139         }
140
141         return p;
142     }
143
144     static void sys_free(void *user_context, void *ptr, size_t size)
145     {
146         VOGL_NOTE_UNUSED(user_context);
147
148         int res = munmap(ptr, size);
149         if (res != 0)
150         {
151             VOGL_FAIL("sys_free: munmap() failed!\n");
152         }
153     }
154 };
155
156 VOGL_NAMESPACE_END(vogl)
157
158 #endif // VOGL_STB_HEAP_H