Skip to content

Commit 97df633

Browse files
authored
makeTempAlloc returns self instead of type-changing (#46)
1 parent 0c0168f commit 97df633

File tree

5 files changed

+47
-59
lines changed

5 files changed

+47
-59
lines changed

src/mmdata.c

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -762,22 +762,23 @@ temp_nmbrString *nmbrTempAlloc(long size)
762762
/* Make string have temporary allocation to be released by next nmbrLet() */
763763
/* Warning: after nmbrMakeTempAlloc() is called, the nmbrString may NOT be
764764
assigned again with nmbrLet() */
765-
void nmbrMakeTempAlloc(nmbrString *s)
765+
temp_nmbrString *nmbrMakeTempAlloc(nmbrString *s)
766766
{
767-
if (g_nmbrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
768-
printf(
769-
"*** FATAL ERROR *** Temporary nmbrString stack overflow in nmbrMakeTempAlloc()\n");
767+
if (g_nmbrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
768+
printf(
769+
"*** FATAL ERROR *** Temporary nmbrString stack overflow in nmbrMakeTempAlloc()\n");
770770
#if __STDC__
771-
fflush(stdout);
771+
fflush(stdout);
772772
#endif
773-
bug(1368);
774-
}
775-
if (s[0] != -1) { /* End of string */
776-
/* Do it only if nmbrString is not empty */
777-
nmbrTempAllocStack[g_nmbrTempAllocStackTop++] = s;
778-
}
773+
bug(1368);
774+
}
775+
if (s[0] != -1) { /* End of string */
776+
/* Do it only if nmbrString is not empty */
777+
nmbrTempAllocStack[g_nmbrTempAllocStackTop++] = s;
778+
}
779779
/*E*/db2=db2+(nmbrLen(s)+1)*(long)(sizeof(nmbrString));
780780
/*E*/db3=db3-(nmbrLen(s)+1)*(long)(sizeof(nmbrString));
781+
return s;
781782
}
782783

783784

@@ -1145,8 +1146,7 @@ temp_vstring nmbrCvtMToVString(const nmbrString *s) {
11451146
}
11461147

11471148
g_startTempAllocStack = saveTempAllocStack;
1148-
if (tmpStr[0]) makeTempAlloc(tmpStr); /* Flag it for deallocation */
1149-
return (tmpStr);
1149+
return makeTempAlloc(tmpStr); /* Flag it for deallocation */
11501150
}
11511151

11521152

@@ -1305,8 +1305,7 @@ temp_vstring nmbrCvtRToVString(const nmbrString *proof,
13051305

13061306
g_startTempAllocStack = saveTempAllocStack;
13071307
g_nmbrStartTempAllocStack = nmbrSaveTempAllocStack;
1308-
if (proofStr[0]) makeTempAlloc(proofStr); /* Flag it for deallocation */
1309-
return (proofStr);
1308+
return makeTempAlloc(proofStr); /* Flag it for deallocation */
13101309
}
13111310

13121311

@@ -1369,8 +1368,7 @@ temp_vstring nmbrCvtAnyToVString(const nmbrString *s) {
13691368
}
13701369

13711370
g_startTempAllocStack = saveTempAllocStack;
1372-
if (tmpStr[0]) makeTempAlloc(tmpStr); /* Flag it for deallocation */
1373-
return (tmpStr);
1371+
return makeTempAlloc(tmpStr); /* Flag it for deallocation */
13741372
}
13751373

13761374

@@ -1567,8 +1565,7 @@ temp_nmbrString *nmbrSquishProof(const nmbrString *proof) {
15671565
} /* Next step */
15681566
nmbrLet(&subProof, NULL_NMBRSTRING);
15691567
nmbrLet(&dummyProof, NULL_NMBRSTRING);
1570-
nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
1571-
return (newProof);
1568+
return nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
15721569
}
15731570

15741571

@@ -1593,8 +1590,7 @@ temp_nmbrString *nmbrUnsquishProof(const nmbrString *proof) {
15931590
step = step + subPrfLen - 1;
15941591
}
15951592
nmbrLet(&subProof, NULL_NMBRSTRING);
1596-
nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
1597-
return (newProof);
1593+
return nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
15981594
}
15991595

16001596

