From 482fb37b688b94f5aff78a8dc34e78c1e2840058 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 3 Apr 2014 11:13:12 -0700 Subject: Recognize NULL as an empty vector/string --- Alc/ALc.c | 2 +- Alc/alstring.h | 4 ++-- Alc/helpers.c | 13 +++++++++---- Alc/vector.h | 10 +++++----- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Alc/ALc.c b/Alc/ALc.c index b6cc3114..d0847018 100644 --- a/Alc/ALc.c +++ b/Alc/ALc.c @@ -2872,7 +2872,7 @@ ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCin ALContext->ActiveSources = calloc(ALContext->MaxActiveSources, sizeof(ALContext->ActiveSources[0])); } - if(!ALContext || !ALContext->ActiveAuxSlots || !ALContext->ActiveSources) + if(!ALContext || !ALContext->ActiveSources) { if(!device->ContextList) { diff --git a/Alc/alstring.h b/Alc/alstring.h index 8f7d1655..f9e16eb8 100644 --- a/Alc/alstring.h +++ b/Alc/alstring.h @@ -12,7 +12,7 @@ DECL_VECTOR(al_string_char_type) typedef vector_al_string_char_type al_string; typedef const_vector_al_string_char_type const_al_string; -#define AL_STRING_INIT(_x) do { (_x) = calloc(1, sizeof(*(_x)) + sizeof((_x)->Data[0])); } while(0) +#define AL_STRING_INIT(_x) VECTOR_INIT(_x) #define AL_STRING_DEINIT(_x) VECTOR_DEINIT(_x) inline ALsizei al_string_length(const_al_string str) @@ -22,7 +22,7 @@ inline ALsizei al_string_empty(const_al_string str) { return al_string_length(str) == 0; } inline const al_string_char_type *al_string_get_cstr(const_al_string str) -{ return &VECTOR_FRONT(str); } +{ return str ? &VECTOR_FRONT(str) : ""; } void al_string_clear(al_string *str); diff --git a/Alc/helpers.c b/Alc/helpers.c index 65c956ec..837b19ad 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -753,8 +753,9 @@ void WriteUnlock(RWLock *lock) ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t obj_size, ALboolean exact) { vector_ *vecptr = ptr; - if((size_t)(*vecptr)->Capacity < obj_count) + if((size_t)(*vecptr ? (*vecptr)->Capacity : 0) < obj_count) { + ALsizei old_size = (*vecptr ? (*vecptr)->Size : 0); void *temp; /* Limit vector sizes to the greatest power-of-two value that an @@ -777,6 +778,7 @@ ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t o *vecptr = temp; (*vecptr)->Capacity = (ALsizei)obj_count; + (*vecptr)->Size = old_size; } return AL_TRUE; } @@ -784,9 +786,12 @@ ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t o ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t obj_size) { vector_ *vecptr = ptr; - if(!vector_reserve(vecptr, base_size, obj_count, obj_size, AL_TRUE)) - return AL_FALSE; - (*vecptr)->Size = (ALsizei)obj_count; + if(*vecptr || obj_count > 0) + { + if(!vector_reserve(vecptr, base_size, obj_count, obj_size, AL_TRUE)) + return AL_FALSE; + (*vecptr)->Size = (ALsizei)obj_count; + } return AL_TRUE; } diff --git a/Alc/vector.h b/Alc/vector.h index 4c994581..57939719 100644 --- a/Alc/vector.h +++ b/Alc/vector.h @@ -18,7 +18,7 @@ typedef struct vector__s { } *vector_##T; \ typedef const struct vector_##T##_s *const_vector_##T; -#define VECTOR_INIT(_x) do { (_x) = calloc(1, sizeof(*(_x))); } while(0) +#define VECTOR_INIT(_x) do { (_x) = NULL; } while(0) #define VECTOR_DEINIT(_x) do { free((_x)); (_x) = NULL; } while(0) /* Helper to increase a vector's reserve. Do not call directly. */ @@ -29,13 +29,13 @@ ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t o ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t obj_size); #define VECTOR_RESIZE(_x, _c) (vector_resize(&(_x), sizeof(*(_x)), (_c), sizeof((_x)->Data[0]))) -#define VECTOR_CAPACITY(_x) ((const ALsizei)(_x)->Capacity) -#define VECTOR_SIZE(_x) ((const ALsizei)(_x)->Size) +#define VECTOR_CAPACITY(_x) ((const ALsizei)((_x) ? (_x)->Capacity : 0)) +#define VECTOR_SIZE(_x) ((const ALsizei)((_x) ? (_x)->Size : 0)) #define VECTOR_ITER_BEGIN(_x) ((_x)->Data) -#define VECTOR_ITER_END(_x) ((_x)->Data + (_x)->Size) +#define VECTOR_ITER_END(_x) ((_x)->Data + VECTOR_SIZE((_x))) -#define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve(&(_x), sizeof(*(_x)), (_x)->Size+1, sizeof((_x)->Data[0]), AL_FALSE) && \ +#define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve(&(_x), sizeof(*(_x)), VECTOR_SIZE(_x)+1, sizeof((_x)->Data[0]), AL_FALSE) && \ (((_x)->Data[(_x)->Size++] = (_obj)),AL_TRUE)) #define VECTOR_POP_BACK(_x) ((void)((_x)->Size--)) -- cgit v1.2.3