From 1d3644a1684bd191f3a36568d94c2587c2d9f24f Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Fri, 10 Apr 2020 23:11:25 +0200 Subject: [PATCH] cc, ?l: fix gethunk() to actually grow allocation the compilers and linkers use ther own memory allocator. free memory is between hunk and hunk+nhunk. allocation works by checking if nhunk is bigger or equal to the amount needed, and if not, repeatedly call gethunk() until there is. after that, the allocated amount is added from hunk and subtracted from nhunk by the user. the problem was when the needed amount was bigger than the default NHUNK size gethunk() allocates per call. gethunk() would not actually grow nhunk, but instead just set hunk and nhunk variables to the last allocated block. this resulted in a infinite loop of calls to gethunk() until sbrk() would hit the maximum size for the BSS segment. this change makes gethunk() actually grow the hunk space, increasing nhunk, and only updating hunk when nhunk was previously zero. we assume that mysbrk() retuns increasing addresses and that the space between the previous hunk+nhunk and the new block base returned by mysbrk() is usable. --- sys/src/cmd/1l/obj.c | 7 +++++-- sys/src/cmd/2l/obj.c | 7 +++++-- sys/src/cmd/5l/obj.c | 7 +++++-- sys/src/cmd/6l/obj.c | 7 +++++-- sys/src/cmd/7l/obj.c | 7 +++++-- sys/src/cmd/8l/obj.c | 7 +++++-- sys/src/cmd/cc/macbody | 7 +++++-- sys/src/cmd/kl/l.h | 2 +- sys/src/cmd/kl/obj.c | 16 +++++++++------- sys/src/cmd/ql/l.h | 2 +- sys/src/cmd/ql/obj.c | 16 +++++++++------- sys/src/cmd/vl/obj.c | 7 +++++-- 12 files changed, 60 insertions(+), 32 deletions(-) diff --git a/sys/src/cmd/1l/obj.c b/sys/src/cmd/1l/obj.c index 9cc82a56e..fae49a6ad 100644 --- a/sys/src/cmd/1l/obj.c +++ b/sys/src/cmd/1l/obj.c @@ -1099,8 +1099,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/2l/obj.c b/sys/src/cmd/2l/obj.c index 411d2a5f5..653f04fa2 100644 --- a/sys/src/cmd/2l/obj.c +++ b/sys/src/cmd/2l/obj.c @@ -1121,8 +1121,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/5l/obj.c b/sys/src/cmd/5l/obj.c index c10d4b8dd..1b28978b5 100644 --- a/sys/src/cmd/5l/obj.c +++ b/sys/src/cmd/5l/obj.c @@ -1152,8 +1152,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/6l/obj.c b/sys/src/cmd/6l/obj.c index 5483eebae..e4e8a44fb 100644 --- a/sys/src/cmd/6l/obj.c +++ b/sys/src/cmd/6l/obj.c @@ -1222,8 +1222,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/7l/obj.c b/sys/src/cmd/7l/obj.c index 75def5096..fc2ba179a 100644 --- a/sys/src/cmd/7l/obj.c +++ b/sys/src/cmd/7l/obj.c @@ -1178,8 +1178,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/8l/obj.c b/sys/src/cmd/8l/obj.c index efdbd4110..7ea392b7f 100644 --- a/sys/src/cmd/8l/obj.c +++ b/sys/src/cmd/8l/obj.c @@ -1196,8 +1196,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/cc/macbody b/sys/src/cmd/cc/macbody index 7d837fb43..e24d6669a 100644 --- a/sys/src/cmd/cc/macbody +++ b/sys/src/cmd/cc/macbody @@ -862,7 +862,10 @@ gethunk(void) yyerror("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; } diff --git a/sys/src/cmd/kl/l.h b/sys/src/cmd/kl/l.h index e296e4509..169f68c3f 100644 --- a/sys/src/cmd/kl/l.h +++ b/sys/src/cmd/kl/l.h @@ -241,7 +241,7 @@ EXTERN long symsize; EXTERN long staticgen; EXTERN Prog* textp; EXTERN long textsize; -EXTERN uintptr tothunk; +EXTERN uintptr thunk; EXTERN char xcmp[C_NCLASS][C_NCLASS]; EXTERN int version; EXTERN Prog zprg; diff --git a/sys/src/cmd/kl/obj.c b/sys/src/cmd/kl/obj.c index b778cfd8d..322a060a3 100644 --- a/sys/src/cmd/kl/obj.c +++ b/sys/src/cmd/kl/obj.c @@ -194,7 +194,7 @@ main(int argc, char *argv[]) out: if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); - Bprint(&bso, "%zud memory used\n", tothunk); + Bprint(&bso, "%zud memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } @@ -983,9 +983,9 @@ gethunk(void) long nh; nh = NHUNK; - if(tothunk >= 5L*NHUNK) { + if(thunk >= 5L*NHUNK) { nh = 5L*NHUNK; - if(tothunk >= 25L*NHUNK) + if(thunk >= 25L*NHUNK) nh = 25L*NHUNK; } h = mysbrk(nh); @@ -993,10 +993,12 @@ gethunk(void) diag("out of memory"); errorexit(); } - - hunk = h; - nhunk = nh; - tothunk += nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; + thunk += nh; } void diff --git a/sys/src/cmd/ql/l.h b/sys/src/cmd/ql/l.h index e7822d01e..ec364d0fd 100644 --- a/sys/src/cmd/ql/l.h +++ b/sys/src/cmd/ql/l.h @@ -234,7 +234,7 @@ EXTERN long symsize; EXTERN long staticgen; EXTERN Prog* textp; EXTERN long textsize; -EXTERN uintptr tothunk; +EXTERN uintptr thunk; EXTERN char xcmp[C_NCLASS][C_NCLASS]; EXTERN int version; EXTERN Prog zprg; diff --git a/sys/src/cmd/ql/obj.c b/sys/src/cmd/ql/obj.c index dba56bd46..c74d240f2 100644 --- a/sys/src/cmd/ql/obj.c +++ b/sys/src/cmd/ql/obj.c @@ -273,7 +273,7 @@ main(int argc, char *argv[]) out: if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); - Bprint(&bso, "%zud memory used\n", tothunk); + Bprint(&bso, "%zud memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } @@ -1117,9 +1117,9 @@ gethunk(void) long nh; nh = NHUNK; - if(tothunk >= 5L*NHUNK) { + if(thunk >= 5L*NHUNK) { nh = 5L*NHUNK; - if(tothunk >= 25L*NHUNK) + if(thunk >= 25L*NHUNK) nh = 25L*NHUNK; } h = mysbrk(nh); @@ -1127,10 +1127,12 @@ gethunk(void) diag("out of memory"); errorexit(); } - - hunk = h; - nhunk = nh; - tothunk += nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; + thunk += nh; } void diff --git a/sys/src/cmd/vl/obj.c b/sys/src/cmd/vl/obj.c index c20e5c98b..6a9acafc3 100644 --- a/sys/src/cmd/vl/obj.c +++ b/sys/src/cmd/vl/obj.c @@ -1075,8 +1075,11 @@ gethunk(void) diag("out of memory"); errorexit(); } - hunk = h; - nhunk = nh; + if(nhunk == 0) + hunk = h; + else + nh += (h - hunk) - nhunk; + nhunk += nh; thunk += nh; }