Skip to content

Commit 32a49c1

Browse files
committed
SourceMgr: simplify API, keep file contents in memory, faster SrcLoc conversion
- add `string_buffer.Buf.detach()` to get buffer with ownership - add `File.detach()` to get buffer with ownership - simplify `string_buffer.Buf.newline()` - add `File.is_const` and `SourceMgr.addResource()` for static contents - unify `File` contents as `File.data_`/`File.data_size` - improve `CheckPoint` system (simpler and much faster) - remove `Max_open_files` - remove `close_oldest()`, all files stay in memory until `clear()` or `free()`.
1 parent b87070e commit 32a49c1

File tree

8 files changed

+203
-358
lines changed

8 files changed

+203
-358
lines changed

ast_utils/constants.c2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public const u32 MaxMultiDecl = (1 << MaxMultiDeclBits) - 1;
2929

3030
public const u32 Max_path = 512;
3131

32-
public const u32 Max_open_files = 200;
33-
3432
public const char* output_dir = "output";
3533

3634
public const char* recipe_name = "recipe.txt";

ast_utils/string_buffer.c2

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public fn const u8* Buf.udata(const Buf* buf) {
7373
return (u8*)buf.data_;
7474
}
7575

76+
public fn void* Buf.detach(Buf* buf, u32 *psize) {
77+
*psize = buf.size_;
78+
if (buf.own) {
79+
// try and shrink allocated buffer
80+
void* data = realloc(buf.data_, buf.size_ + 1);
81+
if (!data) data = buf.data_;
82+
buf.data_ = nil;
83+
buf.size_ = 0;
84+
buf.capacity = 0;
85+
buf.own = false;
86+
return data;
87+
} else {
88+
void* data = malloc(buf.size_ + 1);
89+
return memcpy(data, buf.data_, buf.size_ + 1);
90+
}
91+
}
92+
7693
public fn void Buf.clear(Buf* buf) {
7794
buf.size_ = 0;
7895
}
@@ -97,7 +114,6 @@ public fn void Buf.add(Buf* buf, const char* text) {
97114
buf.add2(text, len);
98115
}
99116

100-
// len = strlen(text)
101117
public fn void Buf.add2(Buf* buf, const char* text, u32 len) {
102118
if (buf.size_ + len + 1 > buf.capacity) {
103119
u32 new_cap = buf.capacity * 2;
@@ -109,21 +125,9 @@ public fn void Buf.add2(Buf* buf, const char* text, u32 len) {
109125
buf.data_[buf.size_] = '\0';
110126
}
111127

112-
public fn void Buf.newline(Buf* buf) {
113-
if (buf.size_ + 2 > buf.capacity) {
114-
u32 new_cap = buf.capacity * 2;
115-
while (buf.size_ + 2 > new_cap) new_cap *= 2;
116-
buf.resize(new_cap);
117-
}
118-
buf.data_[buf.size_] = '\n';
119-
buf.size_ += 1;
120-
buf.data_[buf.size_] = 0;
121-
}
122-
128+
public fn void Buf.newline(Buf* buf) { buf.add1('\n'); }
123129
public fn void Buf.space(Buf* buf) { buf.add1(' '); }
124-
125130
public fn void Buf.lparen(Buf* buf) { buf.add1('('); }
126-
127131
public fn void Buf.rparen(Buf* buf) { buf.add1(')'); }
128132

129133
public fn void Buf.print(Buf* buf, const char* format @(printf_format), ...) {

common/file/reader.c2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public fn void Reader.close(Reader* file) {
111111
}
112112
}
113113

114-
public fn bool Reader.isOpen(const Reader* file) {
114+
public fn bool Reader.isOpen(const Reader* file) @(unused) {
115115
return file.region != nil;
116116
}
117117

@@ -145,3 +145,11 @@ public fn const char* Reader.getError(const Reader* file) @(unused) {
145145
}
146146
return msg;
147147
}
148+
149+
public fn void* Reader.detach(Reader* file, u32 *psize) {
150+
void* data = file.region;
151+
*psize = file.size;
152+
file.region = nil;
153+
file.size = 0;
154+
return data;
155+
}

0 commit comments

Comments
 (0)