@@ -1617,14 +1613,12 @@ temp_nmbrString *nmbrGetIndentation(const nmbrString *proof, long startingLevel)
16171613
indentationLevel[plen - 1] = startingLevel;
16181614
if (stmt < 0) { /* A local label reference or unknown */
16191615
if (plen != 1) bug(1330);
1620-
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
1621-
return (indentationLevel);
1616+
return nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
16221617
}
16231618
type = g_Statement[stmt].type;
16241619
if (type == f_ || type == e_) { /* A hypothesis */
16251620
if (plen != 1) bug(1331);
1626-
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
1627-
return (indentationLevel);
1621+
return nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
16281622
}
16291623
/* An assertion */
16301624
if (type != a_ && type != p_) bug(1332);
@@ -1643,8 +1637,7 @@ temp_nmbrString *nmbrGetIndentation(const nmbrString *proof, long startingLevel)
16431637

16441638
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
16451639
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
1646-
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
1647-
return (indentationLevel);
1640+
return nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
16481641
} /* nmbrGetIndentation */
16491642

16501643

@@ -1670,16 +1663,14 @@ nmbrString *nmbrGetEssential(const nmbrString *proof) {
16701663
/* The only time it should get here is if the original proof has only one
16711664
step, which would be an unknown step */
16721665
if (stmt != -(long)'?' && stmt > -1000) bug(1335);
1673-
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
1674-
return (essentialFlags);
1666+
return nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
16751667
}
16761668
type = g_Statement[stmt].type;
16771669
if (type == f_ || type == e_) { /* A hypothesis */
16781670
/* The only time it should get here is if the original proof has only one
16791671
step */
16801672
if (plen != 1) bug(1336);
1681-
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
1682-
return (essentialFlags);
1673+
return nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
16831674
}
16841675
/* An assertion */
16851676
if (type != a_ && type != p_) bug(1337);
@@ -1701,8 +1692,7 @@ nmbrString *nmbrGetEssential(const nmbrString *proof) {
17011692

17021693
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
17031694
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
1704-
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
1705-
return (essentialFlags);
1695+
return nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
17061696
} /* nmbrGetEssential */
17071697

17081698

@@ -1730,16 +1720,14 @@ temp_nmbrString *nmbrGetTargetHyp(const nmbrString *proof, long statemNum) {
17301720
/* The only time it should get here is if the original proof has only one
17311721
step, which would be an unknown step */
17321722
if (stmt != -(long)'?') bug(1340);
1733-
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
1734-
return (targetHyp);
1723+
return nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
17351724
}
17361725
type = g_Statement[stmt].type;
17371726
if (type == f_ || type == e_) { /* A hypothesis */
17381727
/* The only time it should get here is if the original proof has only one
17391728
step */
17401729
if (plen != 1) bug(1341);
1741-
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
1742-
return (targetHyp);
1730+
return nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
17431731
}
17441732
/* An assertion */
17451733
if (type != a_ && type != p_) bug(1342);
@@ -1764,8 +1752,7 @@ temp_nmbrString *nmbrGetTargetHyp(const nmbrString *proof, long statemNum) {
17641752

17651753
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
17661754
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
1767-
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
1768-
return (targetHyp);
1755+
return nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
17691756
} /* nmbrGetTargetHyp */
17701757

17711758

@@ -2197,8 +2184,7 @@ temp_vstring compressProof(const nmbrString *proof, long statemNum,
21972184
nmbrLet(&explWorth, NULL_NMBRSTRING);
21982185
let(&explIncluded, "");
21992186

2200-
makeTempAlloc(output); /* Flag it for deallocation */
2201-
return(output);
2187+
return makeTempAlloc(output); /* Flag it for deallocation */
22022188
} /* compressProof */
22032189

22042190

