diff options
| author | 2025-04-30 20:30:39 -0400 | |
|---|---|---|
| committer | 2025-04-30 20:30:39 -0400 | |
| commit | 10048b0d31d0db923ae39c6bbd67139ed6252f6f (patch) | |
| tree | 65df1666aacd102f59b4ac0844ccc7f7ddda91db /src/CompatData.zig | |
| parent | Setup variants for all allocating modules (diff) | |
| download | zg-10048b0d31d0db923ae39c6bbd67139ed6252f6f.tar.gz zg-10048b0d31d0db923ae39c6bbd67139ed6252f6f.tar.xz zg-10048b0d31d0db923ae39c6bbd67139ed6252f6f.zip | |
Allocation Failure Tests
These turned up an excessive amount of allocations in CanonData and
CompatData, which have been reduced to two through the somewhat
squirrely use of 'magic numbers'.
There are now allocation tests for every allocated structure in the
library, and they run to completion in a reasonable amount of time.
So, that's nice.
Diffstat (limited to 'src/CompatData.zig')
| -rw-r--r-- | src/CompatData.zig | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/CompatData.zig b/src/CompatData.zig index d787103..794abca 100644 --- a/src/CompatData.zig +++ b/src/CompatData.zig | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | //! Compatibility Data | 1 | //! Compatibility Data |
| 2 | 2 | ||
| 3 | nfkd: [][]u21 = undefined, | 3 | nfkd: [][]u21 = undefined, |
| 4 | cps: []u21 = undefined, | ||
| 4 | 5 | ||
| 5 | const CompatData = @This(); | 6 | const CompatData = @This(); |
| 6 | 7 | ||
| @@ -15,27 +16,35 @@ pub fn init(allocator: mem.Allocator) !CompatData { | |||
| 15 | var cpdata = CompatData{ | 16 | var cpdata = CompatData{ |
| 16 | .nfkd = try allocator.alloc([]u21, 0x110000), | 17 | .nfkd = try allocator.alloc([]u21, 0x110000), |
| 17 | }; | 18 | }; |
| 19 | { | ||
| 20 | errdefer allocator.free(cpdata.nfkd); | ||
| 21 | cpdata.cps = try allocator.alloc(u21, magic.compat_size); | ||
| 22 | } | ||
| 18 | errdefer cpdata.deinit(allocator); | 23 | errdefer cpdata.deinit(allocator); |
| 19 | 24 | ||
| 20 | @memset(cpdata.nfkd, &.{}); | 25 | @memset(cpdata.nfkd, &.{}); |
| 21 | 26 | ||
| 27 | var total_len: usize = 0; | ||
| 28 | |||
| 22 | while (true) { | 29 | while (true) { |
| 23 | const len: u8 = try reader.readInt(u8, endian); | 30 | const len: u8 = try reader.readInt(u8, endian); |
| 24 | if (len == 0) break; | 31 | if (len == 0) break; |
| 25 | const cp = try reader.readInt(u24, endian); | 32 | const cp = try reader.readInt(u24, endian); |
| 26 | cpdata.nfkd[cp] = try allocator.alloc(u21, len - 1); | 33 | const nk_s = cpdata.cps[total_len..][0 .. len - 1]; |
| 27 | for (0..len - 1) |i| { | 34 | for (0..len - 1) |i| { |
| 28 | cpdata.nfkd[cp][i] = @intCast(try reader.readInt(u24, endian)); | 35 | nk_s[i] = @intCast(try reader.readInt(u24, endian)); |
| 29 | } | 36 | } |
| 37 | cpdata.nfkd[cp] = nk_s; | ||
| 38 | total_len += len - 1; | ||
| 30 | } | 39 | } |
| 31 | 40 | ||
| 41 | if (comptime magic.print) std.debug.print("CompatData magic number: {d}", .{total_len}); | ||
| 42 | |||
| 32 | return cpdata; | 43 | return cpdata; |
| 33 | } | 44 | } |
| 34 | 45 | ||
| 35 | pub fn deinit(cpdata: *const CompatData, allocator: mem.Allocator) void { | 46 | pub fn deinit(cpdata: *const CompatData, allocator: mem.Allocator) void { |
| 36 | for (cpdata.nfkd) |slice| { | 47 | allocator.free(cpdata.cps); |
| 37 | if (slice.len != 0) allocator.free(slice); | ||
| 38 | } | ||
| 39 | allocator.free(cpdata.nfkd); | 48 | allocator.free(cpdata.nfkd); |
| 40 | } | 49 | } |
| 41 | 50 | ||
| @@ -48,3 +57,4 @@ const std = @import("std"); | |||
| 48 | const builtin = @import("builtin"); | 57 | const builtin = @import("builtin"); |
| 49 | const compress = std.compress; | 58 | const compress = std.compress; |
| 50 | const mem = std.mem; | 59 | const mem = std.mem; |
| 60 | const magic = @import("magic"); | ||