虽然Alchemy支持编译C++,但使用STL似乎很麻烦,主要是因为std :: string存在问题.奇怪的是Alchemy似乎正在使用GNU libstd ++ v3.4.6.很难相信std :: string在GNU的STL中被破坏了.
有没有人想出这个问题的解决方法?没有STL的C++就像没有水的鱼.
问题不在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.
Run Code Online (Sandbox Code Playgroud)
通常我希望在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;
}
Run Code Online (Sandbox Code Playgroud)
这是一些运行的测试代码,以确保重建的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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,重建的STL似乎解决了map和ifstream的一些相关问题.
| 归档时间: |
|
| 查看次数: |
960 次 |
| 最近记录: |