Question

I'm writing interpreted language on windows, and I use PAGE_GUARD to implement stack and HeapCreate / HeapAlloc for dynamic allocation of my language.

Maybe I'll need to port my lang to other OS.. So, In linux (or POSIX standard..), What is similar to these win32 api? (I hope they is not very different to use..)


Ok, see below if you don't know these win32 APIs:

HeapCreate - simple. Create a new heap:

void *mem = malloc(123); // alloc from default heap
HANDLE hHeap = HeapCreate(...); // create a new heap
void *mem2 = HeapAlloc(hHeap, some_flag, 123); // alloc from new heap

PAGE_GUARD - bit complex; it is used to implement stack. For example, there is a stack, whose max size is 5-pages. For saving memory, I'll alloc only one page and just "reserve" virtual memory address of 4 pages.

---------
| alloc |
---------
|reserve|
---------
|reserve|
---------
|reserve|
---------
|reserve|
---------

When first page of stack is used entirely, and program is about to use more stack, access violation occur. Then, I "commit" second page and continue program.

PAGE_GUARD is just helper to do this. (In win95, there isn't page guard so win95 perform without this helper) If I commit and mark the second page "guarded" ahead-of-time, and program use more stack, then GUARD-exception occur and OS unmark the page automatically. I have to only commit and mark the next page.

Was it helpful?

Solution

Read Advanced Linux Programming. Don't seek an exact equivalent in Linux for each functionality of Win32 that you know or want. Learn to natively think in Linux terms. Study free software similar to yours (see freecode or sourceforge to find some).

And yes, Posix or Linux vs Windows is very different, notably for their notion of processes, etc...

You probably want mmap(2) and mprotect(2); I don't know at all Windows (so I have no idea of what HeapCreate does).

Maybe using the lower layer of cross-platform toolkits like Qt (i.e. QtCore...) or Glib (from Gtk ...) might help you.

Linux C standard library is often GNU libc (but you could use some other, e.g. MUSL libc, which is very readable IMHO). It use syscalls listed in syscalls(2) and implemented by the Linux kernel (in particular, malloc(3) is generally built above mmap(2)...).

Take the habit of studying the source code of free software if that helps you.

BTW, for an interpreter, you could consider using Boehm's conservative garbage collector...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top