Skip to content

Commit

Permalink
corec: use a char for the array pointer
Browse files Browse the repository at this point in the history
It is less flexible than void* be can still be casted to
other types more loosely than other types.
It allows finding bogus cast alignment that void* doesn't see.
  • Loading branch information
robUx4 committed Jan 4, 2025
1 parent 5c177bc commit 6390b11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions corec/corec/array/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ static void SlowSort(array* p, size_t Count, size_t Width, arraycmp Cmp, const v
#else
uint8_t Tmp[Width];
#endif
uint8_t* End = ARRAYBEGIN(*p,uint8_t) + Count*Width;
uint8_t* i;
uint8_t* j;
char* End = ARRAYBEGIN(*p,char) + Count*Width;
char* i;
char* j;

j = p->_Begin;
for (i=j+Width; i!=End; i+=Width)
Expand Down Expand Up @@ -387,7 +387,7 @@ static void SlowSort(array* p, size_t Count, size_t Width, arraycmp Cmp, const v
memcpy(j,i,Width);
}
}
p->_Used = j - (uint8_t*)p->_Begin + Width;
p->_Used = j - p->_Begin + Width;
}
}

Expand Down Expand Up @@ -488,7 +488,7 @@ intptr_t ArrayFindEx(const array* p, size_t Count, size_t Width, const void* Dat
else
{
intptr_t No = 0;
const uint8_t* i;
const char* i;
for (i=p->_Begin;Count--;i+=Width,++No)
if (memcmp(i,Data,Width)==0)
{
Expand Down
4 changes: 2 additions & 2 deletions corec/corec/array/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct cc_memheap cc_memheap;
typedef struct array
{
// these are private members, use ARRAY macros to access them
void* _Begin;
char* _Begin;
size_t _Used;

} array;
Expand Down Expand Up @@ -61,7 +61,7 @@ ARRAY_DLL void ArrayDelete(array* p, size_t Ofs, size_t Length);
#define ArraySort(p,type,Cmp,CmpParam,Unique) ArraySortEx(p,ARRAYCOUNT(*p,type),sizeof(type),Cmp,CmpParam,Unique)

#ifdef CONFIG_DEBUGCHECKS
#define ARRAYBEGIN(Array,Type) (assert(&(Array)!=NULL),(Type*)((Array)._Begin))
#define ARRAYBEGIN(Array,Type) (assert(&(Array)!=NULL),(Type*)(void*)((Array)._Begin))
#define ARRAYEMPTY(Array) (assert(&(Array)!=NULL),(Array)._Used==0)
#define ARRAYCOUNT(Array,Type) (assert(&(Array)!=NULL),(((Array)._Used))/sizeof(Type))
#else
Expand Down
2 changes: 1 addition & 1 deletion corec/corec/memheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef void (*memheap_write)(const void* This,void*,const void* Src,size_t Pos,

#define ARRAY_POINTER_HOLDER \
size_t Size; \
alignas(max_align_t) uint8_t data[]
alignas(max_align_t) char data[]

// we can't use a type with a flexible array inside another structure, so we
// use the same define everywhere
Expand Down

0 comments on commit 6390b11

Please sign in to comment.