4

虽然 Alchemy 支持编译 C++,但使用 STL 似乎很麻烦,主要是由于std::string 的问题。奇怪的是,Alchemy 似乎在使用GNU libstd++ v3.4.6。很难相信 std::string 在 GNU 的 STL 中被破坏了。

有没有人想出解决这个问题的任何方法?没有 STL 的 C++ 就像没有水的鱼。

4

1 回答 1

5

问题不在于 STL 本身。的 GNU 实现std::string使用线程安全函数__gnu_cxx::__exchange_and_add__gnu_cxx::__atomic_add. 问题是__exchange_and_add/__atomic_add坏了。

解决方案是通过这些功能的良好实现来重建 STL。

幸运的是,Alchemy 发行版为我们留下了一些面包屑。请参阅$ALCHEMY_HOME/avm2-libc/README,它告诉我们如何做到这一点:

The sources used to build avm2-libstdc++.l.bc can be downloaded here:
  http://download.macromedia.com/pub/labs/alchemy/alchemy_gnucpp3-4library_121008.zip

To build avm2-libstdc++.l.bc:
 cd $ALCHEMY_HOME/avm2-libc
 unzip alchemy_gnucpp3-4library_121008.zip
 mv lib/avm2-libstdc++.l.bc lib/avm2-libstdc++.l.bc.OLD
 make 

You should *not* run achacks prior to using these building this library with make.
The Makefiles provided have been preconfigured to use LLVM directly where needed.

通常我希望在 libstd++ ( $ALCHEMY_HOME/avm2-libc/lib/avm2-libstdc++.l.bc) 中找到 __exchange_and_add/__atomic_add 的实现,但由于某种原因,它们是在 libc ( $ALCHEMY_HOME/avm2-libc/lib/avm2-libc.l.bc) 中定义的。

我不确定为什么会这样,但我们可以通过修改 atomicity.h 来解决这个问题,原型保存在其中。请注意,解压 alchemy_gnucpp3-4library_121008.zip 后,您需要编辑两个atomicity.h 文件:

  • $ALCHEMY_HOME/avm2-libc/include/c++/3.4/bits/atomicity.h
  • $ALCHEMY_HOME/avm2-libc/libstdc++/include/bits/atomicity.h

这是代码:

  /*
   * __exchange_and_add and __atomic_add are broken in Alchemy's libc.
   * Replace them with functioning implementations.  This isn't
   * cross-platform, but this codebase is only compiling for Alchemy anyway.
   */
  #define __exchange_and_add(x,y) __exchange_and_add_fix((x),(y))
  #define __atomic_add(x,y) __exchange_and_add_fix((x),(y))

  /*
   * Correctly implement __exchange_and_add.  It's not thread-safe,
   * but Alchemy isn't threaded, so we should be ok.
   */
  inline _Atomic_word __exchange_and_add_fix(volatile _Atomic_word* __mem, int __val) {
     int orig= *__mem;
     (*__mem)+= __val;
     return orig;
  }

下面是一些要运行的测试代码,以确保重建的 STL 正常工作:

#include <cstdio>
#include <string>
#include <map>
#include <fstream>
using namespace std;

void string_test() {
    string s1;
    string s2;

    s1 = "a";
    s2 = s1; // copy constructor
    s1 = "b";

    // use your favorite TRACE function here
    printf("s1= %s \n", s1.c_str()); // expected: "b", actual: "b"
    printf("s2= %s \n", s2.c_str()); // expected: "a", actual: "b", ERROR
}

void map_test() {
    map<string, int> test_map;

    test_map["test1"]= 1;    
    test_map["test2"]= 2;
    test_map["test3"]= 3;    

    string tmp= "test1";
    printf("%s : %d \n", tmp.c_str(), test_map[tmp]);
}

void ifstream_test()
{
    std::ifstream in("test.txt");

    // ERROR 1:
    // Trying to seek file throws an error:
    // Error #1006: value is not a function.
    // at: basic_filebuf::char_traits::seekoff
    in.seekg(0, std::ios::end);
    int length = in.tellg();
    in.seekg(0, std::ios::beg);
    printf("File Length: %d \n", length);

    while(in.good()) {
        char buffer[512];

        // ERROR 2:
        // RangeError: Error #1125: The index 1092156 is out of range 721.
        // at basic_filebuf::char_traits::underflow::work()
        in.getline(buffer, 512, '\n');

        printf("buffer= %s \n", buffer);
    }
}

int main() {
    string_test();
    map_test();
    ifstream_test();

    return 0;
}

请注意,重建的 STL 似乎修复了一些与mapifstream相关的问题。

于 2011-09-14T19:24:37.997 回答