summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-28 14:26:16 -0700
committerOri Bernstein <ori@eigenstate.org>2015-06-28 14:26:16 -0700
commitb05bd318302a3b5043a56479d20261d81bc27012 (patch)
treee6a6532e0fd6255321fd6335464e8e57c28548cf /libstd
parentd17587d89ed49739392eb6d768d583c7f5955d2c (diff)
downloadmc-b05bd318302a3b5043a56479d20261d81bc27012.tar.gz
Implement '__init__' functions.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/alloc.myr18
-rw-r--r--libstd/fmt.myr13
2 files changed, 13 insertions, 18 deletions
diff --git a/libstd/alloc.myr b/libstd/alloc.myr
index 306667d..ed3b023 100644
--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -54,7 +54,6 @@ const Pagesz = 4*KiB
const Align = 16 /* minimum allocation alignment */
var buckets : bucket[32] /* excessive */
-var initdone : bool
type slheader = struct
cap : size /* capacity in bytes */
@@ -80,6 +79,14 @@ type chunk = struct /* NB: must be smaller than sizeof(slab) */
next : chunk# /* the next chunk in the free list */
;;
+const __init__ = {
+ var i
+
+ for i = 0; i < buckets.len && (Align << i) <= Bktmax; i++
+ bktinit(&buckets[i], Align << i)
+ ;;
+}
+
/* Allocates an object of type @a, returning a pointer to it. */
generic alloc = {-> @a#
-> bytealloc(sizeof(@a)) castto(@a#)
@@ -209,14 +216,7 @@ const zfill = {sl
/* Allocates a blob that is 'sz' bytes long. Dies if the allocation fails */
const bytealloc = {sz
- var i, bkt, p
-
- if !initdone
- for i = 0; i < buckets.len && (Align << i) <= Bktmax; i++
- bktinit(&buckets[i], Align << i)
- ;;
- initdone = true
- ;;
+ var bkt, p
if (sz <= Bktmax)
bkt = &buckets[bktnum(sz)]
diff --git a/libstd/fmt.myr b/libstd/fmt.myr
index 2fba705..b786e98 100644
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -47,6 +47,10 @@ pkg std =
$noret const fatalv : (fmt : byte[:], ap : valist# -> void)
;;
+const __init__ = {
+ fmtmap = mkht(strhash, streq)
+}
+
type fmtdesc = struct
fn : (sb : strbuf#, ap : valist#, opts : (byte[:],byte[:])[:] -> void)
optdesc : (byte[:], bool)[:]
@@ -67,14 +71,9 @@ const fatalv = {fmt, ap
exit(1)
}
-var fmtmapinited : bool = false
var fmtmap : htab(byte[:], fmtdesc)#
const fmtinstall = {ty, fn, optdesc
- if !fmtmapinited
- fmtmapinited = true
- fmtmap = mkht(strhash, streq)
- ;;
htput(fmtmap, ty, [.fn=fn, .optdesc=optdesc])
}
@@ -149,10 +148,6 @@ const sbfmtv = {sb, fmt, ap -> size
orig = fmt
nparams = ap.tc.nelt
nfmt = 0
- if !fmtmapinited
- fmtmapinited = true
- fmtmap = mkht(strhash, streq)
- ;;
while fmt.len != 0
(c, fmt) = striter(fmt)
match c