SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
GC in C++0x

  2010/8/8 GC本読書会

  新 康孝
  yak_ex
自己紹介
 氏名: 新 康孝 (あたらし やすたか)
 Twitter ID: yak_ex
 Web: http://yak3.myhome.cx:8080/junks

 C++ / Perl が主戦場
 某自動車部品メーカーから某自動車メーカーへ出向中
 現在、仕事でコードに触れていない
 競技プログラミング(TopCoder、Codeforces)で
  潤い補充
 GC は「ど素人」
渡る世間は GC ばかり
 Google Code Jam 2010 Qualifier 使用言語
 1位   C++    4911 6位    Ruby      221
 2位   Java   2762 7位    PHP       170
 3位   Python 1459 8位    Perl      146
 4位   C       751 9位    Haskell   118
 5位   C#      648 10位   Pascal     95
渡る世間は GC ばかり
 Google Code Jam 2010 Qualifier 使用言語
 1位   C++    4911 6位    Ruby      221
 2位   Java   2762 7位    PHP       170
 3位   Python 1459 8位    Perl      146
 4位   C       751 9位    Haskell   118
 5位   C#      648 10位   Pascal     95

 黄色ハイライト=GC有り言語
渡る世間は GC ばかり
 Google Code Jam 2010 Qualifier 使用言語
 1位   C++    4911 6位    Ruby      221
 2位   Java   2762 7位    PHP       170
 3位   Python 1459 8位    Perl      146
 4位   C       751 9位    Haskell   118
 5位   C#      648 10位   Pascal     95

 黄色ハイライト=GC有り言語
 超一線級言語でありながら GC 無し : C++は孤高
C++は孤高
 そんな C++ を支える C++er 達
C++は孤高
 そんな C++ を支える C++er 達
     闇
C++は孤高
 そんな C++ を支える C++er 達
     闇

     の
C++は孤高
 そんな C++ を支える C++er 達
     闇

     の

     軍
C++は孤高
 そんな C++ を支える C++er 達
     闇

     の

     軍

     団
C++は孤高
 そんな C++ を支える C++er 達
     闇へ

     の   ん
     軍た

     団い
マルチパラダイム変態言語C++
 次期標準規格 C++ 0x では

          Support for
       Garbage Collection
             and
Reachability-Based Leak Detection
マルチパラダイム変態言語C++
 次期標準規格 C++ 0x では

  Minimal Support for
       Garbage Collection
             and
Reachability-Based Leak Detection
Minimal Support for GC 以下略
 いくつかの概念
  Traceable pointer object
  Safely-derived pointer (value)
 5つの関数
  get_pointer_safety()
  (un)declare_no_pointers()
  (un)declare_reachable()
いくつかの概念(1)
 Traceable pointer object
   ポインタobj
   ポインタを格納するのに十分なサイズの整数obj
   キャラクタ型の配列の一部分
    キャラクタ型だけな理由は恐らく strict aliasing rule
  →ポインタ値が入っているかもしれないと処理系に
   認識してもらえるobject
  =他の部分にはポインタ値が入っていない前提
いくつかの概念(2)
 Safely-derived pointer (value)
   ::operator new で返ってきた値 (例: new T)
   Safely-derived pointer value に対する
     逆参照→参照の結果 (例: &*p)
     ポインタ値の well-defined な演算結果 (例: p+1)
     ポインタ間の well-defined な変換結果
       (例: static_cast<void*>(p))
     ポインタ・整数値間の reinterpret_cast による変換結果
       (例: reinterpret_cast<intptr_t>(p))
  →::operator new で確保された領域で
    かつ有効であることが
    処理系から確実に追跡可能なポインタ値
いくつかの概念(3)
 Safely-derived pointer (value)
                             これは safely-derived pointer value

  T *p = new T;
  intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555
  a:
  T *q = reinterpret_cast<T*>(x ^ 0x555);
  T y = *q;


                       こっちは safely-derived pointer value ではない

                    これも safely-derived pointer value ではない!
                    ※値としては p と同じだが、結果ではなくて過程で判断
