-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathos.cpp
More file actions
3024 lines (2771 loc) · 86.9 KB
/
os.cpp
File metadata and controls
3024 lines (2771 loc) · 86.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#ifdef SAFETIME // some time routines are not thread safe (not relevant with EVSERVER)
#include <mutex>
static std::mutex mtx;
#endif
bool authorize = false;
bool pseudoServer = false;
char debugdata[MAX_WORD_SIZE];
FILE* logfiles[MAX_LOG_NAMES];
uint64 callStartTime;
int loglimit = 0;
bool prelog = false;
FILE* userlogFile = NULL;
char* indents[100];
int ide = 0;
bool timeout = false;
int inputSize = 0;
bool inputLimitHit = false;
bool convertTabs = true;
bool serverLogTemporary = false;
bool idestop = false;
bool idekey = false;
bool inputAvailable = false;
static char encryptUser[200];
static char encryptLTM[200];
char logLastCharacter = 0;
#define MAX_STRING_SPACE 100000000 // transient+heap space 100MB
size_t maxHeapBytes = MAX_STRING_SPACE;
char* heapBase = NULL; // start of heap space (runs backward)
char* heapFree = NULL; // current free string ptr
char* stackFree = NULL;
static const char* infiniteCaller = "";
char* stackStart = NULL;
char* heapEnd = NULL;
uint64 discard;
bool infiniteStack = false;
bool infiniteHeap = false;
bool userEncrypt = false;
bool ltmEncrypt = false;
unsigned long minHeapAvailable;
bool showDepth = false;
char serverLogfileName[1000]; // file to log server to
char dbTimeLogfileName[1000]; // file to log db time to
char externalBugLog[1000];
char logFilename[MAX_WORD_SIZE]; // file to user log to
bool logUpdated = false; // has logging happened
int holdUserLog;
int holdServerLog;
int userLog = NO_LOG; // where do we log user
int serverLog = NO_LOG; // where do we log server
int bugLog = FILE_LOG; // where do we log bugs
char hide[4000]; // dont log these json fields
unsigned int logsize = MAX_BUFFER_SIZE; // default
static char* logmainbuffer = NULL; // where we build a log line
unsigned int outputsize = MAX_BUFFER_SIZE; // default
bool serverctrlz = false; // close communication with \0 and ctrlz
bool echo = false; // show log output onto console as well
bool oob = false; // show oob data
bool postprocess = true; // showing traces on postprocess
bool detailpattern = false;
bool silent = false; // dont display outputs of chat
bool logged = false;
bool showmem = false;
int filesystemOverride = NORMALFILES;
bool inLog = false;
char* testOutput = NULL; // testing commands output reroute
static char encryptServer[1000];
static char decryptServer[1000];
int adjustIndent = 0;
char* lastheapfree = NULL;
// buffer information
#define MAX_BUFFER_COUNT 80
unsigned int maxReleaseStack = 0;
unsigned int maxReleaseStackGap = 0xffffffff;
unsigned int maxBufferLimit = MAX_BUFFER_COUNT; // default number of system buffers for AllocateBuffer
unsigned int maxBufferSize = MAX_BUFFER_SIZE; // default how big std system buffers from AllocateBuffer should be
unsigned int maxBufferUsed = 0; // worst case buffer use - displayed with :variables
unsigned int bufferIndex = 0; // current allocated index into buffers[]
unsigned baseBufferIndex = 0; // preallocated buffers at start
char* buffers = 0; // collection of output buffers
#define MAX_OVERFLOW_BUFFERS 20
static char* overflowBuffers[MAX_OVERFLOW_BUFFERS]; // malloced extra buffers if base allotment is gone
CALLFRAME* frameList[MAX_GLOBAL]; // ReleaseStack at start of depth
static unsigned int overflowLimit = 0;
unsigned int overflowIndex = 0;
USERFILESYSTEM userFileSystem;
static char staticPath[MAX_WORD_SIZE]; // files that never change
static char readPath[MAX_WORD_SIZE]; // readonly files that might be overwritten from outside
static char writePath[MAX_WORD_SIZE]; // files written by app
unsigned int currentFileLine = 0; // line number in file being read
unsigned int currentLineColumn = 0; // column number in file being read
unsigned int maxFileLine = 0; // line number in file being read
unsigned int peekLine = 0;
char currentFilename[MAX_WORD_SIZE]; // name of file being read
char knownFileTypes[MAX_FILE_TYPES][MAX_WORD_SIZE]; // all known file types read from config (by default ltm)
std::map <WORDP, uint64> timeSummary; // per volley time data about functions etc
// error recover
jmp_buf scriptJump[20];
jmp_buf crashJump;
int jumpIndex = -1;
unsigned int randIndex = 0;
unsigned int oldRandIndex = 0;
char syslogstr[300] = "chatscript"; // header for syslog messages
// #define DO_HEAP_CHECKING
#ifdef WIN32
#include <conio.h>
#include <direct.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <Winbase.h>
#endif
#ifdef LINUX
#include <syslog.h>
#endif
void Bug0()
{
char word[MAX_WORD_SIZE];
GetCurrentDir(word, MAX_WORD_SIZE);
}
void Bug()
{
if (compiling == FULL_COMPILE)
{
jumpIndex = 0; // top level of scripting abort
BADSCRIPT("INFO: Execution error (see LOGS/bugs.txt) - try again.");
}
}
void FreeServerLog()
{
if (logmainbuffer) myfree(logmainbuffer);
logmainbuffer = NULL;
}
void TrackTime(char* name, int elapsed)
{
WORDP D = StoreWord(name);
if (D) TrackTime(D, elapsed);
}
void TrackTime(WORDP D, int elapsed)
{
if (!D) return;
unsigned int count = 0;
unsigned int time = 0;
std::map<WORDP, uint64>::iterator it;
it = timeSummary.find(D);
if (it != timeSummary.end())
{
count = it->second >> TIMESUMMARY_COUNT_OFFSET;
time = it->second & TIMESUMMARY_TIME;
}
count += 1;
time += elapsed;
uint64 summary = count;
summary <<= TIMESUMMARY_COUNT_OFFSET;
summary |= time;
timeSummary[D] = summary;
}
void CloseLogs()
{
for (unsigned int i = 0; i < MAX_LOG_NAMES; ++i)
{
if (logfiles[i]) // found already open
{
fclose(logfiles[i]);
logfiles[i] = NULL;
break;
}
}
}
void InitLogs()
{
memset(logfiles, 0, sizeof(logfiles));
}
/////////////////////////////////////////////////////////
/// KEYBOARD
/////////////////////////////////////////////////////////
bool KeyReady()
{
if (sourceFile && sourceFile != stdin) return true;
#ifdef WIN32
if (ide) return idekey;
return _kbhit() ? true : false;
#else
bool ready = false;
struct termios oldSettings, newSettings;
if (tcgetattr( fileno( stdin ), &oldSettings ) == -1) return false; // could not get terminal attributes
newSettings = oldSettings;
newSettings.c_lflag &= (~ICANON & ~ECHO);
tcsetattr( fileno( stdin ), TCSANOW, &newSettings );
fd_set set;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO( &set );
FD_SET( fileno( stdin ), &set );
int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );
ready = ( res > 0 );
tcsetattr( fileno( stdin ), TCSANOW, &oldSettings );
return ready;
#endif
}
/////////////////////////////////////////////////////////
/// EXCEPTION/ERROR
/////////////////////////////////////////////////////////
void SafeLock()
{
#ifdef SAFETIME
mtx.lock();
#endif
}
void SafeUnlock()
{
#ifdef SAFETIME
mtx.unlock();
#endif
}
void JumpBack()
{
if (jumpIndex < 0) return; // not under handler control
globalDepth = 0;
longjmp(scriptJump[jumpIndex], 1);
}
void CloseDatabases(bool restart)
{
bool usedb = (db == 1 && server) || db == 2;
#ifndef DISCARDPOSTGRES
if (*postgresparams)
{
PostgresScriptShutDown(); // any script connection
PGUserFilesCloseCode(); // filesystem
}
#endif
#ifndef DISCARDMONGO
if (!restart) MongoSystemShutdown();
else MongoUserFilesClose(); // actually just closes files
#endif
#ifndef DISCARDMYSQL
if (*mysqlparams) MySQLFullCloseCode(restart); // filesystem and/or script
#endif
#ifndef DISCARDMICROSOFTSQL
if (*mssqlparams && usedb) MsSqlFullCloseCode(); // filesystem and/or script
#endif
}
void myexit(const char* msg, int code)
{
int oldcompiling = compiling;
int oldloading = loading;
compiling = NOT_COMPILING;
loading = false;
traceTestPatternBuffer = NULL;
#ifdef LINUX
const char* fatal_str = strstr(msg, "FATAL:");
if (fatal_str != NULL) {
syslog(LOG_ERR, "%s user: %s bot: %s msg: %s ",
syslogstr, loginID, computerID, msg);
}
#endif
if (code)
{
printf("%s\r\n", msg);
}
char name[MAX_WORD_SIZE];
sprintf(name, (char*)"%s/exitlog.txt", logsfolder);
FILE* out;
out = FopenUTF8WriteAppend(name);
struct tm ptm;
if (out)
{
fprintf(out, (char*)"%s %d - called myexit at %s\r\n", msg, code, GetTimeInfo(&ptm, true));
FClose(out);
}
bool is_recoverable = (code != 0 && crashset && !crashBack);
if (is_recoverable) // try to recover
{
#ifdef LINUX
siglongjmp(crashJump, 1);
#else
longjmp(crashJump, 1);
#endif
}
// non recoverable
out = NULL;
if (code && !oldcompiling && !oldloading)
{
out = FopenUTF8WriteAppend(name);
if (out)
{
struct tm ptm;
fprintf(out, (char*)"\r\n%s: caller:%s callee:%s ", GetTimeInfo(&ptm, true), loginID, computerID);
fprintf(out, (char*)"input:%s \r\n", currentInput );
BugBacktrace(out);
}
}
if (code == 0 && out) fprintf(out, (char*)"CS exited at %s\r\n", GetTimeInfo(&ptm, true));
else if (out) fprintf(out, (char*)"CS terminated at %s\r\n", GetTimeInfo(&ptm, true));
if (out) FClose(out);
if (!client) CloseSystem();
exit((code == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}
void mystart(char* msg)
{
char name[MAX_WORD_SIZE];
MakeDirectory(usersfolder);
MakeDirectory(logsfolder);
sprintf(name, (char*)"%s/startlog.txt", logsfolder);
FILE* out;
out = FopenUTF8WriteAppend(name);
char word[MAX_WORD_SIZE];
struct tm ptm;
sprintf(word, (char*)"System startup %s %s\r\n", msg, GetTimeInfo(&ptm, true));
if (out )
{
fprintf(out, (char*)"%s", word);
FClose(out);
}
if (server || pseudoServer) Log(SERVERLOG, "%s",word);
}
/////////////////////////////////////////////////////////
/// Fatal Error/signal logging
/////////////////////////////////////////////////////////
#ifdef USESIGNALHANDLER
#include <signal.h>
void setSignalHandlers();
void signalHandler( int signalcode )
{
char word[MAX_WORD_SIZE];
sprintf(word, (char*)"FATAL: Linux Signal code %d", signalcode);
#ifdef PRIVATE_CODE
// Check for private hook function for additional handling
static HOOKPTR fn = FindHookFunction((char*)"SignalHandler");
if (fn)
{
char* ptr = word;
((SignalHandlerHOOKFN) fn)(signalcode, ptr);
}
#endif
myexit(word,1);
}
void setSignalHandlers ()
{
char word[MAX_WORD_SIZE];
struct sigaction sa = {};
sa.sa_handler = &signalHandler;
sigfillset(&sa.sa_mask); // Block every signal during the handler
// Handle relevant signals
if (sigaction(SIGFPE, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGFPE");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGSEGV, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGSEGV");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGBUS, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGBUS");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGILL, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGILL");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGTRAP, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGTRAP");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGHUP, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGHUP");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
if (sigaction(SIGPIPE, &sa, NULL) == -1) {
sprintf(word, (char*)"Error: cannot handle SIGPIPE");
Log(BUGLOG, word);
Log(SERVERLOG, word);
}
}
#endif
/////////////////////////////////////////////////////////
/// MEMORY SYSTEM
/////////////////////////////////////////////////////////
void ResetBuffers()
{
globalDepth = 0;
bufferIndex = baseBufferIndex;
memset(frameList,0,sizeof(frameList));
outputNest = oldOutputIndex = 0;
currentRuleOutputBase = currentOutputBase = ourMainOutputBuffer;
currentOutputLimit = outputsize;
}
void CloseBuffers()
{
while (overflowLimit > 0)
{
myfree(overflowBuffers[--overflowLimit]);
overflowBuffers[overflowLimit] = 0;
}
myfree(buffers);
buffers = NULL;
myfree(readBuffer);
readBuffer = NULL;
myfree(tracebuffer);
tracebuffer = NULL;
myfree(lastInputSubstitution);
lastInputSubstitution = NULL;
myfree(rawSentenceCopy);
rawSentenceCopy = NULL;
myfree(revertBuffer);
revertBuffer = NULL;
myfree(ourMainInputBuffer);
ourMainInputBuffer = NULL;
myfree(ourMainOutputBuffer);
ourMainOutputBuffer = NULL;
free_jp_token_buffers();
free_mssql_buffer();
}
FunctionResult AuthorizedCode(char* buffer)
{
if ((server || pseudoServer ) && !hadAuthCode && !VerifyAuthorization(FopenReadOnly((char*)"authorizedIP.txt"))) return FAILRULE_BIT; // authorizedIP
return NOPROBLEM_BIT;
}
char* PrintX64(uint64 val)
{
static char buffer[32];
#ifdef WIN32
sprintf(buffer, (char*)"0x%016I64x", val);
#else
sprintf(buffer, (char*)"0x%016llx", val);
#endif
return buffer;
}
char* Print64(long long int val)
{
static char buffer[32];
sprintf(buffer, (char*)"%lld", val);
return buffer;
}
char* PrintU64(uint64 val)
{
static char buffer[32];
sprintf(buffer, (char*)"%llu", val);
return buffer;
}
void LoggingCheats(char* incoming)
{
// startup logging
uint64 now = ElapsedMilliseconds();
unsigned long delayMinutes = (unsigned long)((now - timedeployed) / 60000);
if (delayMinutes < startLogDelay) serverLog |= FILE_LOG | PRE_LOG;
// logging overrides
FILE* in = FopenReadOnly("serverlogging.txt"); // per external file created, enable server log
if (in)
{
serverLog |= FILE_LOG | PRE_LOG;
FClose(in);
}
in = FopenReadOnly("prelogging.txt"); // per external file created, enable prelogging
if (in)
{
serverLog |= PRE_LOG;
userLog |= PRE_LOG;
prelog = true;
FClose(in);
}
in = FopenReadOnly("userlogging.txt"); // per external file created, enable user log
if (in)
{
userLog |= FILE_LOG | PRE_LOG;
FClose(in);
}
in = FopenReadOnly("tracelogging.txt"); // per external file created, enable user log
if (in)
{
trace = (unsigned int)-1;
userLog |= FILE_LOG;
FClose(in);
}
*debugdata = 0;
char* authcode = (incoming) ? strstr(incoming, serverlogauthcode) : NULL;
if (!*serverlogauthcode) authcode = NULL;
if (authcode)// dont process authcode as input from user
{
size_t len = strlen(serverlogauthcode);
memset(authcode, ' ', len); // hide auth code entirely
hadAuthCode = 1;
if (authcode[len] == '1') // more detailed cheat choices
{
authcode[len] = ' ';
if (!serverLog) serverLog = FILE_LOG;
}
else if (authcode[len] == '2') // dynamic debugging using debug field
{
authcode[len] = ' ';
hadAuthCode |= 2;
char* end = ReadCompiledWord(authcode, debugdata);
if (*debugdata == '"') // argument
{
size_t x = strlen(debugdata);
debugdata[x - 1] = 0;
memmove(debugdata, debugdata + 1, x); // remove leading dq
authcode += len; // toward the argument
while (authcode < end) *authcode++ = ' '; // erase message
}
else *debugdata = 0;
}
}
else hadAuthCode = 0;
*currentFilename = 0;
}
bool memory_check_m = false;
size_t allocated = 0; // current total allocated via malloc
int n_allocations = 0; // how many allocations have we got ongoing
void* allocations[100];
int allocationline[100];
char* allocationfile[100];
int allocindex = 0; // current index of save
char* mymalloc_imp(size_t size,const char* file, int line)
{
if (!memory_check_m) return (char*)malloc(size);
++n_allocations;
allocated += size;
int* answer = (int*)malloc(size + 4);
if (answer == nullptr) printf("dbg: Error: null returned from malloc!\n");
*answer = size;
allocationline[allocindex] = line;
allocationfile[allocindex] = (char*)file;
allocations[allocindex++] = answer + 1;
return (char*)(answer + 1);
}
void myfree(void* ptr)
{
if (!memory_check_m)
{
free(ptr);
return;
}
int* answer = ((int*)ptr) - 1;
int i;
for ( i = allocindex-1; i >= 0; --i)
{
if (ptr == allocations[i])
{
allocationline[i] = allocationline[--allocindex];
allocationfile[i] = allocationfile[allocindex];
allocations[i] = allocations[allocindex];
break;
}
}
if (i < 0) // did not find
{
int xx = 0;
}
allocated -= *answer;
free(answer);
}
char* Myfgets(char* buffer, int size, FILE* in)
{
return fgets(buffer, size, in);
//char* answer = fgets(buffer, size, in);
//PurifyInput(buffer, true, false); // leave ending crlfs
//return answer;
}
char* AllocateBuffer(char* name,char*content)
{
if (!buffers) return (char*)""; // IDE before start
char* buffer = buffers + (maxBufferSize * bufferIndex);
if (++bufferIndex >= maxBufferLimit ) // want more than nominally allowed
{
if (bufferIndex > (maxBufferLimit+2) || overflowIndex > 20)
{
char word[MAX_WORD_SIZE];
sprintf(word,(char*)"FATAL: Corrupt bufferIndex %u or overflowIndex %u\r\n",bufferIndex,overflowIndex);
ReportBug(word);
}
--bufferIndex;
// try to acquire more space, permanently
if (overflowIndex >= overflowLimit)
{
overflowBuffers[overflowLimit] = (char*) mymalloc(maxBufferSize);
if (!overflowBuffers[overflowLimit]) ReportBug((char*)"FATAL: out of buffers\r\n");
overflowLimit++;
if (overflowLimit >= MAX_OVERFLOW_BUFFERS) ReportBug((char*)"FATAL: Out of overflow buffers\r\n");
Log(USERLOG,"Allocated extra buffer %d\r\n",overflowLimit);
}
buffer = overflowBuffers[overflowIndex++];
}
else if (bufferIndex > maxBufferUsed) maxBufferUsed = bufferIndex;
if (showmem) Log(USERLOG,"%d Buffer alloc %d %s %s\r\n",globalDepth,bufferIndex,name, frameList[globalDepth]->name);
*buffer = 0; // empty string
if (content)
{
size_t len = strlen(content);
strcpy(buffer, content);
}
return buffer;
}
void FreeBuffer(char* name)
{ // if longjump happens this may not be called. manually restore counts in setjump code
if (showmem) Log(USERLOG,"%d Buffer free %s %d %s\r\n", globalDepth,name, bufferIndex, frameList[globalDepth]->name);
if (overflowIndex) --overflowIndex; // keep the dynamically allocated memory for now.
else if (bufferIndex) --bufferIndex;
else ReportBug((char*)"Buffer allocation underflow");
}
void ResetHeapFree(char* val)
{
lastheapfree = heapFree = val;
}
void InitStackHeap()
{
size_t size = maxHeapBytes / 64; // stack + heap size
size = (size * 64) + 64; // 64 bit align both ends
if (!heapEnd)
{
heapEnd = mymalloc(size); // point to end of heap (start of stack)
if (!heapEnd)
{
(*printer)((char*)"Out of memory space for text space %d\r\n", (int)size);
ReportBug((char*)"FATAL: Cannot allocate memory space for text %d\r\n", (int)size);
}
}
ResetHeapFree( heapEnd + size); // allocate backwards
heapBase = heapFree;
stackStart = stackFree = heapEnd;
minHeapAvailable = maxHeapBytes;
PrepIndent();
}
void FreeStackHeap()
{
if (heapEnd) myfree(heapEnd);
heapEnd = NULL;
}
char* AllocateStack(const char* word, size_t len,bool localvar,int align) // call with (0,len) to get a buffer
{
if (!stackFree) ReportBug("FATAL: Allocating stack with null free\r\n");
if (infiniteStack)
{
infiniteStack = false;
ReportBug("FATAL: Allocating stack while InfiniteStack in progress from %s\r\n", infiniteCaller);
}
if (len == 0) // compute size needed
{
if (!word ) return NULL; // not passing in anything
len = strlen(word);
}
if (align == 1 || align == 4) // 1 is old true value
{
stackFree += 3;
uint64 x = (uint64)stackFree;
x &= 0xfffffffffffffffc;
stackFree = (char*)x;
}
else if (align == 8)
{
stackFree += 7;
uint64 x = (uint64)stackFree;
x &= 0xfffffffffffffff8;
stackFree = (char*)x;
}
unsigned int avail = heapFree - (stackFree + len + 1);
if (avail < 5000) ReportBug((char*)"FATAL: Out of stack space stringSpace:%d ReleaseStackspace:%d \r\n",heapBase-heapFree,stackFree - stackStart);
char* answer = stackFree;
if (localvar) // give hidden data
{
*answer = '`';
answer[1] = '`';
answer += 2;
}
*answer = 0;
if (word && *word) strncpy(answer, word, len); // dont copy if word is actually an empty string allowed with a nonzero length
answer[len++] = 0;
answer[len++] = 0;
if (localvar) len += 2;
stackFree += len;
len = stackFree - stackStart; // how much stack used are we?
len = heapBase - heapFree; // how much heap used are we?
len = heapFree - stackFree; // size of gap between
if (len < maxReleaseStackGap) maxReleaseStackGap = len;
return answer;
}
void ReleaseStack(char* word)
{
stackFree = word;
}
bool AllocateStackSlot(char* variable)
{
WORDP D = StoreWord(variable,AS_IS);
unsigned int len = sizeof(char*);
if ((stackFree + len + 1) >= (heapFree - 5000)) // dont get close
{
ReportBug((char*)"Out of stack space\r\n");
return false;
}
char* answer = stackFree;
memcpy(answer,&D->w.userValue,sizeof(char*));
if (D->word[1] == LOCALVAR_PREFIX) D->w.userValue = NULL; // autoclear local var
stackFree += sizeof(char*);
len = heapFree - stackFree;
if (len < maxReleaseStackGap) maxReleaseStackGap = len;
return true;
}
unsigned int Vmemory()
{
uint64 vmemory = 0;
#ifdef WIN32
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
vmemory = memInfo.ullTotalVirtual - memInfo.ullAvailVirtual;
//vmemory = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
#elif LINUX
#endif
vmemory /= 1048576;
return (unsigned int) vmemory;
}
char** RestoreStackSlot(char* variable,char** slot)
{
WORDP D = FindWord(variable);
if (!D) return slot; // should never happen, we allocate dict entry on save
memcpy(&D->w.userValue,slot,sizeof(char*));
#ifndef DISCARDTESTING
if (debugVar) (*debugVar)(variable, D->w.userValue);
#endif
return ++slot;
}
char* InfiniteHeap(const char* caller)
{
if (infiniteHeap)
{
infiniteHeap = false;
ReportBug("FATAL: Allocating InfiniteHeap from %s while one already in progress from %s\r\n", caller, infiniteHeap);
}
infiniteCaller = caller;
infiniteHeap = true;
uint64 base = (uint64)(heapFree);
base &= 0xFFFFFFFFFFFFFFF8ULL;
heapFree = (char*)base;
return heapFree;
}
char* InfiniteStack(char*& limit,const char* caller)
{
if (infiniteStack)
{
infiniteStack = false;
ReportBug("FATAL: Allocating InfiniteStack from %s while one already in progress from %s\r\n", caller, infiniteCaller);
}
infiniteCaller = caller;
infiniteStack = true;
limit = heapFree - 5000; // leave safe margin of error
uint64 base = (uint64) (stackFree+7);
base &= 0xFFFFFFFFFFFFFFF8ULL;
stackFree = (char*)base; // slop may be lost when allocated finally, but it can be reclaimed later
return stackFree;
}
void ReleaseInfiniteStack()
{
infiniteStack = false;
infiniteCaller = "";
}
void CheckHeap(HEAPREF linkval, const char* file, unsigned int line) {
static int call_count = 0;
uint64* cur = (uint64*)linkval;
int loop_count = 0;
printf("CheckHeap: entering # %d with %p\n", call_count++, linkval);
while (cur && loop_count++ < 1000) {
printf("CheckHeap: cur %p %s %u\n", cur, file, line);
if (!InHeap((char*)cur)) {
printf("CheckHeap: bad\n");
exit(-1);
}
cur = (uint64*)cur[0];
}
if (loop_count >= 1000) printf("CheckHeap: large loop_count\n");
printf("CheckHeap: good\n");
}
HEAPREF AllocateHeapval(unsigned int descriptor,HEAPREF linkval, uint64 val1, uint64 val2, uint64 val3)
{
uint64* heapval = (uint64*)AllocateHeap(NULL, 5, sizeof(uint64), false);
heapval[0] = (uint64)linkval;
heapval[1] = val1;
heapval[2] = val2;
heapval[3] = val3;
heapval[4] = descriptor;
#ifdef DO_HEAP_CHECKING
CheckHeap((HEAPREF)heapval);
#endif
return (HEAPREF)heapval;
}
HEAPREF UnpackHeapval(HEAPREF linkval, uint64 & val1, uint64 & val2, uint64 & val3)
{
uint64* data = (uint64*)linkval;
val1 = data[1];
val2 = data[2];
val3 = data[3];
// heapval[4] = descriptor; // not unpacked, ignored
#ifdef DO_HEAP_CHECKING
CheckHeap((HEAPREF)data);
#endif
return (HEAPREF)data[0];
}
void RestoreCallingDirectory()
{
SetDirectory(incomingDir); // restore any outside caller
}
STACKREF AllocateStackval(STACKREF linkval, uint64 val1, uint64 val2, uint64 val3)
{
uint64* stackval = (uint64*)AllocateStack(NULL, 5 * sizeof(uint64), false,8);
stackval[0] = (uint64)linkval;
stackval[1] = val1;
stackval[2] = val2;
stackval[3] = val3;
return (STACKREF)stackval;
}
STACKREF UnpackStackval(STACKREF linkval, uint64& val1, uint64& val2, uint64& val3)
{ // factThreadList requires all 5, writes on its own
uint64* data = (uint64*) linkval;
val1 = data[1];
val2 = data[2];
val3 = data[3];
return (STACKREF)data[0];
}
void CompleteBindStack64(int n,char* base)
{
stackFree = base + ((n+1) * sizeof(FACT*)); // convert infinite allocation to fixed one given element count
size_t len = heapFree - stackFree;
if (len < maxReleaseStackGap) maxReleaseStackGap = len;
infiniteStack = false;
}
void CompleteBindStack(int used)
{
if (!used) stackFree += strlen(stackFree) + 1; // convert infinite allocation to fixed one
else stackFree += used;
size_t len = heapFree - stackFree;
if (len < maxReleaseStackGap) maxReleaseStackGap = len;
infiniteStack = false;
}
HEAPREF Index2Heap(HEAPINDEX offset)
{
if (!offset) return NULL;
char* ptr = heapBase - offset;
if (ptr < heapFree)
{
ReportBug((char*)"INFO: String offset into free space\r\n");
return NULL;
}
if (ptr > heapBase)
{
ReportBug((char*)"INFO: String offset before heap space\r\n");
return NULL;
}
return ptr;
}
bool PreallocateHeap(size_t len) // do we have the space
{
if (infiniteHeap)
{
infiniteHeap = false;
ReportBug("FATAL: Preallocating heap while InfiniteHeap in progress from %s\r\n", infiniteCaller);
}
char* used = heapFree - len;
if (used <= ((char*)stackFree + 2000))
{
ReportBug("Heap preallocation fails for %d bytes", len);
return false;
}
return true;
}
bool InHeap(char* ptr)
{
return (ptr >= heapFree && ptr <= heapBase);
}
bool InStack(char* ptr)
{
return (ptr < heapFree && ptr >= stackStart);
}
void ShowMemory(char* label)
{
(*printer)("%s: HeapUsed: %d Gap: %d\r\n", label, heapBase - heapFree,heapFree - stackFree);
}
void CompleteInfiniteHeap(char* base)
{
infiniteHeap = false;
infiniteCaller = "";
if (base >= lastheapfree) ReportBug("Heap growing backwards");
size_t len = (heapFree - base);
lastheapfree = heapFree = (char*)base;
int nominalLeft = maxHeapBytes - (heapBase - heapFree);
if ((unsigned long)nominalLeft < minHeapAvailable) minHeapAvailable = nominalLeft;
char* used = heapFree - len;
if (used <= ((char*)stackFree + 2000) || nominalLeft < 0)
ReportBug((char*)"FATAL: Out of permanent heap space\r\n");
}
char* AllocateHeap(const char* word,size_t len,int bytes,bool clear, bool purelocal) // BYTES means size of unit
{ // string allocation moves BACKWARDS from end of dictionary space (as do meanings)
/* Allocations during setup as :
2 when setting up cs using current dictionary and livedata for extensions (plurals, comparatives, tenses, canonicals)
3 preserving flags or properties when removing them or adding them while the dictionary is unlocked - only during builds
4 reading strings during dictionary setup
5 assigning meanings or glosses or posconditionsfor the dictionary