Fix refcount alignment;

Once and for all?  How does alignment even work.
This commit is contained in:
bjorn 2019-12-14 16:19:15 -08:00
parent 16b93869ee
commit fd437423e7
2 changed files with 4 additions and 4 deletions

View File

@ -3,8 +3,8 @@
#include <stdlib.h>
void* _lovrAlloc(size_t size) {
Ref* ref = calloc(1, sizeof(Ref) + size);
char* ref = calloc(1, sizeof(size_t) + size);
lovrAssert(ref, "Out of memory");
*ref = 1;
return ref + 1;
*((Ref*) ref) = 1;
return ref + sizeof(size_t);
}

View File

@ -50,7 +50,7 @@ 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*) (o) - 1
#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 in %s:%d", __FILE__, __LINE__); }
#define lovrRelease(T, o) if (o && !ref_dec(toRef(o))) lovr ## T ## Destroy(o), free(toRef(o));