Minimal Support for GC 以下略
 いくつかの概念
  Traceable pointer object
  Safely-derived pointer (value)
 5つの関数
  get_pointer_safety()
  (un)declare_no_pointers()
  (un)declare_reachable()
関数: get_pointer_safety()
 処理系のポインタ安全性(pointer safety)を
  返す
                Safely-derived pointer であるかによって
  relaxed:     処理が変わらない=C++03相当
                ※relaxed と preferred は処理系定義
  preferred:     preferred だと leak detector があったりする
                  かもしれない
                ※VC2010 は relaxed 返して他の関数も何もしない


  strict:      safely-derived pointer でないポインタ値
                (かつ後述の declare_reachable() が呼ばれてい
                ない値)を介して動的確保された領域を逆参照、解放
                すると未定義動作
Strict pointer safety
 Safely-derived pointer (value)
                             これは safely-derived pointer value

  T *p = new T;
  intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555
  a:
  T *q = reinterpret_cast<T*>(x ^ 0x555);
  T y = *q;


                       こっちは safely-derived pointer value ではない

                    これも safely-derived pointer value ではない!
   未定義動作
                    ※値としては p と同じだが、結果ではなくて過程で判断
関数: (un)declare_no_pointers()
 void declare_no_pointers(char *p,
  size_t n);
 void undeclare_no_pointers(char *p,
  size_t n);
 指定された領域内にポインタ値がないことを
  指定 or 指定解除する
→GC のスキャン範囲を狭められる
関数: (un)declare_reachable
 void declare_reachable(void *p);
   safely-derived な pointer 値 p の参照先を
    reachable だと宣言する
  →参照先を GC 対象からはずす
 template<class T>
  T* undeclare_reachable(T *p);
   参照先が reachable な p に対して、
    safely-derived な pointer 値(pと同じ値)を返す
  →参照先が GC 対象に復帰する
   (※返値が safely-derived なので有効である間はGCされ
    ない)
(un)declare_reachable()の使い方
 どうして必要なの?                   これは safely-derived pointer value

   T *p = new T;
   intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555
   a:
   T *q = reinterpret_cast<T*>(x ^ 0x555);
   T y = *q;

                        こっちは safely-derived pointer value ではない
    未定義動作            これも safely-derived pointer value ではない!
                     ※値としては p と同じだが、結果ではなくて過程で判断

「ラベル a: の位置で GC がかかると p の参照先が回収される」 ???
(un)declare_reachable()の使い方
 どうして必要なの?                   これは safely-derived pointer value

   T *p = new T;
   intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555
   a:
   T *q = reinterpret_cast<T*>(x ^ 0x555);
   T y = *q;

                        こっちは safely-derived pointer value ではない
    未定義動作            これも safely-derived pointer value ではない!
                     ※値としては p と同じだが、結果ではなくて過程で判断

「ラベル a: の位置で GC がかかると p の参照先が回収される」                 問題は最適化
(un)declare_reachable()の使い方
 どうして必要なの?
   T *p = new T;                                        以降 p の値は
   intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555   使われない
   a:
   T *q = reinterpret_cast<T*>(x ^ 0x555);
   T y = *q;

   ※ p, x, q を同じレジスタに割り当てることが可能                         以降 x の値は
   →ラベル a: の時点で p の値がどこにも存在しない                          使われない
   →p の参照先 = q の参照先が GC の回収対象になる!!
   →*q で未定義動作

「ラベル a: の位置で GC がかかると p の参照先が回収される」                 問題は最適化
(un)declare_reachable()の使い方
     C++0xでの正しいコード
*p は
reachable T *p = new T;
=         declare_reachable(p); // 偽装前に declare_reachable() を呼ぶ
GC対象外 intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555
          a:
          // T z = *reinterpret_cast<T*>(x ^ 0x555); // 合法
          // 偽装終了時に undeclare_reachable() を呼ぶ
          T *q = undeclare_reachable(reinterpret_cast<T*>(x ^ 0x555));
          T y = *q;
