authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 12:04:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 12:04:19-07:00
logcb9405cdbdd7c9dc30c84ea9a8a17ced2cae66af
treec78ef93e0540ef438d20bfec29f058dd28822a72
parent051aadd7810de9b68f415ec00a3867f6f783b961

don't collect stack trace frames in release safe mode by default

We don't pass no-omit-frame-pointer in release safe by default, so it also makes sense to not try to collect stack trace frames by default in release safe mode.

1 files changed, 47 insertions(+), 65 deletions(-)

lib/std/heap/general_purpose_allocator.zig+47-65
......@@ -95,6 +95,7 @@
9595const std = @import("std");
9696const math = std.math;
9797const assert = std.debug.assert;
98const mem = std.mem;
9899const Allocator = std.mem.Allocator;
99100const page_size = std.mem.page_size;
100101const StackTrace = std.builtin.StackTrace;
......@@ -117,11 +118,15 @@ const sys_can_stack_trace = switch (std.Target.current.cpu.arch) {
117118
118119 else => true,
119120};
120const default_stack_trace_frames: usize = if (sys_can_stack_trace) 4 else 0;
121const default_sys_stack_trace_frames: usize = if (sys_can_stack_trace) 4 else 0;
122const default_stack_trace_frames: usize = switch (std.builtin.mode) {
123 .Debug => default_sys_stack_trace_frames,
124 else => 0,
125};
121126
122127pub const Config = struct {
123128 /// Number of stack frames to capture.
124 stack_trace_frames: usize = if (std.debug.runtime_safety) default_stack_trace_frames else @as(usize, 0),
129 stack_trace_frames: usize = default_stack_trace_frames,
125130
126131 /// If true, the allocator will have two fields:
127132 /// * `total_requested_bytes` which tracks the total allocated bytes of memory requested.
......@@ -163,7 +168,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
163168 const one_trace_size = @sizeOf(usize) * stack_n;
164169 const traces_per_slot = 2;
165170
166 pub const Error = std.mem.Allocator.Error;
171 pub const Error = mem.Allocator.Error;
167172
168173 const small_bucket_count = math.log2(page_size);
169174 const largest_bucket_object_size = 1 << (small_bucket_count - 1);
......@@ -246,7 +251,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
246251 }
247252
248253 fn bucketStackFramesStart(size_class: usize) usize {
249 return std.mem.alignForward(
254 return mem.alignForward(
250255 @sizeOf(BucketHeader) + usedBitsCount(size_class),
251256 @alignOf(usize),
252257 );
......@@ -322,7 +327,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
322327 }
323328
324329 fn collectStackTrace(first_trace_addr: usize, addresses: *[stack_n]usize) void {
325 std.mem.set(usize, addresses, 0);
330 if (stack_n == 0) return;
331 mem.set(usize, addresses, 0);
326332 var stack_trace = StackTrace{
327333 .instruction_addresses = addresses,
328334 .index = 0,
......@@ -679,14 +685,14 @@ test "realloc" {
679685 // This reallocation should keep its pointer address.
680686 const old_slice = slice;
681687 slice = try allocator.realloc(slice, 2);
682 assert(old_slice.ptr == slice.ptr);
683 assert(slice[0] == 0x12);
688 std.testing.expect(old_slice.ptr == slice.ptr);
689 std.testing.expect(slice[0] == 0x12);
684690 slice[1] = 0x34;
685691
686692 // This requires upgrading to a larger size class
687693 slice = try allocator.realloc(slice, 17);
688 assert(slice[0] == 0x12);
689 assert(slice[1] == 0x34);
694 std.testing.expect(slice[0] == 0x12);
695 std.testing.expect(slice[1] == 0x34);
690696}
691697
692698test "shrink" {
......@@ -697,18 +703,18 @@ test "shrink" {
697703 var slice = try allocator.alloc(u8, 20);
698704 defer allocator.free(slice);
699705
700 std.mem.set(u8, slice, 0x11);
706 mem.set(u8, slice, 0x11);
701707
702708 slice = allocator.shrink(slice, 17);
703709
704710 for (slice) |b| {
705 assert(b == 0x11);
711 std.testing.expect(b == 0x11);
706712 }
707713
708714 slice = allocator.shrink(slice, 16);
709715
710716 for (slice) |b| {
711 assert(b == 0x11);
717 std.testing.expect(b == 0x11);
712718 }
713719}
714720
......@@ -722,10 +728,10 @@ test "large object - grow" {
722728
723729 var old = slice1;
724730 slice1 = try allocator.realloc(slice1, page_size * 2 - 10);
725 assert(slice1.ptr == old.ptr);
731 std.testing.expect(slice1.ptr == old.ptr);
726732
727733 slice1 = try allocator.realloc(slice1, page_size * 2);
728 assert(slice1.ptr == old.ptr);
734 std.testing.expect(slice1.ptr == old.ptr);
729735
730736 slice1 = try allocator.realloc(slice1, page_size * 2 + 1);
731737}
......@@ -743,8 +749,8 @@ test "realloc small object to large object" {
743749 // This requires upgrading to a large object
744750 const large_object_size = page_size * 2 + 50;
745751 slice = try allocator.realloc(slice, large_object_size);
746 assert(slice[0] == 0x12);
747 assert(slice[60] == 0x34);
752 std.testing.expect(slice[0] == 0x12);
753 std.testing.expect(slice[60] == 0x34);
748754}
749755
750756test "shrink large object to large object" {
......@@ -758,16 +764,16 @@ test "shrink large object to large object" {
758764 slice[60] = 0x34;
759765
760766 slice = try allocator.resize(slice, page_size * 2 + 1);
761 assert(slice[0] == 0x12);
762 assert(slice[60] == 0x34);
767 std.testing.expect(slice[0] == 0x12);
768 std.testing.expect(slice[60] == 0x34);
763769
764770 slice = allocator.shrink(slice, page_size * 2 + 1);
765 assert(slice[0] == 0x12);
766 assert(slice[60] == 0x34);
771 std.testing.expect(slice[0] == 0x12);
772 std.testing.expect(slice[60] == 0x34);
767773
768774 slice = try allocator.realloc(slice, page_size * 2);
769 assert(slice[0] == 0x12);
770 assert(slice[60] == 0x34);
775 std.testing.expect(slice[0] == 0x12);
776 std.testing.expect(slice[60] == 0x34);
771777}
772778
773779test "shrink large object to large object with larger alignment" {
......@@ -783,7 +789,7 @@ test "shrink large object to large object with larger alignment" {
783789 defer allocator.free(slice);
784790
785791 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
786 while (isAligned(@ptrToInt(slice.ptr), page_size * 2)) {
792 while (mem.isAligned(@ptrToInt(slice.ptr), page_size * 2)) {
787793 try stuff_to_free.append(slice);
788794 slice = try allocator.alignedAlloc(u8, 16, alloc_size);
789795 }
......@@ -794,8 +800,8 @@ test "shrink large object to large object with larger alignment" {
794800 slice[60] = 0x34;
795801
796802 slice = try allocator.reallocAdvanced(slice, page_size * 2, alloc_size / 2, .exact);
797 assert(slice[0] == 0x12);
798 assert(slice[60] == 0x34);
803 std.testing.expect(slice[0] == 0x12);
804 std.testing.expect(slice[60] == 0x34);
799805}
800806
801807test "realloc large object to small object" {
......@@ -809,8 +815,8 @@ test "realloc large object to small object" {
809815 slice[16] = 0x34;
810816
811817 slice = try allocator.realloc(slice, 19);
812 assert(slice[0] == 0x12);
813 assert(slice[16] == 0x34);
818 std.testing.expect(slice[0] == 0x12);
819 std.testing.expect(slice[16] == 0x34);
814820}
815821
816822test "non-page-allocator backing allocator" {
......@@ -834,7 +840,7 @@ test "realloc large object to larger alignment" {
834840 defer allocator.free(slice);
835841
836842 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
837 while (isAligned(@ptrToInt(slice.ptr), page_size * 2)) {
843 while (mem.isAligned(@ptrToInt(slice.ptr), page_size * 2)) {
838844 try stuff_to_free.append(slice);
839845 slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);
840846 }
......@@ -845,40 +851,16 @@ test "realloc large object to larger alignment" {
845851 slice[16] = 0x34;
846852
847853 slice = try allocator.reallocAdvanced(slice, 32, page_size * 2 + 100, .exact);
848 assert(slice[0] == 0x12);
849 assert(slice[16] == 0x34);
854 std.testing.expect(slice[0] == 0x12);
855 std.testing.expect(slice[16] == 0x34);
850856
851857 slice = try allocator.reallocAdvanced(slice, 32, page_size * 2 + 25, .exact);
852 assert(slice[0] == 0x12);
853 assert(slice[16] == 0x34);
858 std.testing.expect(slice[0] == 0x12);
859 std.testing.expect(slice[16] == 0x34);
854860
855861 slice = try allocator.reallocAdvanced(slice, page_size * 2, page_size * 2 + 100, .exact);
856 assert(slice[0] == 0x12);
857 assert(slice[16] == 0x34);
858}
859
860fn isAligned(addr: usize, alignment: usize) bool {
861 // 000010000 // example addr
862 // 000001111 // subtract 1
863 // 111110000 // binary not
864 const aligned_addr = (addr & ~(alignment - 1));
865 return aligned_addr == addr;
866}
867
868test "isAligned works" {
869 assert(isAligned(0, 4));
870 assert(isAligned(1, 1));
871 assert(isAligned(2, 1));
872 assert(isAligned(2, 2));
873 assert(!isAligned(2, 4));
874 assert(isAligned(3, 1));
875 assert(!isAligned(3, 2));
876 assert(!isAligned(3, 4));
877 assert(isAligned(4, 4));
878 assert(isAligned(4, 2));
879 assert(isAligned(4, 1));
880 assert(!isAligned(4, 8));
881 assert(!isAligned(4, 16));
862 std.testing.expect(slice[0] == 0x12);
863 std.testing.expect(slice[16] == 0x34);
882864}
883865
884866test "large object shrinks to small but allocation fails during shrink" {
......@@ -895,8 +877,8 @@ test "large object shrinks to small but allocation fails during shrink" {
895877 // Next allocation will fail in the backing allocator of the GeneralPurposeAllocator
896878
897879 slice = allocator.shrink(slice, 4);
898 assert(slice[0] == 0x12);
899 assert(slice[3] == 0x34);
880 std.testing.expect(slice[0] == 0x12);
881 std.testing.expect(slice[3] == 0x34);
900882}
901883
902884test "objects of size 1024 and 2048" {
......@@ -919,20 +901,20 @@ test "setting a memory cap" {
919901 gpda.setRequestedMemoryLimit(1010);
920902
921903 const small = try allocator.create(i32);
922 assert(gpda.total_requested_bytes == 4);
904 std.testing.expect(gpda.total_requested_bytes == 4);
923905
924906 const big = try allocator.alloc(u8, 1000);
925 assert(gpda.total_requested_bytes == 1004);
907 std.testing.expect(gpda.total_requested_bytes == 1004);
926908
927909 std.testing.expectError(error.OutOfMemory, allocator.create(u64));
928910
929911 allocator.destroy(small);
930 assert(gpda.total_requested_bytes == 1000);
912 std.testing.expect(gpda.total_requested_bytes == 1000);
931913
932914 allocator.free(big);
933 assert(gpda.total_requested_bytes == 0);
915 std.testing.expect(gpda.total_requested_bytes == 0);
934916
935917 const exact = try allocator.alloc(u8, 1010);
936 assert(gpda.total_requested_bytes == 1010);
918 std.testing.expect(gpda.total_requested_bytes == 1010);
937919 allocator.free(exact);
938920}