2

I'm working on a PostScript interpreter in C#. PostScript has memory management. It has virtual memory (VM). It also has objects. Two types of objects: simple and composite objects. Composite objects consist of other objects itself.

VM is the storage in which the values of composite objects reside. They can either be in local or global VM.

Windows itself doesn't have a concept anymore of local and global VM (they point to the same) and C# is doing its own memory management, so might get too complex to 'hack' C# and adding interop/C/C++ code.

That is why I'm going to need to 'simulate' the memory allocation/management in C# by creating an own structure of classes etc...

And the question is simple: how to start with creating a basic simple memory allocation structure in C# ?

Is there a scheme or overview how this should look like in its very basic form? Should I create an own Heap structure and create two instances (one for local and one for global) and create an own "pointer" struct or class?

The goal is to implement the memory management/allocation in C# for that interpreter.

Looking forward to learn more from your answers and examples.

  • 1
    The "typical" solution is to identify objects of the implemented language with objects of the host language and let the host language's memory management system deal with memory management. That is, after all, one of the reasons for implementing a language on top of a managed platform such as the CLI, JVM, Parrot, etc., to get access to the managed platform's infrastructure (memory management, execution engine / JIT compiler, …) Are you saying that the memory management semantics of PostScript are so different that mapping them to the CLI's semantics is too complex? – Jörg W Mittag Apr 26 '17 at 8:41
  • I must admit that I am not familiar with PostScript's memory management semantics. I always just blindly assumed that being a stack language, it would allocate everything on the stack, like e.g. Forth does. – Jörg W Mittag Apr 26 '17 at 8:43
  • Well the problem is that values of composite objects should be either allocated on local or global vm according to the current VM allocation mode (global/local). This itself is not a problem if I just assign a boolean property to each object to track if it was created in local or global. The problem comes for example with the "save" and "restore" opeators. From the PostScript Language Reference Manual page 61 (75/912) "The save operator takes a snapshot of the state of local VM and returns a save object that represetns the snapshot." so I need to get the local vm. No idea how to do it in c#. – juFo Apr 26 '17 at 8:58
  • 1
    Would the simplicity of multiple Dictionary objects work? One dictionary would be for the global "memory" and another for the local "memory" which can be cleared and/or discarded when you have a new local context. If necessary, a stack of dictionaries can also handle the changing definition of "local" in a complex document. – Berin Loritsch Apr 26 '17 at 18:54
  • I've been thinking about it but not sure if I will get stuck in the future. Another alternative might be using the C# MemoryMappedFile class, but then I really have to do everything myself (the hard way). Are there any schemes or pictures available how the structure of virtual memory normally works? – juFo Apr 27 '17 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.