虽然Alchemy支持编译c++,但使用STL似乎很麻烦,主要是由于std::string的问题。奇怪的是,Alchemy似乎正在使用GNU libstd++ v3.4.6。很难相信std::string在GNU的STL中被破坏了。
有没有人想出解决这个问题的办法?没有STL的c++就像没有水的鱼。
问题不在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
)中定义。
我不确定为什么会出现这种情况,但是我们可以通过破解保存原型的atom .h来解决这个问题。注意,在解压缩alchemy_gnucpp3-4library_121008.zip之后,您需要编辑两个原子性.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似乎修复了map和ifstream的一些相关问题。