Skip to content

Commit 8a7ffda

Browse files
committed
Cleanup source_t codebase & fix usage
Previously SOURCE is overhauled with a dynamic array implementation, but parser.c and inliner.c didn't actually benefited from it, and could still cause overflow if size exceeds capacity. In this patch, now extending source_t respects computed result size and expands if need. inliner.c's implementation is now also replaced with dynamic array implementation. Additionally, few functions are renamed to keep function naming convention.
1 parent ff83f01 commit 8a7ffda

File tree

3 files changed

+115
-24
lines changed

3 files changed

+115
-24
lines changed

src/globals.c

+27-8
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ void add_insn(block_t *block,
911911
bb->insn_list.tail = n;
912912
}
913913

914-
source_t *create_source(int init_capacity)
914+
source_t *source_create(int init_capacity)
915915
{
916916
source_t *array = malloc(sizeof(source_t));
917917
if (!array)
@@ -928,12 +928,18 @@ source_t *create_source(int init_capacity)
928928
return array;
929929
}
930930

931-
bool source_expand(source_t *src)
931+
bool source_extend(source_t *src, int len)
932932
{
933-
if (src->size < src->capacity)
933+
int new_size = src->size + len;
934+
935+
if (new_size < src->capacity)
934936
return true;
935937

936-
src->capacity <<= 1;
938+
if (new_size > src->capacity << 1)
939+
src->capacity = new_size;
940+
else
941+
src->capacity <<= 1;
942+
937943
char *new_arr = malloc(src->capacity * sizeof(char));
938944

939945
if (!new_arr)
@@ -949,7 +955,7 @@ bool source_expand(source_t *src)
949955

950956
bool source_push(source_t *src, char value)
951957
{
952-
if (!source_expand(src))
958+
if (!source_extend(src, 1))
953959
return false;
954960

955961
src->elements[src->size] = value;
@@ -958,7 +964,20 @@ bool source_push(source_t *src, char value)
958964
return true;
959965
}
960966

961-
void source_release(source_t *src)
967+
bool source_push_str(source_t *src, char *value)
968+
{
969+
int len = strlen(value);
970+
971+
if (!source_extend(src, len))
972+
return false;
973+
974+
strncpy(src->elements + src->size, value, len);
975+
src->size += len;
976+
977+
return true;
978+
}
979+
980+
void source_free(source_t *src)
962981
{
963982
if (!src)
964983
return;
@@ -985,7 +1004,7 @@ void global_init()
9851004
BB_ARENA = arena_init(DEFAULT_ARENA_SIZE);
9861005
PH2_IR_FLATTEN = malloc(MAX_IR_INSTR * sizeof(ph2_ir_t *));
9871006
LABEL_LUT = malloc(MAX_LABEL * sizeof(label_lut_t));
988-
SOURCE = create_source(MAX_SOURCE);
1007+
SOURCE = source_create(MAX_SOURCE);
9891008
INCLUSION_MAP = hashmap_create(MAX_INCLUSIONS);
9901009
ALIASES = malloc(MAX_ALIASES * sizeof(alias_t));
9911010
CONSTANTS = malloc(MAX_CONSTANTS * sizeof(constant_t));
@@ -1018,7 +1037,7 @@ void global_release()
10181037
arena_free(BB_ARENA);
10191038
free(PH2_IR_FLATTEN);
10201039
free(LABEL_LUT);
1021-
source_release(SOURCE);
1040+
source_free(SOURCE);
10221041
hashmap_free(INCLUSION_MAP);
10231042
free(ALIASES);
10241043
free(CONSTANTS);

src/parser.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -3428,8 +3428,7 @@ void load_source_file(char *file)
34283428
snprintf(path + c + 1, inclusion_path_len, "%s", buffer + 10);
34293429
load_source_file(path);
34303430
} else {
3431-
strcpy(SOURCE->elements + SOURCE->size, buffer);
3432-
SOURCE->size += strlen(buffer);
3431+
source_push_str(SOURCE, buffer);
34333432
}
34343433
}
34353434

tools/inliner.c

+87-14
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,98 @@
1212
* C runtime and essential libraries.
1313
*/
1414

