how does using C++ templates affect memory usage? - VxWorks

This is a discussion on how does using C++ templates affect memory usage? - VxWorks ; Hi, I am having a problem with large object files. My problem seems to come from using templates. I have several classes that use templates. I declare pointers to objects of these types, and then "new" them in my constructor. ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: how does using C++ templates affect memory usage?

  1. how does using C++ templates affect memory usage?

    Hi,
    I am having a problem with large object files. My problem seems to
    come from using templates. I have several classes that use templates.
    I declare pointers to objects of these types, and then "new" them in my
    constructor. I wouldn't expect that to affect my stack usage, but it
    is causing my stack usage to grow quite alot. Also, the resulting
    object files are unreasonably large. A source file that uses some of
    these template classes that has 100 SLOC can result in an object file
    over 100 kb. Does anyone have any insight into how the complier is
    doing this, and how I can reduce the object size and stack usage?
    Thanks.

    Ricardo


  2. Re: how does using C++ templates affect memory usage?

    rjhii wrote:
    > Hi,
    > I am having a problem with large object files. My problem seems to
    > come from using templates. I have several classes that use templates.
    > I declare pointers to objects of these types, and then "new" them in my
    > constructor. I wouldn't expect that to affect my stack usage, but it
    > is causing my stack usage to grow quite a lot.

    I can't explain this part.

    > Also, the resulting
    > object files are unreasonably large. A source file that uses some of
    > these template classes that has 100 SLOC can result in an object file
    > over 100 kb. Does anyone have any insight into how the complier is
    > doing this, and how I can reduce the object size and stack usage?

    If you're not using the GNU "-frepo" option, or the Diab equivalent,
    you're probably getting an instantiation of a template in every object
    file where the template type is referred to. These instantiations are
    marked by the mangled name of the template, and are collapsed by the
    linker so that only a single copy of each instantiation actually takes
    space in memory.

    In the earlier GNU compilers from Wind, these instantiations are called
    "linkonce" sections. In later compilers, they are called COMDAT
    sections. You could look for sections with the string "linkonce" in
    them in the output of nm to confirm m guess.

    Note again that these instantiations are only represented multiple times
    in the object files. In the partial link, they are optimized to a
    single section.



    > Thanks.
    >
    > Ricardo
    >


  3. Re: how does using C++ templates affect memory usage?

    Ok, I tried compiling with the -frepo flag, but I am not able to link.
    I'm statically linking a rommable object, and the final link fails with
    many undefined references to the templates. How do I get the
    definitions in?

    Ricardo


  4. Re: how does using C++ templates affect memory usage?

    rjhii wrote:
    > Ok, I tried compiling with the -frepo flag, but I am not able to link.
    > I'm statically linking a rommable object, and the final link fails with
    > many undefined references to the templates. How do I get the
    > definitions in?


    Please send us the build log. Right now, it's impossible to tell.

    >
    > Ricardo
    >


  5. Re: how does using C++ templates affect memory usage?

    Here is the build log for my final rommable image build:

    The problem is in the final link call. All the other .a libraries that
    I'm linking are build with the -frepo flag.

    ccppc -c -mcpu=604 -mstrict-align -ansi -fno-builtin -P
    -xassembler-with-cpp -I.. -IC:\Tor
    nado2.2\target\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\
    src -IC:\Tornado2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu
    -DTOOL=gnu -DPRJ_BUILD -
    IC:\Tornado2.2\target\config\dy4182\h
    -IC:\Tornado2.2\target\config\dy4182\src -IC:\Tornad
    o2.2\target\config\dy4182\h\drv\dy4
    -IC:\Tornado2.2\host\x86-win32\include\g++-3 -mlongcal
    l -DTORNADO2_2 -DCPU0_IMAGE -fvolatile
    C:\Tornado2.2\target\config\dy4182\sysALib.s -o sys
    ALib.o
    ccppc -c -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\targe
    t\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Torn
    ado2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.
    2\target\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\c
    onfig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_
    2 -DCPU0_IMAGE -DFLASH_BUILD -fvolatile
    C:\Tornado2.2\target\config\dy4182\sysLib.c -o sys
    Lib.o
    ccppc -c -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\targe
    t\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Torn
    ado2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.
    2\target\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\c
    onfig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_
    2 -DCPU0_IMAGE -DFLASH_BUILD -fvolatile ..\prjConfig.c -o prjConfig.o
    ccppc -c -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\targe
    t\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Torn
    ado2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.
    2\target\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\c
    onfig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_
    2 -DCPU0_IMAGE -DFLASH_BUILD -fvolatile ..\linkSyms.c -o linkSyms.o
    ccppc -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\target\c
    onfig\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Tornado
    2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.2\t
    arget\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\conf
    ig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_2 -
    DCPU0_IMAGE -DFLASH_BUILD -c
    C:\Tornado2.2\target\config\dy4182\src\drv\thermal \dy4max6634
    ..c
    vxrm ..\prjObjs.lst
    Generating ..\prjObjs.lst...
    ccppc -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\target\c
    onfig\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Tornado
    2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.2\t
    arget\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\conf
    ig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_2 -
    DCPU0_IMAGE -DFLASH_BUILD -c
    C:\Tornado2.2\target\config\comps\src\version.c
    ccppc -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\target\c
    onfig\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Tornado
    2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.2\t
    arget\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\conf
    ig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_2 -
    DCPU0_IMAGE -DFLASH_BUILD -c
    C:\Tornado2.2\target\config\comps\src\usrEntry.c
    ccppc -r -nostdlib -Wl,-X -frepo usrEntry.o -Wl,@..\prjObjs.lst \
    C:\Tornado2.2\target\config\dy4182\excArchLib.obj
    C:\Tornado2.2\target\config\dy4182\excA
    Lib.obj C:\Tornado2.2\target\config\dy4182\intALib.obj
    C:\Tornado2.2\target\config\dy4182\
    sysL2BackCache.obj
    C:\Tornado2.2\target\config\dy4182\sysL3BackCache. obj C:\Tornado2.2\X11

    R6\lib\libX11.o C:\Tornado2.2\X11R6\lib\libICE.o
    C:\Tornado2.2\X11R6\lib\libSM.o C:\Tornad
    o2.2\X11R6\lib\libXext.o C:\Tornado2.2\X11R6\lib\libXmu.o
    C:\Tornado2.2\X11R6\lib\libXm.o
    C:\Tornado2.2\target\proj\Dy4_1553_T221_182\PPC604 gnu\dy4_1553,o
    version.o -Wl,--start-gro
    up \
    -LC:\Tornado2.2\target\lib\ppc\PPC604\gnu
    -LC:\Tornado2.2\target\lib\ppc\PPC604\common C
    :\Tornado2.2\target\lib\libDY4182PPC604gnuvx.a
    C:\Tornado2.2\target\lib\pci.a C:\Tornado2.
    2\target\lib\182fpga.a C:\Tornado2.2\target\lib\libusb.a -lcplus
    -lgnucplus -lvxcom
    -larch -lcommoncc -ldcc -ldrv -lgcc -lnet -los -lrpc -lwdb
    -lwind -lwindview C:\
    Tornado2.2\target\lib\libPPC604gnuvx.a
    C:\MHP\TornadoProjects\Registry\Archive\Registry.a
    C:\MHP\TornadoProjects\MHP_BuildM2\Archive\SM_M2.a
    C:\MHP\TornadoProjects\gsm\PPC604gnu\GS
    M.a \
    -Wl,--end-group -o partialImage.o
    nmppc partialImage.o @..\prjObjs.lst | wtxtcl
    C:\Tornado2.2\host\src\hutils\munch.tcl -c p
    pc > ctdt.c
    ccppc -c -fdollars-in-identifiers -O0 -mcpu=604 -mstrict-align
    -fno-builtin -frepo -I.. -I
    C:\Tornado2.2\target\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\
    comps\src -IC:\Tornado2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu
    -DTOOL=gnu -DPRJ_B
    UILD -IC:\Tornado2.2\target\config\dy4182\h
    -IC:\Tornado2.2\target\config\dy4182\src -IC:\
    Tornado2.2\target\config\dy4182\h\drv\dy4
    -IC:\Tornado2.2\host\x86-win32\include\g++-3 -ml
    ongcall -DTORNADO2_2 -DCPU0_IMAGE -DFLASH_BUILD ctdt.c -o ctdt.o
    copy partialImage.o tmp.o
    1 file(s) copied.
    C:\Tornado2.2\host\x86-win32\bin\makeSymTbl.bat ppc tmp.o symTbl.c
    ccppc -c -fdollars-in-identifiers -O0 -mcpu=604 -mstrict-align -ansi
    -fno-builtin -frepo -
    I.. -IC:\Tornado2.2\target\config\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\c
    onfig\comps\src -IC:\Tornado2.2\target\src\drv -DCPU=PPC604
    -DTOOL_FAMILY=gnu -DTOOL=gnu -
    DPRJ_BUILD -IC:\Tornado2.2\target\config\dy4182\h
    -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\config\dy4182\h\drv\dy4
    -IC:\Tornado2.2\host\x86-win32\include\g++
    -3 -mlongcall -DTORNADO2_2 -DCPU0_IMAGE -DFLASH_BUILD -w symTbl.c -o
    symTbl.o
    ccppc -O0 -mcpu=604 -mstrict-align -ansi -fno-builtin -frepo -I..
    -IC:\Tornado2.2\target\c
    onfig\dy4182 -IC:\Tornado2.2\target\h
    -IC:\Tornado2.2\target\config\comps\src -IC:\Tornado
    2.2\target\src\drv -DCPU=PPC604 -DTOOL_FAMILY=gnu -DTOOL=gnu
    -DPRJ_BUILD -IC:\Tornado2.2\t
    arget\config\dy4182\h -IC:\Tornado2.2\target\config\dy4182\src
    -IC:\Tornado2.2\target\conf
    ig\dy4182\h\drv\dy4 -IC:\Tornado2.2\host\x86-win32\include\g++-3
    -mlongcall -DTORNADO2_2 -
    DCPU0_IMAGE -DFLASH_BUILD -c
    C:\Tornado2.2\target\config\comps\src\dataSegPad.c
    ldppc -X -N -e _sysInit -Ttext 00300000 \
    dataSegPad.o partialImage.o ctdt.o symTbl.o \
    -T C:\Tornado2.2\target\h\tool\gnu\ldscripts\link.RAM -o vxWorks
    partialImage.o: In function `sscResetPort':
    C:\code\ffw1_3a\FFW9\FPGA\src\ssc.c:980: undefined reference to
    `vector pe, allocator
    >::_M_insert_aux(ReadyToStartInfoType *, ReadyToStartI

    nfoType const &)'

    .... many many undefined template references to follow


  6. Re: how does using C++ templates affect memory usage?

    I know one of my problems was I was missing a compiler patch from
    windriver. Now at least I can see the collect tool running. However
    I'm still not liking my template references. I think it's because I'm
    building most of my code as a .a library and then linking it with the
    kernel. Do you know, can I expect the -frepo flag to work correctly
    when building a .a archive and statically linking it with the kernel?

    Ricardo


  7. Re: how does using C++ templates affect memory usage?

    rjhii wrote:
    > I know one of my problems was I was missing a compiler patch from
    > windriver. Now at least I can see the collect tool running.

    You're using the "-frepo" option aren't you?

    However
    > I'm still not liking my template references.

    Do you mean that there are still unresolved references to template
    symbols?

    I think it's because I'm
    > building most of my code as a .a library and then linking it with the
    > kernel.

    The "=frepo" option works by doing a trial link of your application
    as a whole, and looking for unresolved symbols that are mangled like
    template instantiations. It puts the instantiation code, AFAICT, in
    the object file that made the reference. It has no ability to add
    template code to an archive.



    Do you know, can I expect the -frepo flag to work correctly
    > when building a .a archive and statically linking it with the kernel?

    I think not. Why are you building the archive?

    >
    > Ricardo
    >


  8. Re: how does using C++ templates affect memory usage?


    Please try this compiler option:
    "-fmerge-templates"



    Cheers,
    Tang kun


  9. Re: how does using C++ templates affect memory usage?

    Been there, done that, not good enough.


  10. Re: how does using C++ templates affect memory usage?

    Hi Guys,

    I am having similar problem with -frepo (undefined references to template function).
    One of the notes that mentioned in vxworks docs is that this flag should be used with ccarch and not ldarch.
    Does any one knows why?
    The alternate solution for unresolved references is to use LD_PARTIAL_FLAGS for linking, but then the file that is created is not ELF file. Any idea why is that so?
    Does any one knows is there any solution for this problem as a result of linking with LD_PARTIAL_FLAGS?

    BTW, it seems that the flag "fmerge-templates" is not supported in vxworks 5.4.
    (We are working with vxworks 5.4 and tornado 2.0.2, make output should be vxWorks.st)

    Thank you,
    Regards.

+ Reply to Thread