1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
27 // File: vogl_stb_heap.h
28 #ifndef VOGL_STB_HEAP_H
29 #define VOGL_STB_HEAP_H
31 #include "vogl_core.h"
33 #include <sys/types.h>
39 #include "stb_malloc.h"
41 VOGL_NAMESPACE_BEGIN(vogl)
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>
48 VOGL_NO_COPY_OR_ASSIGNMENT_OP(stb_heap);
52 : m_page_size(sysconf(_SC_PAGE_SIZE))
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;
60 m_pHeap = stbm_heap_init(m_initial_heap_storage, sizeof(m_initial_heap_storage), &config);
67 stbm_heap_free(m_pHeap);
71 stbm_heap *get_stb_heap()
88 // Purposely not named malloc, etc. in case they are macros.
89 void *malloc_block(size_t size)
92 void *p = stbm_alloc(NULL, m_pHeap, size, 0);
97 void *realloc_block(void *p, size_t size)
100 void *q = stbm_realloc(NULL, m_pHeap, p, size, 0);
105 void free_block(void *p)
108 stbm_free(NULL, m_pHeap, p);
112 size_t msize_block(void *p)
115 size_t n = stbm_get_allocation_size(p);
124 uint64_t m_initial_heap_storage[(STBM_HEAP_SIZEOF + initialize_size + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
126 static void *sys_alloc(void *user_context, size_t size_requested, size_t *size_provided)
128 stb_heap *pHeap = static_cast<stb_heap *>(user_context);
130 size_requested = (size_requested + pHeap->m_page_size - 1) & (~(pHeap->m_page_size - 1));
132 void *p = mmap(NULL, size_requested, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
138 *size_provided = size_requested;
144 static void sys_free(void *user_context, void *ptr, size_t size)
146 VOGL_NOTE_UNUSED(user_context);
148 int res = munmap(ptr, size);
151 VOGL_FAIL("sys_free: munmap() failed!\n");
156 VOGL_NAMESPACE_END(vogl)
158 #endif // VOGL_STB_HEAP_H