Stack Allocator FAQ. Written by Alex Verstak. License: use at your own risk. ------------------------------ Q1: What is stack allocator? A1: It's the most primitive memory manager I know of. Q2: How does it generally perform? A2: Good locality, no fragmentation, guaranteed speed O(1). Q3: What is it for? A3: It's for managing physical RAM. Q4: Why stack allocator for RAM? A4: It has several advantages: 1. Quick allocation/deallocation. 2. No headers for free or used blocks, only a small fixed-size stack. 3. Convenient block size 4K. 4. Good locality. Q5: Why not use stack allocator for all physical RAM? A5: Because it supports only one block size. Q6: Why not use stack allocator for virtual RAM? A6: See Q5. Q7: What cookies does it have? Q7: There are some: 1. Deallocation at fixed address. 2. It never touches RAM being managed. 3. Therefore, one can easily cut out the regions with no RAM. Q8: Can I have several stack allocators? A8: No. Q9: How long is a page? A9: 4KB by default. You can change it to 8, 16, etc by defining ALLOCATION_GRANULARITY=1, 2, etc. Q10: How dumb-proof is it? A10: Not at all. Only minimal error checking. Q11: How to compile it? A11: I hope, any ANSI C compiler will do. I used GCC 2.8.1. Code uses two include files: assert.h for assert and stdio.h for printf. Q12: How to integrate it in OS? A12: It needs access to several static variables and a tree. Re-entrancy is not allowed. Calling convention is, well, C. The routines won't eat more than 128 bytes of stack. Q13: Example, please!!! A13: #include "stack-a.h" //Suggested Initialization int s=stacka_size(16384*1024); //get tree size for 16MB buddy_init((char*)0x74000,0x1000000,0x1000000); //initialize 16MB starting at 16MB, //use RAM at 0x74000 for stack stacka_free_at(0x1000000,0x800000); //free RAM 16MB-24MB stacka_free_at(0x1c00000,0x400000); //free RAM 28MB-32MB ... #ifdef DEBUG stacka_stat(); //print statistics #endif ... //Suggested Runtime void *f=stacka_alloc(4096); //allocate page for VM stacka_free(f); //...and free it --------------------------- end of stack allocator FAQ