简体   繁体   English

C ++标准库和Boehm垃圾收集器

[英]C++ standard library and Boehm garbage collector

I want to develop a multi-threaded C++ application (where eventually most of the C++ code would become generated by the application itself, which could be viewed as a high-level domain specific language) on Linux/AMD64/Debian with GCC 4.6 (and probably latest C++11 standard). 我想开发一个多线程C ++应用程序(最终大部分C ++代码将由应用程序本身生成,可以被视为高级域特定语言)在Linux / AMD64 / Debian上使用GCC 4.6(和可能是最新的C ++ 11标准)。

I really want to use Boehm's conservative garbage collector for all my heap allocations, because I want to allocate with new(GC) and never bother about delete . 我真的想在我的所有堆分配中使用Boehm的保守垃圾收集器 ,因为我想用new(GC)分配并且从不打扰delete I am assuming that Boehm's GC is working well enough. 我假设Boehm的GC工作得很好。

The main motivation for using C++ (instead of C) is all the algorithms and collections std::map ... std::vector provided by the C++ standard library. 使用C ++(而不是C)的主要动机是由C ++标准库提供的所有算法和集合std::map ... std::vector

Boehm's GC provide a gc_allocator<T> template (in its file gc/gc_allocator.h). Boehm的GC提供了一个gc_allocator<T>模板(在其文件gc / gc_allocator.h中)。

Should I redefine operator ::new as Boehm's one? 我应该重新定义operator ::new作为Boehm的那个吗?

Or should I use all the collection templates with an explicit allocator template argument set to some gc_allocator ? 或者我应该使用所有集合模板,并将显式的allocator模板参数设置为某些gc_allocator吗? I don't understand exactly the role of the second template argument (the allocator) to std::vector ? 我不完全理解第二个模板参数(分配器)对std :: vector的作用 Is it used to allocate the vector internal data, or to allocate each individual element? 它是用来分配矢量内部数据,还是分配每个单独的元素?

And what about std::string -s? 那么std::string -s呢? How to make their data GC-allocated? 如何使他们的数据GC分配? Should I have my own string, using basic_string template with gc_allocator ? 我应该使用带有gc_allocator basic_string模板吗? Is there some way to get the internal array-s of char allocated with GC_malloc_atomic not GC_malloc ? 有没有办法获得用GC_malloc_atomic而不是GC_malloc分配的内部数组char?

Or do you advise not using Boehm GC with an application compiled by g++ ? 或者您是否建议不要将Boehm GC与g ++编译的应用程序一起使用?

Regards. 问候。

To answer partly my own question, the following code 部分回答我自己的问题,以下代码

// file myvec.cc
#include <gc/gc.h>
#include <gc/gc_cpp.h>
#include <gc/gc_allocator.h>
#include <vector>

class Myvec {
  std::vector<int,gc_allocator<int> > _vec;
public:
  Myvec(size_t sz=0) : _vec(sz) {};
  Myvec(const Myvec& v) : _vec(v._vec) {};
  const Myvec& operator=(const Myvec &rhs) 
    { if (this != &rhs) _vec = rhs._vec; return *this; };
  void resize (size_t sz=0) { _vec.resize(sz); };
  int& operator [] (size_t ix) { return _vec[ix];};
  const int& operator [] (size_t ix) const { return _vec[ix]; };
  ~Myvec () {};
};

extern "C" Myvec* myvec_make(size_t sz=0) { return new(GC) Myvec(sz); }
extern "C" void myvec_resize(Myvec*vec, size_t sz) { vec->resize(sz); }
extern "C" int myvec_get(Myvec*vec, size_t ix) { return (*vec)[ix]; }
extern "C" void myvec_put(Myvec*vec, size_t ix, int v) { (*vec)[ix] = v; }

when compiled with g++ -O3 -Wall -c myvec.cc produces an object file with 当使用g++ -O3 -Wall -c myvec.cc编译时, g++ -O3 -Wall -c myvec.cc会生成一个目标文件

 % nm -C myvec.o
                 U GC_free
                 U GC_malloc
                 U GC_malloc_atomic
                 U _Unwind_Resume
0000000000000000 W std::vector<int, gc_allocator<int> >::_M_fill_insert(__gnu_cxx::__normal_iterator<int*, std::vector<int, gc_allocator<int> > >, unsigned long, int const&)
                 U std::__throw_length_error(char const*)
                 U __gxx_personality_v0
                 U memmove
00000000000000b0 T myvec_get
0000000000000000 T myvec_make
00000000000000c0 T myvec_put
00000000000000d0 T myvec_resize

So there is no plain malloc or ::operator new in the generated code. 因此生成的代码中没有普通的malloc或::operator new

So by using gc_allocator and new(GC) I apparently can be sure that plain ::opertor new or malloc is not used without my knowledge, and I don't need to redefine ::operator new 所以通过使用gc_allocatornew(GC)我显然可以确定在我不知情的情况下不使用plain ::opertor newmalloc ,而且我不需要重新定义::operator new


addenda (january 2017) 附录(2017年1月)

For future reference (thanks to Sergey Zubkov for mentioning it on Quora in a comment), see also n2670 and <memory> and garbage collection support (like std::declare_reachable , std::declare_no_pointers , std::pointer_safety etc...). 供将来参考(感谢Sergey Zubkov在评论中提到Quora ),另请参阅n2670<memory>以及垃圾收集支持 (如std :: declare_reachablestd :: declare_no_pointersstd :: pointer_safety等...) 。 However, that has not been implemented (except in the trivial but acceptable way of making it a no-op) in current GCC or Clang at least. 然而,至少在目前的GCC或Clang中,尚未实施(除了以微不足道但可接受的方式使其成为无操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM