www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - prolog and epilog code

reply Rufus Smith <RufusSmith indi.com> writes:
Can one add code that executes before the GC and any memory is 
normally allocated(even static) and after all of it was suppose 
to be released?

A sort of static this for the whole app. I would like to monitor 
the memory of the app to make sure that the total memory before 
and after is equal. I use my own memory routines but would like 
something where I can write out the information after all 
destructors have been called.

I have malloc and free wrapped and simply add the total memory 
used and released. These, of course, should be the same *after* 
program termination. But, I can't wait that long ;)
Aug 01 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/1/16 9:24 PM, Rufus Smith wrote:
 Can one add code that executes before the GC and any memory is normally
 allocated(even static) and after all of it was suppose to be released?
Of course! You just have to modify druntime :) One thing you can do instead is compile without a D main function, and use C main, initialize the runtime manually. Then you can put your measurement code around the runtime initialization and termination. See here: http://dlang.org/phobos/core_runtime.html#.rt_init -Steve
Aug 02 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Tuesday, 2 August 2016 at 11:37:05 UTC, Steven Schveighoffer 
wrote:
 On 8/1/16 9:24 PM, Rufus Smith wrote:
 Can one add code that executes before the GC and any memory is 
 normally
 allocated(even static) and after all of it was suppose to be 
 released?
Of course! You just have to modify druntime :)
That doesn't sound like fun. Why doesn't D add a hook for program level static this(we have module level, need something more)?
 One thing you can do instead is compile without a D main 
 function, and use C main, initialize the runtime manually. Then 
 you can put your measurement code around the runtime 
 initialization and termination.
How does one use C main? extern C? Or do I have to actually write C code?
Aug 02 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 2 August 2016 at 16:21:07 UTC, Rufus Smith wrote:
 How does one use C main? extern C?
extern(C) int main() should do it
Aug 02 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Tuesday, 2 August 2016 at 16:30:08 UTC, Adam D. Ruppe wrote:
 On Tuesday, 2 August 2016 at 16:21:07 UTC, Rufus Smith wrote:
 How does one use C main? extern C?
extern(C) int main() should do it
It doesn't seem to be that easy! https://wiki.dlang.org/Runtime_internals If I do this then I get lots of missing imports(the D runtime). import std.stdio; import core.runtime; private alias extern(C) int function(char[][] args) MainFunc; extern int _d_run_main(int argc, char **argv, MainFunc mainFunc); extern(C) void main(int argc, char **argv) { rt_init(); _d_run_main(argc, argv, &_main); rt_term(); } extern(C) int _main(char[][] args) { return 0; } Or even using the code from the wiki, I get unresolved externals. Mainly the druntime like stuff(typeinfo, aa stuff, etc...).
Aug 02 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 2 August 2016 at 17:04:08 UTC, Rufus Smith wrote:
 It doesn't seem to be that easy!
_d_run_main is extern(C), not just extern. Then your code should work. You can also simplify more: --- import std.stdio; import core.runtime; extern(C) int main() { rt_init(); writeln("Hello from D"); rt_term(); return 0; } ---
Aug 02 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 2 August 2016 at 17:23:57 UTC, Adam D. Ruppe wrote:
 _d_run_main is extern(C), not just extern. Then your code 
 should work.
Also, you don't actually have to rt_init and rt_term if you do _d_run_main because it does it for you...
Aug 02 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/2/16 1:26 PM, Adam D. Ruppe wrote:
 On Tuesday, 2 August 2016 at 17:23:57 UTC, Adam D. Ruppe wrote:
 _d_run_main is extern(C), not just extern. Then your code should work.
Also, you don't actually have to rt_init and rt_term if you do _d_run_main because it does it for you...
I don't think this is supposed to be exposed. You should call the runtime in the expected way (i.e. rt_init, rt_term), because this may change in ways that would still compile but crash horrifically. -Steve
Aug 02 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/2/16 1:04 PM, Rufus Smith wrote:
 On Tuesday, 2 August 2016 at 16:30:08 UTC, Adam D. Ruppe wrote:
 On Tuesday, 2 August 2016 at 16:21:07 UTC, Rufus Smith wrote:
 How does one use C main? extern C?
