This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Slightly better way to __USE_MALLOC


Hi, all..  When debugging recently, I found it necessary to use a 
__USE_MALLOCed version of the library.  Having followed the
discussions about this topic a while back, I was aware that doing this
is somewhat clumsy and (perhaps) error-prone.  After I had actually
rebuilt the library (and found my bug quite quickly with "mpatrol",
BTW), I decided that it might be useful to have a malloc-using version
of the library "hanging around".  Since "a patch would be
appreciated", here's one, albeit not a perfect one.

I'm no configure wizard, but I think I managed to hack together
a patch that creates a new "--enable-use-malloc=yes" feature.  I
changed __USE_MALLOC to be _GLIBCPP_USE_MALLOC in the process, and
eliminated the "#if defined" check in c++config because I think the
linker will always notice if you mismatch allocation mechanisms
between library and application.

To test this, I wrote a small program (before I renamed the macro):

#include <vector>
#include <iostream>

int
main()
{
  std::vector<int> x;
  
#ifdef __USE_MALLOC
  std::cout << "malloc use" << std::endl;
#else
  std::cout << "not malloc use" << std::endl;
#endif // __USE_MALLOC
  return 0;
}

Then, by compiling it four different ways, it seems like only the
valid combinations will link:

$ g++ -Wall -o foo1 foo.cc                               # Ok
$ g++ -Wall -D__USE_MALLOC -L$malloc_dir -o foo2 foo.cc  # Ok

$ g++ -Wall -D__USE_MALLOC -o foo3 foo.cc                # Errors; good
/tmp/ccHFHnA5.o: In function `std::__simple_alloc<int,
std::__malloc_alloc_template<(int)0> >::deallocate(int*, unsigned)':
/tmp/ccHFHnA5.o(.gnu.linkonce.t._ZNSt14__simple_allocIiSt23__malloc_alloc_templateILi0EEE10deallocateEPij+0x1d):
undefined reference to
`std::__malloc_alloc_template<(int)0>::deallocate(void*, unsigned)'
collect2: ld returned 1 exit status

$ g++ -Wall -L$malloc_dir -o foo4 foo.cc                 # Errors; good
/tmp/ccjemhTF.o: In function `std::__simple_alloc<int,
std::__default_alloc_template<(bool)1, (int)0> >::deallocate(int*,
unsigned)':
/tmp/ccjemhTF.o(.gnu.linkonce.t._ZNSt14__simple_allocIiSt24__default_alloc_templateILb1ELi0EEE10deallocateEPij+0x1d):
undefined reference to `std::__default_alloc_template<(bool)1,
(int)0>::deallocate(void*, unsigned)'
collect2: ld returned 1 exit status

Granted, the diagnostic is far from clear, but it does prevent use.
Perhaps a symbol named "__library_not_built_with_use_malloc" would
help? :)

I plan to use this with my build system to let me do something like

  g++ -D_GLIBCPP_USE_MALLOC -L$prefix/.../lib/debug

by placing a "use-malloc"-built version of the library in the debug
directory inside the compiler's installation.  Someone more familiar
with configure could probably make this happen automatically (by
building and installing the library twice) when --enable-use-malloc
was enabled (or perhaps set to "as-alternate" or something).

Caveat: I've likely patched at least one automatically generated file
:( in the diff I have attached.  Hopefully someone will point out if I
have.  But I did learn how to write a change log :)  This diff is
against the gcc-3.2 release version of libstdc++-v3.

2002-10-08  Brad Spencer <spencer@infointeractive.com>

        * acconfig.h, acinclude.m4, config.h.in, configure.in,
        include/backward/alloc.h, include/bits/c++config,
        include/bits/stl_alloc.h, src/stl-inst.cc: Changed
        __USE_MALLOC to _GLIBCPP_USE_MALLOC and added 
        --enable-use-malloc to configure.
        
-- 
------------------------------------------------------------------
Brad Spencer - spencer@infointeractive.com - "It's quite nice..."
Systems Architect | InfoInterActive Corp. | A Canadian AOL Company

Attachment: gcc-3.2-libstdc++-v3-use-malloc.patch
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]