Simplify some vector size range checks

This commit is contained in:
Chris Robinson
2014-08-10 10:52:08 -07:00
parent 936a79da7d
commit a3c236598a
2 changed files with 19 additions and 17 deletions
+14 -12
View File
@@ -594,25 +594,25 @@ void SetRTPriority(void)
}
ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_count, size_t obj_size, ALboolean exact)
ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count, ALboolean exact)
{
vector_ *vecptr = (vector_*)ptr;
if((size_t)(*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
if(obj_count < 0)
return AL_FALSE;
if((*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
* ALsizei can hold. */
if(obj_count > (INT_MAX>>1)+1)
return AL_FALSE;
/* 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 = NextPowerOf2((ALuint)obj_count);
if(obj_count < 0) return AL_FALSE;
}
/* 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,
@@ -621,20 +621,22 @@ ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_count, size_t o
if(temp == NULL) return AL_FALSE;
*vecptr = temp;
(*vecptr)->Capacity = (ALsizei)obj_count;
(*vecptr)->Capacity = obj_count;
(*vecptr)->Size = old_size;
}
return AL_TRUE;
}
ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_count, size_t obj_size)
ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count)
{
vector_ *vecptr = (vector_*)ptr;
if(obj_count < 0)
return AL_FALSE;
if(*vecptr || obj_count > 0)
{
if(!vector_reserve((char*)vecptr, base_size, obj_count, obj_size, AL_TRUE))
if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
return AL_FALSE;
(*vecptr)->Size = (ALsizei)obj_count;
(*vecptr)->Size = obj_count;
}
return AL_TRUE;
}
@@ -651,7 +653,7 @@ ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_
assert(numins > 0);
if(INT_MAX-VECTOR_SIZE(*vecptr) <= numins ||
!vector_reserve((char*)vecptr, base_size, VECTOR_SIZE(*vecptr)+numins, obj_size, AL_TRUE))
!vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
return AL_FALSE;
/* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
+5 -5
View File
@@ -30,11 +30,11 @@ typedef const _##N* const_##N;
#define VECTOR_DEINIT(_x) do { 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_count, size_t obj_size, ALboolean exact);
#define VECTOR_RESERVE(_x, _c) (vector_reserve((char*)&(_x), sizeof(*(_x)), (_c), sizeof((_x)->Data[0]), AL_TRUE))
ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, ALsizei 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_count, size_t obj_size);
#define VECTOR_RESIZE(_x, _c) (vector_resize((char*)&(_x), sizeof(*(_x)), (_c), sizeof((_x)->Data[0])))
ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, ALsizei obj_count);
#define VECTOR_RESIZE(_x, _c) (vector_resize((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_c)))
#define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
#define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)
@@ -57,7 +57,7 @@ ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_
#define VECTOR_INSERT(_x, _i, _s, _e) (vector_insert((char*)&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_i), (_s), (_e)))
#endif
#define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve((char*)&(_x), sizeof(*(_x)), VECTOR_SIZE(_x)+1, sizeof((_x)->Data[0]), AL_FALSE) && \
#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_POP_BACK(_x) ((void)((_x)->Size--))