Use asserts in array
This commit is contained in:
		| @@ -100,7 +100,11 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): | |||||||
|     source = CSource( |     source = CSource( | ||||||
|         name=header.name, |         name=header.name, | ||||||
|         decl_types=[*common_decl_types], |         decl_types=[*common_decl_types], | ||||||
|         includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")], |         includes=[ | ||||||
|  |             CInclude(header, local=True, same_dir=True), | ||||||
|  |             CInclude(header="stddef.h"), | ||||||
|  |             CInclude(header="assert.h"), | ||||||
|  |         ], | ||||||
|         internal_funcs=[], |         internal_funcs=[], | ||||||
|         funcs=header.funcs |         funcs=header.funcs | ||||||
|     ) |     ) | ||||||
|   | |||||||
| @@ -1,11 +1,7 @@ | |||||||
|  |   assert(allocator != NULL); | ||||||
|  |  | ||||||
|   u64 allocation_size = sizeof({ArrayType}) + item_size * capacity; |   u64 allocation_size = sizeof({ArrayType}) + item_size * capacity; | ||||||
|   {ArrayType} *array   = NULL; |   {ArrayType} *array  = wapp_mem_allocator_alloc(allocator, allocation_size); | ||||||
|  |  | ||||||
|   if (!allocator) {{ |  | ||||||
|     goto RETURN_GENERIC_ARRAY_ALLOC; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   array = wapp_mem_allocator_alloc(allocator, allocation_size); |  | ||||||
|   if (!array) {{ |   if (!array) {{ | ||||||
|     goto RETURN_GENERIC_ARRAY_ALLOC; |     goto RETURN_GENERIC_ARRAY_ALLOC; | ||||||
|   }} |   }} | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
|   {ArrayType} *output = array; |   assert(allocator != NULL && array != NULL); | ||||||
|  |  | ||||||
|   if (!allocator || !array) {{ |   {ArrayType} *output = array; | ||||||
|     goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   if (array->count >= array->capacity) {{ |   if (array->count >= array->capacity) {{ | ||||||
|     u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); |     u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2); | ||||||
|   | |||||||
| @@ -1,6 +1,4 @@ | |||||||
|   if (!array || array->count >= array->capacity) {{ |   assert(array != NULL && array->count < array->capacity); | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   u64 index = (array->count)++; |   u64 index = (array->count)++; | ||||||
|   wapp_{Tlower}_array_set(array, index, item); |   wapp_{Tlower}_array_set(array, index, item); | ||||||
|   | |||||||
| @@ -1,6 +1,4 @@ | |||||||
|   if (!array || index >= array->count) {{ |   assert(array != NULL & index < array->count); | ||||||
|     return NULL; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   u8 *ptr = (u8 *)(array->items) + (array->item_size * index); |   u8 *ptr = (u8 *)(array->items) + (array->item_size * index); | ||||||
|   return ({T} *)ptr; |   return ({T} *)ptr; | ||||||
|   | |||||||
| @@ -1,6 +1,3 @@ | |||||||
|   {T} *ptr = wapp_{Tlower}_array_get(array, index); |   {T} *ptr = wapp_{Tlower}_array_get(array, index); | ||||||
|   if (!ptr) {{ |  | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   memcpy((void *)ptr, (void *)item, array->item_size); |   memcpy((void *)ptr, (void *)item, array->item_size); | ||||||
|   | |||||||
| @@ -1,5 +1,2 @@ | |||||||
|   if (!array) {{ |   assert(array != NULL); | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   array->count = 0; |   array->count = 0; | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
|   {ArrayType} *output = dst; |   assert(allocator != NULL & src != NULL && dst != NULL); | ||||||
|  |  | ||||||
|   if (!allocator || !src || !dst) {{ |   {ArrayType} *output = dst; | ||||||
|     goto RETURN_{Tupper}_ARRAY_COPY_ALLOC; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   if (src->count >= dst->capacity) {{ |   if (src->count >= dst->capacity) {{ | ||||||
|     u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); |     u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2); | ||||||
|   | |||||||
| @@ -1,6 +1,4 @@ | |||||||
|   if (!src || !dst) {{ |   assert(src != NULL && dst != NULL); | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   wapp_{Tlower}_array_clear(dst); |   wapp_{Tlower}_array_clear(dst); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
|   {ArrayType} *output = array; |   assert(allocator != NULL && array != NULL && other != NULL); | ||||||
|  |  | ||||||
|   if (!allocator || !array || !other) {{ |   {ArrayType} *output = array; | ||||||
|     goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   u64 remaining_capacity = array->capacity - array->count; |   u64 remaining_capacity = array->capacity - array->count; | ||||||
|   if (other->count >= remaining_capacity) {{ |   if (other->count >= remaining_capacity) {{ | ||||||
|   | |||||||
| @@ -1,11 +1,7 @@ | |||||||
|   if (!array || !other) {{ |   assert(array != NULL && other != NULL); | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   u64 remaining_capacity = array->capacity - array->count; |   u64 remaining_capacity = array->capacity - array->count; | ||||||
|   if (other->count >= remaining_capacity) {{ |   assert(other->count < remaining_capacity); | ||||||
|     return; |  | ||||||
|   }} |  | ||||||
|  |  | ||||||
|   {T} *item; |   {T} *item; | ||||||
|  |  | ||||||
|   | |||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -86,7 +86,6 @@ TestFuncResult test_i32_array_append_capped(void) { | |||||||
|  |  | ||||||
|   array = wapp_i32_array(1); |   array = wapp_i32_array(1); | ||||||
|   wapp_i32_array_append_capped(&array, &((i32){10})); |   wapp_i32_array_append_capped(&array, &((i32){10})); | ||||||
|   wapp_i32_array_append_capped(&array, &((i32){20})); |  | ||||||
|  |  | ||||||
|   result = result && array.count == 2; |   result = result && array.count == 2; | ||||||
|  |  | ||||||
| @@ -105,10 +104,6 @@ TestFuncResult test_i32_array_extend_capped(void) { | |||||||
|  |  | ||||||
|   result = result && array1.count == 6; |   result = result && array1.count == 6; | ||||||
|  |  | ||||||
|   wapp_i32_array_extend_capped(&array1, &array1); |  | ||||||
|  |  | ||||||
|   result = result && array1.count == 6; |  | ||||||
|  |  | ||||||
|   return wapp_tester_result(result); |   return wapp_tester_result(result); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user