hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 02f24e201c8e7c49e959d77dba92fbc02248905a
parent 5720c45f454e4f86d647ac841415a38bbc4e8414
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat,  4 Sep 2021 07:58:31 +0200

hash::*: use caller allocation throughout

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mcompress/zlib/reader.ha | 7+++----
Mhash/adler32/adler32.ha | 21+++++++++++----------
Mhash/crc16/crc16.ha | 51+++++++++++++++++++++++----------------------------
Mhash/crc32/crc32.ha | 41+++++++++++++++++++----------------------
Mhash/crc64/crc64.ha | 31+++++++++++++++----------------
Mhash/fnv/fnv.ha | 2+-
6 files changed, 72 insertions(+), 81 deletions(-)

diff --git a/compress/zlib/reader.ha b/compress/zlib/reader.ha @@ -19,7 +19,7 @@ type reader = struct { stream: io::stream, source: *io::stream, flate: *io::stream, - hash: *hash::hash, + hash: adler32::state, }; type decompress_err = enum { @@ -57,7 +57,7 @@ fn verifysum(s: *reader) (io::EOF | io::error) = { }; }; - return if (adler32::sum32(s.hash) == endian::begetu32(hash)) io::EOF + return if (adler32::sum32(&s.hash) == endian::begetu32(hash)) io::EOF else wraperror(decompress_err::CHECKSUM); }; @@ -67,13 +67,12 @@ fn read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = { io::EOF => return verifysum(s), z: size => buf = buf[..z], }; - return hash::write(s.hash, buf); + return hash::write(&s.hash, buf); }; fn close(s: *io::stream) void = { const s = s: *reader; io::close(s.flate); - hash::close(s.hash); }; // Creates a stream which decompresses zlib (RFC 1950) data. diff --git a/hash/adler32/adler32.ha b/hash/adler32/adler32.ha @@ -6,14 +6,16 @@ use strings; // The size, in bytes, of an Adler-32 checksum. export def SIZE: size = 4; -type state = struct { +export type state = struct { hash::hash, a: u32, b: u32, }; -// Creates a [[hash::hash]] which computes the Adler-32 checksum algorithm. -export fn adler32() *hash::hash = alloc(state { +// Creates a [[hash::hash]] which computes the Adler-32 checksum algorithm. This +// hash does not allocate any state, so you do not need to call [[hash::close]] +// when you are done with it. +export fn adler32() state = state { writer = &write, closer = &close, sum = &sum, @@ -21,9 +23,9 @@ export fn adler32() *hash::hash = alloc(state { sz = 4, a = 1, b = 0, -}); +}; -fn close(s: *io::stream) void = free(s); +fn close(s: *io::stream) void = void; fn write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *state; @@ -66,17 +68,16 @@ export fn sum32(h: *hash::hash) u32 = { ]; let hash = adler32(); - defer hash::close(hash); let s: [4]u8 = [0...]; for (let i = 0z; i < len(vectors); i += 1) { let vec = vectors[i]; - hash::reset(hash); - hash::write(hash, strings::toutf8(vec.0)); + hash::reset(&hash); + hash::write(&hash, strings::toutf8(vec.0)); - hash::sum(hash, s); + hash::sum(&hash, s); assert(endian::begetu32(s) == vec.1); - assert(sum32(hash) == vec.1); + assert(sum32(&hash) == vec.1); }; }; diff --git a/hash/crc16/crc16.ha b/hash/crc16/crc16.ha @@ -171,18 +171,20 @@ export fn memoize(polynomial: u16, buf: *[256]u16) void = { }; }; -type state = struct { +export type state = struct { hash::hash, table: *[256]u16, cval: u16, }; -// Creates a [[hash::hash]] which computes the CRC-16 algorithm. +// Creates a [[hash::hash]] which computes the CRC-16 algorithm. This hash +// function does not allocate any state, so you do not need to call +// [[hash::close]] when you are done with it. // // It takes a table of memoized values for a given polynomail (for example, // [[ccitt_table]], [[dect_table]], or [[ansi_table]]); a table for a // custom polynomial, populated by [[memoize]] function, may also be used. -export fn crc16(table: *[256]u16) *hash::hash = alloc(state { +export fn crc16(table: *[256]u16) state = state { writer = &write, closer = &close, sum = &sum, @@ -190,9 +192,9 @@ export fn crc16(table: *[256]u16) *hash::hash = alloc(state { sz = SIZE, table = table, cval = ~0u16, -}); +}; -fn close(s: *io::stream) void = free(s); +fn close(s: *io::stream) void = void; fn write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *state; @@ -233,44 +235,37 @@ export fn sum16(h: *hash::hash) u16 = { ]; let crc_ccitt = crc16(&ccitt_table); - defer hash::close(crc_ccitt); - let crc_cmda2000 = crc16(&cmda2000_table); - defer hash::close(crc_cmda2000); - let crc_dect = crc16(&dect_table); - defer hash::close(crc_dect); - let crc_ansi = crc16(&ansi_table); - defer hash::close(crc_ansi); let buf: [SIZE]u8 = [0...]; for (let i = 0z; i < len(vectors); i += 1) { let vec = vectors[i]; - hash::reset(crc_ccitt); - hash::write(crc_ccitt, strings::toutf8(vec.0)); - hash::sum(crc_ccitt, buf); + hash::reset(&crc_ccitt); + hash::write(&crc_ccitt, strings::toutf8(vec.0)); + hash::sum(&crc_ccitt, buf); assert(endian::host.getu16(buf) == vec.1); - assert(sum16(crc_ccitt) == vec.1); + assert(sum16(&crc_ccitt) == vec.1); - hash::reset(crc_cmda2000); - hash::write(crc_cmda2000, strings::toutf8(vec.0)); - hash::sum(crc_cmda2000, buf); + hash::reset(&crc_cmda2000); + hash::write(&crc_cmda2000, strings::toutf8(vec.0)); + hash::sum(&crc_cmda2000, buf); assert(endian::host.getu16(buf) == vec.2); - assert(sum16(crc_cmda2000) == vec.2); + assert(sum16(&crc_cmda2000) == vec.2); - hash::reset(crc_dect); - hash::write(crc_dect, strings::toutf8(vec.0)); - hash::sum(crc_dect, buf); + hash::reset(&crc_dect); + hash::write(&crc_dect, strings::toutf8(vec.0)); + hash::sum(&crc_dect, buf); assert(endian::host.getu16(buf) == vec.3); - assert(sum16(crc_dect) == vec.3); + assert(sum16(&crc_dect) == vec.3); - hash::reset(crc_ansi); - hash::write(crc_ansi, strings::toutf8(vec.0)); - hash::sum(crc_ansi, buf); + hash::reset(&crc_ansi); + hash::write(&crc_ansi, strings::toutf8(vec.0)); + hash::sum(&crc_ansi, buf); assert(endian::host.getu16(buf) == vec.4); - assert(sum16(crc_ansi) == vec.4); + assert(sum16(&crc_ansi) == vec.4); }; }; diff --git a/hash/crc32/crc32.ha b/hash/crc32/crc32.ha @@ -178,18 +178,20 @@ export fn memoize(polynomial: u32, buf: *[256]u32) void = { }; }; -type state = struct { +export type state = struct { hash::hash, table: *[256]u32, cval: u32, }; -// Creates a [[hash::hash]] which computes the CRC-32 algorithm. +// Creates a [[hash::hash]] which computes the CRC-32 algorithm. This hash +// function does not allocate any state, so you do not need to call +// [[hash::close]] when you are done with it. // // It takes a table of memoized values for a given polynomail (for example, // [[ieee_table]], [[castagnoli_table]], or [[koopman_table]]); a table for a // custom polynomial, populated by [[memoize]] function, may also be used. -export fn crc32(table: *[256]u32) *hash::hash = alloc(state { +export fn crc32(table: *[256]u32) state = state { writer = &write, closer = &close, sum = &sum, @@ -197,9 +199,9 @@ export fn crc32(table: *[256]u32) *hash::hash = alloc(state { sz = SIZE, table = table, cval = ~0u32, -}); +}; -fn close(s: *io::stream) void = free(s); +fn close(s: *io::stream) void = void; fn write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *state; @@ -239,35 +241,30 @@ export fn sum32(h: *hash::hash) u32 = { ]; let crc_ieee = crc32(&ieee_table); - defer hash::close(crc_ieee); - let crc_castagnoli = crc32(&castagnoli_table); - defer hash::close(crc_castagnoli); - let crc_koopman = crc32(&koopman_table); - defer hash::close(crc_koopman); let buf: [SIZE]u8 = [0...]; for (let i = 0z; i < len(vectors); i += 1) { let vec = vectors[i]; - hash::reset(crc_ieee); - hash::write(crc_ieee, strings::toutf8(vec.0)); - hash::sum(crc_ieee, buf); + hash::reset(&crc_ieee); + hash::write(&crc_ieee, strings::toutf8(vec.0)); + hash::sum(&crc_ieee, buf); assert(endian::host.getu32(buf) == vec.1); - assert(sum32(crc_ieee) == vec.1); + assert(sum32(&crc_ieee) == vec.1); - hash::reset(crc_castagnoli); - hash::write(crc_castagnoli, strings::toutf8(vec.0)); - hash::sum(crc_castagnoli, buf); + hash::reset(&crc_castagnoli); + hash::write(&crc_castagnoli, strings::toutf8(vec.0)); + hash::sum(&crc_castagnoli, buf); assert(endian::host.getu32(buf) == vec.2); - assert(sum32(crc_castagnoli) == vec.2); + assert(sum32(&crc_castagnoli) == vec.2); - hash::reset(crc_koopman); - hash::write(crc_koopman, strings::toutf8(vec.0)); - hash::sum(crc_koopman, buf); + hash::reset(&crc_koopman); + hash::write(&crc_koopman, strings::toutf8(vec.0)); + hash::sum(&crc_koopman, buf); assert(endian::host.getu32(buf) == vec.3); - assert(sum32(crc_koopman) == vec.3); + assert(sum32(&crc_koopman) == vec.3); }; }; diff --git a/hash/crc64/crc64.ha b/hash/crc64/crc64.ha @@ -210,18 +210,20 @@ export fn memoize(polynomial: u64, buf: *[256]u64) void = { }; }; -type state = struct { +export type state = struct { hash::hash, table: *[256]u64, cval: u64, }; -// Creates a [[hash::hash]] which computes the CRC-64 algorithm. +// Creates a [[hash::hash]] which computes the CRC-64 algorithm. This hash +// function does not allocate any state, so you do not need to call +// [[hash::close]] when you are done with it. // // It takes a table of memoized values for a given polynomail (for example, // [[iso_table]] or [[ecma_table]]); a table for a custom polynomial, populated // by [[memoize]] function, may also be used. -export fn crc64(table: *[256]u64) *hash::hash = alloc(state { +export fn crc64(table: *[256]u64) state = state { writer = &write, closer = &close, sum = &sum, @@ -229,9 +231,9 @@ export fn crc64(table: *[256]u64) *hash::hash = alloc(state { sz = SIZE, table = table, cval = ~0u64, -}); +}; -fn close(s: *io::stream) void = free(s); +fn close(s: *io::stream) void = void; fn write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *state; @@ -270,26 +272,23 @@ export fn sum64(h: *hash::hash) u64 = { ]; let crc_ecma = crc64(&ecma_table); - defer hash::close(crc_ecma); - let crc_iso = crc64(&iso_table); - defer hash::close(crc_iso); let buf: [SIZE]u8 = [0...]; for (let i = 0z; i < len(vectors); i += 1) { let vec = vectors[i]; - hash::reset(crc_ecma); - hash::write(crc_ecma, strings::toutf8(vec.0)); - hash::sum(crc_ecma, buf); + hash::reset(&crc_ecma); + hash::write(&crc_ecma, strings::toutf8(vec.0)); + hash::sum(&crc_ecma, buf); assert(endian::host.getu64(buf) == vec.1); - assert(sum64(crc_ecma) == vec.1); + assert(sum64(&crc_ecma) == vec.1); - hash::reset(crc_iso); - hash::write(crc_iso, strings::toutf8(vec.0)); - hash::sum(crc_iso, buf); + hash::reset(&crc_iso); + hash::write(&crc_iso, strings::toutf8(vec.0)); + hash::sum(&crc_iso, buf); assert(endian::host.getu64(buf) == vec.2); - assert(sum64(crc_iso) == vec.2); + assert(sum64(&crc_iso) == vec.2); }; }; diff --git a/hash/fnv/fnv.ha b/hash/fnv/fnv.ha @@ -70,7 +70,7 @@ export fn fnv64a() state64 = state64 { v = basis64, }; -fn fnv_close(s: *io::stream) void = free(s); +fn fnv_close(s: *io::stream) void = void; fn fnv32_write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *state32;