diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d9f5398..61147aa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,7 +268,6 @@ set(LOVR_SRC src/core/arr.c src/core/fs.c src/core/map.c - src/core/ref.c src/core/utf.c src/core/util.c src/core/zip.c diff --git a/Tupfile b/Tupfile index fa9b206f..80396bef 100644 --- a/Tupfile +++ b/Tupfile @@ -10,7 +10,6 @@ SRC += src/core/map.c ifneq (@(PICO),y) SRC += src/core/os_$(PLATFORM).c endif -SRC += src/core/ref.c SRC += src/core/utf.c SRC += src/core/util.c SRC += src/core/zip.c diff --git a/src/api/api.c b/src/api/api.c index a1a6e7de..4d8e8ebc 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1,6 +1,5 @@ #include "api.h" #include "core/os.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/api/l_audio.c b/src/api/l_audio.c index 39772713..f53c7bbf 100644 --- a/src/api/l_audio.c +++ b/src/api/l_audio.c @@ -3,7 +3,6 @@ #include "data/blob.h" #include "data/soundData.h" #include "core/maf.h" -#include "core/ref.h" #include "core/util.h" #include diff --git a/src/api/l_data.c b/src/api/l_data.c index 486046d6..1b52c562 100644 --- a/src/api/l_data.c +++ b/src/api/l_data.c @@ -4,7 +4,6 @@ #include "data/rasterizer.h" #include "data/soundData.h" #include "data/textureData.h" -#include "core/ref.h" #include #include diff --git a/src/api/l_event.c b/src/api/l_event.c index d559f498..977a4628 100644 --- a/src/api/l_event.c +++ b/src/api/l_event.c @@ -2,7 +2,6 @@ #include "event/event.h" #include "thread/thread.h" #include "core/os.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/api/l_filesystem.c b/src/api/l_filesystem.c index 1093abf9..f4eb5d9a 100644 --- a/src/api/l_filesystem.c +++ b/src/api/l_filesystem.c @@ -3,7 +3,6 @@ #include "data/blob.h" #include "core/fs.h" #include "core/os.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 9f1e75d7..078ef52e 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -11,7 +11,6 @@ #include "data/rasterizer.h" #include "data/textureData.h" #include "core/arr.h" -#include "core/ref.h" #include #include #include diff --git a/src/api/l_graphics_canvas.c b/src/api/l_graphics_canvas.c index 7f1ac1bb..6667a8fe 100644 --- a/src/api/l_graphics_canvas.c +++ b/src/api/l_graphics_canvas.c @@ -1,7 +1,6 @@ #include "api.h" #include "graphics/canvas.h" #include "graphics/graphics.h" -#include "core/ref.h" #include static int luax_checkattachment(lua_State* L, int index, Attachment* attachment) { diff --git a/src/api/l_graphics_mesh.c b/src/api/l_graphics_mesh.c index 5288c8c9..212492ba 100644 --- a/src/api/l_graphics_mesh.c +++ b/src/api/l_graphics_mesh.c @@ -4,7 +4,6 @@ #include "graphics/material.h" #include "graphics/mesh.h" #include "data/blob.h" -#include "core/ref.h" #include static int l_lovrMeshAttachAttributes(lua_State* L) { diff --git a/src/api/l_headset.c b/src/api/l_headset.c index aa6488de..c3f472fc 100644 --- a/src/api/l_headset.c +++ b/src/api/l_headset.c @@ -6,7 +6,6 @@ #include "graphics/texture.h" #include "core/arr.h" #include "core/maf.h" -#include "core/ref.h" #include StringEntry lovrHeadsetDriver[] = { diff --git a/src/api/l_math.c b/src/api/l_math.c index 1df84554..f3ad60a5 100644 --- a/src/api/l_math.c +++ b/src/api/l_math.c @@ -4,7 +4,6 @@ #include "math/pool.h" #include "math/randomGenerator.h" #include "core/maf.h" -#include "core/ref.h" #include "core/util.h" #include diff --git a/src/api/l_physics.c b/src/api/l_physics.c index 14c09d22..1902bd4c 100644 --- a/src/api/l_physics.c +++ b/src/api/l_physics.c @@ -1,6 +1,5 @@ #include "api.h" #include "physics/physics.h" -#include "core/ref.h" #include "core/util.h" StringEntry lovrShapeType[] = { diff --git a/src/api/l_physics_world.c b/src/api/l_physics_world.c index 0e7e2d17..3c15ad74 100644 --- a/src/api/l_physics_world.c +++ b/src/api/l_physics_world.c @@ -1,6 +1,5 @@ #include "api.h" #include "physics/physics.h" -#include "core/ref.h" #include "core/util.h" #include diff --git a/src/api/l_thread.c b/src/api/l_thread.c index 5753f689..2ec95676 100644 --- a/src/api/l_thread.c +++ b/src/api/l_thread.c @@ -2,7 +2,6 @@ #include "event/event.h" #include "thread/thread.h" #include "thread/channel.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/core/ref.c b/src/core/ref.c deleted file mode 100644 index 3ecbf37f..00000000 --- a/src/core/ref.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "ref.h" -#include "util.h" -#include - -void* _lovrAlloc(size_t size) { - char* ref = calloc(1, sizeof(size_t) + size); - lovrAssert(ref, "Out of memory"); - *((Ref*) ref) = 1; - return ref + sizeof(size_t); -} diff --git a/src/core/ref.h b/src/core/ref.h deleted file mode 100644 index 10e56de5..00000000 --- a/src/core/ref.h +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -#pragma once - -#ifndef __has_builtin -#define __has_builtin(x) 0 -#endif - -#ifdef LOVR_DISABLE_THREAD - -// Thread module is off, don't use atomics - -typedef uint32_t Ref; -static inline uint32_t ref_inc(Ref* ref) { return ++*ref; } -static inline uint32_t ref_dec(Ref* ref) { return --*ref; } - -#elif defined(_MSC_VER) - -// MSVC atomics - -#include -typedef uint32_t Ref; -static inline uint32_t ref_inc(Ref* ref) { return _InterlockedIncrement((volatile long*) ref); } -static inline uint32_t ref_dec(Ref* ref) { return _InterlockedDecrement((volatile long*) ref); } - -#elif (defined(__GNUC_MINOR__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))) \ - || (__has_builtin(__atomic_add_fetch) && __has_builtin(__atomic_sub_fetch)) - -// GCC/Clang atomics - -typedef uint32_t Ref; -static inline uint32_t ref_inc(Ref* ref) { return __atomic_add_fetch(ref, 1, __ATOMIC_SEQ_CST); } -static inline uint32_t ref_dec(Ref* ref) { return __atomic_sub_fetch(ref, 1, __ATOMIC_SEQ_CST); } - -#else - -// No known compiler-specific atomics-- fall back to C11 atomics - -// stdatomic.h doesn't work in c++ (except on Android, where it is fine) -#if !defined(__ANDROID__) && defined(__cplusplus) -#error "The header core/ref.h cannot currently be included from C++ when threading is enabled with this compiler. Either remove your ref.h include from any C++ files, or rebuild LOVR with -DLOVR_DISABLE_THREAD=ON" -#endif - -#include -typedef _Atomic(uint32_t) Ref; -static inline uint32_t ref_inc(Ref* ref) { return atomic_fetch_add(ref, 1) + 1; } -static inline uint32_t ref_dec(Ref* ref) { return atomic_fetch_sub(ref, 1) - 1; } - -#endif - -void* _lovrAlloc(size_t size); -#define toRef(o) ((Ref*) (((char*) (o)) - sizeof(size_t))) -#define lovrAlloc(T) (T*) _lovrAlloc(sizeof(T)) -#define lovrRetain(o) if (o && !ref_inc(toRef(o))) { lovrThrow("Refcount overflow"); } -#define lovrRelease(T, o) if (o && !ref_dec(toRef(o))) lovr ## T ## Destroy(o), free(toRef(o)); -#define _lovrRelease(o, f) if (o && !ref_dec(toRef(o))) f(o), free(toRef(o)); diff --git a/src/core/util.h b/src/core/util.h index 00666b8e..5017effd 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -1,6 +1,7 @@ #include #include #include +#include #pragma once @@ -56,3 +57,9 @@ static inline uint64_t hash64(const void* data, size_t length) { } return hash; } + +// Refcounting +typedef atomic_uint ref_t; +#define lovrRetain(o) if (o) { atomic_fetch_add((ref_t*) (o), 1); } +#define lovrRelease(T, o) if (o && atomic_fetch_sub((ref_t*) (o), 1) == 1) lovr ## T ## Destroy(o), free(o); +#define _lovrRelease(o, f) if (o && atomic_fetch_sub((ref_t*) (o), 1) == 1) f(o), free(o); diff --git a/src/modules/audio/audio.c b/src/modules/audio/audio.c index 53cf51ef..2a75c9f8 100644 --- a/src/modules/audio/audio.c +++ b/src/modules/audio/audio.c @@ -2,7 +2,7 @@ #include "audio/spatializer.h" #include "data/soundData.h" #include "core/arr.h" -#include "core/ref.h" +#include "core/maf.h" #include "core/os.h" #include "core/util.h" #include "lib/miniaudio/miniaudio.h" @@ -21,6 +21,7 @@ static const ma_format miniaudioFormats[] = { #define BUFFER_SIZE 256 struct Source { + ref_t ref; Source* next; SoundData* sound; ma_data_converter* converter; @@ -324,7 +325,9 @@ struct SoundData* lovrAudioGetCaptureStream() { // Source Source* lovrSourceCreate(SoundData* sound, bool spatial) { - Source* source = lovrAlloc(Source); + Source* source = calloc(1, sizeof(Source)); + lovrAssert(source, "Out of memory"); + source->ref = 1; source->sound = sound; lovrRetain(source->sound); diff --git a/src/modules/data/blob.c b/src/modules/data/blob.c index e699b19a..a1af4201 100644 --- a/src/modules/data/blob.c +++ b/src/modules/data/blob.c @@ -1,7 +1,10 @@ #include "data/blob.h" #include -Blob* lovrBlobInit(Blob* blob, void* data, size_t size, const char* name) { +Blob* lovrBlobCreate(void* data, size_t size, const char* name) { + Blob* blob = calloc(1, sizeof(Blob)); + lovrAssert(blob, "Out of memory"); + blob->ref = 1; blob->data = data; blob->size = size; blob->name = name; diff --git a/src/modules/data/blob.h b/src/modules/data/blob.h index f534f0b1..f835a801 100644 --- a/src/modules/data/blob.h +++ b/src/modules/data/blob.h @@ -1,13 +1,14 @@ #include +#include "core/util.h" #pragma once typedef struct Blob { + ref_t ref; void* data; size_t size; const char* name; } Blob; -Blob* lovrBlobInit(Blob* blob, void* data, size_t size, const char* name); -#define lovrBlobCreate(...) lovrBlobInit(lovrAlloc(Blob), __VA_ARGS__) +Blob* lovrBlobCreate(void* data, size_t size, const char* name); void lovrBlobDestroy(void* ref); diff --git a/src/modules/data/modelData.c b/src/modules/data/modelData.c index 9c5c73f4..79e8709b 100644 --- a/src/modules/data/modelData.c +++ b/src/modules/data/modelData.c @@ -1,10 +1,13 @@ #include "data/modelData.h" #include "data/blob.h" #include "data/textureData.h" -#include "core/ref.h" #include -ModelData* lovrModelDataInit(ModelData* model, Blob* source, ModelDataIO* io) { +ModelData* lovrModelDataCreate(Blob* source, ModelDataIO* io) { + ModelData* model = calloc(1, sizeof(ModelData)); + lovrAssert(model, "Out of memory"); + model->ref = 1; + if (lovrModelDataInitGltf(model, source, io)) { return model; } else if (lovrModelDataInitObj(model, source, io)) { diff --git a/src/modules/data/modelData.h b/src/modules/data/modelData.h index 979713d4..54c8994a 100644 --- a/src/modules/data/modelData.h +++ b/src/modules/data/modelData.h @@ -179,6 +179,7 @@ typedef struct { } ModelSkin; typedef struct ModelData { + ref_t ref; void* data; struct Blob** blobs; ModelBuffer* buffers; @@ -217,8 +218,7 @@ typedef struct ModelData { typedef void* ModelDataIO(const char* filename, size_t* bytesRead); -ModelData* lovrModelDataInit(ModelData* model, struct Blob* blob, ModelDataIO* io); -#define lovrModelDataCreate(...) lovrModelDataInit(lovrAlloc(ModelData), __VA_ARGS__) +ModelData* lovrModelDataCreate(struct Blob* blob, ModelDataIO* io); ModelData* lovrModelDataInitGltf(ModelData* model, struct Blob* blob, ModelDataIO* io); ModelData* lovrModelDataInitObj(ModelData* model, struct Blob* blob, ModelDataIO* io); void lovrModelDataDestroy(void* ref); diff --git a/src/modules/data/modelData_gltf.c b/src/modules/data/modelData_gltf.c index bbdee8d3..435b69c0 100644 --- a/src/modules/data/modelData_gltf.c +++ b/src/modules/data/modelData_gltf.c @@ -2,7 +2,6 @@ #include "data/blob.h" #include "data/textureData.h" #include "core/maf.h" -#include "core/ref.h" #include "lib/jsmn/jsmn.h" #include #include diff --git a/src/modules/data/modelData_obj.c b/src/modules/data/modelData_obj.c index 6b4c86ba..876270c0 100644 --- a/src/modules/data/modelData_obj.c +++ b/src/modules/data/modelData_obj.c @@ -4,7 +4,6 @@ #include "core/arr.h" #include "core/maf.h" #include "core/map.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/modules/data/rasterizer.c b/src/modules/data/rasterizer.c index 79fbcf27..8ea9003f 100644 --- a/src/modules/data/rasterizer.c +++ b/src/modules/data/rasterizer.c @@ -2,7 +2,6 @@ #include "data/blob.h" #include "data/textureData.h" #include "resources/VarelaRound.ttf.h" -#include "core/ref.h" #include "core/utf.h" #include "lib/stb/stb_truetype.h" #include @@ -10,7 +9,11 @@ #include #include -Rasterizer* lovrRasterizerInit(Rasterizer* rasterizer, Blob* blob, float size) { +Rasterizer* lovrRasterizerCreate(Blob* blob, float size) { + Rasterizer* rasterizer = calloc(1, sizeof(Rasterizer)); + lovrAssert(rasterizer, "Out of memory"); + rasterizer->ref = 1; + stbtt_fontinfo* font = &rasterizer->font; const unsigned char* data = blob ? blob->data : src_resources_VarelaRound_ttf; if (!stbtt_InitFont(font, data, stbtt_GetFontOffsetForIndex(data, 0))) { diff --git a/src/modules/data/rasterizer.h b/src/modules/data/rasterizer.h index 84763de1..2607bdd7 100644 --- a/src/modules/data/rasterizer.h +++ b/src/modules/data/rasterizer.h @@ -1,4 +1,5 @@ #include "lib/stb/stb_truetype.h" +#include "core/util.h" #include #include @@ -10,6 +11,7 @@ struct Blob; struct TextureData; typedef struct Rasterizer { + ref_t ref; stbtt_fontinfo font; struct Blob* blob; float size; @@ -34,8 +36,7 @@ typedef struct { struct TextureData* data; } Glyph; -Rasterizer* lovrRasterizerInit(Rasterizer* rasterizer, struct Blob* blob, float size); -#define lovrRasterizerCreate(...) lovrRasterizerInit(lovrAlloc(Rasterizer), __VA_ARGS__) +Rasterizer* lovrRasterizerCreate(struct Blob* blob, float size); void lovrRasterizerDestroy(void* ref); bool lovrRasterizerHasGlyph(Rasterizer* fontData, uint32_t character); bool lovrRasterizerHasGlyphs(Rasterizer* fontData, const char* str); diff --git a/src/modules/data/soundData.c b/src/modules/data/soundData.c index ce5a3a02..2beeb959 100644 --- a/src/modules/data/soundData.c +++ b/src/modules/data/soundData.c @@ -1,7 +1,6 @@ #include "data/soundData.h" #include "data/blob.h" #include "core/util.h" -#include "core/ref.h" #include "lib/stb/stb_vorbis.h" #include "lib/miniaudio/miniaudio.h" #define MINIMP3_FLOAT_OUTPUT @@ -16,6 +15,7 @@ static const ma_format miniaudioFormats[] = { }; struct SoundData { + ref_t ref; uint32_t (*read)(SoundData* soundData, uint32_t offset, uint32_t count, void* data); struct Blob* blob; void* decoder; @@ -73,7 +73,9 @@ static uint32_t lovrSoundDataReadMp3(SoundData* soundData, uint32_t offset, uint // SoundData SoundData* lovrSoundDataCreateRaw(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate, struct Blob* blob) { - SoundData* soundData = lovrAlloc(SoundData); + SoundData* soundData = calloc(1, sizeof(SoundData)); + lovrAssert(soundData, "Out of memory"); + soundData->ref = 1; soundData->frames = frames; soundData->format = format; soundData->channels = channels; @@ -92,7 +94,9 @@ SoundData* lovrSoundDataCreateRaw(uint32_t frames, SampleFormat format, uint32_t } SoundData* lovrSoundDataCreateStream(uint32_t frames, SampleFormat format, uint32_t channels, uint32_t sampleRate) { - SoundData* soundData = lovrAlloc(SoundData); + SoundData* soundData = calloc(1, sizeof(SoundData)); + lovrAssert(soundData, "Out of memory"); + soundData->ref = 1; soundData->frames = frames; soundData->format = format; soundData->channels = channels; @@ -110,7 +114,9 @@ SoundData* lovrSoundDataCreateStream(uint32_t frames, SampleFormat format, uint3 } SoundData* lovrSoundDataCreateFromFile(struct Blob* blob, bool decode) { - SoundData* soundData = lovrAlloc(SoundData); + SoundData* soundData = calloc(1, sizeof(SoundData)); + lovrAssert(soundData, "Out of memory"); + soundData->ref = 1; if (blob->size >= 4 && !memcmp(blob->data, "OggS", 4)) { soundData->decoder = stb_vorbis_open_memory(blob->data, (int) blob->size, NULL, NULL); diff --git a/src/modules/data/textureData.c b/src/modules/data/textureData.c index c2659393..fd457083 100644 --- a/src/modules/data/textureData.c +++ b/src/modules/data/textureData.c @@ -1,5 +1,4 @@ #include "data/textureData.h" -#include "core/ref.h" #include "lib/stb/stb_image.h" #include #include @@ -453,7 +452,10 @@ static bool parseASTC(uint8_t* bytes, size_t size, TextureData* textureData) { return true; } -TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format) { +TextureData* lovrTextureDataCreate(uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format) { + TextureData* textureData = calloc(1, sizeof(TextureData)); + lovrAssert(textureData, "Out of memory"); + textureData->ref = 1; size_t pixelSize = getPixelSize(format); size_t size = width * height * pixelSize; lovrAssert(width > 0 && height > 0, "TextureData dimensions must be positive"); @@ -473,8 +475,11 @@ TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint3 return textureData; } -TextureData* lovrTextureDataInitFromBlob(TextureData* textureData, Blob* blob, bool flip) { - textureData->blob = lovrAlloc(Blob); +TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip) { + TextureData* textureData = calloc(1, sizeof(TextureData)); + lovrAssert(textureData, "Out of memory"); + textureData->ref = 1; + textureData->blob = lovrBlobCreate(NULL, 0, NULL); if (parseDDS(blob->data, blob->size, textureData)) { textureData->source = blob; lovrRetain(blob); diff --git a/src/modules/data/textureData.h b/src/modules/data/textureData.h index a66dce20..4f022738 100644 --- a/src/modules/data/textureData.h +++ b/src/modules/data/textureData.h @@ -51,6 +51,7 @@ typedef struct { } Mipmap; typedef struct TextureData { + ref_t ref; Blob* blob; uint32_t width; uint32_t height; @@ -60,10 +61,8 @@ typedef struct TextureData { uint32_t mipmapCount; } TextureData; -TextureData* lovrTextureDataInit(TextureData* textureData, uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format); -TextureData* lovrTextureDataInitFromBlob(TextureData* textureData, Blob* blob, bool flip); -#define lovrTextureDataCreate(...) lovrTextureDataInit(lovrAlloc(TextureData), __VA_ARGS__) -#define lovrTextureDataCreateFromBlob(...) lovrTextureDataInitFromBlob(lovrAlloc(TextureData), __VA_ARGS__) +TextureData* lovrTextureDataCreate(uint32_t width, uint32_t height, Blob* contents, uint8_t value, TextureFormat format); +TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip); Color lovrTextureDataGetPixel(TextureData* textureData, uint32_t x, uint32_t y); void lovrTextureDataSetPixel(TextureData* textureData, uint32_t x, uint32_t y, Color color); Blob* lovrTextureDataEncode(TextureData* textureData); diff --git a/src/modules/event/event.c b/src/modules/event/event.c index 55f76974..74cc5c5c 100644 --- a/src/modules/event/event.c +++ b/src/modules/event/event.c @@ -2,7 +2,6 @@ #include "thread/thread.h" #include "core/arr.h" #include "core/os.h" -#include "core/ref.h" #include "core/util.h" #include "core/utf.h" #include diff --git a/src/modules/graphics/font.c b/src/modules/graphics/font.c index a9960fed..65cc6b46 100644 --- a/src/modules/graphics/font.c +++ b/src/modules/graphics/font.c @@ -4,7 +4,6 @@ #include "data/textureData.h" #include "core/arr.h" #include "core/map.h" -#include "core/ref.h" #include "core/utf.h" #include #include @@ -21,6 +20,7 @@ typedef struct { } FontAtlas; struct Font { + ref_t ref; Rasterizer* rasterizer; Texture* texture; FontAtlas atlas; @@ -50,7 +50,10 @@ static void lovrFontExpandTexture(Font* font); static void lovrFontCreateTexture(Font* font); Font* lovrFontCreate(Rasterizer* rasterizer) { - Font* font = lovrAlloc(Font); + Font* font = calloc(1, sizeof(Font)); + lovrAssert(font, "Out of memory"); + font->ref = 1; + lovrRetain(rasterizer); font->rasterizer = rasterizer; font->lineHeight = 1.f; diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 1c658061..4dc259f3 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -9,7 +9,6 @@ #include "event/event.h" #include "math/math.h" #include "core/maf.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/modules/graphics/material.c b/src/modules/graphics/material.c index 83e4a679..f89a94d2 100644 --- a/src/modules/graphics/material.c +++ b/src/modules/graphics/material.c @@ -3,11 +3,14 @@ #include "graphics/shader.h" #include "graphics/texture.h" #include "resources/shaders.h" -#include "core/ref.h" #include #include -Material* lovrMaterialInit(Material* material) { +Material* lovrMaterialCreate() { + Material* material = calloc(1, sizeof(Material)); + lovrAssert(material, "Out of memory"); + material->ref = 1; + for (int i = 0; i < MAX_MATERIAL_SCALARS; i++) { material->scalars[i] = 1.f; } diff --git a/src/modules/graphics/material.h b/src/modules/graphics/material.h index 60667c0f..722d054d 100644 --- a/src/modules/graphics/material.h +++ b/src/modules/graphics/material.h @@ -7,14 +7,14 @@ struct Texture; struct Shader; typedef struct Material { + ref_t ref; float scalars[MAX_MATERIAL_SCALARS]; Color colors[MAX_MATERIAL_COLORS]; struct Texture* textures[MAX_MATERIAL_TEXTURES]; float transform[9]; } Material; -Material* lovrMaterialInit(Material* material); -#define lovrMaterialCreate() lovrMaterialInit(lovrAlloc(Material)) +Material* lovrMaterialCreate(void); void lovrMaterialDestroy(void* ref); void lovrMaterialBind(Material* material, struct Shader* shader); float lovrMaterialGetScalar(Material* material, MaterialScalar scalarType); diff --git a/src/modules/graphics/model.c b/src/modules/graphics/model.c index 11b452a2..de5113e7 100644 --- a/src/modules/graphics/model.c +++ b/src/modules/graphics/model.c @@ -6,7 +6,6 @@ #include "graphics/texture.h" #include "resources/shaders.h" #include "core/maf.h" -#include "core/ref.h" #include #include #include @@ -16,6 +15,7 @@ typedef struct { } NodeTransform; struct Model { + ref_t ref; struct ModelData* data; struct Buffer** buffers; struct Mesh** meshes; @@ -76,7 +76,9 @@ static void renderNode(Model* model, uint32_t nodeIndex, uint32_t instances) { } Model* lovrModelCreate(ModelData* data) { - Model* model = lovrAlloc(Model); + Model* model = calloc(1, sizeof(Model)); + lovrAssert(model, "Out of memory"); + model->ref = 1; model->data = data; lovrRetain(data); diff --git a/src/modules/graphics/opengl.c b/src/modules/graphics/opengl.c index 8fec2216..01e3f0f6 100644 --- a/src/modules/graphics/opengl.c +++ b/src/modules/graphics/opengl.c @@ -8,7 +8,6 @@ #include "resources/shaders.h" #include "data/modelData.h" #include "math/math.h" -#include "core/ref.h" #include #include #include @@ -40,6 +39,7 @@ #define LOVR_SHADER_DRAW_ID 7 struct Buffer { + ref_t ref; uint32_t id; void* data; size_t size; @@ -53,6 +53,7 @@ struct Buffer { }; struct Texture { + ref_t ref; GLuint id; GLuint msaaId; GLenum target; @@ -74,6 +75,7 @@ struct Texture { }; struct Canvas { + ref_t ref; uint32_t framebuffer; uint32_t resolveBuffer; uint32_t depthBuffer; @@ -89,6 +91,7 @@ struct Canvas { }; struct ShaderBlock { + ref_t ref; BlockType type; arr_uniform_t uniforms; map_t uniformMap; @@ -96,6 +99,7 @@ struct ShaderBlock { }; struct Shader { + ref_t ref; uint32_t program; ShaderType type; arr_uniform_t uniforms; @@ -107,6 +111,7 @@ struct Shader { }; struct Mesh { + ref_t ref; uint32_t vao; uint32_t ibo; DrawMode mode; @@ -1665,13 +1670,15 @@ const GpuStats* lovrGpuGetStats() { // Texture Texture* lovrTextureCreate(TextureType type, TextureData** slices, uint32_t sliceCount, bool srgb, bool mipmaps, uint32_t msaa) { - Texture* texture = lovrAlloc(Texture); - state.stats.textureCount++; + Texture* texture = calloc(1, sizeof(Texture)); + lovrAssert(texture, "Out of memory"); + texture->ref = 1; texture->type = type; texture->srgb = srgb; texture->mipmaps = mipmaps; texture->target = convertTextureTarget(type); texture->compareMode = COMPARE_NONE; + state.stats.textureCount++; WrapMode wrap = type == TEXTURE_CUBE ? WRAP_CLAMP : WRAP_REPEAT; glGenTextures(1, &texture->id); @@ -1694,13 +1701,15 @@ Texture* lovrTextureCreate(TextureType type, TextureData** slices, uint32_t slic } Texture* lovrTextureCreateFromHandle(uint32_t handle, TextureType type, uint32_t depth, uint32_t msaa) { - Texture* texture = lovrAlloc(Texture); - state.stats.textureCount++; + Texture* texture = calloc(1, sizeof(Texture)); + lovrAssert(texture, "Out of memory"); + texture->ref = 1; texture->type = type; texture->id = handle; texture->target = convertTextureTarget(type); texture->compareMode = COMPARE_NONE; texture->native = true; + state.stats.textureCount++; int width, height; lovrGpuBindTexture(texture, 0); @@ -1978,7 +1987,10 @@ void lovrTextureSetWrap(Texture* texture, TextureWrap wrap) { // Canvas Canvas* lovrCanvasCreate(uint32_t width, uint32_t height, CanvasFlags flags) { - Canvas* canvas = lovrAlloc(Canvas); + Canvas* canvas = calloc(1, sizeof(Canvas)); + lovrAssert(canvas, "Out of memory"); + canvas->ref = 1; + if (flags.stereo && state.singlepass != MULTIVIEW) { width *= 2; } @@ -2023,7 +2035,9 @@ Canvas* lovrCanvasCreate(uint32_t width, uint32_t height, CanvasFlags flags) { } Canvas* lovrCanvasCreateFromHandle(uint32_t width, uint32_t height, CanvasFlags flags, uint32_t framebuffer, uint32_t depthBuffer, uint32_t resolveBuffer, uint32_t attachmentCount, bool immortal) { - Canvas* canvas = lovrAlloc(Canvas); + Canvas* canvas = calloc(1, sizeof(Canvas)); + lovrAssert(canvas, "Out of memory"); + canvas->ref = 1; canvas->framebuffer = framebuffer; canvas->depthBuffer = depthBuffer; canvas->resolveBuffer = resolveBuffer; @@ -2200,7 +2214,10 @@ Texture* lovrCanvasGetDepthTexture(Canvas* canvas) { // Buffer Buffer* lovrBufferCreate(size_t size, void* data, BufferType type, BufferUsage usage, bool readable) { - Buffer* buffer = lovrAlloc(Buffer); + Buffer* buffer = calloc(1, sizeof(Buffer)); + lovrAssert(buffer, "Out of memory"); + buffer->ref = 1; + state.stats.bufferCount++; state.stats.bufferMemory += size; buffer->size = size; @@ -2591,7 +2608,10 @@ static char* lovrShaderGetFlagCode(ShaderFlag* flags, uint32_t flagCount) { } Shader* lovrShaderCreateGraphics(const char* vertexSource, int vertexSourceLength, const char* fragmentSource, int fragmentSourceLength, ShaderFlag* flags, uint32_t flagCount, bool multiview) { - Shader* shader = lovrAlloc(Shader); + Shader* shader = calloc(1, sizeof(Shader)); + lovrAssert(shader, "Out of memory"); + shader->ref = 1; + #if defined(LOVR_WEBGL) || defined(LOVR_GLES) const char* version = "#version 300 es\n"; #else @@ -2687,7 +2707,9 @@ Shader* lovrShaderCreateDefault(DefaultShader type, ShaderFlag* flags, uint32_t } Shader* lovrShaderCreateCompute(const char* source, int length, ShaderFlag* flags, uint32_t flagCount) { - Shader* shader = lovrAlloc(Shader); + Shader* shader = calloc(1, sizeof(Shader)); + lovrAssert(shader, "Out of memory"); + shader->ref = 1; #ifdef LOVR_WEBGL lovrThrow("Compute shaders are not supported on this system"); #else @@ -2840,7 +2862,10 @@ size_t lovrShaderComputeUniformLayout(arr_uniform_t* uniforms) { } ShaderBlock* lovrShaderBlockCreate(BlockType type, Buffer* buffer, arr_uniform_t* uniforms) { - ShaderBlock* block = lovrAlloc(ShaderBlock); + ShaderBlock* block = calloc(1, sizeof(ShaderBlock)); + lovrAssert(block, "Out of memory"); + block->ref = 1; + arr_init(&block->uniforms); map_init(&block->uniformMap, (uint32_t) uniforms->length); @@ -2931,7 +2956,9 @@ Buffer* lovrShaderBlockGetBuffer(ShaderBlock* block) { // Mesh Mesh* lovrMeshCreate(DrawMode mode, Buffer* vertexBuffer, uint32_t vertexCount) { - Mesh* mesh = lovrAlloc(Mesh); + Mesh* mesh = calloc(1, sizeof(Mesh)); + lovrAssert(mesh, "Out of memory"); + mesh->ref = 1; mesh->mode = mode; mesh->vertexBuffer = vertexBuffer; mesh->vertexCount = vertexCount; diff --git a/src/modules/headset/headset_oculus.c b/src/modules/headset/headset_oculus.c index 9714d697..b7d0efee 100644 --- a/src/modules/headset/headset_oculus.c +++ b/src/modules/headset/headset_oculus.c @@ -6,7 +6,6 @@ #include "core/arr.h" #include "core/maf.h" #include "core/map.h" -#include "core/ref.h" #include #include #include diff --git a/src/modules/headset/headset_openvr.c b/src/modules/headset/headset_openvr.c index d1b3b2b3..9f014597 100644 --- a/src/modules/headset/headset_openvr.c +++ b/src/modules/headset/headset_openvr.c @@ -23,7 +23,6 @@ #include "graphics/model.h" #include "core/maf.h" #include "core/os.h" -#include "core/ref.h" #include #include #include @@ -618,7 +617,9 @@ static ModelData* openvr_newModelData(Device device, bool animated) { } } - ModelData* model = lovrAlloc(ModelData); + ModelData* model = calloc(1, sizeof(ModelData)); + lovrAssert(model, "Out of memory"); + model->ref = 1; model->blobCount = 2; model->nodeCount = animated ? (1 + modelCount) : 1; model->bufferCount = 2 * modelCount; diff --git a/src/modules/headset/headset_openxr.c b/src/modules/headset/headset_openxr.c index 88a7766c..6b9ab084 100644 --- a/src/modules/headset/headset_openxr.c +++ b/src/modules/headset/headset_openxr.c @@ -4,7 +4,6 @@ #include "graphics/graphics.h" #include "graphics/canvas.h" #include "graphics/texture.h" -#include "core/ref.h" #include "core/util.h" #include #include diff --git a/src/modules/headset/headset_vrapi.c b/src/modules/headset/headset_vrapi.c index a7a9431f..1db0a0df 100644 --- a/src/modules/headset/headset_vrapi.c +++ b/src/modules/headset/headset_vrapi.c @@ -5,7 +5,6 @@ #include "graphics/model.h" #include "core/maf.h" #include "core/os.h" -#include "core/ref.h" #include #include #include @@ -470,7 +469,9 @@ static struct ModelData* vrapi_newModelData(Device device, bool animated) { return NULL; } - ModelData* model = lovrAlloc(ModelData); + ModelData* model = calloc(1, sizeof(ModelData)); + lovrAssert(model, "Out of memory"); + model->ref = 1; model->blobCount = 2; model->bufferCount = 6; model->attributeCount = 6; diff --git a/src/modules/math/curve.c b/src/modules/math/curve.c index 66e12c45..79c3c64f 100644 --- a/src/modules/math/curve.c +++ b/src/modules/math/curve.c @@ -1,12 +1,12 @@ #include "math/curve.h" #include "core/arr.h" #include "core/maf.h" -#include "core/ref.h" #include "core/util.h" #include #include struct Curve { + ref_t ref; arr_t(float) points; }; @@ -51,7 +51,9 @@ static void evaluate(float* LOVR_RESTRICT P, size_t n, float t, vec3 p) { } Curve* lovrCurveCreate(void) { - Curve* curve = lovrAlloc(Curve); + Curve* curve = calloc(1, sizeof(Curve)); + lovrAssert(curve, "Out of memory"); + curve->ref = 1; arr_init(&curve->points); arr_reserve(&curve->points, 16); return curve; diff --git a/src/modules/math/math.c b/src/modules/math/math.c index 8af81ea2..6f1dca10 100644 --- a/src/modules/math/math.c +++ b/src/modules/math/math.c @@ -1,7 +1,6 @@ #include "math.h" #include "math/randomGenerator.h" #include "core/maf.h" -#include "core/ref.h" #include "core/util.h" #include "lib/noise1234/noise1234.h" #include diff --git a/src/modules/math/pool.c b/src/modules/math/pool.c index afad93dd..ec35c042 100644 --- a/src/modules/math/pool.c +++ b/src/modules/math/pool.c @@ -10,7 +10,10 @@ static const size_t vectorComponents[] = { [V_MAT4] = 16 }; -Pool* lovrPoolInit(Pool* pool) { +Pool* lovrPoolCreate() { + Pool* pool = calloc(1, sizeof(Pool)); + lovrAssert(pool, "Out of memory"); + pool->ref = 1; lovrPoolGrow(pool, 1 << 12); return pool; } diff --git a/src/modules/math/pool.h b/src/modules/math/pool.h index 4e8e3c9a..aaefda5e 100644 --- a/src/modules/math/pool.h +++ b/src/modules/math/pool.h @@ -1,5 +1,6 @@ #include #include +#include "core/util.h" #pragma once @@ -24,14 +25,14 @@ typedef union { } Vector; typedef struct Pool { + ref_t ref; float* data; size_t count; size_t cursor; size_t generation; } Pool; -Pool* lovrPoolInit(Pool* pool); -#define lovrPoolCreate(...) lovrPoolInit(lovrAlloc(Pool)) +Pool* lovrPoolCreate(void); void lovrPoolDestroy(void* ref); void lovrPoolGrow(Pool* pool, size_t count); Vector lovrPoolAllocate(Pool* pool, VectorType type, float** data); diff --git a/src/modules/math/randomGenerator.c b/src/modules/math/randomGenerator.c index 4513a061..a8ac3d32 100644 --- a/src/modules/math/randomGenerator.c +++ b/src/modules/math/randomGenerator.c @@ -1,5 +1,4 @@ #include "math/randomGenerator.h" -#include "core/ref.h" #include "core/util.h" #include #include @@ -7,6 +6,7 @@ #include struct RandomGenerator { + ref_t ref; Seed seed; Seed state; double lastRandomNormal; @@ -30,7 +30,9 @@ static uint64_t wangHash64(uint64_t key) { // Use an 'Xorshift*' variant, as shown here: http://xorshift.di.unimi.it RandomGenerator* lovrRandomGeneratorCreate(void) { - RandomGenerator* generator = lovrAlloc(RandomGenerator); + RandomGenerator* generator = calloc(1, sizeof(RandomGenerator)); + lovrAssert(generator, "Out of memory"); + generator->ref = 1; Seed seed = { .b32 = { .lo = 0xCBBF7A44, .hi = 0x0139408D } }; lovrRandomGeneratorSetSeed(generator, seed); generator->lastRandomNormal = HUGE_VAL; diff --git a/src/modules/physics/physics.c b/src/modules/physics/physics.c index 5d235868..9888b776 100644 --- a/src/modules/physics/physics.c +++ b/src/modules/physics/physics.c @@ -1,5 +1,4 @@ #include "physics.h" -#include "core/ref.h" #include "core/util.h" #include #include @@ -54,7 +53,10 @@ void lovrPhysicsDestroy() { initialized = false; } -World* lovrWorldInit(World* world, float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount) { +World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount) { + World* world = calloc(1, sizeof(World)); + lovrAssert(world, "Out of memory"); + world->ref = 1; world->id = dWorldCreate(); world->space = dHashSpaceCreate(0); dHashSpaceSetLevels(world->space, -4, 8); @@ -289,7 +291,10 @@ int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const cha return (world->masks[i] & (1 << j)) && (world->masks[j] & (1 << i)); } -Collider* lovrColliderInit(Collider* collider, World* world, float x, float y, float z) { +Collider* lovrColliderCreate(World* world, float x, float y, float z) { + Collider* collider = calloc(1, sizeof(Collider)); + lovrAssert(collider, "Out of memory"); + collider->ref = 1; collider->body = dBodyCreate(world->id); collider->world = world; collider->friction = 0; @@ -817,7 +822,10 @@ void lovrShapeGetAABB(Shape* shape, float aabb[6]) { dGeomGetAABB(shape->id, aabb); } -SphereShape* lovrSphereShapeInit(SphereShape* sphere, float radius) { +SphereShape* lovrSphereShapeCreate(float radius) { + SphereShape* sphere = calloc(1, sizeof(SphereShape)); + lovrAssert(sphere, "Out of memory"); + sphere->ref = 1; sphere->type = SHAPE_SPHERE; sphere->id = dCreateSphere(0, radius); dGeomSetData(sphere->id, sphere); @@ -832,7 +840,10 @@ void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) { dGeomSphereSetRadius(sphere->id, radius); } -BoxShape* lovrBoxShapeInit(BoxShape* box, float x, float y, float z) { +BoxShape* lovrBoxShapeCreate(float x, float y, float z) { + BoxShape* box = calloc(1, sizeof(BoxShape)); + lovrAssert(box, "Out of memory"); + box->ref = 1; box->type = SHAPE_BOX; box->id = dCreateBox(0, x, y, z); dGeomSetData(box->id, box); @@ -851,7 +862,10 @@ void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z) { dGeomBoxSetLengths(box->id, x, y, z); } -CapsuleShape* lovrCapsuleShapeInit(CapsuleShape* capsule, float radius, float length) { +CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) { + CapsuleShape* capsule = calloc(1, sizeof(CapsuleShape)); + lovrAssert(capsule, "Out of memory"); + capsule->ref = 1; capsule->type = SHAPE_CAPSULE; capsule->id = dCreateCapsule(0, radius, length); dGeomSetData(capsule->id, capsule); @@ -878,7 +892,10 @@ void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) { dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length); } -CylinderShape* lovrCylinderShapeInit(CylinderShape* cylinder, float radius, float length) { +CylinderShape* lovrCylinderShapeCreate(float radius, float length) { + CylinderShape* cylinder = calloc(1, sizeof(CylinderShape)); + lovrAssert(cylinder, "Out of memory"); + cylinder->ref = 1; cylinder->type = SHAPE_CYLINDER; cylinder->id = dCreateCylinder(0, radius, length); dGeomSetData(cylinder->id, cylinder); @@ -905,7 +922,10 @@ void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) { dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length); } -MeshShape* lovrMeshShapeInit(MeshShape* mesh, int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) { +MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) { + MeshShape* mesh = calloc(1, sizeof(MeshShape)); + lovrAssert(mesh, "Out of memory"); + mesh->ref = 1; dTriMeshDataID dataID = dGeomTriMeshDataCreate(); dGeomTriMeshDataBuildSingle(dataID, vertices, 3 * sizeof(float), vertexCount, indices, indexCount, 3 * sizeof(dTriIndex)); dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL); @@ -964,8 +984,11 @@ void lovrJointSetEnabled(Joint* joint, bool enable) { } } -BallJoint* lovrBallJointInit(BallJoint* joint, Collider* a, Collider* b, float x, float y, float z) { +BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float x, float y, float z) { lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); + BallJoint* joint = calloc(1, sizeof(BallJoint)); + lovrAssert(joint, "Out of memory"); + joint->ref = 1; joint->type = JOINT_BALL; joint->id = dJointCreateBall(a->world->id, 0); dJointSetData(joint->id, joint); @@ -1007,8 +1030,11 @@ void lovrBallJointSetTightness(Joint* joint, float tightness) { dJointSetBallParam(joint->id, dParamERP, tightness); } -DistanceJoint* lovrDistanceJointInit(DistanceJoint* joint, Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2) { +DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2) { lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); + DistanceJoint* joint = calloc(1, sizeof(DistanceJoint)); + lovrAssert(joint, "Out of memory"); + joint->ref = 1; joint->type = JOINT_DISTANCE; joint->id = dJointCreateDBall(a->world->id, 0); dJointSetData(joint->id, joint); @@ -1059,8 +1085,11 @@ void lovrDistanceJointSetTightness(Joint* joint, float tightness) { dJointSetDBallParam(joint->id, dParamERP, tightness); } -HingeJoint* lovrHingeJointInit(HingeJoint* joint, Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az) { +HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az) { lovrAssert(a->world == b->world, "Joint bodies must exist in same World"); + HingeJoint* joint = calloc(1, sizeof(HingeJoint)); + lovrAssert(joint, "Out of memory"); + joint->ref = 1; joint->type = JOINT_HINGE; joint->id = dJointCreateHinge(a->world->id, 0); dJointSetData(joint->id, joint); @@ -1119,8 +1148,11 @@ void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit) { dJointSetHingeParam(joint->id, dParamHiStop, limit); } -SliderJoint* lovrSliderJointInit(SliderJoint* joint, Collider* a, Collider* b, float ax, float ay, float az) { +SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az) { lovrAssert(a->world == b->world, "Joint bodies must exist in the same world"); + SliderJoint* joint = calloc(1, sizeof(SliderJoint)); + lovrAssert(joint, "Out of memory"); + joint->ref = 1; joint->type = JOINT_SLIDER; joint->id = dJointCreateSlider(a->world->id, 0); dJointSetData(joint->id, joint); diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index 32f2c490..c656c7f3 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -1,5 +1,6 @@ #include "core/arr.h" #include "core/maf.h" +#include "core/util.h" #include #include #include @@ -30,6 +31,7 @@ typedef struct Shape Shape; typedef struct Joint Joint; typedef struct { + ref_t ref; dWorldID id; dSpaceID space; dJointGroupID contactGroup; @@ -40,6 +42,7 @@ typedef struct { } World; struct Collider { + ref_t ref; dBodyID body; World* world; Collider* prev; @@ -53,6 +56,7 @@ struct Collider { }; struct Shape { + ref_t ref; ShapeType type; dGeomID id; Collider* collider; @@ -67,6 +71,7 @@ typedef Shape CylinderShape; typedef Shape MeshShape; struct Joint { + ref_t ref; JointType type; dJointID id; void* userdata; @@ -88,8 +93,7 @@ typedef struct { bool lovrPhysicsInit(void); void lovrPhysicsDestroy(void); -World* lovrWorldInit(World* world, float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount); -#define lovrWorldCreate(...) lovrWorldInit(lovrAlloc(World), __VA_ARGS__) +World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char** tags, uint32_t tagCount); void lovrWorldDestroy(void* ref); void lovrWorldDestroyData(World* world); void lovrWorldUpdate(World* world, float dt, CollisionResolver resolver, void* userdata); @@ -115,8 +119,7 @@ int lovrWorldDisableCollisionBetween(World* world, const char* tag1, const char* int lovrWorldEnableCollisionBetween(World* world, const char* tag1, const char* tag2); int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const char* tag); -Collider* lovrColliderInit(Collider* collider, World* world, float x, float y, float z); -#define lovrColliderCreate(...) lovrColliderInit(lovrAlloc(Collider), __VA_ARGS__) +Collider* lovrColliderCreate(World* world, float x, float y, float z); void lovrColliderDestroy(void* ref); void lovrColliderDestroyData(Collider* collider); void lovrColliderInitInertia(Collider* collider, Shape* shape); @@ -186,36 +189,31 @@ void lovrShapeSetOrientation(Shape* shape, quat orientation); void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]); void lovrShapeGetAABB(Shape* shape, float aabb[6]); -SphereShape* lovrSphereShapeInit(SphereShape* sphere, float radius); -#define lovrSphereShapeCreate(...) lovrSphereShapeInit(lovrAlloc(SphereShape), __VA_ARGS__) +SphereShape* lovrSphereShapeCreate(float radius); #define lovrSphereShapeDestroy lovrShapeDestroy float lovrSphereShapeGetRadius(SphereShape* sphere); void lovrSphereShapeSetRadius(SphereShape* sphere, float radius); -BoxShape* lovrBoxShapeInit(BoxShape* box, float x, float y, float z); -#define lovrBoxShapeCreate(...) lovrBoxShapeInit(lovrAlloc(BoxShape), __VA_ARGS__) +BoxShape* lovrBoxShapeCreate(float x, float y, float z); #define lovrBoxShapeDestroy lovrShapeDestroy void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z); void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z); -CapsuleShape* lovrCapsuleShapeInit(CapsuleShape* capsule, float radius, float length); -#define lovrCapsuleShapeCreate(...) lovrCapsuleShapeInit(lovrAlloc(CapsuleShape), __VA_ARGS__) +CapsuleShape* lovrCapsuleShapeCreate(float radius, float length); #define lovrCapsuleShapeDestroy lovrShapeDestroy float lovrCapsuleShapeGetRadius(CapsuleShape* capsule); void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius); float lovrCapsuleShapeGetLength(CapsuleShape* capsule); void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length); -CylinderShape* lovrCylinderShapeInit(CylinderShape* cylinder, float radius, float length); -#define lovrCylinderShapeCreate(...) lovrCylinderShapeInit(lovrAlloc(CylinderShape), __VA_ARGS__) +CylinderShape* lovrCylinderShapeCreate(float radius, float length); #define lovrCylinderShapeDestroy lovrShapeDestroy float lovrCylinderShapeGetRadius(CylinderShape* cylinder); void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius); float lovrCylinderShapeGetLength(CylinderShape* cylinder); void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length); -MeshShape* lovrMeshShapeInit(MeshShape* mesh, int vertexCount, float vertices[], int indexCount, dTriIndex indices[]); -#define lovrMeshShapeCreate(...) lovrMeshShapeInit(lovrAlloc(MeshShape), __VA_ARGS__) +MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]); #define lovrMeshShapeDestroy lovrShapeDestroy void lovrJointDestroy(void* ref); @@ -231,8 +229,7 @@ void lovrJointSetUserData(Joint* joint, void* data); bool lovrJointIsEnabled(Joint* joint); void lovrJointSetEnabled(Joint* joint, bool enable); -BallJoint* lovrBallJointInit(BallJoint* joint, Collider* a, Collider* b, float x, float y, float z); -#define lovrBallJointCreate(...) lovrBallJointInit(lovrAlloc(BallJoint), __VA_ARGS__) +BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float x, float y, float z); #define lovrBallJointDestroy lovrJointDestroy void lovrBallJointGetAnchors(BallJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrBallJointSetAnchor(BallJoint* joint, float x, float y, float z); @@ -241,8 +238,7 @@ void lovrBallJointSetResponseTime(Joint* joint, float responseTime); float lovrBallJointGetTightness(Joint* joint); void lovrBallJointSetTightness(Joint* joint, float tightness); -DistanceJoint* lovrDistanceJointInit(DistanceJoint* joint, Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2); -#define lovrDistanceJointCreate(...) lovrDistanceJointInit(lovrAlloc(DistanceJoint), __VA_ARGS__) +DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2); #define lovrDistanceJointDestroy lovrJointDestroy void lovrDistanceJointGetAnchors(DistanceJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrDistanceJointSetAnchors(DistanceJoint* joint, float x1, float y1, float z1, float x2, float y2, float z2); @@ -253,8 +249,7 @@ void lovrDistanceJointSetResponseTime(Joint* joint, float responseTime); float lovrDistanceJointGetTightness(Joint* joint); void lovrDistanceJointSetTightness(Joint* joint, float tightness); -HingeJoint* lovrHingeJointInit(HingeJoint* joint, Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az); -#define lovrHingeJointCreate(...) lovrHingeJointInit(lovrAlloc(HingeJoint), __VA_ARGS__) +HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az); #define lovrHingeJointDestroy lovrJointDestroy void lovrHingeJointGetAnchors(HingeJoint* joint, float* x1, float* y1, float* z1, float* x2, float* y2, float* z2); void lovrHingeJointSetAnchor(HingeJoint* joint, float x, float y, float z); @@ -266,8 +261,7 @@ void lovrHingeJointSetLowerLimit(HingeJoint* joint, float limit); float lovrHingeJointGetUpperLimit(HingeJoint* joint); void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit); -SliderJoint* lovrSliderJointInit(SliderJoint* joint, Collider* a, Collider* b, float ax, float ay, float az); -#define lovrSliderJointCreate(...) lovrSliderJointInit(lovrAlloc(SliderJoint), __VA_ARGS__) +SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az); #define lovrSliderJointDestroy lovrJointDestroy void lovrSliderJointGetAxis(SliderJoint* joint, float* x, float* y, float* z); void lovrSliderJointSetAxis(SliderJoint* joint, float x, float y, float z); diff --git a/src/modules/thread/channel.c b/src/modules/thread/channel.c index 7d8e74d5..687a1dd3 100644 --- a/src/modules/thread/channel.c +++ b/src/modules/thread/channel.c @@ -1,7 +1,6 @@ #include "thread/channel.h" #include "event/event.h" #include "core/arr.h" -#include "core/ref.h" #include "core/util.h" #include "lib/tinycthread/tinycthread.h" #include @@ -9,6 +8,7 @@ #include struct Channel { + ref_t ref; mtx_t lock; cnd_t cond; arr_t(Variant) messages; @@ -19,7 +19,9 @@ struct Channel { }; Channel* lovrChannelCreate(uint64_t hash) { - Channel* channel = lovrAlloc(Channel); + Channel* channel = calloc(1, sizeof(Channel)); + lovrAssert(channel, "Out of memory"); + channel->ref = 1; arr_init(&channel->messages); mtx_init(&channel->lock, mtx_plain | mtx_timed); cnd_init(&channel->cond); diff --git a/src/modules/thread/thread.c b/src/modules/thread/thread.c index 0b4bd1f8..05eed5b4 100644 --- a/src/modules/thread/thread.c +++ b/src/modules/thread/thread.c @@ -2,7 +2,6 @@ #include "thread/channel.h" #include "core/arr.h" #include "core/map.h" -#include "core/ref.h" #include "core/util.h" #include #include @@ -53,11 +52,14 @@ Channel* lovrThreadGetChannel(const char* name) { return entry.channel; } -Thread* lovrThreadInit(Thread* thread, int (*runner)(void*), Blob* body) { - lovrRetain(body); +Thread* lovrThreadCreate(int (*runner)(void*), Blob* body) { + Thread* thread = calloc(1, sizeof(Thread)); + lovrAssert(thread, "Out of memory"); + thread->ref = 1; thread->runner = runner; thread->body = body; mtx_init(&thread->lock, mtx_plain); + lovrRetain(body); return thread; } diff --git a/src/modules/thread/thread.h b/src/modules/thread/thread.h index 57c5a19c..65713b0e 100644 --- a/src/modules/thread/thread.h +++ b/src/modules/thread/thread.h @@ -1,5 +1,6 @@ #include "data/blob.h" #include "event/event.h" +#include "core/util.h" #include "lib/tinycthread/tinycthread.h" #include #include @@ -14,6 +15,7 @@ struct Channel; typedef struct Thread { + ref_t ref; thrd_t handle; mtx_t lock; Blob* body; @@ -29,8 +31,7 @@ void lovrThreadModuleDestroy(void); struct Channel* lovrThreadGetChannel(const char* name); void lovrThreadRemoveChannel(uint64_t hash); -Thread* lovrThreadInit(Thread* thread, int (*runner)(void*), Blob* body); -#define lovrThreadCreate(...) lovrThreadInit(lovrAlloc(Thread), __VA_ARGS__) +Thread* lovrThreadCreate(int (*runner)(void*), Blob* body); void lovrThreadDestroy(void* ref); void lovrThreadStart(Thread* thread, Variant* arguments, uint32_t argumentCount); void lovrThreadWait(Thread* thread);