1 //uchime by Robert C. Edgar http://drive5.com/uchime This code is donated to the public domain.
\r
5 #include <sys/stat.h>
\r
18 #include <process.h>
\r
19 #include <windows.h>
\r
23 #include <sys/time.h>
\r
24 #include <sys/resource.h>
\r
31 #include "myutils.h"
\r
33 const char *SVN_VERSION =
\r
34 #include "svnversion.h"
\r
37 #define TEST_UTILS 0
\r
39 using namespace std;
\r
41 const unsigned MY_IO_BUFSIZ = 32000;
\r
42 const unsigned MAX_FORMATTED_STRING_LENGTH = 64000;
\r
44 static char *g_IOBuffers[256];
\r
45 static time_t g_StartTime = time(0);
\r
46 static vector<string> g_Argv;
\r
47 static double g_PeakMemUseBytes;
\r
52 const int C = 100000000;
\r
53 for (int i = 0; i < C; ++i)
\r
54 ProgressStep(i, C, "something or other");
\r
57 Progress("Longer message\r");
\r
59 Progress("Short\r");
\r
61 Progress("And longer again\r");
\r
65 const unsigned N = 10;
\r
67 for (unsigned i = 0; i < N; ++i)
\r
69 ProgressStep(i, N, "Allocating 1MB blocks");
\r
70 for (unsigned j = 0; j < M; ++j)
\r
72 ProgressStep(j, M, "Inner loop");
\r
78 #endif // TEST_UTILS
\r
80 static void AllocBuffer(FILE *f)
\r
83 if (fd < 0 || fd >= 256)
\r
85 if (g_IOBuffers[fd] == 0)
\r
86 g_IOBuffers[fd] = myalloc(char, MY_IO_BUFSIZ);
\r
87 setvbuf(f, g_IOBuffers[fd], _IOFBF, MY_IO_BUFSIZ);
\r
90 static void FreeBuffer(FILE *f)
\r
93 if (fd < 0 || fd >= 256)
\r
95 if (g_IOBuffers[fd] == 0)
\r
97 myfree(g_IOBuffers[fd]);
\r
98 g_IOBuffers[fd] = 0;
\r
101 unsigned GetElapsedSecs()
\r
103 return (unsigned) (time(0) - g_StartTime);
\r
106 static unsigned g_NewCalls;
\r
107 static unsigned g_FreeCalls;
\r
108 static double g_InitialMemUseBytes;
\r
109 static double g_TotalAllocBytes;
\r
110 static double g_TotalFreeBytes;
\r
111 static double g_NetBytes;
\r
112 static double g_MaxNetBytes;
\r
114 void LogAllocStats()
\r
117 Log(" Allocs %u\n", g_NewCalls);
\r
118 Log(" Frees %u\n", g_FreeCalls);
\r
119 Log("Initial alloc %s\n", MemBytesToStr(g_InitialMemUseBytes));
\r
120 Log(" Total alloc %s\n", MemBytesToStr(g_TotalAllocBytes));
\r
121 Log(" Total free %s\n", MemBytesToStr(g_TotalFreeBytes));
\r
122 Log(" Net bytes %s\n", MemBytesToStr(g_NetBytes));
\r
123 Log("Max net bytes %s\n", MemBytesToStr(g_MaxNetBytes));
\r
124 Log(" Peak total %s\n", MemBytesToStr(g_MaxNetBytes + g_InitialMemUseBytes));
\r
127 bool StdioFileExists(const string &FileName)
\r
130 int i = stat(FileName.c_str(), &SD);
\r
134 void myassertfail(const char *Exp, const char *File, unsigned Line)
\r
136 Die("%s(%u) assert failed: %s", File, Line, Exp);
\r
139 bool myisatty(int fd)
\r
141 return isatty(fd) != 0;
\r
146 int fseeko(FILE *stream, off_t offset, int whence)
\r
148 off_t FilePos = _fseeki64(stream, offset, whence);
\r
149 return (FilePos == -1L) ? -1 : 0;
\r
151 #define ftello(fm) (off_t) _ftelli64(fm)
\r
154 void LogStdioFileState(FILE *f)
\r
156 unsigned long tellpos = (unsigned long) ftello(f);
\r
157 long fseek_pos = fseek(f, 0, SEEK_CUR);
\r
158 int fd = fileno(f);
\r
159 Log("FILE * %p\n", f);
\r
160 Log("fileno %d\n", fd);
\r
161 Log("feof %d\n", feof(f));
\r
162 Log("ferror %d\n", ferror(f));
\r
163 Log("ftell %ld\n", tellpos);
\r
164 Log("fseek %ld\n", fseek_pos);
\r
165 #if !defined(_GNU_SOURCE) && !defined(__APPLE_CC__)
\r
167 int fgetpos_retval = fgetpos(f, &fpos);
\r
168 Log("fpos %ld (retval %d)\n", (long) fpos, fgetpos_retval);
\r
169 // Log("eof %d\n", _eof(fd));
\r
172 __int64 pos64 = _ftelli64(f);
\r
173 Log("_ftelli64 %lld\n", pos64);
\r
177 FILE *OpenStdioFile(const string &FileName)
\r
179 const char *Mode = "rb";
\r
180 FILE *f = fopen(FileName.c_str(), Mode);
\r
183 if (errno == EFBIG)
\r
185 if (sizeof(off_t) == 4)
\r
186 Die("File too big, off_t is 32 bits, recompile needed");
\r
188 Die("Cannot open '%s', file too big (off_t=%u bits)",
\r
189 FileName.c_str(), sizeof(off_t)*8);
\r
191 Die("Cannot open %s, errno=%d %s",
\r
192 FileName.c_str(), errno, strerror(errno));
\r
198 FILE *CreateStdioFile(const string &FileName)
\r
200 FILE *f = fopen(FileName.c_str(), "wb+");
\r
202 Die("Cannot create %s, errno=%d %s",
\r
203 FileName.c_str(), errno, strerror(errno));
\r
208 void SetStdioFilePos(FILE *f, off_t Pos)
\r
211 Die("SetStdioFilePos failed, f=NULL");
\r
212 int Ok = fseeko(f, Pos, SEEK_SET);
\r
213 off_t NewPos = ftello(f);
\r
214 if (Ok != 0 || Pos != NewPos)
\r
216 LogStdioFileState(f);
\r
217 Die("SetStdioFilePos(%d) failed, Ok=%d NewPos=%d",
\r
218 (int) Pos, Ok, (int) NewPos);
\r
222 void ReadStdioFile(FILE *f, off_t Pos, void *Buffer, unsigned Bytes)
\r
225 Die("ReadStdioFile failed, f=NULL");
\r
226 SetStdioFilePos(f, Pos);
\r
227 unsigned BytesRead = fread(Buffer, 1, Bytes, f);
\r
228 if (BytesRead != Bytes)
\r
230 LogStdioFileState(f);
\r
231 Die("ReadStdioFile failed, attempted %d bytes, read %d bytes, errno=%d",
\r
232 (int) Bytes, (int) BytesRead, errno);
\r
236 void ReadStdioFile(FILE *f, void *Buffer, unsigned Bytes)
\r
239 Die("ReadStdioFile failed, f=NULL");
\r
240 unsigned BytesRead = fread(Buffer, 1, Bytes, f);
\r
241 if (BytesRead != Bytes)
\r
243 LogStdioFileState(f);
\r
244 Die("ReadStdioFile failed, attempted %d bytes, read %d bytes, errno=%d",
\r
245 (int) Bytes, (int) BytesRead, errno);
\r
249 // Return values from functions like lseek, ftell, fgetpos are
\r
250 // "undefined" for files that cannot seek. Attempt to detect
\r
251 // whether a file can seek by checking for error returns.
\r
252 bool CanSetStdioFilePos(FILE *f)
\r
254 // Common special cases
\r
255 if (f == stdin || f == stdout || f == stderr)
\r
259 int ok1 = fgetpos(f, &CurrPos);
\r
262 int ok2 = fseek(f, 0, SEEK_END);
\r
266 int ok3 = fgetpos(f, &EndPos);
\r
267 int ok4 = fsetpos(f, &CurrPos);
\r
273 byte *ReadAllStdioFile(FILE *f, unsigned &FileSize)
\r
275 const unsigned BUFF_SIZE = 1024*1024;
\r
277 if (CanSetStdioFilePos(f))
\r
279 off_t Pos = GetStdioFilePos(f);
\r
280 off_t FileSize = GetStdioFileSize(f);
\r
281 if (FileSize > UINT_MAX)
\r
282 Die("ReadAllStdioFile: file size > UINT_MAX");
\r
283 SetStdioFilePos(f, 0);
\r
284 byte *Buffer = myalloc(byte, unsigned(FileSize));
\r
285 ReadStdioFile(f, Buffer, unsigned(FileSize));
\r
286 SetStdioFilePos(f, Pos);
\r
287 FileSize = unsigned(FileSize);
\r
291 // Can't seek, read one buffer at a time.
\r
294 // Just to initialize so that first call to realloc works.
\r
295 byte *Buffer = (byte *) malloc(4);
\r
297 Die("ReadAllStdioFile, out of memory");
\r
300 Buffer = (byte *) realloc(Buffer, FileSize + BUFF_SIZE);
\r
301 unsigned BytesRead = fread(Buffer + FileSize, 1, BUFF_SIZE, f);
\r
302 FileSize += BytesRead;
\r
303 if (BytesRead < BUFF_SIZE)
\r
305 Buffer = (byte *) realloc(Buffer, FileSize);
\r
311 byte *ReadAllStdioFile(const std::string &FileName, off_t &FileSize)
\r
314 FILE *f = OpenStdioFile(FileName);
\r
315 FileSize = GetStdioFileSize(f);
\r
318 HANDLE h = CreateFile(FileName.c_str(), GENERIC_READ, FILE_SHARE_READ,
\r
319 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
\r
320 if (h == INVALID_HANDLE_VALUE)
\r
321 Die("ReadAllStdioFile:Open(%s) failed", FileName.c_str());
\r
323 unsigned uFileSize = (unsigned) FileSize;
\r
324 if ((off_t) uFileSize != FileSize)
\r
325 Die("File too big (%.1f Gb): %s", double(FileSize)/1e9, FileName.c_str());
\r
327 byte *Buffer = myalloc(byte, uFileSize);
\r
329 ReadFile(h, Buffer, uFileSize, &BytesRead, NULL);
\r
330 if (FileSize != BytesRead)
\r
331 Die("ReadAllStdioFile:Error reading %s, attempted %u got %u",
\r
332 FileName.c_str(), FileSize, (unsigned) BytesRead);
\r
337 int h = open(FileName.c_str(), O_RDONLY);
\r
339 Die("ReadAllStdioFile:Cannot open %s", FileName.c_str());
\r
340 FileSize = lseek(h, 0, SEEK_END);
\r
341 if (FileSize == (off_t) (-1))
\r
342 Die("ReadAllStdioFile:Error seeking %s", FileName.c_str());
\r
343 // byte *Buffer = myalloc<byte>(FileSize);
\r
344 size_t stBytes = (size_t) FileSize;
\r
345 if ((off_t) stBytes != FileSize)
\r
346 Die("ReadAllStdioFile: off_t overflow");
\r
347 byte *Buffer = (byte *) malloc(stBytes);
\r
349 Die("ReadAllStdioFile: failed to allocate %s", MemBytesToStr(stBytes));
\r
350 lseek(h, 0, SEEK_SET);
\r
351 size_t n = read(h, Buffer, stBytes);
\r
353 Die("ReadAllStdioFile, Error reading %s, attempted %g got %g",
\r
354 FileName.c_str(), (double) FileSize, (double) n);
\r
360 void WriteStdioFile(FILE *f, off_t Pos, const void *Buffer, unsigned Bytes)
\r
363 Die("WriteStdioFile failed, f=NULL");
\r
364 SetStdioFilePos(f, Pos);
\r
365 unsigned BytesWritten = fwrite(Buffer, 1, Bytes, f);
\r
366 if (BytesWritten != Bytes)
\r
368 LogStdioFileState(f);
\r
369 Die("WriteStdioFile failed, attempted %d bytes, wrote %d bytes, errno=%d",
\r
370 (int) Bytes, (int) BytesWritten, errno);
\r
374 void WriteStdioFile(FILE *f, const void *Buffer, unsigned Bytes)
\r
377 Die("WriteStdioFile failed, f=NULL");
\r
378 unsigned BytesWritten = fwrite(Buffer, 1, Bytes, f);
\r
379 if (BytesWritten != Bytes)
\r
381 LogStdioFileState(f);
\r
382 Die("WriteStdioFile failed, attempted %d bytes, wrote %d bytes, errno=%d",
\r
383 (int) Bytes, (int) BytesWritten, errno);
\r
387 // Return false on EOF, true if line successfully read.
\r
388 bool ReadLineStdioFile(FILE *f, char *Line, unsigned Bytes)
\r
392 if ((int) Bytes < 0)
\r
393 Die("ReadLineStdioFile: Bytes < 0");
\r
394 char *RetVal = fgets(Line, (int) Bytes, f);
\r
395 if (NULL == RetVal)
\r
400 Die("ReadLineStdioFile: errno=%d", errno);
\r
401 Die("ReadLineStdioFile: fgets=0, feof=0, ferror=0");
\r
404 if (RetVal != Line)
\r
405 Die("ReadLineStdioFile: fgets != Buffer");
\r
406 unsigned n = strlen(Line);
\r
407 if (n < 1 || Line[n-1] != '\n')
\r
408 Die("ReadLineStdioFile: line too long or missing end-of-line");
\r
409 if (n > 0 && (Line[n-1] == '\r' || Line[n-1] == '\n'))
\r
411 if (n > 1 && (Line[n-2] == '\r' || Line[n-2] == '\n'))
\r
416 // Return false on EOF, true if line successfully read.
\r
417 bool ReadLineStdioFile(FILE *f, string &Line)
\r
431 Die("ReadLineStdioFile, errno=%d", errno);
\r
437 Line.push_back((char) c);
\r
441 // Copies all of fFrom regardless of current
\r
442 // file position, appends to fTo.
\r
443 void AppendStdioFileToFile(FILE *fFrom, FILE *fTo)
\r
445 off_t SavedFromPos = GetStdioFilePos(fFrom);
\r
446 off_t FileSize = GetStdioFileSize(fFrom);
\r
447 const off_t BUFF_SIZE = 1024*1024;
\r
448 char *Buffer = myalloc(char, BUFF_SIZE);
\r
449 SetStdioFilePos(fFrom, 0);
\r
450 off_t BytesRemaining = FileSize;
\r
451 while (BytesRemaining > 0)
\r
453 off_t BytesToRead = BytesRemaining;
\r
454 if (BytesToRead > BUFF_SIZE)
\r
455 BytesToRead = BUFF_SIZE;
\r
456 ReadStdioFile(fFrom, Buffer, (unsigned) BytesToRead);
\r
457 WriteStdioFile(fTo, Buffer, (unsigned) BytesToRead);
\r
458 BytesRemaining -= BytesToRead;
\r
460 SetStdioFilePos(fFrom, SavedFromPos);
\r
463 void RenameStdioFile(const string &FileNameFrom, const string &FileNameTo)
\r
465 int Ok = rename(FileNameFrom.c_str(), FileNameTo.c_str());
\r
467 Die("RenameStdioFile(%s,%s) failed, errno=%d %s",
\r
468 FileNameFrom.c_str(), FileNameTo.c_str(), errno, strerror(errno));
\r
471 void FlushStdioFile(FILE *f)
\r
473 int Ok = fflush(f);
\r
475 Die("fflush(%p)=%d,", f, Ok);
\r
478 void CloseStdioFile(FILE *f)
\r
482 int Ok = fclose(f);
\r
484 Die("fclose(%p)=%d", f, Ok);
\r
488 off_t GetStdioFilePos(FILE *f)
\r
490 off_t FilePos = ftello(f);
\r
492 Die("ftello=%d", (int) FilePos);
\r
496 off_t GetStdioFileSize(FILE *f)
\r
498 off_t CurrentPos = GetStdioFilePos(f);
\r
499 int Ok = fseeko(f, 0, SEEK_END);
\r
501 Die("fseek in GetFileSize");
\r
503 off_t Length = ftello(f);
\r
505 Die("ftello in GetFileSize");
\r
506 SetStdioFilePos(f, CurrentPos);
\r
510 void DeleteStdioFile(const string &FileName)
\r
512 int Ok = remove(FileName.c_str());
\r
514 Die("remove(%s) failed, errno=%d %s", FileName.c_str(), errno, strerror(errno));
\r
517 void myvstrprintf(string &Str, const char *Format, va_list ArgList)
\r
519 static char szStr[MAX_FORMATTED_STRING_LENGTH];
\r
520 vsnprintf(szStr, MAX_FORMATTED_STRING_LENGTH-1, Format, ArgList);
\r
521 szStr[MAX_FORMATTED_STRING_LENGTH - 1] = '\0';
\r
525 void myvstrprintf(string &Str, const char *Format, ...)
\r
528 va_start(ArgList, Format);
\r
529 myvstrprintf(Str, Format, ArgList);
\r
535 void SetLogFileName(const string &FileName)
\r
538 CloseStdioFile(g_fLog);
\r
540 if (FileName.empty())
\r
542 g_fLog = CreateStdioFile(FileName);
\r
545 void Log(const char *Format, ...)
\r
550 static bool InLog = false;
\r
556 va_start(ArgList, Format);
\r
557 vfprintf(g_fLog, Format, ArgList);
\r
563 void Die(const char *Format, ...)
\r
565 static bool InDie = false;
\r
574 va_start(ArgList, Format);
\r
575 myvstrprintf(Msg, Format, ArgList);
\r
578 fprintf(stderr, "\n\n");
\r
580 time_t t = time(0);
\r
581 Log("%s", asctime(localtime(&t)));
\r
582 for (unsigned i = 0; i < g_Argv.size(); i++)
\r
584 fprintf(stderr, (i == 0) ? "%s" : " %s", g_Argv[i].c_str());
\r
585 Log((i == 0) ? "%s" : " %s", g_Argv[i].c_str());
\r
587 fprintf(stderr, "\n");
\r
590 time_t CurrentTime = time(0);
\r
591 unsigned ElapsedSeconds = unsigned(CurrentTime - g_StartTime);
\r
592 const char *sstr = SecsToStr(ElapsedSeconds);
\r
593 Log("Elapsed time: %s\n", sstr);
\r
595 const char *szStr = Msg.c_str();
\r
596 fprintf(stderr, "\n---Fatal error---\n%s\n", szStr);
\r
597 Log("\n---Fatal error---\n%s\n", szStr);
\r
600 if (IsDebuggerPresent())
\r
608 void Warning(const char *Format, ...)
\r
613 va_start(ArgList, Format);
\r
614 myvstrprintf(Msg, Format, ArgList);
\r
617 const char *szStr = Msg.c_str();
\r
619 fprintf(stderr, "\nWARNING: %s\n", szStr);
\r
620 if (g_fLog != stdout)
\r
622 Log("\nWARNING: %s\n", szStr);
\r
628 double GetMemUseBytes()
\r
630 HANDLE hProc = GetCurrentProcess();
\r
631 PROCESS_MEMORY_COUNTERS PMC;
\r
632 BOOL bOk = GetProcessMemoryInfo(hProc, &PMC, sizeof(PMC));
\r
635 double Bytes = (double) PMC.WorkingSetSize;
\r
636 if (Bytes > g_PeakMemUseBytes)
\r
637 g_PeakMemUseBytes = Bytes;
\r
640 #elif linux || __linux__
\r
641 double GetMemUseBytes()
\r
643 static char statm[64];
\r
644 static int PageSize = 1;
\r
647 PageSize = sysconf(_SC_PAGESIZE);
\r
648 pid_t pid = getpid();
\r
649 sprintf(statm, "/proc/%d/statm", (int) pid);
\r
652 int fd = open(statm, O_RDONLY);
\r
656 int n = read(fd, Buffer, sizeof(Buffer) - 1);
\r
664 double Pages = atof(Buffer);
\r
666 double Bytes = Pages*PageSize;
\r
667 if (Bytes > g_PeakMemUseBytes)
\r
668 g_PeakMemUseBytes = Bytes;
\r
671 #elif defined(__MACH__)
\r
672 #include <memory.h>
\r
673 #include <stdlib.h>
\r
675 #include <unistd.h>
\r
676 #include <sys/types.h>
\r
677 #include <sys/sysctl.h>
\r
678 #include <sys/socket.h>
\r
679 #include <sys/gmon.h>
\r
680 #include <mach/vm_param.h>
\r
681 #include <netinet/in.h>
\r
682 #include <netinet/icmp6.h>
\r
683 #include <sys/vmmeter.h>
\r
684 #include <sys/proc.h>
\r
685 #include <mach/task_info.h>
\r
686 #include <mach/task.h>
\r
687 #include <mach/mach_init.h>
\r
688 #include <mach/vm_statistics.h>
\r
690 #define DEFAULT_MEM_USE 100000000.0
\r
692 double GetMemUseBytes()
\r
694 task_t mytask = mach_task_self();
\r
695 struct task_basic_info ti;
\r
696 memset((void *) &ti, 0, sizeof(ti));
\r
697 mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
\r
698 kern_return_t ok = task_info(mytask, TASK_BASIC_INFO, (task_info_t) &ti, &count);
\r
699 if (ok == KERN_INVALID_ARGUMENT)
\r
700 return DEFAULT_MEM_USE;
\r
702 if (ok != KERN_SUCCESS)
\r
703 return DEFAULT_MEM_USE;
\r
705 double Bytes = (double ) ti.resident_size;
\r
706 if (Bytes > g_PeakMemUseBytes)
\r
707 g_PeakMemUseBytes = Bytes;
\r
711 double GetMemUseBytes()
\r
717 double GetPeakMemUseBytes()
\r
719 return g_PeakMemUseBytes;
\r
722 const char *SecsToHHMMSS(int Secs)
\r
724 int HH = Secs/3600;
\r
725 int MM = (Secs - HH*3600)/60;
\r
727 static char Str[16];
\r
729 sprintf(Str, "%02d:%02d", MM, SS);
\r
731 sprintf(Str, "%02d:%02d:%02d", HH, MM, SS);
\r
735 const char *SecsToStr(double Secs)
\r
738 return SecsToHHMMSS((int) Secs);
\r
740 static char Str[16];
\r
742 sprintf(Str, "%.2gs", Secs);
\r
743 else if (Secs < 1e-3)
\r
744 sprintf(Str, "%.2fms", Secs*1e3);
\r
746 sprintf(Str, "%.3fs", Secs);
\r
750 const char *MemBytesToStr(double Bytes)
\r
752 static char Str[32];
\r
755 sprintf(Str, "%.1fkb", Bytes/1e3);
\r
756 else if (Bytes < 10e6)
\r
757 sprintf(Str, "%.1fMb", Bytes/1e6);
\r
758 else if (Bytes < 1e9)
\r
759 sprintf(Str, "%.0fMb", Bytes/1e6);
\r
760 else if (Bytes < 10e9)
\r
761 sprintf(Str, "%.1fGb", Bytes/1e9);
\r
762 else if (Bytes < 100e9)
\r
763 sprintf(Str, "%.0fGb", Bytes/1e9);
\r
765 sprintf(Str, "%.3gb", Bytes);
\r
769 const char *IntToStr(unsigned i)
\r
771 static char Str[32];
\r
773 double d = (double) i;
\r
775 sprintf(Str, "%u", i);
\r
777 sprintf(Str, "%.1fk", d/1e3);
\r
779 sprintf(Str, "%.1fM", d/1e6);
\r
781 sprintf(Str, "%.0fM", d/1e6);
\r
783 sprintf(Str, "%.1fG", d/1e9);
\r
784 else if (i < 100e9)
\r
785 sprintf(Str, "%.0fG", d/1e9);
\r
787 sprintf(Str, "%.3g", d);
\r
791 const char *FloatToStr(double d)
\r
793 static char Str[32];
\r
795 double a = fabs(d);
\r
797 sprintf(Str, "%.3g", a);
\r
798 else if (a >= 0.01 && a < 1)
\r
799 sprintf(Str, "%.3f", a);
\r
800 else if (a <= 10 && a >= 1)
\r
803 if (modf(a, &intpart) < 0.05)
\r
804 sprintf(Str, "%.0f", d);
\r
806 sprintf(Str, "%.1f", d);
\r
808 else if (a > 10 && a < 10000)
\r
809 sprintf(Str, "%.0f", d);
\r
811 sprintf(Str, "%.1fk", d/1e3);
\r
813 sprintf(Str, "%.1fM", d/1e6);
\r
815 sprintf(Str, "%.0fM", d/1e6);
\r
817 sprintf(Str, "%.1fG", d/1e9);
\r
818 else if (a < 100e9)
\r
819 sprintf(Str, "%.0fG", d/1e9);
\r
821 sprintf(Str, "%.3g", d);
\r
825 bool opt_quiet = false;
\r
826 bool opt_version = false;
\r
827 bool opt_logopts = false;
\r
828 bool opt_compilerinfo = false;
\r
829 bool opt_help = false;
\r
830 string opt_log = "";
\r
832 bool optset_quiet = false;
\r
833 bool optset_version = false;
\r
834 bool optset_logopts = false;
\r
835 bool optset_compilerinfo = false;
\r
836 bool optset_help = false;
\r
837 bool optset_log = false;
\r
839 static string g_CurrentProgressLine;
\r
840 static string g_ProgressDesc;
\r
841 static unsigned g_ProgressIndex;
\r
842 static unsigned g_ProgressCount;
\r
844 static unsigned g_CurrProgressLineLength;
\r
845 static unsigned g_LastProgressLineLength;
\r
846 static unsigned g_CountsInterval;
\r
847 static unsigned g_StepCalls;
\r
848 static time_t g_TimeLastOutputStep;
\r
850 static string &GetProgressPrefixStr(string &s)
\r
852 double Bytes = GetMemUseBytes();
\r
853 unsigned Secs = GetElapsedSecs();
\r
854 s = string(SecsToHHMMSS(Secs));
\r
859 sprintf(Str, "%5.5s", MemBytesToStr(Bytes));
\r
866 void ProgressLog(const char *Format, ...)
\r
870 va_start(ArgList, Format);
\r
871 myvstrprintf(Str, Format, ArgList);
\r
874 Log("%s", Str.c_str());
\r
875 Progress("%s", Str.c_str());
\r
878 void Progress(const char *Format, ...)
\r
885 va_start(ArgList, Format);
\r
886 myvstrprintf(Str, Format, ArgList);
\r
891 for (unsigned i = 0; i < Str.size(); ++i)
\r
896 else if (c == '\n')
\r
904 for (unsigned i = 0; i < Str.size(); ++i)
\r
906 if (g_CurrProgressLineLength == 0)
\r
909 GetProgressPrefixStr(s);
\r
910 for (unsigned j = 0; j < s.size(); ++j)
\r
912 fputc(s[j], stderr);
\r
913 ++g_CurrProgressLineLength;
\r
918 if (c == '\n' || c == '\r')
\r
920 for (unsigned j = g_CurrProgressLineLength; j < g_LastProgressLineLength; ++j)
\r
921 fputc(' ', stderr);
\r
923 g_LastProgressLineLength = 0;
\r
925 g_LastProgressLineLength = g_CurrProgressLineLength;
\r
926 g_CurrProgressLineLength = 0;
\r
932 ++g_CurrProgressLineLength;
\r
937 void ProgressExit()
\r
939 time_t Now = time(0);
\r
940 struct tm *t = localtime(&Now);
\r
941 const char *s = asctime(t);
\r
942 unsigned Secs = GetElapsedSecs();
\r
945 Log("Finished %s", s); // there is a newline in s
\r
946 Log("Elapsed time %s\n", SecsToHHMMSS((int) Secs));
\r
947 Log("Max memory %s\n", MemBytesToStr(g_PeakMemUseBytes));
\r
949 // Skip exit(), which can be very slow in DEBUG build
\r
950 // VERY DANGEROUS practice, because it skips global destructors.
\r
951 // But if you know the rules, you can break 'em, right?
\r
956 const char *PctStr(double x, double y)
\r
965 static char Str[16];
\r
966 double p = x*100.0/y;
\r
967 sprintf(Str, "%5.1f%%", p);
\r
971 string &GetProgressLevelStr(string &s)
\r
973 unsigned Index = g_ProgressIndex;
\r
974 unsigned Count = g_ProgressCount;
\r
975 if (Count == UINT_MAX)
\r
977 if (Index == UINT_MAX)
\r
982 sprintf(Tmp, "%u", Index);
\r
987 s = string(PctStr(Index+1, Count));
\r
988 s += string(" ") + g_ProgressDesc;
\r
992 void ProgressStep(unsigned i, unsigned N, const char *Format, ...)
\r
1001 va_start(ArgList, Format);
\r
1002 myvstrprintf(Str, Format, ArgList);
\r
1004 g_ProgressDesc = Str;
\r
1005 g_ProgressIndex = 0;
\r
1006 g_ProgressCount = N;
\r
1007 g_CountsInterval = 1;
\r
1009 g_TimeLastOutputStep = 0;
\r
1010 if (g_CurrProgressLineLength > 0)
\r
1014 if (i >= N && i != UINT_MAX)
\r
1015 Die("ProgressStep(%u,%u)", i, N);
\r
1016 bool IsLastStep = (i == UINT_MAX || i + 1 == N);
\r
1020 if (g_StepCalls%g_CountsInterval != 0)
\r
1023 time_t Now = time(0);
\r
1024 if (Now == g_TimeLastOutputStep)
\r
1026 if (g_CountsInterval < 128)
\r
1027 g_CountsInterval = (g_CountsInterval*3)/2;
\r
1029 g_CountsInterval += 64;
\r
1034 time_t Secs = Now - g_TimeLastOutputStep;
\r
1036 g_CountsInterval = unsigned(g_CountsInterval/(Secs*8));
\r
1039 if (g_CountsInterval < 1)
\r
1040 g_CountsInterval = 1;
\r
1042 g_TimeLastOutputStep = Now;
\r
1045 g_ProgressIndex = i;
\r
1050 va_start(ArgList, Format);
\r
1051 myvstrprintf(g_ProgressDesc, Format, ArgList);
\r
1055 GetProgressLevelStr(LevelStr);
\r
1056 Progress(" %s\r", LevelStr.c_str());
\r
1060 g_CountsInterval = 1;
\r
1061 fputc('\n', stderr);
\r
1088 map<string, unsigned> EnumValues;
\r
1092 unsigned uDefault;
\r
1094 string strDefault;
\r
1098 bool operator<(const OptInfo &rhs) const
\r
1100 return LongName < rhs.LongName;
\r
1104 static set<OptInfo> g_Opts;
\r
1113 for (set<OptInfo>::const_iterator p = g_Opts.begin(); p != g_Opts.end(); ++p)
\r
1115 const OptInfo &Opt = *p;
\r
1118 string LongName = Opt.LongName.c_str();
\r
1119 if (Opt.Type == OT_Tog)
\r
1120 LongName = string("[no]") + LongName;
\r
1121 printf(" --%s ", LongName.c_str());
\r
1139 printf("<float>");
\r
1150 const string &s = Opt.Help;
\r
1151 for (string::const_iterator q = s.begin(); q != s.end(); ++q)
\r
1165 void CmdLineErr(const char *Format, ...)
\r
1168 va_start(ArgList, Format);
\r
1170 myvstrprintf(Str, Format, ArgList);
\r
1172 fprintf(stderr, "\n");
\r
1173 fprintf(stderr, "Invalid command line\n");
\r
1174 fprintf(stderr, "%s\n", Str.c_str());
\r
1175 fprintf(stderr, "For list of command-line options use --help.\n");
\r
1176 fprintf(stderr, "\n");
\r
1180 static set<OptInfo>::iterator GetOptInfo(const string &LongName,
\r
1181 bool ErrIfNotFound)
\r
1183 for (set<OptInfo>::iterator p = g_Opts.begin();
\r
1184 p != g_Opts.end(); ++p)
\r
1186 const OptInfo &Opt = *p;
\r
1187 if (Opt.LongName == LongName)
\r
1189 if (Opt.Type == OT_Tog && "no" + Opt.LongName == LongName)
\r
1192 if (ErrIfNotFound)
\r
1193 CmdLineErr("Option --%s is invalid", LongName.c_str());
\r
1194 return g_Opts.end();
\r
1197 static void AddOpt(const OptInfo &Opt)
\r
1199 if (GetOptInfo(Opt.LongName, false) != g_Opts.end())
\r
1200 Die("Option --%s defined twice", Opt.LongName.c_str());
\r
1201 g_Opts.insert(Opt);
\r
1205 #pragma warning(disable: 4505) // unreferenced local function
\r
1208 static void DefineFlagOpt(const string &LongName, const string &Help,
\r
1209 void *Value, bool *OptSet)
\r
1211 *(bool *) Value = false;
\r
1214 Opt.Value = Value;
\r
1215 Opt.OptSet = OptSet;
\r
1216 Opt.LongName = LongName;
\r
1217 Opt.bDefault = false;
\r
1219 Opt.Type = OT_Flag;
\r
1223 static void DefineTogOpt(const string &LongName, bool Default, const string &Help,
\r
1224 void *Value, bool *OptSet)
\r
1226 *(bool *) Value = Default;
\r
1229 Opt.Value = Value;
\r
1230 Opt.OptSet = OptSet;
\r
1231 Opt.LongName = LongName;
\r
1232 Opt.bDefault = Default;
\r
1234 Opt.Type = OT_Tog;
\r
1238 static void DefineIntOpt(const string &LongName, int Default, int Min, int Max,
\r
1239 const string &Help, void *Value, bool *OptSet)
\r
1241 *(int *) Value = Default;
\r
1244 Opt.Value = Value;
\r
1245 Opt.OptSet = OptSet;
\r
1246 Opt.LongName = LongName;
\r
1247 Opt.iDefault = Default;
\r
1251 Opt.Type = OT_Int;
\r
1255 static void DefineUnsOpt(const string &LongName, unsigned Default, unsigned Min,
\r
1256 unsigned Max, const string &Help, void *Value, bool *OptSet)
\r
1258 *(unsigned *) Value = Default;
\r
1261 Opt.Value = Value;
\r
1262 Opt.OptSet = OptSet;
\r
1263 Opt.LongName = LongName;
\r
1264 Opt.uDefault = Default;
\r
1268 Opt.Type = OT_Uns;
\r
1272 static void DefineFloatOpt(const string &LongName, double Default, double Min,
\r
1273 double Max, const string &Help, void *Value, bool *OptSet)
\r
1275 *(double *) Value = Default;
\r
1278 Opt.Value = Value;
\r
1279 Opt.OptSet = OptSet;
\r
1280 Opt.LongName = LongName;
\r
1281 Opt.dDefault = Default;
\r
1285 Opt.Type = OT_Float;
\r
1289 static void DefineStrOpt(const string &LongName, const char *Default,
\r
1290 const string &Help, void *Value, bool *OptSet)
\r
1292 *(string *) Value = (Default == 0 ? "" : string(Default));
\r
1295 Opt.Value = Value;
\r
1296 Opt.OptSet = OptSet;
\r
1297 Opt.LongName = LongName;
\r
1298 Opt.strDefault = (Default == 0 ? "" : string(Default));
\r
1300 Opt.Type = OT_Str;
\r
1304 static void ParseEnumValues(const string &Values, map<string, unsigned> &EnumValues)
\r
1306 EnumValues.clear();
\r
1311 for (string::const_iterator p = Values.begin(); ; ++p)
\r
1313 char c = (p == Values.end() ? '|' : *p);
\r
1316 else if (c == '|')
\r
1318 if (EnumValues.find(Name) != EnumValues.end())
\r
1319 Die("Invalid enum values, '%s' defined twice: '%s'",
\r
1320 Name.c_str(), Values.c_str());
\r
1321 if (Name.empty() || Value.empty())
\r
1322 Die("Invalid enum values, empty name or value: '%s'",
\r
1325 EnumValues[Name] = atoi(Value.c_str());
\r
1330 else if (c == '=')
\r
1333 Value.push_back(c);
\r
1335 Name.push_back(c);
\r
1336 if (p == Values.end())
\r
1341 static void DefineEnumOpt(const string &LongName, const string &ShortName,
\r
1342 int Default, const string &Values, const string &Help, void *Value)
\r
1344 *(int *) Value = Default;
\r
1347 Opt.Value = Value;
\r
1348 Opt.LongName = LongName;
\r
1349 Opt.iDefault = Default;
\r
1351 Opt.Type = OT_Enum;
\r
1352 ParseEnumValues(Values, Opt.EnumValues);
\r
1362 #define FLAG_OPT(LongName) bool opt_##LongName; bool optset_##LongName;
\r
1363 #define TOG_OPT(LongName, Default) bool opt_##LongName; bool optset_##LongName;
\r
1364 #define INT_OPT(LongName, Default, Min, Max) int opt_##LongName; bool optset_##LongName;
\r
1365 #define UNS_OPT(LongName, Default, Min, Max) unsigned opt_##LongName; bool optset_##LongName;
\r
1366 #define FLT_OPT(LongName, Default, Min, Max) double opt_##LongName; bool optset_##LongName;
\r
1367 #define STR_OPT(LongName, Default) string opt_##LongName; bool optset_##LongName;
\r
1368 #define ENUM_OPT(LongName, Values, Default) int opt_##LongName; bool optset_##LongName;
\r
1369 #include "myopts.h"
\r
1371 static int EnumStrToInt(const OptInfo &Opt, const string &Value)
\r
1373 const map<string, unsigned> &e = Opt.EnumValues;
\r
1375 for (map<string, unsigned>::const_iterator p = e.begin(); p != e.end(); ++p)
\r
1377 if (Value == p->first)
\r
1379 s += " " + p->first;
\r
1381 CmdLineErr("--%s %s not recognized, valid are: %s",
\r
1382 Opt.LongName.c_str(), Value.c_str(), s.c_str());
\r
1386 static void SetOpt(OptInfo &Opt, const string &Value)
\r
1388 *Opt.OptSet = true;
\r
1393 *(int *) Opt.Value = atoi(Value.c_str());
\r
1398 unsigned uValue = 0;
\r
1399 int n = sscanf(Value.c_str(), "%u", &uValue);
\r
1401 CmdLineErr("Invalid value '%s' for --%s",
\r
1402 Value.c_str(), Opt.LongName.c_str());
\r
1403 *(unsigned *) Opt.Value = uValue;
\r
1408 *(double *) Opt.Value = atof(Value.c_str());
\r
1413 *(string *) Opt.Value = Value;
\r
1418 *(int *) Opt.Value = EnumStrToInt(Opt, Value);
\r
1428 for (set<OptInfo>::const_iterator p = g_Opts.begin(); p != g_Opts.end(); ++p)
\r
1430 const OptInfo &Opt = *p;
\r
1431 Log("%s = ", Opt.LongName.c_str());
\r
1435 Log("%s", (*(bool *) Opt.Value) ? "yes" : "no");
\r
1438 Log("%s", (*(bool *) Opt.Value) ? "on" : "off");
\r
1441 Log("%d", *(int *) Opt.Value);
\r
1444 Log("%u", *(unsigned *) Opt.Value);
\r
1448 double Value = *(double *) Opt.Value;
\r
1449 if (Value == FLT_MAX)
\r
1456 Log("%s", (*(string *) Opt.Value).c_str());
\r
1459 Log("%d", *(int *) Opt.Value);
\r
1468 static void CompilerInfo()
\r
1470 #ifdef _FILE_OFFSET_BITS
\r
1471 printf("_FILE_OFFSET_BITS=%d\n", _FILE_OFFSET_BITS);
\r
1473 printf("_FILE_OFFSET_BITS not defined\n");
\r
1476 #define x(t) printf("sizeof(" #t ") = %d\n", (int) sizeof(t));
\r
1487 void Split(const string &Str, vector<string> &Fields, char Sep)
\r
1490 const unsigned Length = (unsigned) Str.size();
\r
1492 for (unsigned i = 0; i < Length; ++i)
\r
1495 if ((Sep == 0 && isspace(c)) || c == Sep)
\r
1497 if (!s.empty() || Sep != 0)
\r
1498 Fields.push_back(s);
\r
1505 Fields.push_back(s);
\r
1508 static void GetArgsFromFile(const string &FileName, vector<string> &Args)
\r
1512 FILE *f = OpenStdioFile(FileName);
\r
1514 while (ReadLineStdioFile(f, Line))
\r
1516 size_t n = Line.find('#');
\r
1517 if (n != string::npos)
\r
1518 Line = Line.substr(0, n);
\r
1519 vector<string> Fields;
\r
1520 Split(Line, Fields);
\r
1521 Args.insert(Args.end(), Fields.begin(), Fields.end());
\r
1523 CloseStdioFile(f);
\r
1526 void MyCmdLine(int argc, char **argv)
\r
1528 static unsigned RecurseDepth = 0;
\r
1531 DefineFlagOpt("compilerinfo", "Write info about compiler types and #defines to stdout.",
\r
1532 (void *) &opt_compilerinfo, &optset_compilerinfo);
\r
1533 DefineFlagOpt("quiet", "Turn off progress messages.", (void *) &opt_quiet, &optset_quiet);
\r
1534 DefineFlagOpt("version", "Show version and exit.", (void *) &opt_version, &optset_version);
\r
1535 DefineFlagOpt("logopts", "Log options.", (void *) &opt_logopts, &optset_logopts);
\r
1536 DefineFlagOpt("help", "Display command-line options.", (void *) &opt_help, &optset_help);
\r
1537 DefineStrOpt("log", "", "Log file name.", (void *) &opt_log, &optset_log);
\r
1546 #define FLAG_OPT(LongName) DefineFlagOpt(#LongName, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1547 #define TOG_OPT(LongName, Default) DefineTogOpt(#LongName, Default, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1548 #define INT_OPT(LongName, Default, Min, Max) DefineIntOpt(#LongName, Default, Min, Max, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1549 #define UNS_OPT(LongName, Default, Min, Max) DefineUnsOpt(#LongName, Default, Min, Max, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1550 #define FLT_OPT(LongName, Default, Min, Max) DefineFloatOpt(#LongName, Default, Min, Max, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1551 #define STR_OPT(LongName, Default) DefineStrOpt(#LongName, Default, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1552 #define ENUM_OPT(LongName, Values, Default) DefineEnumOpt(#LongName, Values, Default, "help", (void *) &opt_##LongName, &optset_##LongName);
\r
1553 #include "myopts.h"
\r
1555 if (RecurseDepth == 0)
\r
1558 for (int i = 0; i < argc; ++i)
\r
1559 g_Argv.push_back(string(argv[i]));
\r
1567 const string &Arg = g_Argv[i];
\r
1571 else if (Arg == "file:" && i + 1 < argc)
\r
1573 const string &FileName = g_Argv[i+1];
\r
1574 vector<string> Args;
\r
1575 GetArgsFromFile(FileName, Args);
\r
1576 for (vector<string>::const_iterator p = Args.begin();
\r
1577 p != Args.end(); ++p)
\r
1579 g_Argv.push_back(*p);
\r
1585 else if (Arg.size() > 1 && Arg[0] == '-')
\r
1587 string LongName = (Arg.size() > 2 && Arg[1] == '-' ? Arg.substr(2) : Arg.substr(1));
\r
1588 OptInfo Opt = *GetOptInfo(LongName, true);
\r
1589 *Opt.OptSet = true;
\r
1590 if (Opt.Type == OT_Flag)
\r
1592 g_Opts.erase(Opt);
\r
1593 *(bool *) Opt.Value = true;
\r
1594 g_Opts.insert(Opt);
\r
1598 else if (Opt.Type == OT_Tog)
\r
1600 g_Opts.erase(Opt);
\r
1601 if (string("no") + Opt.LongName == LongName)
\r
1602 *(bool *) Opt.Value = false;
\r
1605 asserta(Opt.LongName == LongName);
\r
1606 *(bool *) Opt.Value = true;
\r
1608 g_Opts.insert(Opt);
\r
1615 CmdLineErr("Missing value for option --%s", LongName.c_str());
\r
1617 string Value = g_Argv[i];
\r
1618 SetOpt(Opt, Value);
\r
1624 CmdLineErr("Expected -option_name or --option_name, got '%s'", Arg.c_str());
\r
1628 if (RecurseDepth > 0)
\r
1634 if (opt_compilerinfo)
\r
1637 SetLogFileName(opt_log);
\r
1639 if (opt_log != "")
\r
1641 for (int i = 0; i < argc; ++i)
\r
1642 Log("%s%s", i == 0 ? "" : " ", g_Argv[i].c_str());
\r
1644 time_t Now = time(0);
\r
1645 struct tm *t = localtime(&Now);
\r
1646 const char *s = asctime(t);
\r
1647 Log("Started %s", s); // there is a newline in s
\r
1648 Log("Version " MY_VERSION ".%s\n", SVN_VERSION);
\r
1656 double Pct(double x, double y)
\r
1660 return (x*100.0f)/y;
\r
1663 void GetCmdLine(string &s)
\r
1666 for (unsigned i = 0; i < SIZE(g_Argv); ++i)
\r
1674 char *mystrsave(const char *s)
\r
1676 unsigned n = unsigned(strlen(s));
\r
1677 char *t = myalloc(char, n+1);
\r
1678 memcpy(t, s, n+1);
\r
1682 void Logu(unsigned u, unsigned w, unsigned prefixspaces)
\r
1684 for (unsigned i = 0; i < prefixspaces; ++i)
\r
1686 if (u == UINT_MAX)
\r
1687 Log("%*.*s", w, w, "*");
\r
1692 void Logf(float x, unsigned w, unsigned prefixspaces)
\r
1694 for (unsigned i = 0; i < prefixspaces; ++i)
\r
1697 Log("%*.*s", w, w, "*");
\r
1699 Log("%*.2f", w, x);
\r
1702 static uint32 g_SLCG_state = 1;
\r
1704 // Numerical values used by Microsoft C, according to wikipedia:
\r
1705 // http://en.wikipedia.org/wiki/Linear_congruential_generator
\r
1706 static uint32 g_SLCG_a = 214013;
\r
1707 static uint32 g_SLCG_c = 2531011;
\r
1709 // Simple Linear Congruential Generator
\r
1710 // Bad properties; used just to initialize the better generator.
\r
1711 static uint32 SLCG_rand()
\r
1713 g_SLCG_state = g_SLCG_state*g_SLCG_a + g_SLCG_c;
\r
1714 return g_SLCG_state;
\r
1717 static void SLCG_srand(uint32 Seed)
\r
1719 g_SLCG_state = Seed;
\r
1720 for (int i = 0; i < 10; ++i)
\r
1725 A multiply-with-carry random number generator, see:
\r
1726 http://en.wikipedia.org/wiki/Multiply-with-carry
\r
1728 The particular multipliers used here were found on
\r
1729 the web where they are attributed to George Marsaglia.
\r
1732 static bool g_InitRandDone = false;
\r
1733 static uint32 g_X[5];
\r
1735 uint32 RandInt32()
\r
1739 uint64 Sum = 2111111111*(uint64) g_X[3] + 1492*(uint64) g_X[2] +
\r
1740 1776*(uint64) g_X[1] + 5115*(uint64) g_X[0] + g_X[4];
\r
1744 g_X[4] = (uint32) (Sum >> 32);
\r
1745 g_X[0] = (uint32) Sum;
\r
1749 unsigned randu32()
\r
1751 return (unsigned) RandInt32();
\r
1756 if (g_InitRandDone)
\r
1758 // Do this first to avoid recursion
\r
1759 g_InitRandDone = true;
\r
1761 unsigned Seed = (optset_randseed ? opt_randseed : (unsigned) (time(0)*getpid()));
\r
1762 Log("RandSeed=%u\n", Seed);
\r
1765 for (unsigned i = 0; i < 5; i++)
\r
1766 g_X[i] = SLCG_rand();
\r
1768 for (unsigned i = 0; i < 100; i++)
\r
1772 // MUST COME AT END BECAUSE OF #undef
\r
1777 void *mymalloc(unsigned bytes, const char *FileName, int Line)
\r
1779 void *rce_malloc(unsigned bytes, const char *FileName, int Line);
\r
1780 return rce_malloc(bytes, FileName, Line);
\r
1783 void myfree(void *p, const char *FileName, int Line)
\r
1785 void rce_free(void *p, const char *FileName, int Line);
\r
1786 rce_free(p, FileName, Line);
\r
1789 void myfree2(void *p, unsigned bytes, const char *FileName, int Line)
\r
1791 void rce_free(void *p, const char *FileName, int Line);
\r
1792 rce_free(p, FileName, Line);
\r
1795 #else // RCE_MALLOC
\r
1796 void *mymalloc(unsigned bytes)
\r
1799 if (g_InitialMemUseBytes == 0)
\r
1800 g_InitialMemUseBytes = GetMemUseBytes();
\r
1802 g_TotalAllocBytes += bytes;
\r
1803 g_NetBytes += bytes;
\r
1804 if (g_NetBytes > g_MaxNetBytes)
\r
1806 if (g_NetBytes > g_MaxNetBytes + 10000000)
\r
1807 GetMemUseBytes();//to force update of peak
\r
1808 g_MaxNetBytes = g_NetBytes;
\r
1810 void *p = malloc(bytes);
\r
1811 //void *p = _malloc_dbg(bytes, _NORMAL_BLOCK, __FILE__, __LINE__);
\r
1814 double b = GetMemUseBytes();
\r
1815 fprintf(stderr, "\nOut of memory mymalloc(%u), curr %.3g bytes",
\r
1816 (unsigned) bytes, b);
\r
1819 #if DEBUG && defined(_MSC_VER)
\r
1820 asserta(_CrtCheckMemory());
\r
1822 Die("Out of memory, mymalloc(%u), curr %.3g bytes\n",
\r
1823 (unsigned) bytes, b);
\r
1828 void myfree(void *p)
\r
1833 //_free_dbg(p, _NORMAL_BLOCK);
\r
1836 void myfree2(void *p, unsigned bytes)
\r
1839 g_TotalFreeBytes += bytes;
\r
1840 g_NetBytes -= bytes;
\r