Skip to content

Instantly share code, notes, and snippets.

@iffy

iffy/Makefile Secret

Created January 25, 2019 04:37
Show Gist options
  • Save iffy/7ad0755869240ebe295b1306179b7042 to your computer and use it in GitHub Desktop.
Save iffy/7ad0755869240ebe295b1306179b7042 to your computer and use it in GitHub Desktop.
Nim memory growth
import strformat
import strutils
import sequtils
echo "clib initialized"
echo GC_getStatistics()
proc hello_echo*(message:cstring):cstring {.exportc.} =
## Echo a message back
var s_message:string = $message
result = &"{s_message} echo"
# echo &"Nim echo result: {result}"
proc stats*() {.exportc.} =
echo GC_getStatistics()
NIMBASE = ~/lib/Nim/lib/nimbase.h
.PHONY: run clean
run: a.out
./a.out
a.out: test.cpp csrc/clib.cpp csrc/nimbase.h
clang++ -Icsrc csrc/*.cpp test.cpp
csrc/clib.cpp: clib.nim
mkdir -p csrc
nim cpp --genScript --checks:on -d:traceGC --memTracker:on -d:memProfiler -d:lineDir -d:leakDetector -d:useSysAssert -d:useGcAssert --header --nimcache:csrc --noMain --compileOnly ./clib.nim
csrc/nimbase.h: $(NIMBASE) csrc/clib.cpp
cp $(NIMBASE) csrc/nimbase.h
clean:
-rm a.out
-rm -r csrc
clang++ -Icsrc csrc/*.cpp test.cpp
./a.out
clib initialized
[GC] total memory: 528384
[GC] occupied memory: 61528
[GC] stack scans: 0
[GC] stack cells: 0
[GC] cycle collections: 0
[GC] max threshold: 0
[GC] zct capacity: 1024
[GC] max cycle table size: 0
[GC] max pause time [ms]: 0
[GC] stack bottom: 0x7fff5bbaaec8
[GC] max stack size: 0
Hello world
C++ passing message: Hello
C++ message addr: 0x7fff5bbaaf19
C++ retval addr : 0x1040f3158
Got reply: Hello echo
C++ passing message: Hello -
C++ message addr: 0x7fff5bbaaf19
C++ retval addr : 0x1040f31a8
Got reply: Hello - echo
-- snip --
C++ message addr: 0x7fc75b002800
C++ retval addr : 0x105124050
Got reply: Hello --- (snip)
[GC] total memory: 22351872
[GC] occupied memory: 254440
[GC] stack scans: 86
[GC] stack cells: 10
[GC] cycle collections: 0
[GC] max threshold: 0
[GC] zct capacity: 1024
[GC] max cycle table size: 0
[GC] max pause time [ms]: 0
[GC] stack bottom: 0x7fff5bbaaec8
[GC] max stack size: 944
#include <iostream>
#include "csrc/clib.h"
using namespace std;
N_CDECL(void, NimMain)(void);
std::string echo(std::string arg) {
const char* message = arg.c_str();
cout << "C++ passing message: " << message << "\n";
cout << "C++ message addr: " << (void*)message << "\n";
char* retval = hello_echo(
(char *)(message)
);
cout << "C++ retval addr : " << (void*)retval << "\n";
return std::string(retval);
}
int main(void) {
NimMain();
cout << "Hello world\n";
int i = 0;
for (i = 0; i < 10000; i++) {
std::string msg = "Hello " + std::string(i, '-');
std::string ret = echo(msg);
cout << "Got reply: " << ret << "\n";
}
stats();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment