authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-31 01:31:23-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-31 01:31:23-05:00
log76fa6cdce36671bd9ad54248d77a3941f2eb3e34
tree85595ca0bb300121c12544cb401eac4f43054ff0
parent29bb175f4f9018bfb0e17686c05165527530de25

eradicate use of zeroes in std


5 files changed, 16 insertions(+), 35 deletions(-)

src/all_types.hpp+1-1
...@@ -1382,7 +1382,7 @@ enum AtomicOrder {...@@ -1382,7 +1382,7 @@ enum AtomicOrder {
1382// A basic block contains no branching. Branches send control flow1382// A basic block contains no branching. Branches send control flow
1383// to another basic block.1383// to another basic block.
1384// Phi instructions must be first in a basic block.1384// Phi instructions must be first in a basic block.
1385// The last instruction in a basic block must be an expression of type unreachable.1385// The last instruction in a basic block must be of type unreachable.
1386struct IrBasicBlock {1386struct IrBasicBlock {
1387 ZigList<IrInstruction *> instruction_list;1387 ZigList<IrInstruction *> instruction_list;
1388 IrBasicBlock *other;1388 IrBasicBlock *other;
src/ir.cpp+6-5
...@@ -2741,10 +2741,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco...@@ -2741,10 +2741,11 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
2741static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,2741static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
2742 bool gen_error_defers, bool gen_maybe_defers)2742 bool gen_error_defers, bool gen_maybe_defers)
2743{2743{
2744 while (inner_scope != outer_scope) {2744 Scope *scope = inner_scope;
2745 assert(inner_scope);2745 while (scope != outer_scope) {
2746 if (inner_scope->id == ScopeIdDefer) {2746 assert(scope);
2747 AstNode *defer_node = inner_scope->source_node;2747 if (scope->id == ScopeIdDefer) {
2748 AstNode *defer_node = scope->source_node;
2748 assert(defer_node->type == NodeTypeDefer);2749 assert(defer_node->type == NodeTypeDefer);
2749 ReturnKind defer_kind = defer_node->data.defer.kind;2750 ReturnKind defer_kind = defer_node->data.defer.kind;
2750 if (defer_kind == ReturnKindUnconditional ||2751 if (defer_kind == ReturnKindUnconditional ||
...@@ -2756,7 +2757,7 @@ static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o...@@ -2756,7 +2757,7 @@ static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
2756 }2757 }
27572758
2758 }2759 }
2759 inner_scope = inner_scope->parent;2760 scope = scope->parent;
2760 }2761 }
2761}2762}
27622763
std/hash_map.zig+7-27
...@@ -9,22 +9,12 @@ const debug_u32 = if (want_modification_safety) u32 else void;...@@ -9,22 +9,12 @@ const debug_u32 = if (want_modification_safety) u32 else void;
99
10pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,10pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,
11 inline eql: fn(a: K, b: K)->bool) -> type11 inline eql: fn(a: K, b: K)->bool) -> type
12{
13 SmallHashMap(K, V, hash, eql, @sizeOf(usize))
14}
15
16pub fn SmallHashMap(inline K: type, inline V: type,
17 inline hash: fn(key: K)->u32, inline eql: fn(a: K, b: K)->bool,
18 inline static_size: usize) -> type
19{12{
20 struct {13 struct {
21 entries: []Entry,14 entries: []Entry,
22 size: usize,15 size: usize,
23 max_distance_from_start_index: usize,16 max_distance_from_start_index: usize,
24 allocator: &Allocator,17 allocator: &Allocator,
25 // if the hash map is small enough, we use linear search through these
26 // entries instead of allocating memory
27 prealloc_entries: [static_size]Entry,
28 // this is used to detect bugs where a hashtable is edited while an iterator is running.18 // this is used to detect bugs where a hashtable is edited while an iterator is running.
29 modification_count: debug_u32,19 modification_count: debug_u32,
3020
...@@ -64,18 +54,16 @@ pub fn SmallHashMap(inline K: type, inline V: type,...@@ -64,18 +54,16 @@ pub fn SmallHashMap(inline K: type, inline V: type,
64 };54 };
6555
66 pub fn init(hm: &Self, allocator: &Allocator) {56 pub fn init(hm: &Self, allocator: &Allocator) {
67 hm.entries = hm.prealloc_entries[0...];57 hm.entries = []Entry{};
68 hm.allocator = allocator;58 hm.allocator = allocator;
69 hm.size = 0;59 hm.size = 0;
70 hm.max_distance_from_start_index = 0;60 hm.max_distance_from_start_index = 0;
71 hm.prealloc_entries = zeroes; // sets used to false for all entries61 // it doesn't actually matter what we set this to since we use wrapping integer arithmetic
72 hm.modification_count = zeroes;62 hm.modification_count = undefined;
73 }63 }
7464
75 pub fn deinit(hm: &Self) {65 pub fn deinit(hm: &Self) {
76 if (hm.entries.ptr != &hm.prealloc_entries[0]) {66 hm.allocator.free(Entry, hm.entries);
77 hm.allocator.free(Entry, hm.entries);
78 }
79 }67 }
8068
81 pub fn clear(hm: &Self) {69 pub fn clear(hm: &Self) {
...@@ -90,14 +78,8 @@ pub fn SmallHashMap(inline K: type, inline V: type,...@@ -90,14 +78,8 @@ pub fn SmallHashMap(inline K: type, inline V: type,
90 pub fn put(hm: &Self, key: K, value: V) -> %void {78 pub fn put(hm: &Self, key: K, value: V) -> %void {
91 hm.incrementModificationCount();79 hm.incrementModificationCount();
9280
93 const resize = if (hm.entries.ptr == &hm.prealloc_entries[0]) {81 // if we get too full (60%), double the capacity
94 // preallocated entries table is full82 if (hm.size * 5 >= hm.entries.len * 3) {
95 hm.size == hm.entries.len
96 } else {
97 // if we get too full (60%), double the capacity
98 hm.size * 5 >= hm.entries.len * 3
99 };
100 if (resize) {
101 const old_entries = hm.entries;83 const old_entries = hm.entries;
102 %return hm.initCapacity(hm.entries.len * 2);84 %return hm.initCapacity(hm.entries.len * 2);
103 // dump all of the old elements into the new table85 // dump all of the old elements into the new table
...@@ -106,9 +88,7 @@ pub fn SmallHashMap(inline K: type, inline V: type,...@@ -106,9 +88,7 @@ pub fn SmallHashMap(inline K: type, inline V: type,
106 hm.internalPut(old_entry.key, old_entry.value);88 hm.internalPut(old_entry.key, old_entry.value);
107 }89 }
108 }90 }
109 if (old_entries.ptr != &hm.prealloc_entries[0]) {91 hm.allocator.free(Entry, old_entries);
110 hm.allocator.free(Entry, old_entries);
111 }
112 }92 }
11393
114 hm.internalPut(key, value);94 hm.internalPut(key, value);
std/list.zig+1-1
...@@ -13,7 +13,7 @@ pub fn List(inline T: type) -> type{...@@ -13,7 +13,7 @@ pub fn List(inline T: type) -> type{
1313
14 pub fn init(allocator: &Allocator) -> Self {14 pub fn init(allocator: &Allocator) -> Self {
15 Self {15 Self {
16 .items = zeroes,16 .items = []T{},
17 .len = 0,17 .len = 0,
18 .allocator = allocator,18 .allocator = allocator,
19 }19 }
std/mem.zig+1-1
...@@ -67,7 +67,7 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {...@@ -67,7 +67,7 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
67}67}
6868
69pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {69pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
70 var result: T = zeroes;70 var result: T = undefined;
71 const result_slice = ([]u8)((&result)[0...1]);71 const result_slice = ([]u8)((&result)[0...1]);
72 const padding = @sizeOf(T) - buf.len;72 const padding = @sizeOf(T) - buf.len;
7373