extern(C) int main() should do it
It doesn't seem to be that easy! https://wiki.dlang.org/Runtime_internals
This is just explaining how the runtime currently works, not how you should override it. It's really easy actually. Example: $ cat main.c #include <stdio.h> extern int rt_init(); extern void d_func(); int main(int argc, char *argv[]) { printf("hello from C!\n"); rt_init(); printf("done initializing runtime\n"); d_func(); } $ cat dmain.d extern(C) void d_func() { import std.stdio; writeln("hello from D!"); } shared static this() { import std.stdio; writeln("doing some pre-run init!"); } $ gcc -c main.c $ dmd dmain.d main.o $ ./dmain hello from C! doing some pre-run init! done initializing runtime hello from D! -Steve
Aug 02 2016
parent reply Rufus Smith <RS14552 mail.com> writes:
On Tuesday, 2 August 2016 at 17:25:21 UTC, Steven Schveighoffer 
wrote:
 On 8/2/16 1:04 PM, Rufus Smith wrote:
 On Tuesday, 2 August 2016 at 16:30:08 UTC, Adam D. Ruppe wrote:
 On Tuesday, 2 August 2016 at 16:21:07 UTC, Rufus Smith wrote:
 How does one use C main? extern C?
extern(C) int main() should do it
It doesn't seem to be that easy! https://wiki.dlang.org/Runtime_internals
This is just explaining how the runtime currently works, not how you should override it. It's really easy actually. Example: $ cat main.c #include <stdio.h> extern int rt_init(); extern void d_func(); int main(int argc, char *argv[]) { printf("hello from C!\n"); rt_init(); printf("done initializing runtime\n"); d_func(); } $ cat dmain.d extern(C) void d_func() { import std.stdio; writeln("hello from D!"); } shared static this() { import std.stdio; writeln("doing some pre-run init!"); } $ gcc -c main.c $ dmd dmain.d main.o $ ./dmain hello from C! doing some pre-run init! done initializing runtime hello from D! -Steve
When I do this in my project and try to link in the lib or obj created from the C project, I get "entry point must be defined" error. I tried to add both to the "command line". If I don't have a main function in D, I get all kinds of unresolved externals. Not sure why, but it seems to not import in the runtime when there is no main defined. Basically I created a side C++ project, copied your code in to the main.cpp. I then added the obj file to the linker command line path. Then I get the missing entry point error. It is very similar to yours, just using VisualD and windows. Seems like it would be easier just to create a wrapper process and use shared memory to transfer data between them ;/ For example, if I just create a new D project. Add a new cpp file and paste your C code in, paste the D code in the empty D file, and try to compile, I get: main.cpp main.obj : error LNK2019: unresolved external symbol "int __cdecl rt_init(void)" (?rt_init YAHXZ) referenced in function main main.obj : error LNK2019: unresolved external symbol "void __cdecl d_func(void)" (?d_func YAXXZ) referenced in function main CMain.obj : error LNK2001: unresolved external symbol _D14TypeInfo_Const6__vtblZ CMain.obj : error LNK2019: unresolved external symbol _d_throwc referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12er noEnforceFNfiLAyaZi ( safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D12TypeInfo_Aya6__initZ CMain.obj : error LNK2019: unresolved external symbol _D3std3utf12isValidDcharFNaNbNiNfwZb referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _D3std9exception14ErrnoException6__ctorMFNeAyaAyamZC3std9excep ion14ErrnoException ( trusted std.exception.ErrnoException std.exception.ErrnoException.__ctor(immutable(char)[], immutable(char)[], ulong)) referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12er noEnforceFNfiLAyaZi ( safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _aApplycd1 referenced in function _D3std5stdio4File17LockingTextWriter12__T3putTAyaZ3putMFNfAyaZv ( safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio8__assertFiZv (void std.stdio.__assert(int)) referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File17lockingTextWriterMFNfZS3std5stdio4File17LockingTextWriter ( safe std.stdio.File.LockingTextWriter std.stdio.File.lockingTextWriter()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv ( safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D3std5stdio12__ModuleInfoZ CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio13trustedStdoutFNdNeZS3std5stdio4File ( property trusted std.stdio.File std.stdio.trustedStdout()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv ( safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std3utf6toUTF8FNaNbNiNfNkJG4awZAa referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _d_assert_msg referenced in function _D3std5stdio4File17LockingTextWriter12__T3putTAyaZ3putMFNfAyaZv ( safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D10TypeInfo_k6__initZ CMain.obj : error LNK2019: unresolved external symbol _D3std9exception14ErrnoException7__ClassZ referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12er noEnforceFNfiLAyaZi ( safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File6__dtorMFNfZv ( safe void std.stdio.File.__dtor()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv ( safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _d_newclass referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12er noEnforceFNfiLAyaZi ( safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio7__arrayZ referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _d_assert referenced in function _D4main8__assertFiZv (void main.__assert(int)) CMain.obj : error LNK2019: unresolved external symbol _d_unittest referenced in function _D4main15__unittest_failFiZv (void main.__unittest_fail(int)) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File17LockingTextWriter6__dtorMFNeZv ( trusted void std.stdio.File.LockingTextWriter.__dtor()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv ( safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _d_arraybounds referenced in function _D4main7__arrayZ x64\Debug DMD\CMain.exe : fatal error LNK1120: 23 unresolved externals So, something funky is going on. Any ideas?
Aug 02 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/2/16 2:25 PM, Rufus Smith wrote:

 So, something funky is going on. Any ideas?
