// examples/varargs #include "examples/varargs_preamble.h" // BEGIN mycpp output #include "gc_heap.h" using gc_heap::Alloc; using gc_heap::kZeroMask; using gc_heap::StackRoots; #include "my_runtime.h" #include "mylib2.h" using gc_heap::NewStr; using gc_heap::NewList; using gc_heap::NewDict; GLOBAL_STR(str0, "myconst"); GLOBAL_STR(str1, "constant string"); GLOBAL_STR(str2, "stderr_line"); GLOBAL_STR(str3, "log %d %s"); GLOBAL_STR(str4, "LL"); GLOBAL_STR(str5, "[%%] %d %s"); GLOBAL_STR(str6, "LL"); GLOBAL_STR(str7, "[%%] %d %s"); GLOBAL_STR(str8, "LL"); namespace varargs { // forward declare } // forward declare namespace varargs namespace varargs { // declare extern Str* CONST; void run_tests(); void run_benchmarks(); inline Str* fmt0(int a0, Str* a1) { gBuf.reset(); gBuf.write_const("log ", 4); gBuf.format_d(a0); gBuf.write_const(" ", 1); gBuf.format_s(a1); return gBuf.getvalue(); } inline Str* fmt1(int a0, Str* a1) { gBuf.reset(); gBuf.write_const("[", 1); gBuf.write_const("%", 1); gBuf.write_const("] ", 2); gBuf.format_d(a0); gBuf.write_const(" ", 1); gBuf.format_s(a1); return gBuf.getvalue(); } inline Str* fmt2(int a0, Str* a1) { gBuf.reset(); gBuf.write_const("[", 1); gBuf.write_const("%", 1); gBuf.write_const("] ", 2); gBuf.format_d(a0); gBuf.write_const(" ", 1); gBuf.format_s(a1); return gBuf.getvalue(); } } // declare namespace varargs namespace varargs { // define Str* CONST = str0; void run_tests() { println_stderr(str1); println_stderr(str2); println_stderr(fmt0(42, str4)); println_stderr(fmt1(42, str6)); println_stderr(CONST); } void run_benchmarks() { for (int i = 0; i < 1000; ++i) { println_stderr(fmt2(42, str8)); } } } // define namespace varargs int main(int argc, char **argv) { // gc_heap::gHeap.Init(512); gc_heap::gHeap.Init(128 << 10); // 128 KiB; doubling in size // gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection if (getenv("BENCHMARK")) { fprintf(stderr, "Benchmarking...\n"); varargs::run_benchmarks(); } else { varargs::run_tests(); } }