From 5e11a738c6b18a367070450ec46dfb714fe433d7 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 26 Jul 2016 11:45:20 -0700 Subject: Combine VECTOR_RESIZE and VECTOR_RESERVE --- Alc/backends/alsa.c | 2 +- Alc/backends/dsound.c | 2 +- Alc/backends/mmdevapi.c | 8 +--- Alc/backends/pulseaudio.c | 2 +- Alc/backends/qsa.c | 7 ++- Alc/backends/winmm.c | 6 +-- Alc/helpers.c | 107 ++++++++++++---------------------------------- Alc/vector.h | 39 +++++++++++++---- 8 files changed, 69 insertions(+), 104 deletions(-) (limited to 'Alc') diff --git a/Alc/backends/alsa.c b/Alc/backends/alsa.c index 280f5019..7a9045bb 100644 --- a/Alc/backends/alsa.c +++ b/Alc/backends/alsa.c @@ -242,7 +242,7 @@ static void clear_devlist(vector_DevMap *devlist) AL_STRING_DEINIT((i)->device_name); \ } while(0) VECTOR_FOR_EACH(DevMap, *devlist, FREE_DEV); - VECTOR_RESIZE(*devlist, 0); + VECTOR_RESIZE(*devlist, 0, 0); #undef FREE_DEV } diff --git a/Alc/backends/dsound.c b/Alc/backends/dsound.c index 1ff99352..da6fbacf 100644 --- a/Alc/backends/dsound.c +++ b/Alc/backends/dsound.c @@ -123,7 +123,7 @@ static void clear_devlist(vector_DevMap *list) { #define DEINIT_STR(i) AL_STRING_DEINIT((i)->name) VECTOR_FOR_EACH(DevMap, *list, DEINIT_STR); - VECTOR_RESIZE(*list, 0); + VECTOR_RESIZE(*list, 0, 0); #undef DEINIT_STR } diff --git a/Alc/backends/mmdevapi.c b/Alc/backends/mmdevapi.c index f102e385..60d3be00 100644 --- a/Alc/backends/mmdevapi.c +++ b/Alc/backends/mmdevapi.c @@ -79,7 +79,7 @@ static void clear_devlist(vector_DevMap *list) (i)->devid = NULL; \ } while(0) VECTOR_FOR_EACH(DevMap, *list, CLEAR_DEVMAP); - VECTOR_RESIZE(*list, 0); + VECTOR_RESIZE(*list, 0, 0); #undef CLEAR_DEVMAP } @@ -258,11 +258,7 @@ static HRESULT probe_devices(IMMDeviceEnumerator *devenum, EDataFlow flowdir, ve if(SUCCEEDED(hr) && count > 0) { clear_devlist(list); - if(!VECTOR_RESERVE(*list, count)) - { - IMMDeviceCollection_Release(coll); - return E_OUTOFMEMORY; - } + VECTOR_RESIZE(*list, 0, count); hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flowdir, eMultimedia, &defdev); diff --git a/Alc/backends/pulseaudio.c b/Alc/backends/pulseaudio.c index d317b576..220c1b7d 100644 --- a/Alc/backends/pulseaudio.c +++ b/Alc/backends/pulseaudio.c @@ -443,7 +443,7 @@ static void clear_devlist(vector_DevMap *list) #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name)) VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS); #undef DEINIT_STRS - VECTOR_RESIZE(*list, 0); + VECTOR_RESIZE(*list, 0, 0); } diff --git a/Alc/backends/qsa.c b/Alc/backends/qsa.c index a1fbce63..b7923517 100644 --- a/Alc/backends/qsa.c +++ b/Alc/backends/qsa.c @@ -117,8 +117,7 @@ static void deviceList(int type, vector_DevMap *devmap) if(max_cards < 0) return; - VECTOR_RESERVE(*devmap, max_cards+1); - VECTOR_RESIZE(*devmap, 0); + VECTOR_RESIZE(*devmap, 0, max_cards+1); entry.name = strdup(qsaDevice); entry.card = 0; @@ -893,8 +892,8 @@ void alc_qsa_probe(enum DevProbe type) case ALL_DEVICE_PROBE: #define FREE_NAME(iter) free((iter)->name) VECTOR_FOR_EACH(DevMap, DeviceNameMap, FREE_NAME); + VECTOR_RESIZE(DeviceNameMap, 0, 0); #undef FREE_NAME - VECTOR_RESIZE(DeviceNameMap, 0); deviceList(SND_PCM_CHANNEL_PLAYBACK, &DeviceNameMap); #define APPEND_DEVICE(iter) AppendAllDevicesList((iter)->name) @@ -905,8 +904,8 @@ void alc_qsa_probe(enum DevProbe type) case CAPTURE_DEVICE_PROBE: #define FREE_NAME(iter) free((iter)->name) VECTOR_FOR_EACH(DevMap, CaptureNameMap, FREE_NAME); + VECTOR_RESIZE(CaptureNameMap, 0, 0); #undef FREE_NAME - VECTOR_RESIZE(CaptureNameMap, 0); deviceList(SND_PCM_CHANNEL_CAPTURE, &CaptureNameMap); #define APPEND_DEVICE(iter) AppendCaptureDeviceList((iter)->name) diff --git a/Alc/backends/winmm.c b/Alc/backends/winmm.c index 180f764b..6e990a35 100644 --- a/Alc/backends/winmm.c +++ b/Alc/backends/winmm.c @@ -46,7 +46,7 @@ static vector_al_string CaptureDevices; static void clear_devlist(vector_al_string *list) { VECTOR_FOR_EACH(al_string, *list, al_string_deinit); - VECTOR_RESIZE(*list, 0); + VECTOR_RESIZE(*list, 0, 0); } @@ -58,7 +58,7 @@ static void ProbePlaybackDevices(void) clear_devlist(&PlaybackDevices); numdevs = waveOutGetNumDevs(); - VECTOR_RESERVE(PlaybackDevices, numdevs); + VECTOR_RESIZE(PlaybackDevices, 0, numdevs); for(i = 0;i < numdevs;i++) { WAVEOUTCAPSW WaveCaps; @@ -101,7 +101,7 @@ static void ProbeCaptureDevices(void) clear_devlist(&CaptureDevices); numdevs = waveInGetNumDevs(); - VECTOR_RESERVE(CaptureDevices, numdevs); + VECTOR_RESIZE(CaptureDevices, 0, numdevs); for(i = 0;i < numdevs;i++) { WAVEINCAPSW WaveCaps; diff --git a/Alc/helpers.c b/Alc/helpers.c index e065b022..2a059c68 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -838,51 +838,6 @@ void SetRTPriority(void) } -ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact) -{ - vector_ *vecptr = (vector_*)ptr; - if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count) - { - size_t old_size = (*vecptr ? (*vecptr)->Size : 0); - void *temp; - - /* Use the next power-of-2 size if we don't need to allocate the exact - * amount. This is preferred when regularly increasing the vector since - * it means fewer reallocations. Though it means it also wastes some - * memory. */ - if(exact == AL_FALSE && obj_count < INT_MAX) - obj_count = NextPowerOf2((ALuint)obj_count); - - /* Need to be explicit with the caller type's base size, because it - * could have extra padding before the start of the array (that is, - * sizeof(*vector_) may not equal base_size). */ - temp = al_calloc(16, base_size + obj_size*obj_count); - if(temp == NULL) return AL_FALSE; - if(*vecptr) - memcpy(((ALubyte*)temp)+base_size, ((ALubyte*)*vecptr)+base_size, - obj_size*old_size); - - al_free(*vecptr); - *vecptr = temp; - (*vecptr)->Capacity = obj_count; - (*vecptr)->Size = old_size; - } - return AL_TRUE; -} - -ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count) -{ - vector_ *vecptr = (vector_*)ptr; - if(*vecptr || obj_count > 0) - { - if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE)) - return AL_FALSE; - (*vecptr)->Size = obj_count; - } - return AL_TRUE; -} - - extern inline void al_string_deinit(al_string *str); extern inline size_t al_string_length(const_al_string str); extern inline ALboolean al_string_empty(const_al_string str); @@ -896,9 +851,8 @@ void al_string_clear(al_string *str) * is to ensure we have space to add a null terminator in the string * data so it can be used as a C-style string. */ - VECTOR_RESERVE(*str, 1); - VECTOR_RESIZE(*str, 0); - *VECTOR_END(*str) = 0; + VECTOR_RESIZE(*str, 0, 1); + VECTOR_ELEM(*str, 0) = 0; } } @@ -930,11 +884,10 @@ void al_string_copy(al_string *str, const_al_string from) size_t len = al_string_length(from); size_t i; - VECTOR_RESERVE(*str, len+1); - VECTOR_RESIZE(*str, len); + VECTOR_RESIZE(*str, len, len+1); for(i = 0;i < len;i++) VECTOR_ELEM(*str, i) = VECTOR_ELEM(from, i); - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, i) = 0; } void al_string_copy_cstr(al_string *str, const al_string_char_type *from) @@ -942,11 +895,10 @@ void al_string_copy_cstr(al_string *str, const al_string_char_type *from) size_t len = strlen(from); size_t i; - VECTOR_RESERVE(*str, len+1); - VECTOR_RESIZE(*str, len); + VECTOR_RESIZE(*str, len, len+1); for(i = 0;i < len;i++) VECTOR_ELEM(*str, i) = from[i]; - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, i) = 0; } void al_string_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to) @@ -954,18 +906,18 @@ void al_string_copy_range(al_string *str, const al_string_char_type *from, const size_t len = to - from; size_t i; - VECTOR_RESERVE(*str, len+1); - VECTOR_RESIZE(*str, len); + VECTOR_RESIZE(*str, len, len+1); for(i = 0;i < len;i++) VECTOR_ELEM(*str, i) = from[i]; - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, i) = 0; } void al_string_append_char(al_string *str, const al_string_char_type c) { - VECTOR_RESERVE(*str, al_string_length(*str)+2); + size_t len = al_string_length(*str); + VECTOR_RESIZE(*str, len, len+2); VECTOR_PUSH_BACK(*str, c); - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, len+1) = 0; } void al_string_append_cstr(al_string *str, const al_string_char_type *from) @@ -976,27 +928,25 @@ void al_string_append_cstr(al_string *str, const al_string_char_type *from) size_t base = al_string_length(*str); size_t i; - VECTOR_RESERVE(*str, base+len+1); - VECTOR_RESIZE(*str, base+len); + VECTOR_RESIZE(*str, base+len, base+len+1); for(i = 0;i < len;i++) VECTOR_ELEM(*str, base+i) = from[i]; - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, base+i) = 0; } } void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to) { - if(to != from) + size_t len = to - from; + if(len != 0) { size_t base = al_string_length(*str); - size_t len = to - from; size_t i; - VECTOR_RESERVE(*str, base+len+1); - VECTOR_RESIZE(*str, base+len); + VECTOR_RESIZE(*str, base+len, base+len+1); for(i = 0;i < len;i++) VECTOR_ELEM(*str, base+i) = from[i]; - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, base+i) = 0; } } @@ -1006,10 +956,9 @@ void al_string_copy_wcstr(al_string *str, const wchar_t *from) int len; if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0) { - VECTOR_RESERVE(*str, len); - VECTOR_RESIZE(*str, len-1); + VECTOR_RESIZE(*str, len-1, len); WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL); - *VECTOR_END(*str) = 0; + VECTOR_ELEM(*str, len-1) = 0; } } @@ -1018,11 +967,10 @@ void al_string_append_wcstr(al_string *str, const wchar_t *from) int len; if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0) { - size_t strlen = al_string_length(*str); - VECTOR_RESERVE(*str, strlen+len); - VECTOR_RESIZE(*str, strlen+len-1); - WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL); - *VECTOR_END(*str) = 0; + size_t base = al_string_length(*str); + VECTOR_RESIZE(*str, base+len-1, base+len); + WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_ELEM(*str, base), len, NULL, NULL); + VECTOR_ELEM(*str, base+len-1) = 0; } } @@ -1031,11 +979,10 @@ void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t int len; if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0) { - size_t strlen = al_string_length(*str); - VECTOR_RESERVE(*str, strlen+len+1); - VECTOR_RESIZE(*str, strlen+len); - WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL); - *VECTOR_END(*str) = 0; + size_t base = al_string_length(*str); + VECTOR_RESIZE(*str, base+len, base+len+1); + WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_ELEM(*str, base), len+1, NULL, NULL); + VECTOR_ELEM(*str, base+len) = 0; } } #endif diff --git a/Alc/vector.h b/Alc/vector.h index 12de74f5..b340fe71 100644 --- a/Alc/vector.h +++ b/Alc/vector.h @@ -31,12 +31,32 @@ typedef const _##N* const_##N; #define VECTOR_INIT_STATIC() NULL #define VECTOR_DEINIT(_x) do { al_free((_x)); (_x) = NULL; } while(0) -/* Helper to increase a vector's reserve. Do not call directly. */ -ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact); -#define VECTOR_RESERVE(_x, _c) (vector_reserve((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_c), AL_TRUE)) - -ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count); -#define VECTOR_RESIZE(_x, _c) (vector_resize((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_c))) +#define VECTOR_RESIZE(_x, _s, _c) do { \ + size_t _size = (_s); \ + size_t _cap = (_c); \ + if(_size > _cap) \ + _cap = _size; \ + \ + if(!(_x) && _cap == 0) \ + break; \ + \ + if(((_x) ? (_x)->Capacity : 0) < _cap) \ + { \ + size_t old_size = ((_x) ? (_x)->Size : 0); \ + void *temp; \ + \ + temp = al_calloc(16, sizeof(*(_x)) + sizeof((_x)->Data[0])*_cap); \ + assert(temp != NULL); \ + if((_x)) \ + memcpy(((ALubyte*)temp)+sizeof(*(_x)), (_x)->Data, \ + sizeof((_x)->Data[0])*old_size); \ + \ + al_free((_x)); \ + (_x) = temp; \ + (_x)->Capacity = _cap; \ + } \ + (_x)->Size = _size; \ +} while(0) \ #define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0) #define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0) @@ -44,8 +64,11 @@ ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj #define VECTOR_BEGIN(_x) ((_x) ? (_x)->Data + 0 : NULL) #define VECTOR_END(_x) ((_x) ? (_x)->Data + (_x)->Size : NULL) -#define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), VECTOR_SIZE(_x)+1, AL_FALSE) && \ - (((_x)->Data[(_x)->Size++] = (_obj)),AL_TRUE)) +#define VECTOR_PUSH_BACK(_x, _obj) do { \ + size_t _pbsize = VECTOR_SIZE(_x)+1; \ + VECTOR_RESIZE(_x, _pbsize, _pbsize); \ + (_x)->Data[(_x)->Size-1] = (_obj); \ +} while(0) #define VECTOR_POP_BACK(_x) ((void)((_x)->Size--)) #define VECTOR_BACK(_x) ((_x)->Data[(_x)->Size-1]) -- cgit v1.2.3