hare

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

commit 25dc61582fee9d8c225f2058228805929c26cc03
parent 31a916a6dd69bd47821a2f05a2aa52e7eb2cdf51
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon, 21 Jun 2021 12:02:19 -0400

unix::resolvconf: cache return value

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

Diffstat:
Mnet/dns/query.ha | 11+----------
Munix/resolvconf/load.ha | 14+++++++++++---
2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/net/dns/query.ha b/net/dns/query.ha @@ -2,21 +2,12 @@ use net::ip; use net::udp; use unix::resolvconf; -let defaults: []ip::addr = []; - -@fini fn fini() void = { - free(defaults); -}; - // Performs a DNS query using the provided list of DNS servers. The caller must // free the return value with [[message_free]]. If no DNS servers are provided, // the system defaults (if any) are used. export fn query(query: *message, addr: ip::addr...) (*message | error) = { if (len(addr) == 0) { - if (len(defaults) == 0) { - defaults = resolvconf::load(); - }; - addr = defaults; + addr = resolvconf::load(); }; let socket = udp::listen(ip::ANY_V4, 0)?; diff --git a/unix/resolvconf/load.ha b/unix/resolvconf/load.ha @@ -7,12 +7,20 @@ use strings; // XXX: Different platforms may want to configure a different path def path: str = "/etc/resolv.conf"; +let cache: []ip::addr = []; + +@fini fn fini() void = { + free(cache); +}; + // Reads a list of nameservers from resolv.conf. Aborts the program if the file // does not exist, is written in an invalid format, or if any other error // occurs. export fn load() []ip::addr = { // XXX: Would be cool if we could do this without allocating anything - let servers: []ip::addr = []; + if (len(cache) != 0) { + return cache; + }; const file = os::open(path)!; defer io::close(file); @@ -41,8 +49,8 @@ export fn load() []ip::addr = { tok: []u8 => tok, }; defer free(tok); - append(servers, ip::parse(strings::fromutf8(tok))!); + append(cache, ip::parse(strings::fromutf8(tok))!); }; - return servers; + return cache; };