diff --git a/CMakeLists.txt b/CMakeLists.txt index 61147aa6..534e8b1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,7 +265,6 @@ endforeach() set(LOVR_SRC src/main.c - src/core/arr.c src/core/fs.c src/core/map.c src/core/utf.c diff --git a/Tupfile b/Tupfile index 80396bef..c2539ebc 100644 --- a/Tupfile +++ b/Tupfile @@ -4,7 +4,6 @@ include_rules ifneq (@(PICO),y) SRC += src/main.c endif -SRC += src/core/arr.c SRC += src/core/fs.c SRC += src/core/map.c ifneq (@(PICO),y) diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 7001128a..496fe379 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -10,7 +10,7 @@ #include "data/modelData.h" #include "data/rasterizer.h" #include "data/textureData.h" -#include "core/arr.h" +#include "core/util.h" #include #include #include @@ -1579,7 +1579,7 @@ static int l_lovrGraphicsNewComputeShader(lua_State* L) { static int l_lovrGraphicsNewShaderBlock(lua_State* L) { arr_uniform_t uniforms; - arr_init(&uniforms); + arr_init(&uniforms, realloc); BlockType type = luax_checkenum(L, 1, BlockType, NULL); diff --git a/src/api/l_headset.c b/src/api/l_headset.c index 12352a02..d2691ec5 100644 --- a/src/api/l_headset.c +++ b/src/api/l_headset.c @@ -4,7 +4,6 @@ #include "graphics/graphics.h" #include "graphics/model.h" #include "graphics/texture.h" -#include "core/arr.h" #include "core/maf.h" #include diff --git a/src/core/arr.c b/src/core/arr.c deleted file mode 100644 index 2ec5033e..00000000 --- a/src/core/arr.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "arr.h" -#include "util.h" -#include - -void _arr_reserve(void** data, size_t n, size_t* capacity, size_t stride) { - if (*capacity == 0) { - *capacity = 1; - } - - while (*capacity < n) { - *capacity <<= 1; - lovrAssert(*capacity > 0, "Out of memory"); - } - - *data = realloc(*data, *capacity * stride); - lovrAssert(*data, "Out of memory"); -} diff --git a/src/core/arr.h b/src/core/arr.h deleted file mode 100644 index 01cf2420..00000000 --- a/src/core/arr.h +++ /dev/null @@ -1,43 +0,0 @@ -#include - -#pragma once - -#define arr_t(T)\ - struct { T* data; size_t length, capacity; } - -#define arr_init(a)\ - (a)->data = NULL,\ - (a)->length = 0,\ - (a)->capacity = 0 - -#define arr_free(a)\ - free((a)->data) - -#define arr_reserve(a, n)\ - n > (a)->capacity ?\ - _arr_reserve((void**) &((a)->data), n, &(a)->capacity, sizeof(*(a)->data)) :\ - (void) 0 - -#define arr_expand(a, n)\ - arr_reserve(a, (a)->length + n) - -#define arr_push(a, x)\ - arr_reserve(a, (a)->length + 1),\ - (a)->data[(a)->length++] = x - -#define arr_pop(a)\ - (a)->data[--(a)->length] - -#define arr_append(a, p, n)\ - arr_reserve(a, (a)->length + n),\ - memcpy((a)->data + (a)->length, p, n * sizeof(*(p))),\ - (a)->length += n - -#define arr_splice(a, i, n)\ - memmove((a)->data + (i), (a)->data + ((i) + n), ((a)->length - (n)) * sizeof(*(a)->data)),\ - (a)->length -= n - -#define arr_clear(a)\ - (a)->length = 0 - -void _arr_reserve(void** data, size_t n, size_t* capacity, size_t stride); diff --git a/src/core/util.h b/src/core/util.h index e2d375b6..e450917f 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -62,3 +62,24 @@ static inline uint64_t hash64(const void* data, size_t length) { typedef atomic_uint ref_t; #define lovrRetain(o) if (o) { atomic_fetch_add((ref_t*) (o), 1); } #define lovrRelease(o, f) if (o && atomic_fetch_sub((ref_t*) (o), 1) == 1) f(o) + +// Dynamic Array +typedef void* arr_allocator(void* data, size_t size); +#define arr_t(T) struct { T* data; arr_allocator* alloc; size_t length, capacity; } +#define arr_init(a, allocator) (a)->data = NULL, (a)->length = 0, (a)->capacity = 0, (a)->alloc = allocator +#define arr_free(a) (a)->alloc((a)->data, 0) +#define arr_reserve(a, n) _arr_reserve((void**) &((a)->data), n, &(a)->capacity, sizeof(*(a)->data), (a)->alloc) +#define arr_expand(a, n) arr_reserve(a, (a)->length + n) +#define arr_push(a, x) arr_reserve(a, (a)->length + 1), (a)->data[(a)->length++] = x +#define arr_pop(a) (a)->data[--(a)->length] +#define arr_append(a, p, n) arr_reserve(a, (a)->length + n), memcpy((a)->data + (a)->length, p, n * sizeof(*(p))), (a)->length += n +#define arr_splice(a, i, n) memmove((a)->data + (i), (a)->data + ((i) + n), ((a)->length - (n)) * sizeof(*(a)->data)), (a)->length -= n +#define arr_clear(a) (a)->length = 0 + +static inline void _arr_reserve(void** data, size_t n, size_t* capacity, size_t stride, arr_allocator* allocator) { + if (*capacity >= n) return; + if (*capacity == 0) *capacity = 1; + while (*capacity < n) *capacity *= 2; + *data = allocator(*data, *capacity * stride); + lovrAssert(*data, "Out of memory"); +} diff --git a/src/modules/audio/audio.c b/src/modules/audio/audio.c index de209691..03bb77fe 100644 --- a/src/modules/audio/audio.c +++ b/src/modules/audio/audio.c @@ -1,8 +1,6 @@ #include "audio/audio.h" #include "audio/spatializer.h" #include "data/soundData.h" -#include "core/arr.h" -#include "core/maf.h" #include "core/os.h" #include "core/util.h" #include "lib/miniaudio/miniaudio.h" @@ -204,7 +202,7 @@ bool lovrAudioInit(const char* spatializer) { } lovrAssert(state.spatializer, "Must have at least one spatializer"); - arr_init(&state.converters); + arr_init(&state.converters, realloc); return state.initialized = true; } diff --git a/src/modules/data/modelData_obj.c b/src/modules/data/modelData_obj.c index 56997f92..aa98b85a 100644 --- a/src/modules/data/modelData_obj.c +++ b/src/modules/data/modelData_obj.c @@ -1,7 +1,6 @@ #include "data/modelData.h" #include "data/blob.h" #include "data/textureData.h" -#include "core/arr.h" #include "core/maf.h" #include "core/map.h" #include "core/util.h" @@ -107,16 +106,16 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source, ModelDataIO* io) arr_t(float) normals; arr_t(float) uvs; - arr_init(&groups); - arr_init(&textures); - arr_init(&materials); + arr_init(&groups, realloc); + arr_init(&textures, realloc); + arr_init(&materials, realloc); map_init(&materialMap, 0); - arr_init(&vertexBlob); - arr_init(&indexBlob); + arr_init(&vertexBlob, realloc); + arr_init(&indexBlob, realloc); map_init(&vertexMap, 0); - arr_init(&positions); - arr_init(&normals); - arr_init(&uvs); + arr_init(&positions, realloc); + arr_init(&normals, realloc); + arr_init(&uvs, realloc); arr_push(&groups, ((objGroup) { .material = -1 })); diff --git a/src/modules/event/event.c b/src/modules/event/event.c index 1b1fba9d..e050ac1c 100644 --- a/src/modules/event/event.c +++ b/src/modules/event/event.c @@ -1,6 +1,5 @@ #include "event/event.h" #include "thread/thread.h" -#include "core/arr.h" #include "core/os.h" #include "core/util.h" #include "core/utf.h" @@ -49,7 +48,7 @@ void lovrVariantDestroy(Variant* variant) { bool lovrEventInit() { if (state.initialized) return false; - arr_init(&state.events); + arr_init(&state.events, realloc); lovrPlatformOnKeyboardEvent(onKeyboardEvent); lovrPlatformOnTextEvent(onTextEvent); lovrPlatformOnPermissionEvent(onPermissionEvent); diff --git a/src/modules/filesystem/filesystem.c b/src/modules/filesystem/filesystem.c index 8071d86b..c856b9cb 100644 --- a/src/modules/filesystem/filesystem.c +++ b/src/modules/filesystem/filesystem.c @@ -1,5 +1,4 @@ #include "filesystem/filesystem.h" -#include "core/arr.h" #include "core/fs.h" #include "core/map.h" #include "core/os.h" @@ -112,7 +111,7 @@ bool lovrFilesystemInit(const char* argExe, const char* argGame, const char* arg if (state.initialized) return false; state.initialized = true; - arr_init(&state.archives); + arr_init(&state.archives, realloc); arr_reserve(&state.archives, 2); lovrFilesystemSetRequirePath("?.lua;?/init.lua"); @@ -169,7 +168,7 @@ bool lovrFilesystemMount(const char* path, const char* mountpoint, bool append, } Archive archive; - arr_init(&archive.strings); + arr_init(&archive.strings, realloc); if (!dir_init(&archive, path, mountpoint, root) && !zip_init(&archive, path, mountpoint, root)) { arr_free(&archive.strings); @@ -593,7 +592,7 @@ static void zip_close(Archive* archive) { static bool zip_init(Archive* archive, const char* filename, const char* mountpoint, const char* root) { char path[LOVR_PATH_MAX]; memset(&archive->lookup, 0, sizeof(archive->lookup)); - arr_init(&archive->nodes); + arr_init(&archive->nodes, realloc); // mmap the zip file, try to parse it, and figure out how many files there are archive->zip.data = fs_map(filename, &archive->zip.size); diff --git a/src/modules/graphics/font.c b/src/modules/graphics/font.c index 409d42bc..807da13e 100644 --- a/src/modules/graphics/font.c +++ b/src/modules/graphics/font.c @@ -2,7 +2,6 @@ #include "graphics/texture.h" #include "data/rasterizer.h" #include "data/textureData.h" -#include "core/arr.h" #include "core/map.h" #include "core/utf.h" #include @@ -67,7 +66,7 @@ Font* lovrFontCreate(Rasterizer* rasterizer) { font->atlas.width = 128; font->atlas.height = 128; font->atlas.padding = padding; - arr_init(&font->atlas.glyphs); + arr_init(&font->atlas.glyphs, realloc); map_init(&font->atlas.glyphMap, 0); // Set initial atlas size diff --git a/src/modules/graphics/opengl.c b/src/modules/graphics/opengl.c index 730009a5..347d8998 100644 --- a/src/modules/graphics/opengl.c +++ b/src/modules/graphics/opengl.c @@ -1344,7 +1344,7 @@ void lovrGpuInit(void* (*getProcAddress)(const char*), bool debug) { #endif for (int i = 0; i < MAX_BARRIERS; i++) { - arr_init(&state.incoherents[i]); + arr_init(&state.incoherents[i], realloc); } TextureData* textureData = lovrTextureDataCreate(1, 1, NULL, 0xff, FORMAT_RGBA); @@ -2384,7 +2384,7 @@ static void lovrShaderSetupUniforms(Shader* shader) { lovrAssert((size_t) blockCount <= MAX_BLOCK_BUFFERS, "Shader has too many uniform blocks (%d) the max is %d", blockCount, MAX_BLOCK_BUFFERS); map_init(&shader->blockMap, blockCount); arr_block_t* uniformBlocks = &shader->blocks[BLOCK_UNIFORM]; - arr_init(uniformBlocks); + arr_init(uniformBlocks, realloc); arr_reserve(uniformBlocks, (size_t) blockCount); for (int i = 0; i < blockCount; i++) { UniformBlock block = { .slot = i, .source = NULL }; @@ -2396,12 +2396,12 @@ static void lovrShaderSetupUniforms(Shader* shader) { int blockId = (i << 1) + BLOCK_UNIFORM; map_set(&shader->blockMap, hash64(name, length), blockId); arr_push(uniformBlocks, block); - arr_init(&uniformBlocks->data[uniformBlocks->length - 1].uniforms); + arr_init(&uniformBlocks->data[uniformBlocks->length - 1].uniforms, realloc); } // Shader storage buffers and their buffer variables arr_block_t* computeBlocks = &shader->blocks[BLOCK_COMPUTE]; - arr_init(computeBlocks); + arr_init(computeBlocks, realloc); #ifndef LOVR_WEBGL if ((GLAD_GL_ARB_shader_storage_buffer_object && GLAD_GL_ARB_program_interface_query) || GLAD_GL_ES_VERSION_3_1) { @@ -2417,7 +2417,7 @@ static void lovrShaderSetupUniforms(Shader* shader) { #else glShaderStorageBlockBinding(program, i, block.slot); #endif - arr_init(&block.uniforms); + arr_init(&block.uniforms, realloc); GLsizei length; char name[LOVR_MAX_UNIFORM_LENGTH]; @@ -2459,7 +2459,7 @@ static void lovrShaderSetupUniforms(Shader* shader) { int imageSlot = 0; glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount); map_init(&shader->uniformMap, 0); - arr_init(&shader->uniforms); + arr_init(&shader->uniforms, realloc); for (uint32_t i = 0; i < (uint32_t) uniformCount; i++) { Uniform uniform; GLenum glType; @@ -2870,7 +2870,7 @@ ShaderBlock* lovrShaderBlockCreate(BlockType type, Buffer* buffer, arr_uniform_t lovrAssert(block, "Out of memory"); block->ref = 1; - arr_init(&block->uniforms); + arr_init(&block->uniforms, realloc); map_init(&block->uniformMap, (uint32_t) uniforms->length); arr_append(&block->uniforms, uniforms->data, uniforms->length); diff --git a/src/modules/graphics/shader.h b/src/modules/graphics/shader.h index 5b5605fd..819621d2 100644 --- a/src/modules/graphics/shader.h +++ b/src/modules/graphics/shader.h @@ -1,5 +1,5 @@ #include "graphics/texture.h" -#include "core/arr.h" +#include "core/util.h" #include #pragma once diff --git a/src/modules/headset/headset_oculus.c b/src/modules/headset/headset_oculus.c index 093d4754..606a3213 100644 --- a/src/modules/headset/headset_oculus.c +++ b/src/modules/headset/headset_oculus.c @@ -3,7 +3,6 @@ #include "graphics/graphics.h" #include "graphics/canvas.h" #include "graphics/texture.h" -#include "core/arr.h" #include "core/maf.h" #include "core/map.h" #include diff --git a/src/modules/headset/headset_pico.c b/src/modules/headset/headset_pico.c index 70ececca..30a75a2f 100644 --- a/src/modules/headset/headset_pico.c +++ b/src/modules/headset/headset_pico.c @@ -4,7 +4,6 @@ #include "graphics/canvas.h" #include "resources/boot.lua.h" #include "api/api.h" -#include "core/arr.h" #include "core/maf.h" #include "core/os.h" #include "core/util.h" diff --git a/src/modules/math/curve.c b/src/modules/math/curve.c index 7d572d4a..7e9f83f1 100644 --- a/src/modules/math/curve.c +++ b/src/modules/math/curve.c @@ -1,5 +1,4 @@ #include "math/curve.h" -#include "core/arr.h" #include "core/maf.h" #include "core/util.h" #include @@ -54,7 +53,7 @@ Curve* lovrCurveCreate(void) { Curve* curve = calloc(1, sizeof(Curve)); lovrAssert(curve, "Out of memory"); curve->ref = 1; - arr_init(&curve->points); + arr_init(&curve->points, realloc); arr_reserve(&curve->points, 16); return curve; } diff --git a/src/modules/physics/physics.c b/src/modules/physics/physics.c index 6d49a168..3adbfc34 100644 --- a/src/modules/physics/physics.c +++ b/src/modules/physics/physics.c @@ -61,7 +61,7 @@ World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char world->space = dHashSpaceCreate(0); dHashSpaceSetLevels(world->space, -4, 8); world->contactGroup = dJointGroupCreate(0); - arr_init(&world->overlaps); + arr_init(&world->overlaps, realloc); lovrWorldSetGravity(world, xg, yg, zg); lovrWorldSetSleepingAllowed(world, allowSleep); for (uint32_t i = 0; i < tagCount; i++) { @@ -302,8 +302,8 @@ Collider* lovrColliderCreate(World* world, float x, float y, float z) { collider->restitution = 0; collider->tag = NO_TAG; dBodySetData(collider->body, collider); - arr_init(&collider->shapes); - arr_init(&collider->joints); + arr_init(&collider->shapes, realloc); + arr_init(&collider->joints, realloc); lovrColliderSetPosition(collider, x, y, z); diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index c656c7f3..39937255 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -1,4 +1,3 @@ -#include "core/arr.h" #include "core/maf.h" #include "core/util.h" #include diff --git a/src/modules/thread/channel.c b/src/modules/thread/channel.c index cbf64b52..218ff5e6 100644 --- a/src/modules/thread/channel.c +++ b/src/modules/thread/channel.c @@ -1,6 +1,5 @@ #include "thread/channel.h" #include "event/event.h" -#include "core/arr.h" #include "core/util.h" #include "lib/tinycthread/tinycthread.h" #include @@ -22,7 +21,7 @@ Channel* lovrChannelCreate(uint64_t hash) { Channel* channel = calloc(1, sizeof(Channel)); lovrAssert(channel, "Out of memory"); channel->ref = 1; - arr_init(&channel->messages); + arr_init(&channel->messages, realloc); mtx_init(&channel->lock, mtx_plain | mtx_timed); cnd_init(&channel->cond); channel->hash = hash; diff --git a/src/modules/thread/thread.c b/src/modules/thread/thread.c index a9e9a666..c48fc47b 100644 --- a/src/modules/thread/thread.c +++ b/src/modules/thread/thread.c @@ -1,6 +1,5 @@ #include "thread/thread.h" #include "thread/channel.h" -#include "core/arr.h" #include "core/map.h" #include "core/util.h" #include