Well, I've been thinking further about implementations. I want to design a language which can be implemented on the CLR and JVM, as well as native code. Now, all the metaprogramming goodness can be achieved by icky name mangling and is platform independent.
Now, I've been thinking about implementing pointers as integers. Consider the following simple pseudo-Csharp:
class memory {
static ArrayList<Object> heap;
static int Create<T>() {
lock(heap) {
heap.Add(new T());
return heap.size() - 1;
}
}
}
Obviously this will need to become more complex to actually fulfill the requirements. In any case, this handily converts from a GC reference to a pointer-style type. The integer is POD and can be memory-copied around, it needs to be manually removed, and the GC won't collect the object. In a theoretical implementation of a C++-like language on the CLR, then the return value would serve as a "pointer".
The problem is when you start wanting to insert values into memory yourself. For example, if I were to attempt to emulate a class like this:
class int_or_string {
bool is_int;
memory buffer[max(sizeof(int), sizeof(string))];
public:
int_or_string() {
is_int = false;
string* ptr = new (buffer) string();
}
// etc
};
For POD types, then there are BinaryWriter classes and such available. As such, I can probably achieve a reasonably close implementation. In addition, all classes that don't have GC references in them are, arguably, implementable by binary writing them into the memory, since emulated pointers are binary writable. As all arrays must be allocated off the GC, it should be relatively simple to implement the "address" returned from placement new as, again, an index into an array of GC references.
The problem comes when I want to put a GC reference there. The GC won't exactly follow my not-exactly-pointers to find the reference.
I've been thinking about just ignoring the "placement" part of the placement new. Just allocate the supposedly placed object off the GC. The problem with this comes when you want to start placing objects into other kinds of memory- for example, how would you place objects into memory provided by external libraries?
What I really need is to differentiate between an object placed into memory which will always have a strong pointer pointing to it, and an object which is binary copied into memory. For objects which are always strong pointed to, I can just ignore the placement part and allocate off the GC. For other objects, well, you can't put GC references in there anyway.
This mandates two ways of writing into memory. So far, I've been thinking about a relatively simple kind of buffer = object; for the second kind. I can guarantee at compile-time that object is not a type which contains GC references. For objects which contain GC references, I would require something like placement new, where you would have to maintain a strongly-typed pointer to the object (including through inheritance, or as part of an array) at all times or risk UB.
No comments:
Post a Comment