虽然Alchemy支持编译C ++,但使用STL似乎很麻烦,主要是因为problem with std::string。奇怪的是,Alchemy似乎正在使用GNU libstd++ v3.4.6。很难相信在GNU的STL中std :: string被破坏了。
有没有人想出这个问题的解决方法?没有STL的C ++就像没有水的鱼。
答案 0 :(得分:5)
问题不在STL中。 std::string
的GNU实现使用线程安全函数__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
)中定义的。
我不确定为什么会出现这种情况,但是我们可以通过破解原型来保存原型。请注意,在解压缩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;
}