phobos is not being resolved. I don't use visualD, so I'm not sure what the issue is, it's probably a compiler or linker ordering issue. -Steve
Aug 02 2016
parent reply Rufus Smith <RS14552 mail.com> writes:
On Tuesday, 2 August 2016 at 18:34:49 UTC, Steven Schveighoffer 
wrote:
 On 8/2/16 2:25 PM, Rufus Smith wrote:

 So, something funky is going on. Any ideas?
phobos is not being resolved. I don't use visualD, so I'm not sure what the issue is, it's probably a compiler or linker ordering issue. -Steve
Um, but it works as long as I a main function! So while it might be something weird with visualD, it's probably something more related to dmd(flag or something). Cause I am using phobo's... that is, unless it does not link in phobo's if there is no D main file(trying to be smart).
Aug 02 2016
parent Rufus Smith <RS14552 mail.com> writes:
On Tuesday, 2 August 2016 at 20:28:51 UTC, Rufus Smith wrote:
 On Tuesday, 2 August 2016 at 18:34:49 UTC, Steven Schveighoffer 
 wrote:
 On 8/2/16 2:25 PM, Rufus Smith wrote:

 So, something funky is going on. Any ideas?
phobos is not being resolved. I don't use visualD, so I'm not sure what the issue is, it's probably a compiler or linker ordering issue. -Steve
Um, but it works as long as I a main function! So while it might be something weird with visualD, it's probably something more related to dmd(flag or something). Cause I am using phobo's... that is, unless it does not link in phobo's if there is no D main file(trying to be smart).
If I link in phobos manually most of the errors go away, I'm left with" main.obj : error LNK2019: unresolved external symbol "int __cdecl rt_init(void)" (?rt_init YAHXZ) referenced in function main main.obj : error LNK2019: unresolved external symbol "void __cdecl Dmain(void)" (?Dmain YAXXZ) referenced in function main phobos64.lib(sections_win64_2317_4e2.obj) : error LNK2019: unresolved external symbol _deh_beg referenced in function _D2rt14sections_win6412SectionGroup8ehTablesMxFNdZAyS2rt15deh_w n64_posix9FuncTable (const( property immutable(rt.deh_win64_posix.FuncTable)[] function()) rt.sections_win64.SectionGroup.ehTables) phobos64.lib(sections_win64_2317_4e2.obj) : error LNK2019: unresolved external symbol _deh_end referenced in function _D2rt14sections_win6412SectionGroup8ehTablesMxFNdZAyS2rt15deh_w n64_posix9FuncTable (const( property immutable(rt.deh_win64_posix.FuncTable)[] function()) rt.sections_win64.SectionGroup.ehTables) This mainly seems that the d code is not being linked with the c code. But I guess this is all on VisualD's end since since it seems just to be the command line not getting the correct stuff, for some oddball reasons.
Aug 02 2016