mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Prepped for #160, fixed a stack-shrink bug
This commit is contained in:
@@ -21,26 +21,6 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);
|
||||
#define TOY_ARRAY_EXPANSION_RATE 2
|
||||
#endif
|
||||
|
||||
//quick allocate
|
||||
#ifndef TOY_ARRAY_ALLOCATE
|
||||
#define TOY_ARRAY_ALLOCATE() Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY)
|
||||
#endif
|
||||
|
||||
//quick free
|
||||
#ifndef TOY_ARRAY_FREE
|
||||
#define TOY_ARRAY_FREE(array) (array = Toy_resizeArray(array, 0))
|
||||
#endif
|
||||
|
||||
//one line to expand the array
|
||||
#ifndef TOY_ARRAY_EXPAND
|
||||
#define TOY_ARRAY_EXPAND(array) (array = (array != NULL && (array)->count + 1 > (array)->capacity ? Toy_resizeArray(array, (array)->capacity * TOY_ARRAY_EXPANSION_RATE) : array))
|
||||
#endif
|
||||
|
||||
//quick push back
|
||||
#ifndef TOY_ARRAY_PUSHBACK
|
||||
#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array), (array)->data[(array)->count++] = (value))
|
||||
#endif
|
||||
|
||||
//TODO: array.getLength()
|
||||
//TODO: array.pushFront(x)
|
||||
//TODO: array.pushBack(x)
|
||||
|
||||
@@ -31,7 +31,7 @@ void Toy_freeStack(Toy_Stack* stack) {
|
||||
|
||||
void Toy_pushStack(Toy_Stack** stackHandle, Toy_Value value) {
|
||||
//don't go overboard
|
||||
if ((*stackHandle)->count >= TOY_STACK_OVERFLOW) {
|
||||
if ((*stackHandle)->count >= TOY_STACK_OVERFLOW_THRESHOLD) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack overflow\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
@@ -72,9 +72,9 @@ Toy_Value Toy_popStack(Toy_Stack** stackHandle) {
|
||||
}
|
||||
|
||||
//shrink if possible
|
||||
if ((*stackHandle)->count > TOY_STACK_INITIAL_CAPACITY && (*stackHandle)->count < (*stackHandle)->capacity * TOY_STACK_CONTRACTION_THRESHOLD) {
|
||||
(*stackHandle)->capacity /= 2;
|
||||
unsigned int newCapacity = (*stackHandle)->capacity;
|
||||
if ((*stackHandle)->capacity > TOY_STACK_INITIAL_CAPACITY && (*stackHandle)->count <= (*stackHandle)->capacity / TOY_STACK_CONTRACTION_THRESHOLD) {
|
||||
(*stackHandle)->capacity /= TOY_STACK_CONTRACTION_THRESHOLD;
|
||||
unsigned int newCapacity = (*stackHandle)->capacity; //cache for the msg
|
||||
|
||||
(*stackHandle) = realloc((*stackHandle), (*stackHandle)->capacity * sizeof(Toy_Value) + sizeof(Toy_Stack));
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ TOY_API Toy_Value Toy_popStack(Toy_Stack** stackHandle);
|
||||
#endif
|
||||
|
||||
#ifndef TOY_STACK_CONTRACTION_THRESHOLD
|
||||
#define TOY_STACK_CONTRACTION_THRESHOLD (1 / 4)
|
||||
#define TOY_STACK_CONTRACTION_THRESHOLD 4 // this integer means 'shrink when count drops below one-forth of capacity'
|
||||
#endif
|
||||
|
||||
//prevent an infinite expansion, limited to 1MB
|
||||
#ifndef TOY_STACK_OVERFLOW
|
||||
#define TOY_STACK_OVERFLOW (1024 * 1024 / sizeof(Toy_Value))
|
||||
#ifndef TOY_STACK_OVERFLOW_THRESHOLD
|
||||
#define TOY_STACK_OVERFLOW_THRESHOLD (1024 * 1024 / sizeof(Toy_Value) - sizeof(Toy_Stack))
|
||||
#endif
|
||||
|
||||
@@ -107,7 +107,7 @@ void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value) {
|
||||
}
|
||||
|
||||
//expand the capacity
|
||||
if ((*tableHandle)->count > (*tableHandle)->capacity * TOY_TABLE_EXPANSION_THRESHOLD) {
|
||||
if ((*tableHandle)->count >= (*tableHandle)->capacity * TOY_TABLE_EXPANSION_THRESHOLD) {
|
||||
(*tableHandle) = Toy_private_adjustTableCapacity((*tableHandle), (*tableHandle)->capacity * TOY_TABLE_EXPANSION_RATE);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ TOY_API Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned
|
||||
#define TOY_TABLE_EXPANSION_RATE 2
|
||||
#endif
|
||||
|
||||
//expand when the contents passes a certain percentage of the capacity
|
||||
//expand when the contents passes a certain percentage (80%) of the capacity
|
||||
#ifndef TOY_TABLE_EXPANSION_THRESHOLD
|
||||
#define TOY_TABLE_EXPANSION_THRESHOLD 0.8
|
||||
#define TOY_TABLE_EXPANSION_THRESHOLD 0.8f
|
||||
#endif
|
||||
|
||||
@@ -139,7 +139,7 @@ void Toy_freeValue(Toy_Value value) {
|
||||
Toy_freeValue(ptr->data[i]);
|
||||
}
|
||||
|
||||
TOY_ARRAY_FREE(ptr);
|
||||
Toy_resizeArray(ptr, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user