Prefer 'sizeof(t[n])' over 'sizeof(t)*n'

This commit is contained in:
Bruce Hill 2024-06-06 15:05:35 -04:00
parent b4dc858794
commit e9abf8a370
5 changed files with 10 additions and 10 deletions

View File

@ -93,7 +93,7 @@ static file_t *_load_file(const char* filename, FILE *file)
int64_t line_len = 0;
while ((line_len = getline(&line_buf, &line_cap, file)) >= 0) {
if (ret->line_capacity <= ret->num_lines)
ret->line_offsets = GC_REALLOC(ret->line_offsets, sizeof(int64_t)*(ret->line_capacity += 32));
ret->line_offsets = GC_REALLOC(ret->line_offsets, sizeof(int64_t[ret->line_capacity += 32]));
ret->line_offsets[ret->num_lines++] = file_size;
fwrite(line_buf, sizeof(char), line_len, mem);
fflush(mem);

View File

@ -43,7 +43,7 @@
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \
} \
public array_t KindOfInt ## $bits(c_type x) { \
array_t bit_array = (array_t){.data=GC_MALLOC_ATOMIC(sizeof(bool)*8*sizeof(c_type)), .atomic=1, .stride=sizeof(bool), .length=8*sizeof(c_type)}; \
array_t bit_array = (array_t){.data=GC_MALLOC_ATOMIC(sizeof(bool[8*sizeof(c_type)])), .atomic=1, .stride=sizeof(bool), .length=8*sizeof(c_type)}; \
bool *bits = bit_array.data + sizeof(c_type)*8; \
for (size_t i = 0; i < 8*sizeof(c_type); i++) { \
*(bits--) = x & 1; \

View File

@ -102,7 +102,7 @@ static void maybe_copy_on_write(table_t *t, const TypeInfo *type)
}
if (t->bucket_info && t->bucket_info->data_refcount) {
int64_t size = sizeof(bucket_info_t) + t->bucket_info->count*sizeof(bucket_t);
int64_t size = sizeof(bucket_info_t) + sizeof(bucket_t[t->bucket_info->count]);
t->bucket_info = memcpy(GC_MALLOC(size), t->bucket_info, size);
t->bucket_info->data_refcount = 0;
}
@ -208,9 +208,9 @@ static void hashmap_resize_buckets(table_t *t, uint32_t new_capacity, const Type
{
hdebug("About to resize from %u to %u\n", t->bucket_info ? t->bucket_info->count : 0, new_capacity);
hshow(t);
int64_t alloc_size = sizeof(bucket_info_t) + (int64_t)(new_capacity)*sizeof(bucket_t);
int64_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[new_capacity]);
t->bucket_info = GC_MALLOC_ATOMIC(alloc_size);
memset(t->bucket_info->buckets, 0, (int64_t)new_capacity * sizeof(bucket_t));
memset(t->bucket_info->buckets, 0, sizeof(bucket_t[new_capacity]));
t->bucket_info->count = new_capacity;
t->bucket_info->last_free = new_capacity-1;
// Rehash:

View File

@ -273,7 +273,7 @@ public array_t Text$split(CORD str, CORD split)
size_t non_split = u8_strcspn(ustr + i, usplit);
CORD chunk = CORD_substr((CORD)ustr, i, non_split);
if (capacity <= 0)
strings.data = GC_REALLOC(strings.data, sizeof(CORD)*(capacity += 10));
strings.data = GC_REALLOC(strings.data, sizeof(CORD[capacity += 10]));
((CORD*)strings.data)[strings.length++] = chunk;
i += non_split;
@ -327,7 +327,7 @@ public array_t Text$codepoints(CORD text)
uint32_t *codepoints = u8_to_u32(normalized, norm_len-1, codepoint_buf, &codepoint_len);
array_t ret = {
.length=codepoint_len,
.data=memcpy(GC_MALLOC_ATOMIC(sizeof(int32_t)*codepoint_len), codepoints, sizeof(int32_t)*codepoint_len),
.data=memcpy(GC_MALLOC_ATOMIC(sizeof(int32_t[codepoint_len])), codepoints, sizeof(int32_t[codepoint_len])),
.stride=sizeof(int32_t),
.atomic=1,
};
@ -345,7 +345,7 @@ public array_t Text$bytes(CORD text)
--norm_len; // NUL byte
array_t ret = {
.length=norm_len,
.data=memcpy(GC_MALLOC_ATOMIC(sizeof(uint8_t)*norm_len), normalized, sizeof(uint8_t)*norm_len),
.data=memcpy(GC_MALLOC_ATOMIC(sizeof(uint8_t[norm_len])), normalized, sizeof(uint8_t[norm_len])),
.stride=sizeof(uint8_t),
.atomic=1,
};
@ -389,7 +389,7 @@ public int64_t Text$num_bytes(CORD text)
public array_t Text$character_names(CORD text)
{
array_t codepoints = Text$codepoints(text);
array_t ret = {.length=codepoints.length, .stride=sizeof(CORD), .data=GC_MALLOC(sizeof(CORD)*codepoints.length)};
array_t ret = {.length=codepoints.length, .stride=sizeof(CORD), .data=GC_MALLOC(sizeof(CORD[codepoints.length]))};
for (int64_t i = 0; i < codepoints.length; i++) {
char buf[UNINAME_MAX];
unicode_character_name(*(ucs4_t*)(codepoints.data + codepoints.stride*i), buf);

View File

@ -62,7 +62,7 @@ typedef struct TypeInfo {
.tag=TableInfo, .TableInfo.key=key_expr, .TableInfo.value=value_expr})
#define $FunctionInfo(typestr) &((TypeInfo){.size=sizeof(void*), .align=__alignof__(void*), \
.tag=FunctionInfo, .FunctionInfo.type_str=typestr})
#define $ClosureInfo(typestr) &((TypeInfo){.size=2*sizeof(void*), .align=__alignof__(void*), \
#define $ClosureInfo(typestr) &((TypeInfo){.size=sizeof(void*[2]), .align=__alignof__(void*), \
.tag=FunctionInfo, .FunctionInfo.type_str=typestr})
#define $TypeInfoInfo(typestr) &((TypeInfo){.size=sizeof(TypeInfo), .align=__alignof__(TypeInfo), \
.tag=TypeInfoInfo, .TypeInfoInfo.type_str=typestr})