Simplify arr;

This commit is contained in:
bjorn 2019-08-21 23:16:08 -07:00
parent ba1e97e626
commit 28344928c9
12 changed files with 40 additions and 47 deletions

View File

@ -599,8 +599,8 @@ LOVR_EXPORT int luaopen_lovr_headset(lua_State* L) {
luax_pushconf(L);
lua_getfield(L, -1, "headset");
arr_t(HeadsetDriver, 8) drivers;
arr_init(&drivers);
size_t driverCount = 0;
HeadsetDriver drivers[8];
float offset = 1.7f;
int msaa = 4;
@ -611,7 +611,8 @@ LOVR_EXPORT int luaopen_lovr_headset(lua_State* L) {
int n = luax_len(L, -1);
for (int i = 0; i < n; i++) {
lua_rawgeti(L, -1, i + 1);
arr_push(&drivers, luaL_checkoption(L, -1, NULL, HeadsetDrivers));
drivers[driverCount++] = luaL_checkoption(L, -1, NULL, HeadsetDrivers);
lovrAssert(driverCount < sizeof(drivers) / sizeof(drivers[0]), "Too many headset drivers specified in conf.lua");
lua_pop(L, 1);
}
lua_pop(L, 1);
@ -627,14 +628,11 @@ LOVR_EXPORT int luaopen_lovr_headset(lua_State* L) {
lua_pop(L, 1);
}
if (lovrHeadsetInit(drivers.data, drivers.length, offset, msaa)) {
if (lovrHeadsetInit(drivers, driverCount, offset, msaa)) {
luax_atexit(L, lovrHeadsetDestroy);
}
arr_free(&drivers);
lua_pop(L, 2);
headsetRenderData.ref = LUA_NOREF;
return 1;
}

View File

@ -1,22 +1,17 @@
#include "arr.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
void _arr_reserve(void** data, void* temp, size_t n, size_t* capacity, size_t stride) {
size_t oldCapacity = *capacity;
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");
}
if (*data == temp) {
*data = realloc(NULL, *capacity * stride);
lovrAssert(*data, "Out of memory");
memcpy(*data, temp, oldCapacity * stride);
} else {
*data = realloc(*data, *capacity * stride);
lovrAssert(*data, "Out of memory");
}
*data = realloc(*data, *capacity * stride);
lovrAssert(*data, "Out of memory");
}

View File

@ -2,22 +2,20 @@
#pragma once
// A dynamic array that uses the stack initially and switches to the heap if it gets big enough.
#define arr_t(T, n)\
struct { T *data; T temp[n]; size_t length, capacity; }
#define arr_t(T)\
struct { T *data; size_t length, capacity; }
#define arr_init(a)\
(a)->data = (a)->temp,\
(a)->data = NULL,\
(a)->length = 0,\
(a)->capacity = sizeof((a)->temp) / sizeof((a)->temp[0])
(a)->capacity = 0
#define arr_free(a)\
if ((a)->data != (a)->temp) free((a)->data)
free((a)->data)
#define arr_reserve(a, n)\
n > (a)->capacity ?\
_arr_reserve((void**) &((a)->data), (a)->temp, n, &(a)->capacity, sizeof(*(a)->data)) :\
_arr_reserve((void**) &((a)->data), n, &(a)->capacity, sizeof(*(a)->data)) :\
(void) 0
#define arr_push(a, x)\
@ -39,4 +37,4 @@
#define arr_clear(a)\
(a)->length = 0
void _arr_reserve(void** data, void* temp, size_t n, size_t* capacity, size_t stride);
void _arr_reserve(void** data, size_t n, size_t* capacity, size_t stride);

View File

@ -18,7 +18,7 @@ static struct {
float LOVR_ALIGN(16) orientation[4];
float LOVR_ALIGN(16) position[4];
float LOVR_ALIGN(16) velocity[4];
arr_t(Source*, 32) sources;
arr_t(Source*) sources;
} state;
ALenum lovrAudioConvertFormat(uint32_t bitDepth, uint32_t channelCount) {

View File

@ -16,9 +16,9 @@ typedef struct {
int count;
} objGroup;
typedef arr_t(ModelMaterial, 1) arr_material_t;
typedef arr_t(TextureData*, 1) arr_texturedata_t;
typedef arr_t(objGroup, 1) arr_group_t;
typedef arr_t(ModelMaterial) arr_material_t;
typedef arr_t(TextureData*) arr_texturedata_t;
typedef arr_t(objGroup) arr_group_t;
#define STARTS_WITH(a, b) !strncmp(a, b, strlen(b))
@ -95,13 +95,13 @@ ModelData* lovrModelDataInitObj(ModelData* model, Blob* source) {
arr_group_t groups;
arr_texturedata_t textures;
arr_material_t materials;
arr_t(float, 1) vertexBlob;
arr_t(int, 1) indexBlob;
arr_t(float) vertexBlob;
arr_t(int) indexBlob;
map_u32_t materialMap;
map_int_t vertexMap;
arr_t(float, 1) positions;
arr_t(float, 1) normals;
arr_t(float, 1) uvs;
arr_t(float) positions;
arr_t(float) normals;
arr_t(float) uvs;
arr_init(&groups);
arr_init(&textures);

View File

@ -8,7 +8,7 @@
static struct {
bool initialized;
arr_t(Event, 8) events;
arr_t(Event) events;
size_t head;
} state;

View File

@ -203,7 +203,7 @@ typedef struct {
typedef struct {
int shaderSwitches;
int drawCalls;
arr_t(GpuTimer, 4) timers;
arr_t(GpuTimer) timers;
} GpuStats;
typedef struct {

View File

@ -82,7 +82,7 @@ static struct {
Image images[MAX_IMAGES];
float viewports[2][4];
uint32_t viewportCount;
arr_t(void*, 2) incoherents[MAX_BARRIERS];
arr_t(void*) incoherents[MAX_BARRIERS];
map_t(TimerList) timers;
GpuFeatures features;
GpuLimits limits;
@ -1860,7 +1860,7 @@ static void lovrShaderSetupUniforms(Shader* shader) {
int blockId = (i << 1) + BLOCK_UNIFORM;
map_set(&shader->blockMap, name, blockId);
arr_push(uniformBlocks, block);
arr_init(&uniformBlocks->data[uniformBlocks->length - 1].uniforms); // Initialize arr after copying stack variable
arr_init(&uniformBlocks->data[uniformBlocks->length - 1].uniforms);
}
// Shader storage buffers and their buffer variables
@ -1918,6 +1918,7 @@ static void lovrShaderSetupUniforms(Shader* shader) {
int imageSlot = 0;
map_init(&shader->uniformMap);
arr_init(&shader->uniforms);
arr_reserve(&shader->uniforms, 4);
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount);
for (uint32_t i = 0; i < (uint32_t) uniformCount; i++) {
Uniform uniform;

View File

@ -90,7 +90,7 @@ typedef struct Uniform {
bool dirty;
} Uniform;
typedef arr_t(Uniform, 8) arr_uniform_t;
typedef arr_t(Uniform) arr_uniform_t;
typedef struct {
BlockType type;
@ -108,7 +108,7 @@ typedef struct {
int slot;
} UniformBlock;
typedef arr_t(UniformBlock, 1) arr_block_t;
typedef arr_t(UniformBlock) arr_block_t;
typedef struct Shader {
ShaderType type;

View File

@ -7,7 +7,7 @@
#include <math.h>
struct Curve {
arr_t(float, 16) points;
arr_t(float) points;
};
// Explicit curve evaluation, unroll simple cases to avoid pow overhead
@ -53,6 +53,7 @@ static void evaluate(float* P, size_t n, float t, vec3 p) {
Curve* lovrCurveCreate(void) {
Curve* curve = lovrAlloc(Curve);
arr_init(&curve->points);
arr_reserve(&curve->points, 16);
return curve;
}

View File

@ -32,7 +32,7 @@ typedef struct {
dWorldID id;
dSpaceID space;
dJointGroupID contactGroup;
arr_t(Shape*, 8) overlaps;
arr_t(Shape*) overlaps;
map_int_t tags;
uint16_t masks[MAX_TAGS];
Collider* head;
@ -45,8 +45,8 @@ struct Collider {
Collider* next;
void* userdata;
int tag;
arr_t(Shape*, 2) shapes;
arr_t(Joint*, 2) joints;
arr_t(Shape*) shapes;
arr_t(Joint*) joints;
float friction;
float restitution;
};

View File

@ -11,7 +11,7 @@
struct Channel {
mtx_t lock;
cnd_t cond;
arr_t(Variant, 1) messages;
arr_t(Variant) messages;
size_t head;
uint64_t sent;
uint64_t received;