@@ -2262,20 +2248,21 @@ temp_pntrString *pntrTempAlloc(long size) {
22622248
/* Make string have temporary allocation to be released by next pntrLet() */
22632249
/* Warning: after pntrMakeTempAlloc() is called, the pntrString may NOT be
22642250
assigned again with pntrLet() */
2265-
void pntrMakeTempAlloc(pntrString *s) {
2266-
if (g_pntrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
2267-
printf(
2268-
"*** FATAL ERROR *** Temporary pntrString stack overflow in pntrMakeTempAlloc()\n");
2251+
temp_pntrString *pntrMakeTempAlloc(pntrString *s) {
2252+
if (g_pntrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
2253+
printf(
2254+
"*** FATAL ERROR *** Temporary pntrString stack overflow in pntrMakeTempAlloc()\n");
22692255
#if __STDC__
2270-
fflush(stdout);
2256+
fflush(stdout);
22712257
#endif
2272-
bug(1370);
2273-
}
2274-
if (s[0] != NULL) { /* Don't do it if pntrString is empty */
2275-
pntrTempAllocStack[g_pntrTempAllocStackTop++] = s;
2276-
}
2258+
bug(1370);
2259+
}
2260+
if (s[0] != NULL) { /* Don't do it if pntrString is empty */
2261+
pntrTempAllocStack[g_pntrTempAllocStackTop++] = s;
2262+
}
22772263
/*E*/db2=db2+(pntrLen(s)+1)*(long)(sizeof(pntrString));
22782264
/*E*/db3=db3-(pntrLen(s)+1)*(long)(sizeof(pntrString));
2265+
return s;
22792266
}
22802267

22812268

src/mmdata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ extern long g_nmbrStartTempAllocStack; /* Where to start freeing temporary
247247
/* Make string have temporary allocation to be released by next nmbrLet() */
248248
/* Warning: after nmbrMakeTempAlloc() is called, the nmbrString may NOT be
249249
assigned again with nmbrLet() */
250-
void nmbrMakeTempAlloc(nmbrString *s);
250+
temp_nmbrString *nmbrMakeTempAlloc(nmbrString *s);
251251
/* Make string have temporary allocation to be
252252
released by next nmbrLet() */
253253

@@ -380,7 +380,7 @@ extern long g_pntrStartTempAllocStack; /* Where to start freeing temporary
380380
/* Make string have temporary allocation to be released by next pntrLet() */
381381
/* Warning: after pntrMakeTempAlloc() is called, the pntrString may NOT be
382382
assigned again with pntrLet() */
383-
void pntrMakeTempAlloc(pntrString *s);
383+
temp_pntrString *pntrMakeTempAlloc(pntrString *s);
384384
/* Make string have temporary allocation to be
385385
released by next pntrLet() */
386386

src/mmpars.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,7 +4876,6 @@ nmbrString *parseMathTokens(vstring userText, long statemNum)
48764876

48774877
g_startTempAllocStack = saveTempAllocStack;
48784878
g_nmbrStartTempAllocStack = nmbrSaveTempAllocStack;
4879-
if (mathStringLen) nmbrMakeTempAlloc(mathString); /* Flag for dealloc*/
48804879

48814880
/* Deallocate temporary space */
48824881
free(mathTokenSameAs);
@@ -4887,8 +4886,7 @@ nmbrString *parseMathTokens(vstring userText, long statemNum)
48874886
let(&tmpStr, "");
48884887
let(&nlUserText, "");
48894888

4890-
return (mathString);
4891-
4889+
return nmbrMakeTempAlloc(mathString); /* Flag for dealloc */
48924890
} /* parseMathTokens */
48934891

48944892

src/mmvstr.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ static void* tempAlloc(long size) /* String memory allocation/deallocation */
8989

9090

9191
/* Put string in temporary allocation arena */
92-
void makeTempAlloc(vstring s) {
93-
pushTempAlloc(s);
92+
temp_vstring makeTempAlloc(vstring s) {
93+
if (s[0]) { /* Don't do it if vstring is empty */
94+
pushTempAlloc(s);
9495
/*E*/INCDB1((long)strlen(s) + 1);
9596
/*E*/db-=(long)strlen(s) + 1;
9697
/*E* /printf("%ld temping[%s]\n", db1, s);*/
98+
}
99+
return s;
97100
} /* makeTempAlloc */
98101

99102

src/mmvstr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,6 @@ extern long g_startTempAllocStack; /* Where to start freeing temporary allocatio
251251
you can do with temporary strings.
252252
In particular, after makeTempAlloc() is called, the vstring may NOT be
253253
assigned again with let(). */
254-
void makeTempAlloc(vstring s); /* Make string have temporary allocation to be
254+
temp_vstring makeTempAlloc(vstring s); /* Make string have temporary allocation to be
255255
released by next let() */
256256
#endif /* METAMATH_MMVSTR_H_ */

0 commit comments

Comments
 (0)