Refs know their type;

This commit is contained in:
bjorn 2018-07-24 19:14:29 -07:00
parent 307d002954
commit bc2d638b00
25 changed files with 46 additions and 44 deletions

View File

@ -2,7 +2,7 @@
#include "audio/audio.h"
Microphone* lovrMicrophoneCreate(const char* name, int samples, int sampleRate, int bitDepth, int channelCount) {
Microphone* microphone = lovrAlloc(sizeof(Microphone), lovrMicrophoneDestroy);
Microphone* microphone = lovrAlloc(Microphone, lovrMicrophoneDestroy);
if (!microphone) return NULL;
ALCdevice* device = alcCaptureOpenDevice(name, sampleRate, lovrAudioConvertFormat(bitDepth, channelCount), samples);

View File

@ -12,7 +12,7 @@ static ALenum lovrSourceGetState(Source* source) {
}
Source* lovrSourceCreateStatic(SoundData* soundData) {
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
Source* source = lovrAlloc(Source, lovrSourceDestroy);
if (!source) return NULL;
ALenum format = lovrAudioConvertFormat(soundData->bitDepth, soundData->channelCount);
@ -28,7 +28,7 @@ Source* lovrSourceCreateStatic(SoundData* soundData) {
}
Source* lovrSourceCreateStream(AudioStream* stream) {
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
Source* source = lovrAlloc(Source, lovrSourceDestroy);
if (!source) return NULL;
source->type = SOURCE_STREAM;

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
AudioStream* lovrAudioStreamCreate(Blob* blob, size_t bufferSize) {
AudioStream* stream = lovrAlloc(sizeof(AudioStream), lovrAudioStreamDestroy);
AudioStream* stream = lovrAlloc(AudioStream, lovrAudioStreamDestroy);
if (!stream) return NULL;
stb_vorbis* decoder = stb_vorbis_open_memory(blob->data, blob->size, NULL, NULL);

View File

@ -1,7 +1,7 @@
#include "data/blob.h"
Blob* lovrBlobCreate(void* data, size_t size, const char* name) {
Blob* blob = lovrAlloc(sizeof(Blob), lovrBlobDestroy);
Blob* blob = lovrAlloc(Blob, lovrBlobDestroy);
if (!blob) return NULL;
blob->data = data;

View File

@ -248,7 +248,7 @@ static void assimpFileClose(struct aiFileIO* io, struct aiFile* assimpFile) {
}
ModelData* lovrModelDataCreate(Blob* blob) {
ModelData* modelData = lovrAlloc(sizeof(ModelData), lovrModelDataDestroy);
ModelData* modelData = lovrAlloc(ModelData, lovrModelDataDestroy);
if (!modelData) return NULL;
struct aiFileIO assimpIO;

View File

@ -77,7 +77,7 @@ Rasterizer* lovrRasterizerCreate(Blob* blob, int size) {
err = err || FT_Set_Pixel_Sizes(face, 0, size);
lovrAssert(!err, "Problem loading font");
Rasterizer* rasterizer = lovrAlloc(sizeof(Rasterizer), lovrRasterizerDestroy);
Rasterizer* rasterizer = lovrAlloc(Rasterizer, lovrRasterizerDestroy);
rasterizer->ftHandle = face;
rasterizer->blob = blob;
rasterizer->size = size;

View File

@ -3,7 +3,7 @@
#include <limits.h>
SoundData* lovrSoundDataCreate(int samples, int sampleRate, int bitDepth, int channelCount) {
SoundData* soundData = lovrAlloc(sizeof(SoundData), lovrSoundDataDestroy);
SoundData* soundData = lovrAlloc(SoundData, lovrSoundDataDestroy);
if (!soundData) return NULL;
soundData->samples = samples;
@ -17,7 +17,7 @@ SoundData* lovrSoundDataCreate(int samples, int sampleRate, int bitDepth, int ch
}
SoundData* lovrSoundDataCreateFromAudioStream(AudioStream* audioStream) {
SoundData* soundData = lovrAlloc(sizeof(SoundData), lovrSoundDataDestroy);
SoundData* soundData = lovrAlloc(SoundData, lovrSoundDataDestroy);
if (!soundData) return NULL;
soundData->samples = audioStream->samples;
@ -39,7 +39,7 @@ SoundData* lovrSoundDataCreateFromAudioStream(AudioStream* audioStream) {
}
SoundData* lovrSoundDataCreateFromBlob(Blob* blob) {
SoundData* soundData = lovrAlloc(sizeof(SoundData), lovrSoundDataDestroy);
SoundData* soundData = lovrAlloc(SoundData, lovrSoundDataDestroy);
if (!soundData) return NULL;
soundData->bitDepth = 16;

View File

@ -115,7 +115,7 @@ static int parseDDS(uint8_t* data, size_t size, TextureData* textureData) {
}
TextureData* lovrTextureDataGetBlank(int width, int height, uint8_t value, TextureFormat format) {
TextureData* textureData = lovrAlloc(sizeof(TextureData), lovrTextureDataDestroy);
TextureData* textureData = lovrAlloc(TextureData, lovrTextureDataDestroy);
if (!textureData) return NULL;
size_t pixelSize = 0;
@ -151,7 +151,7 @@ TextureData* lovrTextureDataGetBlank(int width, int height, uint8_t value, Textu
}
TextureData* lovrTextureDataGetEmpty(int width, int height, TextureFormat format) {
TextureData* textureData = lovrAlloc(sizeof(TextureData), lovrTextureDataDestroy);
TextureData* textureData = lovrAlloc(TextureData, lovrTextureDataDestroy);
if (!textureData) return NULL;
lovrAssert(width > 0 && height > 0, "TextureData dimensions must be positive");
@ -164,7 +164,7 @@ TextureData* lovrTextureDataGetEmpty(int width, int height, TextureFormat format
}
TextureData* lovrTextureDataFromBlob(Blob* blob) {
TextureData* textureData = lovrAlloc(sizeof(TextureData), lovrTextureDataDestroy);
TextureData* textureData = lovrAlloc(TextureData, lovrTextureDataDestroy);
if (!textureData) return NULL;
vec_init(&textureData->mipmaps);

View File

@ -15,7 +15,7 @@ void vertexFormatAppend(VertexFormat* format, const char* name, AttributeType ty
}
VertexData* lovrVertexDataCreate(uint32_t count, VertexFormat* format) {
VertexData* vertexData = lovrAlloc(sizeof(VertexData), lovrBlobDestroy);
VertexData* vertexData = lovrAlloc(VertexData, lovrBlobDestroy);
if (!vertexData) return NULL;
if (format) {

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
File* lovrFileCreate(const char* path) {
File* file = lovrAlloc(sizeof(File), lovrFileDestroy);
File* file = lovrAlloc(File, lovrFileDestroy);
if (!file) return NULL;
file->path = path;

View File

@ -16,7 +16,7 @@ static int trackSortCallback(const void* a, const void* b) {
}
Animator* lovrAnimatorCreate(ModelData* modelData) {
Animator* animator = lovrAlloc(sizeof(Animator), lovrAnimatorDestroy);
Animator* animator = lovrAlloc(Animator, lovrAnimatorDestroy);
if (!animator) return NULL;
lovrRetain(modelData);

View File

@ -25,7 +25,7 @@ static float* lovrFontAlignLine(float* x, float* lineEnd, float width, Horizonta
}
Font* lovrFontCreate(Rasterizer* rasterizer) {
Font* font = lovrAlloc(sizeof(Font), lovrFontDestroy);
Font* font = lovrAlloc(Font, lovrFontDestroy);
if (!font) return NULL;
lovrRetain(rasterizer);

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
Material* lovrMaterialCreate() {
Material* material = lovrAlloc(sizeof(Material), lovrMaterialDestroy);
Material* material = lovrAlloc(Material, lovrMaterialDestroy);
if (!material) return NULL;
for (int i = 0; i < MAX_MATERIAL_SCALARS; i++) {

View File

@ -56,7 +56,7 @@ static void renderNode(Model* model, int nodeIndex, int instances) {
}
Model* lovrModelCreate(ModelData* modelData) {
Model* model = lovrAlloc(sizeof(Model), lovrModelDestroy);
Model* model = lovrAlloc(Model, lovrModelDestroy);
if (!model) return NULL;
lovrRetain(modelData);

View File

@ -862,7 +862,7 @@ static void lovrTextureAllocate(Texture* texture, TextureData* textureData) {
}
Texture* lovrTextureCreate(TextureType type, TextureData** slices, int depth, bool srgb, bool mipmaps) {
Texture* texture = lovrAlloc(sizeof(Texture), lovrTextureDestroy);
Texture* texture = lovrAlloc(Texture, lovrTextureDestroy);
if (!texture) return NULL;
texture->type = type;
@ -1052,7 +1052,7 @@ Canvas* lovrCanvasCreate(int width, int height, TextureFormat format, CanvasFlag
Texture* texture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, flags.mipmaps);
if (!texture) return NULL;
Canvas* canvas = lovrAlloc(sizeof(Canvas), lovrCanvasDestroy);
Canvas* canvas = lovrAlloc(Canvas, lovrCanvasDestroy);
canvas->texture = *texture;
canvas->flags = flags;
@ -1210,7 +1210,7 @@ static GLuint linkShaders(GLuint vertexShader, GLuint fragmentShader) {
}
Shader* lovrShaderCreate(const char* vertexSource, const char* fragmentSource) {
Shader* shader = lovrAlloc(sizeof(Shader), lovrShaderDestroy);
Shader* shader = lovrAlloc(Shader, lovrShaderDestroy);
if (!shader) return NULL;
// Vertex
@ -1468,7 +1468,7 @@ void lovrShaderSetTexture(Shader* shader, const char* name, Texture** data, int
// Mesh
Mesh* lovrMeshCreate(uint32_t count, VertexFormat format, MeshDrawMode drawMode, MeshUsage usage) {
Mesh* mesh = lovrAlloc(sizeof(Mesh), lovrMeshDestroy);
Mesh* mesh = lovrAlloc(Mesh, lovrMeshDestroy);
if (!mesh) return NULL;
mesh->count = count;

View File

@ -147,7 +147,7 @@ static bool fakeInit(float offset) {
vec3_set(state.position, 0, 0, 0);
vec_init(&state.controllers);
Controller* controller = lovrAlloc(sizeof(Controller), free);
Controller* controller = lovrAlloc(Controller, free);
controller->id = 0;
vec_push(&state.controllers, controller);

View File

@ -69,7 +69,7 @@ static Controller* openvrAddController(unsigned int deviceIndex) {
}
}
controller = lovrAlloc(sizeof(Controller), free);
controller = lovrAlloc(Controller, free);
controller->id = deviceIndex;
vec_push(&state.controllers, controller);
return controller;

View File

@ -39,7 +39,7 @@ typedef struct {
static HeadsetState state;
static void onControllerAdded(uint32_t id) {
Controller* controller = lovrAlloc(sizeof(Controller), free);
Controller* controller = lovrAlloc(Controller, free);
controller->id = id;
vec_push(&state.controllers, controller);
lovrRetain(controller);

View File

@ -22,7 +22,7 @@ static uint64_t wangHash64(uint64_t key) {
// Use an 'Xorshift*' variant, as shown here: http://xorshift.di.unimi.it
RandomGenerator* lovrRandomGeneratorCreate() {
RandomGenerator* generator = lovrAlloc(sizeof(RandomGenerator), free);
RandomGenerator* generator = lovrAlloc(RandomGenerator, free);
if (!generator) return NULL;
Seed seed = { .b32 = { .lo = 0xCBBF7A44, .hi = 0x0139408D } };

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
Transform* lovrTransformCreate(mat4 transfrom) {
Transform* transform = lovrAlloc(sizeof(Transform), free);
Transform* transform = lovrAlloc(Transform, free);
if (!transform) return NULL;
transform->isDirty = true;

View File

@ -44,7 +44,7 @@ void lovrPhysicsDestroy() {
}
World* lovrWorldCreate(float xg, float yg, float zg, bool allowSleep, const char** tags, int tagCount) {
World* world = lovrAlloc(sizeof(World), lovrWorldDestroy);
World* world = lovrAlloc(World, lovrWorldDestroy);
if (!world) return NULL;
world->id = dWorldCreate();
@ -269,7 +269,7 @@ int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const cha
Collider* lovrColliderCreate(World* world, float x, float y, float z) {
lovrAssert(world, "No world specified");
Collider* collider = lovrAlloc(sizeof(Collider), lovrColliderDestroy);
Collider* collider = lovrAlloc(Collider, lovrColliderDestroy);
if (!collider) return NULL;
collider->body = dBodyCreate(world->id);
@ -753,7 +753,7 @@ void lovrShapeGetAABB(Shape* shape, float aabb[6]) {
}
SphereShape* lovrSphereShapeCreate(float radius) {
SphereShape* sphere = lovrAlloc(sizeof(SphereShape), lovrShapeDestroy);
SphereShape* sphere = lovrAlloc(SphereShape, lovrShapeDestroy);
if (!sphere) return NULL;
sphere->type = SHAPE_SPHERE;
@ -772,7 +772,7 @@ void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) {
}
BoxShape* lovrBoxShapeCreate(float x, float y, float z) {
BoxShape* box = lovrAlloc(sizeof(BoxShape), lovrShapeDestroy);
BoxShape* box = lovrAlloc(BoxShape, lovrShapeDestroy);
if (!box) return NULL;
box->type = SHAPE_BOX;
@ -795,7 +795,7 @@ void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z) {
}
CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) {
CapsuleShape* capsule = lovrAlloc(sizeof(CapsuleShape), lovrShapeDestroy);
CapsuleShape* capsule = lovrAlloc(CapsuleShape, lovrShapeDestroy);
if (!capsule) return NULL;
capsule->type = SHAPE_CAPSULE;
@ -826,7 +826,7 @@ void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) {
}
CylinderShape* lovrCylinderShapeCreate(float radius, float length) {
CylinderShape* cylinder = lovrAlloc(sizeof(CylinderShape), lovrShapeDestroy);
CylinderShape* cylinder = lovrAlloc(CylinderShape, lovrShapeDestroy);
if (!cylinder) return NULL;
cylinder->type = SHAPE_CYLINDER;
@ -896,7 +896,7 @@ void lovrJointSetUserData(Joint* joint, void* data) {
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 = lovrAlloc(sizeof(BallJoint), lovrJointDestroy);
BallJoint* joint = lovrAlloc(BallJoint, lovrJointDestroy);
if (!joint) return NULL;
joint->type = JOINT_BALL;
@ -926,7 +926,7 @@ void lovrBallJointSetAnchor(BallJoint* joint, float x, float y, float z) {
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 = lovrAlloc(sizeof(DistanceJoint), lovrJointDestroy);
DistanceJoint* joint = lovrAlloc(DistanceJoint, lovrJointDestroy);
if (!joint) return NULL;
joint->type = JOINT_DISTANCE;
@ -965,7 +965,7 @@ void lovrDistanceJointSetDistance(DistanceJoint* joint, float distance) {
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 = lovrAlloc(sizeof(HingeJoint), lovrJointDestroy);
HingeJoint* joint = lovrAlloc(HingeJoint, lovrJointDestroy);
if (!joint) return NULL;
joint->type = JOINT_HINGE;
@ -1028,7 +1028,7 @@ void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit) {
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 = lovrAlloc(sizeof(SliderJoint), lovrJointDestroy);
SliderJoint* joint = lovrAlloc(SliderJoint, lovrJointDestroy);
if (!joint) return NULL;
joint->type = JOINT_SLIDER;

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
Channel* lovrChannelCreate() {
Channel* channel = lovrAlloc(sizeof(Channel), lovrChannelDestroy);
Channel* channel = lovrAlloc(Channel, lovrChannelDestroy);
if (!channel) return NULL;
vec_init(&channel->messages);

View File

@ -34,7 +34,7 @@ Channel* lovrThreadGetChannel(const char* name) {
}
Thread* lovrThreadCreate(int (*runner)(void*), const char* body) {
Thread* thread = lovrAlloc(sizeof(Thread), lovrThreadDestroy);
Thread* thread = lovrAlloc(Thread, lovrThreadDestroy);
if (!thread) return NULL;
thread->runner = runner;

View File

@ -30,10 +30,10 @@ void lovrSleep(double seconds) {
#endif
}
void* lovrAlloc(size_t size, void (*destructor)(void* object)) {
void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*)) {
void* object = calloc(1, size);
if (!object) return NULL;
*((Ref*) object) = (Ref) { destructor, 1 };
*((Ref*) object) = (Ref) { 1, type, destructor };
return object;
}

View File

@ -10,12 +10,14 @@
#pragma once
#define lovrAssert(c, ...) if (!(c)) { lovrThrow(__VA_ARGS__); }
#define lovrAlloc(T, destructor) (T*) _lovrAlloc(#T, sizeof(T), destructor)
typedef vec_t(unsigned int) vec_uint_t;
typedef struct ref {
void (*free)(void* object);
int count;
const char* type;
void (*free)(void*);
} Ref;
typedef struct {
@ -26,7 +28,7 @@ extern _Thread_local void* lovrErrorContext;
void lovrThrow(const char* format, ...);
void lovrSleep(double seconds);
void* lovrAlloc(size_t size, void (*destructor)(void* object));
void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*));
void lovrRetain(void* object);
void lovrRelease(void* object);
void* lovrLoadLibrary(const char* filename);