util.h no longer uses atomics;

This commit is contained in:
bjorn 2021-02-11 16:37:55 -07:00
parent b21fd987ca
commit 023067ec27
18 changed files with 45 additions and 30 deletions

View File

@ -1,6 +1,7 @@
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdatomic.h>
// Error handling
static void defaultErrorCallback(void* p, const char* format, va_list args) {
@ -41,6 +42,23 @@ void lovrLog(int level, const char* tag, const char* format, ...) {
va_end(args);
}
// Refcounting
#if ATOMIC_INT_LOCK_FREE != 2
#error "Lock-free integer atomics are not supported on this platform, but are required for refcounting
#endif
void lovrRetain(void* object) {
if (object) {
atomic_fetch_add((atomic_uint*) object, 1);
}
}
void lovrRelease(void* object, void (*destructor)(void*)) {
if (object && atomic_fetch_sub((atomic_uint*) object, 1) == 1) {
destructor(object);
}
}
// UTF-8
// https://github.com/starwing/luautf8
size_t utf8_decode(const char *s, const char *e, unsigned *pch) {

View File

@ -1,7 +1,6 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdatomic.h>
#pragma once
@ -59,9 +58,8 @@ static inline uint64_t hash64(const void* data, size_t length) {
}
// Refcounting
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)
void lovrRetain(void* ref);
void lovrRelease(void* ref, void (*destructor)(void*));
// Dynamic Array
typedef void* arr_allocator(void* data, size_t size);

View File

@ -19,7 +19,7 @@ static const ma_format miniaudioFormats[] = {
#define BUFFER_SIZE 256
struct Source {
ref_t ref;
uint32_t ref;
Source* next;
Sound* sound;
ma_data_converter* converter;

View File

@ -1,10 +1,10 @@
#include <stddef.h>
#include "core/util.h"
#include <stdint.h>
#pragma once
typedef struct Blob {
ref_t ref;
uint32_t ref;
void* data;
size_t size;
const char* name;

View File

@ -52,7 +52,7 @@ typedef struct {
} Mipmap;
typedef struct Image {
ref_t ref;
uint32_t ref;
struct Blob* blob;
uint32_t width;
uint32_t height;

View File

@ -179,7 +179,7 @@ typedef struct {
} ModelSkin;
typedef struct ModelData {
ref_t ref;
uint32_t ref;
void* data;
struct Blob** blobs;
ModelBuffer* buffers;

View File

@ -10,7 +10,7 @@
#include <math.h>
struct Rasterizer {
ref_t ref;
uint32_t ref;
stbtt_fontinfo font;
struct Blob* blob;
float size;

View File

@ -15,7 +15,7 @@ static const ma_format miniaudioFormats[] = {
};
struct Sound {
ref_t ref;
uint32_t ref;
uint32_t (*read)(Sound* sound, uint32_t offset, uint32_t count, void* data);
struct Blob* blob;
void* decoder;

View File

@ -18,7 +18,7 @@ typedef struct {
} FontAtlas;
struct Font {
ref_t ref;
uint32_t ref;
Rasterizer* rasterizer;
Texture* texture;
FontAtlas atlas;

View File

@ -8,7 +8,7 @@
#include <math.h>
struct Material {
ref_t ref;
uint32_t ref;
float scalars[MAX_MATERIAL_SCALARS];
Color colors[MAX_MATERIAL_COLORS];
struct Texture* textures[MAX_MATERIAL_TEXTURES];

View File

@ -15,7 +15,7 @@ typedef struct {
} NodeTransform;
struct Model {
ref_t ref;
uint32_t ref;
struct ModelData* data;
struct Buffer** buffers;
struct Mesh** meshes;

View File

@ -40,7 +40,7 @@
#define LOVR_SHADER_DRAW_ID 7
struct Buffer {
ref_t ref;
uint32_t ref;
uint32_t id;
void* data;
size_t size;
@ -54,7 +54,7 @@ struct Buffer {
};
struct Texture {
ref_t ref;
uint32_t ref;
GLuint id;
GLuint msaaId;
GLenum target;
@ -76,7 +76,7 @@ struct Texture {
};
struct Canvas {
ref_t ref;
uint32_t ref;
uint32_t framebuffer;
uint32_t resolveBuffer;
uint32_t depthBuffer;
@ -92,7 +92,7 @@ struct Canvas {
};
struct ShaderBlock {
ref_t ref;
uint32_t ref;
BlockType type;
arr_uniform_t uniforms;
map_t uniformMap;
@ -100,7 +100,7 @@ struct ShaderBlock {
};
struct Shader {
ref_t ref;
uint32_t ref;
uint32_t program;
ShaderType type;
arr_uniform_t uniforms;
@ -112,7 +112,7 @@ struct Shader {
};
struct Mesh {
ref_t ref;
uint32_t ref;
uint32_t vao;
uint32_t ibo;
DrawMode mode;

View File

@ -5,7 +5,7 @@
#include <math.h>
struct Curve {
ref_t ref;
uint32_t ref;
arr_t(float) points;
};

View File

@ -11,7 +11,7 @@ static const size_t vectorComponents[] = {
};
struct Pool {
ref_t ref;
uint32_t ref;
float* data;
size_t count;
size_t cursor;

View File

@ -6,7 +6,7 @@
#include <inttypes.h>
struct RandomGenerator {
ref_t ref;
uint32_t ref;
Seed seed;
Seed state;
double lastRandomNormal;

View File

@ -30,7 +30,7 @@ typedef struct Shape Shape;
typedef struct Joint Joint;
typedef struct {
ref_t ref;
uint32_t ref;
dWorldID id;
dSpaceID space;
dJointGroupID contactGroup;
@ -41,7 +41,7 @@ typedef struct {
} World;
struct Collider {
ref_t ref;
uint32_t ref;
dBodyID body;
World* world;
Collider* prev;
@ -55,7 +55,7 @@ struct Collider {
};
struct Shape {
ref_t ref;
uint32_t ref;
ShapeType type;
dGeomID id;
Collider* collider;
@ -70,7 +70,7 @@ typedef Shape CylinderShape;
typedef Shape MeshShape;
struct Joint {
ref_t ref;
uint32_t ref;
JointType type;
dJointID id;
void* userdata;

View File

@ -7,7 +7,7 @@
#include <math.h>
struct Channel {
ref_t ref;
uint32_t ref;
mtx_t lock;
cnd_t cond;
arr_t(Variant) messages;

View File

@ -1,6 +1,5 @@
#include "data/blob.h"
#include "event/event.h"
#include "core/util.h"
#include "lib/tinycthread/tinycthread.h"
#include <stdbool.h>
#include <stdint.h>
@ -15,7 +14,7 @@
struct Channel;
typedef struct Thread {
ref_t ref;
uint32_t ref;
thrd_t handle;
mtx_t lock;
Blob* body;