15+
#include <stdbool.h>
1516
#include <stdio.h>
1617
#include <stdlib.h>
18+
#include <string.h>
1719

1820
#define MAX_LINE_LEN 200
19-
#define MAX_SIZE 65536
21+
#define DEFAULT_SOURCE_SIZE 65536
2022

21-
char *SOURCE;
22-
int source_idx;
23+
#define write_char(c) source_push(SOURCE, c)
24+
#define write_str(s) source_push_str(SOURCE, s)
2325

24-
void write_char(char c)
26+
typedef struct {
27+
int size;
28+
int capacity;
29+
char *elements;
30+
} source_t;
31+
32+
source_t *SOURCE;
33+
34+
source_t *source_create(int init_capacity)
35+
{
36+
source_t *array = malloc(sizeof(source_t));
37+
if (!array)
38+
return NULL;
39+
40+
array->size = 0;
41+
array->capacity = init_capacity;
42+
array->elements = malloc(array->capacity * sizeof(char));
43+
if (!array->elements) {
44+
free(array);
45+
return NULL;
46+
}
47+
48+
return array;
49+
}
50+
51+
bool source_extend(source_t *src, int len)
52+
{
53+
int new_size = src->size + len;
54+
55+
if (new_size < src->capacity)
56+
return true;
57+
58+
if (new_size > src->capacity << 1)
59+
src->capacity = new_size;
60+
else
61+
src->capacity <<= 1;
62+
63+
char *new_arr = malloc(src->capacity * sizeof(char));
64+
65+
if (!new_arr)
66+
return false;
67+
68+
memcpy(new_arr, src->elements, src->size * sizeof(char));
69+
70+
free(src->elements);
71+
src->elements = new_arr;
72+
73+
return true;
74+
}
75+
76+
bool source_push(source_t *src, char value)
2577
{
26-
SOURCE[source_idx++] = c;
78+
if (!source_extend(src, 1))
79+
return false;
80+
81+
src->elements[src->size] = value;
82+
src->size++;
83+
84+
return true;
2785
}
2886

29-
void write_str(char *str)
87+
bool source_push_str(source_t *src, char *value)
3088
{
31-
for (int i = 0; str[i]; i++)
32-
write_char(str[i]);
89+
int len = strlen(value);
90+
91+
if (!source_extend(src, len))
92+
return false;
93+
94+
strncpy(src->elements + src->size, value, len);
95+
src->size += len;
96+
97+
return true;
98+
}
99+
100+
void source_free(source_t *src)
101+
{
102+
if (!src)
103+
return;
104+
105+
free(src->elements);
106+
free(src);
33107
}
34108

35109
void write_line(char *src)
@@ -66,8 +140,8 @@ void load_from(char *file)
66140
void save_to(char *file)
67141
{
68142
FILE *f = fopen(file, "wb");
69-
for (int i = 0; i < source_idx; i++)
70-
fputc(SOURCE[i], f);
143+
for (int i = 0; i < SOURCE->size; i++)
144+
fputc(SOURCE->elements[i], f);
71145
fclose(f);
72146
}
73147

@@ -78,8 +152,7 @@ int main(int argc, char *argv[])
78152
return -1;
79153
}
80154

81-
source_idx = 0;
82-
SOURCE = malloc(MAX_SIZE);
155+
SOURCE = source_create(DEFAULT_SOURCE_SIZE);
83156

84157
write_str("/* Created by tools/inliner - DO NOT EDIT. */\n");
85158

@@ -94,14 +167,14 @@ int main(int argc, char *argv[])
94167
* __c("}\n");
95168
*/
96169
write_str("void __c(char *src) {\n");
97-
write_str(" for (int i = 0; src[i]; i++)\n");
98-
write_str(" source_push(SOURCE, src[i]);\n");
170+
write_str(" source_push_str(SOURCE, src);\n");
99171
write_str("}\n");
100172

101173
write_str("void libc_generate() {\n");
102174
load_from(argv[1]);
103175
write_str("}\n");
104176
save_to(argv[2]);
177+
source_free(SOURCE);
105178

106179
return 0;
107180
}

0 commit comments

Comments
 (0)