C++ 标准库和 Boehm 垃圾收集器 [英] C++ standard library and Boehm garbage collector

查看:20
本文介绍了C++ 标准库和 Boehm 垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有 GCC 的 Linux/AMD64/Debian 上开发一个多线程 C++ 应用程序(最终大部分 C++ 代码将由应用程序本身生成,这可以被视为一种高级域特定语言)4.6(可能还有最新的 C++11 标准).

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).

我真的想对我的所有堆使用 Boehm 的保守垃圾收集器分配,因为我想用 new(GC) 分配,而从不关心 delete.我假设 Boehm 的 GC 运行良好.

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. I am assuming that Boehm's GC is working well enough.

使用 C++(而不是 C)的主要动机是 C++ 标准提供的所有算法和集合 std::map ... std::vector图书馆.

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.

Boehm 的 GC 提供了一个 gc_allocator 模板(在其文件 gc/gc_allocator.h 中).

Boehm's GC provide a gc_allocator<T> template (in its file gc/gc_allocator.h).

我应该将 operator ::new 重新定义为 Boehm 的那个吗?

Should I redefine operator ::new as Boehm's one?

或者我应该使用所有集合模板,并将显式分配器模板参数设置为某个 gc_allocator?我不完全理解 std::vector 的第二个模板参数(分配器)的作用?是用来分配vector内部数据,还是用来分配每个单独的元素?

Or should I use all the collection templates with an explicit allocator template argument set to some gc_allocator? I don't understand exactly the role of the second template argument (the allocator) to std::vector? Is it used to allocate the vector internal data, or to allocate each individual element?

std::string-s 怎么样?如何让他们的数据被GC分配?我应该有自己的字符串,使用 basic_string 模板和 gc_allocator 吗?有没有办法获得用 GC_malloc_atomic 而不是 GC_malloc 分配的 char 的内部数组?

And what about std::string-s? How to make their data GC-allocated? Should I have my own string, using basic_string template with gc_allocator? Is there some way to get the internal array-s of char allocated with GC_malloc_atomic not GC_malloc ?

或者您是否建议不要将 Boehm GC 与由 g++ 编译的应用程序一起使用?

Or do you advise not using Boehm GC with an application compiled by g++ ?

问候.

推荐答案

部分回答我自己的问题,代码如下

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; }

当用 g++ -O3 -Wall -c myvec.cc 编译时会产生一个目标文件

when compiled with g++ -O3 -Wall -c myvec.cc produces an object file with

 % 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

因此生成的代码中没有普通的 malloc 或 ::operator new.

So there is no plain malloc or ::operator new in the generated code.

所以通过使用 gc_allocatornew(GC) 我显然可以确定普通的 ::opertor newmalloc 没有在我不知情的情况下使用,我不需要重新定义 ::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

供将来参考(感谢 Sergey Zubkov 在 Quora 评论),另见 n2670 和垃圾收集支持(如 std::declare_reachable, std::declare_no_pointers, std::pointer_safety 等...).但是,至少在当前的 GCC 或 Clang 中还没有实现(除非以微不足道但可接受的方式使其成为无操作).

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...). 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.

这篇关于C++ 标准库和 Boehm 垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