authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-28 09:14:18+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
log3aa99f45b8d9b22a2d2d09457f19ca3ef20764f6
tree1315ceb362e846bed103dfe2de69ed0aff5acd04
parentda00e6dd596685e8f0ad9650145309c00485da6f

coff: initial implementation of incremental file allocs


1 files changed, 36 insertions(+), 52 deletions(-)

src/link/Coff.zig+36-52
......@@ -839,58 +839,42 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
839839 math.maxInt(@TypeOf(actual_size));
840840}
841841
842// fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 {
843// const headers_size = self.getSizeOfHeaders();
844// if (start < headers_size)
845// return headers_size;
846
847// const end = start + padToIdeal(size);
848
849// for (self.sections.items(.header)) |header| {
850// const increased_size = padToIdeal(section.sh_size);
851// const test_end = section.sh_offset + increased_size;
852// if (end > section.sh_offset and start < test_end) {
853// return test_end;
854// }
855// }
856// for (self.program_headers.items) |program_header| {
857// const increased_size = padToIdeal(program_header.p_filesz);
858// const test_end = program_header.p_offset + increased_size;
859// if (end > program_header.p_offset and start < test_end) {
860// return test_end;
861// }
862// }
863// return null;
864// }
865
866// // pub fn allocatedSize(self: *Coff, start: u64) u64 {
867// // if (start == 0)
868// // return 0;
869// // var min_pos: u64 = std.math.maxInt(u64);
870// // if (self.shdr_table_offset) |off| {
871// // if (off > start and off < min_pos) min_pos = off;
872// // }
873// // if (self.phdr_table_offset) |off| {
874// // if (off > start and off < min_pos) min_pos = off;
875// // }
876// // for (self.sections.items) |section| {
877// // if (section.sh_offset <= start) continue;
878// // if (section.sh_offset < min_pos) min_pos = section.sh_offset;
879// // }
880// // for (self.program_headers.items) |program_header| {
881// // if (program_header.p_offset <= start) continue;
882// // if (program_header.p_offset < min_pos) min_pos = program_header.p_offset;
883// // }
884// // return min_pos - start;
885// // }
886
887// pub fn findFreeSpace(self: *Coff, object_size: u64, min_alignment: u32) u64 {
888// var start: u64 = 0;
889// while (self.detectAllocCollision(start, object_size)) |item_end| {
890// start = mem.alignForwardGeneric(u64, item_end, min_alignment);
891// }
892// return start;
893// }
842fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 {
843 const headers_size = self.getSizeOfHeaders();
844 if (start < headers_size)
845 return headers_size;
846
847 const end = start + padToIdeal(size);
848
849 for (self.sections.items(.header)) |header| {
850 const increased_size = padToIdeal(header.size_of_raw_data);
851 const test_end = header.pointer_to_raw_data + increased_size;
852 if (end > header.pointer_to_raw_data and start < test_end) {
853 return test_end;
854 }
855 }
856
857 return null;
858}
859
860pub fn allocatedSize(self: *Coff, start: u64) u64 {
861 if (start == 0)
862 return 0;
863 var min_pos: u64 = std.math.maxInt(u64);
864 for (self.sections.items(.header)) |header| {
865 if (header.pointer_to_raw_data <= start) continue;
866 if (header.pointer_to_raw_data < min_pos) min_pos = header.pointer_to_raw_data;
867 }
868 return min_pos - start;
869}
870
871pub fn findFreeSpace(self: *Coff, object_size: u64, min_alignment: u32) u64 {
872 var start: u64 = 0;
873 while (self.detectAllocCollision(start, object_size)) |item_end| {
874 start = mem.alignForwardGeneric(u64, item_end, min_alignment);
875 }
876 return start;
877}
894878
895879inline fn getSizeOfHeaders(self: Coff) usize {
896880 const msdos_hdr_size = msdos_stub.len + 8;