Using sizeof(type[x]) instead of sizeof(type) * x

This commit is contained in:
L 2023-02-05 22:18:20 +01:00
parent ca25bb983f
commit d4e366ee14

View file

@ -31,26 +31,26 @@ MEM_TempAlloc parsetree_allocator;
yyparsedata parsedata;
sval_u node_none = { 0 };
char *str_replace( char *orig, const char *rep, const char *with ) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
char* str_replace(char* orig, const char* rep, const char* with) {
char* result; // the return string
char* ins; // the next insert point
char* tmp; // varies
size_t len_rep; // length of rep
size_t len_with; // length of with
size_t len_front; // distance between rep and end of last rep
int count; // number of replacements
if( !orig )
if (!orig)
return NULL;
if( !rep )
if (!rep)
rep = "";
len_rep = strlen( rep );
if( !with )
len_rep = strlen(rep);
if (!with)
with = "";
len_with = strlen( with );
len_with = strlen(with);
ins = orig;
for( count = 0; (tmp = strstr( ins, rep )) != nullptr; ++count ) {
for (count = 0; (tmp = strstr(ins, rep)) != nullptr; ++count) {
ins = tmp + len_rep;
}
@ -59,19 +59,19 @@ char *str_replace( char *orig, const char *rep, const char *with ) {
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
tmp = result = ( char * )parsetree_allocator.Alloc( strlen( orig ) + ( len_with - len_rep ) * count + 1 );
tmp = result = (char*)parsetree_allocator.Alloc(strlen(orig) + (len_with - len_rep) * count + 1);
if( !result )
if (!result)
return NULL;
while( count-- ) {
ins = strstr( orig, rep );
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy( tmp, orig, len_front ) + len_front;
tmp = strcpy( tmp, with ) + len_with;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
}
strcpy( tmp, orig );
strcpy(tmp, orig);
return result;
}
@ -79,9 +79,9 @@ void parsetree_freeall()
{
parsetree_allocator.FreeAll();
if( showopcodes->integer )
if (showopcodes->integer)
{
glbs.DPrintf( "%d bytes freed\n", parsedata.total_length );
glbs.DPrintf("%d bytes freed\n", parsedata.total_length);
}
}
@ -96,27 +96,27 @@ size_t parsetree_length()
}
#if 0
char *parsetree_string( const char *string )
char* parsetree_string(const char* string)
{
//char *pszString = ( char * )parsetree_allocator.Alloc( strlen( string ) + 1 );
//strcpy( pszString, string );
char *buffer = str_replace( ( char * )string, "\\\"", "\"" );
char* buffer = str_replace((char*)string, "\\\"", "\"");
if( buffer )
if (buffer)
{
char *ptr = buffer;
char* ptr = buffer;
if( ptr[ 0 ] == '"' )
if (ptr[0] == '"')
{
ptr++;
}
int len = strlen( buffer );
int len = strlen(buffer);
if( buffer[ len - 1 ] == '"' )
if (buffer[len - 1] == '"')
{
buffer[ len - 1 ] = 0;
buffer[len - 1] = 0;
}
buffer = ptr;
@ -129,68 +129,68 @@ char *parsetree_string( const char *string )
extern size_t yyleng;
extern size_t prev_yyleng;
char *parsetree_malloc( size_t s )
char* parsetree_malloc(size_t s)
{
parsedata.total_length += s;
return ( char * )parsetree_allocator.Alloc( s );
return (char*)parsetree_allocator.Alloc(s);
}
sval_u append_lists( sval_u val1, sval_u val2 )
sval_u append_lists(sval_u val1, sval_u val2)
{
val1.node[ 1 ].node[ 1 ] = val2.node[ 0 ];
val1.node[ 1 ] = val2.node[ 1 ];
val1.node[1].node[1] = val2.node[0];
val1.node[1] = val2.node[1];
return val1;
}
sval_u append_node( sval_u val1, sval_u val2 )
sval_u append_node(sval_u val1, sval_u val2)
{
sval_u *node;
sval_u* node;
node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 2 );
node = (sval_u*)parsetree_malloc(sizeof(sval_t[2]));
node[ 1 ].node = NULL;
node[ 0 ] = val2;
node[1].node = NULL;
node[0] = val2;
val1.node[ 1 ].node[ 1 ].node = node;
val1.node[ 1 ].node = node;
val1.node[1].node[1].node = node;
val1.node[1].node = node;
return val1;
}
sval_u prepend_node( sval_u val1, sval_u val2 )
sval_u prepend_node(sval_u val1, sval_u val2)
{
sval_u *node;
sval_u* node;
node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 2 );
node = (sval_u*)parsetree_malloc(sizeof(sval_t[2]));
node[ 0 ] = val1;
node[ 1 ] = val2;
node[0] = val1;
node[1] = val2;
val2.node = node;
return val2;
}
sval_u linked_list_end( sval_u val )
sval_u linked_list_end(sval_u val)
{
sval_u *node;
sval_u* node;
sval_u end;
node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 2 );
node = (sval_u*)parsetree_malloc(sizeof(sval_t[2]));
node[ 0 ] = val;
node[ 1 ].node = NULL;
node[0] = val;
node[1].node = NULL;
end.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 2 );
end.node = (sval_u*)parsetree_malloc(sizeof(sval_t[2]));
end.node[ 0 ].node = node;
end.node[ 1 ].node = node;
end.node[0].node = node;
end.node[1].node = node;
return end;
}
sval_u node1_( int val1 )
sval_u node1_(int val1)
{
sval_u val;
@ -199,7 +199,7 @@ sval_u node1_( int val1 )
return val;
}
sval_u node1b( int val1 )
sval_u node1b(int val1)
{
sval_u val;
@ -208,7 +208,7 @@ sval_u node1b( int val1 )
return val;
}
sval_u node_pos( unsigned int pos )
sval_u node_pos(unsigned int pos)
{
sval_u val;
@ -217,7 +217,7 @@ sval_u node_pos( unsigned int pos )
return val;
}
sval_u node_string( char *text )
sval_u node_string(char* text)
{
sval_u val;
@ -226,109 +226,119 @@ sval_u node_string( char *text )
return val;
}
sval_u node0( int type )
sval_u node0(int type)
{
sval_u val;
if( type == sval_none )
if (type == sval_none)
{
// memory optimization
val.node = &node_none;
}
else
{
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 1 );
val.node = (sval_u*)parsetree_malloc(sizeof(sval_u));
val.node[ 0 ].node = NULL;
val.node[ 0 ].type = type;
val.node[0].node = NULL;
val.node[0].type = type;
}
return val;
}
sval_u node1( int type, sval_u val1 )
sval_u node1(int type, sval_u val1)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 2 );
val.node = (sval_u*)parsetree_malloc(sizeof(sval_u[2]));
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[0].type = type;
val.node[1] = val1;
return val;
}
sval_u node2( int type, sval_u val1, sval_u val2 )
sval_u node2(int type, sval_u val1, sval_u val2)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 3 );
assert(type != sval_none);
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[ 2 ] = val2;
val.node = (sval_u*)parsetree_malloc(sizeof(sval_t[3]));
val.node[0].type = type;
val.node[1] = val1;
val.node[2] = val2;
return val;
}
sval_u node3( int type, sval_u val1, sval_u val2, sval_u val3 )
sval_u node3(int type, sval_u val1, sval_u val2, sval_u val3)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 4 );
assert(type != sval_none);
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[ 2 ] = val2;
val.node[ 3 ] = val3;
val.node = (sval_u*)parsetree_malloc(sizeof(sval_t[4]));
val.node[0].type = type;
val.node[1] = val1;
val.node[2] = val2;
val.node[3] = val3;
return val;
}
sval_u node4( int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4 )
sval_u node4(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 5 );
assert(type != sval_none);
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[ 2 ] = val2;
val.node[ 3 ] = val3;
val.node[ 4 ] = val4;
val.node = (sval_u*)parsetree_malloc(sizeof(sval_t[5]));
val.node[0].type = type;
val.node[1] = val1;
val.node[2] = val2;
val.node[3] = val3;
val.node[4] = val4;
return val;
}
sval_u node5( int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5 )
sval_u node5(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 6 );
assert(type != sval_none);
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[ 2 ] = val2;
val.node[ 3 ] = val3;
val.node[ 4 ] = val4;
val.node[ 5 ] = val5;
val.node = (sval_u*)parsetree_malloc(sizeof(sval_t[6]));
val.node[0].type = type;
val.node[1] = val1;
val.node[2] = val2;
val.node[3] = val3;
val.node[4] = val4;
val.node[5] = val5;
return val;
}
sval_u node6( int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5, sval_u val6 )
sval_u node6(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5, sval_u val6)
{
sval_u val;
val.node = ( sval_u * )parsetree_malloc( sizeof( sval_u ) * 7 );
assert(type != sval_none);
val.node[ 0 ].type = type;
val.node[ 1 ] = val1;
val.node[ 2 ] = val2;
val.node[ 3 ] = val3;
val.node[ 4 ] = val4;
val.node[ 5 ] = val5;
val.node[ 6 ] = val6;
val.node = (sval_u*)parsetree_malloc(sizeof(sval_t[7]));
val.node[0].type = type;
val.node[1] = val1;
val.node[2] = val2;
val.node[3] = val3;
val.node[4] = val4;
val.node[5] = val5;
val.node[6] = val6;
return val;
}