注
※ declare_reachable した値と同じであれば safely-derived でなくとも逆参照が可能
※ undeclare_reachable の引数は safely-derived でなくとも reachable であれば良い
※ declare_reachable については「ポインタ値を偽装する」点が問題
   a: で関数が別れている場合を考えれば最適化なしで議論は一緒
まとめ
 C++0x では最小限の GC サポートがある
   Safely-derived pointer という概念
    = GC されていない生きているポインタ値
   5 つの関数が追加
 C++03相当でも規格合致(relaxed)
   しばらくそれ以上の実装はでなさそう?
 ポインタ値を偽装するコードは
  declare_reachable() / undeclare_reachable()
  を使って修正する必要がある
参考文献
 N2670: Minimal Support for Garbage
  Collection and Reachability-Based Leak
  Detection (revised)
  http://www.open-
  std.org/jtc1/sc22/wg21/docs/papers/2008/
  n2670.htm
 Garbage Collection in the Next C++
  Standard
  http://www.hpl.hp.com/techreports/2009/H
  PL-2009-360.pdf

Weitere ähnliche Inhalte

Was ist angesagt?

Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習
JPCERT Coordination Center
 
データサイエンスワールドからC++を眺めてみる
データサイエンスワールドからC++を眺めてみるデータサイエンスワールドからC++を眺めてみる
データサイエンスワールドからC++を眺めてみる
Shintaro Fukushima
 
C++ lecture-2
C++ lecture-2C++ lecture-2
C++ lecture-2
sunaemon
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
fuzzysphere
 

Was ist angesagt? (20)

定理証明支援系Coqについて
定理証明支援系Coqについて定理証明支援系Coqについて
定理証明支援系Coqについて
 
Coqチュートリアル
CoqチュートリアルCoqチュートリアル
Coqチュートリアル
 
Scala 初心者が米田の補題を Scala で考えてみた
Scala 初心者が米田の補題を Scala で考えてみたScala 初心者が米田の補題を Scala で考えてみた
Scala 初心者が米田の補題を Scala で考えてみた
 
Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習Javaセキュアコーディングセミナー東京第2回演習
Javaセキュアコーディングセミナー東京第2回演習
 
Emcpp item31
Emcpp item31Emcpp item31
Emcpp item31
 
Veriloggen: Pythonによるハードウェアメタプログラミング(第3回 高位合成友の会 @ドワンゴ)
Veriloggen: Pythonによるハードウェアメタプログラミング(第3回 高位合成友の会 @ドワンゴ)Veriloggen: Pythonによるハードウェアメタプログラミング(第3回 高位合成友の会 @ドワンゴ)
Veriloggen: Pythonによるハードウェアメタプログラミング(第3回 高位合成友の会 @ドワンゴ)
 
Emcjp item21
Emcjp item21Emcjp item21
Emcjp item21
 
C++入門?
C++入門?C++入門?
C++入門?
 
Pythonを用いた高水準ハードウェア設計環境の検討
Pythonを用いた高水準ハードウェア設計環境の検討Pythonを用いた高水準ハードウェア設計環境の検討
Pythonを用いた高水準ハードウェア設計環境の検討
 
error handling using expected
error handling using expectederror handling using expected
error handling using expected
 
あまぁいRcpp生活
あまぁいRcpp生活あまぁいRcpp生活
あまぁいRcpp生活
 
データサイエンスワールドからC++を眺めてみる
データサイエンスワールドからC++を眺めてみるデータサイエンスワールドからC++を眺めてみる
データサイエンスワールドからC++を眺めてみる
 
PythonとVeriloggenを用いたRTL設計メタプログラミング
PythonとVeriloggenを用いたRTL設計メタプログラミングPythonとVeriloggenを用いたRTL設計メタプログラミング
PythonとVeriloggenを用いたRTL設計メタプログラミング
 
C++ lecture-2
C++ lecture-2C++ lecture-2
C++ lecture-2
 
