Advertisement

Writing a scripting language: memory.

Started by May 24, 2004 09:07 PM
2 comments, last by thedevdan 20 years, 6 months ago
I am writing a scripting language for fun, knowing that it won''t be as fast or useful as Lua/Python/all others. I was going to write in in C#, but I want to make it more cross-platform, so I decided to use C++. I am having trouble with how I am going to manage memory for the script. I was going to just wing it, then I realized that I may as well get some tips from here before wasting lots of time. I am asking about a fairly specific topic: how will I "hand-out" memory? I am thinking of creating a class that hands-out pointers to previously allocated memory, and allocating more if needed. Receivers of the pointers won''t be going past their alotted "block", because their won''t be any pointers in the scripting language, and arrays will be more than adding adresses together, like in C/C++. However, I could really fragment the memory this way, but I will figure that one out. That is assuming this idea is good. Is there a better option than this? What dangers do you see from this idea?
Not giving is not stealing.
Why do you need to hand out memory? Isn''t the concept of memory a little low-level for a scripting language? Usually you might allocate a variable, which in turn is responsible for managing its own memory. My last language simply kept an std::map of names and variables, so when you needed a new variable, it was just entered into that table.

(PS. I moved this to the appropriate forum.)

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Advertisement
In NerveBreak i''ve separated the process in two parts: the script-to-bytecode compiler and the bytecode interpreter (aka VM - Virtual Machine). The compiler keeps a list with used variables and assigns an address to them. Then it saves the biggest address as the first opcode to the bytecode. The VM then allocates the required memory for the script. The VM opcodes are fairly simple such as:

memtoa [address]atomem [address] 


memtoa loads the value at the given address in register A and atomem stores the value of register A in the given address (my VM has two registers, A and B).

For dynamically allocated objects (strings, file handles, etc) i use a simple garbage collector which keeps a list with the allocated objects. Then each xxxx VM opcodes (or when the free memory goes below 2K - NB should be able to run in memory limited systems such as Gameboy) it performs a garbage collection, which actually means that it checks if a pointer referenced in the list exists somewhere in the script''s memory and if not it releases it and removes it''s reference from the list.
--Slashstone - www.slashstone.comNerveBreak free scripting system.
Thank you both for your replies.

Kylotan: I was planning on doing that in C#, but when I decided to use C++, I switched to a low-level mindset. Thanks for setting me back on track.

By the way, I was reffering to reserving memory for variables.

badsector: I think I will just use std::map, but thank you for your input.

Not giving is not stealing.

This topic is closed to new replies.

Advertisement