Effective modern C++ 勉強会 #3 Item 12
Effective modern C++ 勉強会 #3 Item 12Effective modern C++ 勉強会 #3 Item 12
Effective modern C++ 勉強会 #3 Item 12
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 
JavaScript 勉強会 ― 変数・演算子・文
JavaScript 勉強会 ― 変数・演算子・文JavaScript 勉強会 ― 変数・演算子・文
JavaScript 勉強会 ― 変数・演算子・文
 
C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competition
 
Scala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみたScala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみた
 
llvm入門
llvm入門llvm入門
llvm入門
 

Ähnlich wie GC in C++0x

PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
Yosuke Onoue
 
組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
kikairoya
 
T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門
伸男 伊藤
 

Ähnlich wie GC in C++0x (20)

PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
PyOpenCLによるGPGPU入門 Tokyo.SciPy#4 編
 
組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
 
Cython intro prelerease
Cython intro prelereaseCython intro prelerease
Cython intro prelerease
 
NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門
 
C# 9.0 / .NET 5.0
C# 9.0 / .NET 5.0C# 9.0 / .NET 5.0
C# 9.0 / .NET 5.0
 
Python physicalcomputing
Python physicalcomputingPython physicalcomputing
Python physicalcomputing
 
わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61わんくま同盟大阪勉強会#61
わんくま同盟大阪勉強会#61
 
Coq 20100208a
Coq 20100208aCoq 20100208a
Coq 20100208a
 
Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7Pfi Seminar 2010 1 7
Pfi Seminar 2010 1 7
 
Chainer/CuPy v5 and Future (Japanese)
Chainer/CuPy v5 and Future (Japanese)Chainer/CuPy v5 and Future (Japanese)
Chainer/CuPy v5 and Future (Japanese)
 
Visual C++コード分析を支えるSAL
Visual C++コード分析を支えるSALVisual C++コード分析を支えるSAL
Visual C++コード分析を支えるSAL
 
C++0x総復習
C++0x総復習C++0x総復習
C++0x総復習
 
Python standard 2022 Spring
Python standard 2022 SpringPython standard 2022 Spring
Python standard 2022 Spring
 
Chainerの使い方と自然言語処理への応用
Chainerの使い方と自然言語処理への応用Chainerの使い方と自然言語処理への応用
Chainerの使い方と自然言語処理への応用
 
Slide
SlideSlide
Slide
 
T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門T69 c++cli ネイティブライブラリラッピング入門
T69 c++cli ネイティブライブラリラッピング入門
 
マーク&スイープ勉強会
マーク&スイープ勉強会マーク&スイープ勉強会
マーク&スイープ勉強会
 
Introduction to NumPy & SciPy
Introduction to NumPy & SciPyIntroduction to NumPy & SciPy
Introduction to NumPy & SciPy
 
boost - std - C#
boost - std - C#boost - std - C#
boost - std - C#
 
Live Coding で学ぶ C# 7
Live Coding で学ぶ C# 7Live Coding で学ぶ C# 7
Live Coding で学ぶ C# 7
 

Mehr von yak1ex (9)

C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competition
 
Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]
 
Introduction to programming competition [revised]
Introduction to programming competition [revised]Introduction to programming competition [revised]
Introduction to programming competition [revised]
 
Introduction to programming competition
Introduction to programming competitionIntroduction to programming competition
Introduction to programming competition
 
Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]
 
Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]
 
Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICL
 
Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICL
 
GC in C++0x [eng]
GC in C++0x [eng]GC in C++0x [eng]
GC in C++0x [eng]
 

GC in C++0x

  • 1. GC in C++0x 2010/8/8 GC本読書会 新 康孝 yak_ex
  • 2. 自己紹介  氏名: 新 康孝 (あたらし やすたか)  Twitter ID: yak_ex  Web: http://yak3.myhome.cx:8080/junks  C++ / Perl が主戦場  某自動車部品メーカーから某自動車メーカーへ出向中  現在、仕事でコードに触れていない  競技プログラミング(TopCoder、Codeforces)で 潤い補充  GC は「ど素人」
  • 3. 渡る世間は GC ばかり  Google Code Jam 2010 Qualifier 使用言語 1位 C++ 4911 6位 Ruby 221 2位 Java 2762 7位 PHP 170 3位 Python 1459 8位 Perl 146 4位 C 751 9位 Haskell 118 5位 C# 648 10位 Pascal 95
  • 4. 渡る世間は GC ばかり  Google Code Jam 2010 Qualifier 使用言語 1位 C++ 4911 6位 Ruby 221 2位 Java 2762 7位 PHP 170 3位 Python 1459 8位 Perl 146 4位 C 751 9位 Haskell 118 5位 C# 648 10位 Pascal 95  黄色ハイライト=GC有り言語
  • 5. 渡る世間は GC ばかり  Google Code Jam 2010 Qualifier 使用言語 1位 C++ 4911 6位 Ruby 221 2位 Java 2762 7位 PHP 170 3位 Python 1459 8位 Perl 146 4位 C 751 9位 Haskell 118 5位 C# 648 10位 Pascal 95  黄色ハイライト=GC有り言語  超一線級言語でありながら GC 無し : C++は孤高
  • 6. C++は孤高  そんな C++ を支える C++er 達
  • 7. C++は孤高  そんな C++ を支える C++er 達 闇
  • 8. C++は孤高  そんな C++ を支える C++er 達 闇 の
  • 9. C++は孤高  そんな C++ を支える C++er 達 闇 の 軍
  • 10. C++は孤高  そんな C++ を支える C++er 達 闇 の 軍 団
  • 11. C++は孤高  そんな C++ を支える C++er 達 闇へ の ん 軍た 団い
  • 12. マルチパラダイム変態言語C++  次期標準規格 C++ 0x では Support for Garbage Collection and Reachability-Based Leak Detection
  • 13. マルチパラダイム変態言語C++  次期標準規格 C++ 0x では Minimal Support for Garbage Collection and Reachability-Based Leak Detection
  • 14. Minimal Support for GC 以下略  いくつかの概念  Traceable pointer object  Safely-derived pointer (value)  5つの関数  get_pointer_safety()  (un)declare_no_pointers()  (un)declare_reachable()
  • 15. いくつかの概念(1)  Traceable pointer object  ポインタobj  ポインタを格納するのに十分なサイズの整数obj  キャラクタ型の配列の一部分 キャラクタ型だけな理由は恐らく strict aliasing rule →ポインタ値が入っているかもしれないと処理系に 認識してもらえるobject =他の部分にはポインタ値が入っていない前提
  • 16. いくつかの概念(2)  Safely-derived pointer (value)  ::operator new で返ってきた値 (例: new T)  Safely-derived pointer value に対する  逆参照→参照の結果 (例: &*p)  ポインタ値の well-defined な演算結果 (例: p+1)  ポインタ間の well-defined な変換結果 (例: static_cast<void*>(p))  ポインタ・整数値間の reinterpret_cast による変換結果 (例: reinterpret_cast<intptr_t>(p)) →::operator new で確保された領域で かつ有効であることが 処理系から確実に追跡可能なポインタ値
  • 17. いくつかの概念(3)  Safely-derived pointer (value) これは safely-derived pointer value T *p = new T; intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 a: T *q = reinterpret_cast<T*>(x ^ 0x555); T y = *q; こっちは safely-derived pointer value ではない これも safely-derived pointer value ではない! ※値としては p と同じだが、結果ではなくて過程で判断
  • 18. Minimal Support for GC 以下略  いくつかの概念  Traceable pointer object  Safely-derived pointer (value)  5つの関数  get_pointer_safety()  (un)declare_no_pointers()  (un)declare_reachable()
  • 19. 関数: get_pointer_safety()  処理系のポインタ安全性(pointer safety)を 返す Safely-derived pointer であるかによって  relaxed: 処理が変わらない=C++03相当 ※relaxed と preferred は処理系定義  preferred: preferred だと leak detector があったりする かもしれない ※VC2010 は relaxed 返して他の関数も何もしない  strict: safely-derived pointer でないポインタ値 (かつ後述の declare_reachable() が呼ばれてい ない値)を介して動的確保された領域を逆参照、解放 すると未定義動作
  • 20. Strict pointer safety  Safely-derived pointer (value) これは safely-derived pointer value T *p = new T; intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 a: T *q = reinterpret_cast<T*>(x ^ 0x555); T y = *q; こっちは safely-derived pointer value ではない これも safely-derived pointer value ではない! 未定義動作 ※値としては p と同じだが、結果ではなくて過程で判断
  • 21. 関数: (un)declare_no_pointers()  void declare_no_pointers(char *p, size_t n);  void undeclare_no_pointers(char *p, size_t n);  指定された領域内にポインタ値がないことを 指定 or 指定解除する →GC のスキャン範囲を狭められる
  • 22. 関数: (un)declare_reachable  void declare_reachable(void *p);  safely-derived な pointer 値 p の参照先を reachable だと宣言する →参照先を GC 対象からはずす  template<class T> T* undeclare_reachable(T *p);  参照先が reachable な p に対して、 safely-derived な pointer 値(pと同じ値)を返す →参照先が GC 対象に復帰する (※返値が safely-derived なので有効である間はGCされ ない)
  • 23. (un)declare_reachable()の使い方  どうして必要なの? これは safely-derived pointer value T *p = new T; intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 a: T *q = reinterpret_cast<T*>(x ^ 0x555); T y = *q; こっちは safely-derived pointer value ではない 未定義動作 これも safely-derived pointer value ではない! ※値としては p と同じだが、結果ではなくて過程で判断 「ラベル a: の位置で GC がかかると p の参照先が回収される」 ???
  • 24. (un)declare_reachable()の使い方  どうして必要なの? これは safely-derived pointer value T *p = new T; intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 a: T *q = reinterpret_cast<T*>(x ^ 0x555); T y = *q; こっちは safely-derived pointer value ではない 未定義動作 これも safely-derived pointer value ではない! ※値としては p と同じだが、結果ではなくて過程で判断 「ラベル a: の位置で GC がかかると p の参照先が回収される」 問題は最適化
  • 25. (un)declare_reachable()の使い方  どうして必要なの? T *p = new T; 以降 p の値は intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 使われない a: T *q = reinterpret_cast<T*>(x ^ 0x555); T y = *q; ※ p, x, q を同じレジスタに割り当てることが可能 以降 x の値は →ラベル a: の時点で p の値がどこにも存在しない 使われない →p の参照先 = q の参照先が GC の回収対象になる!! →*q で未定義動作 「ラベル a: の位置で GC がかかると p の参照先が回収される」 問題は最適化
  • 26. (un)declare_reachable()の使い方  C++0xでの正しいコード *p は reachable T *p = new T; = declare_reachable(p); // 偽装前に declare_reachable() を呼ぶ GC対象外 intptr_t x = reinterpret_cast<intptr_t>(p) ^ 0x555 a: // T z = *reinterpret_cast<T*>(x ^ 0x555); // 合法 // 偽装終了時に undeclare_reachable() を呼ぶ T *q = undeclare_reachable(reinterpret_cast<T*>(x ^ 0x555)); T y = *q; 注 ※ declare_reachable した値と同じであれば safely-derived でなくとも逆参照が可能 ※ undeclare_reachable の引数は safely-derived でなくとも reachable であれば良い ※ declare_reachable については「ポインタ値を偽装する」点が問題 a: で関数が別れている場合を考えれば最適化なしで議論は一緒
  • 27. まとめ  C++0x では最小限の GC サポートがある  Safely-derived pointer という概念 = GC されていない生きているポインタ値  5 つの関数が追加  C++03相当でも規格合致(relaxed)  しばらくそれ以上の実装はでなさそう?  ポインタ値を偽装するコードは declare_reachable() / undeclare_reachable() を使って修正する必要がある
  • 28. 参考文献  N2670: Minimal Support for Garbage Collection and Reachability-Based Leak Detection (revised) http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2008/ n2670.htm  Garbage Collection in the Next C++ Standard http://www.hpl.hp.com/techreports/2009/H PL-2009-360.pdf