TD2 II.4 Various fixes on stack allocation

This commit is contained in:
Yorick Barbanneau 2021-11-19 16:52:57 +01:00
parent 3877e32b59
commit c3892a2fa7
4 changed files with 13 additions and 11 deletions

View file

@ -14,7 +14,7 @@ void f(int c) {
int main(){ int main(){
int i; int i;
for (i=65; i < 91; i++){ for (i=65; i < 68; i++){
ThreadCreate(f, i); ThreadCreate(f, i);
} }
// PutString("end of main()\n"); // PutString("end of main()\n");

View file

@ -132,7 +132,7 @@ AddrSpace::AddrSpace (OpenFile * executable)
pageTable[0].valid = FALSE; // Catch NULL dereference pageTable[0].valid = FALSE; // Catch NULL dereference
#ifdef CHANGED #ifdef CHANGED
int bitmapSize =( UserStacksAreaSize / UserStackSize)-1; int bitmapSize =( UserStacksAreaSize / UserStackSize);
DEBUG('x', "Initialise thread counter\n"); DEBUG('x', "Initialise thread counter\n");
threads = 1; threads = 1;
@ -140,7 +140,7 @@ AddrSpace::AddrSpace (OpenFile * executable)
semThreadsCounter = new Semaphore("AddrSpace_thread_counter", 1); semThreadsCounter = new Semaphore("AddrSpace_thread_counter", 1);
semAllocateUserStack = new Semaphore("Stack Avaiable", 1); semAllocateUserStack = new Semaphore("Stack Avaiable", 1);
DEBUG('x', "Initialize memory map size:%d", bitmapSize); DEBUG('x', "Initialize memory map size:%d\n", bitmapSize);
memoryMap = new BitMap(bitmapSize); memoryMap = new BitMap(bitmapSize);
memoryMap->Mark(0); memoryMap->Mark(0);
#endif //CHANGED #endif //CHANGED
@ -329,22 +329,23 @@ AddrSpace::AllocateUserStack()
semAllocateUserStack->P(); semAllocateUserStack->P();
bit = memoryMap->Find(); bit = memoryMap->Find();
semAllocateUserStack->V(); semAllocateUserStack->V();
if ( bit == - 1 ) { if ( bit == - 1 ) {
DEBUG('x', "No slot avaible on User Stack\n"); DEBUG('x', "No slot avaible on User Stack\n");
return -1; return -1;
} }
int addr = memory - UserStackSize * bit; int addr = memory - (UserStackSize * bit);
DEBUG('x', "Allocate User Stack bit %d, addr:%x\n", bit, addr); DEBUG('x', "Allocate User Stack bit %d, addr:0x%x\n", bit, (int)addr);
return addr; return addr;
} }
void void
AddrSpace::DeAllocateUserStack(int addr){ AddrSpace::DeAllocateUserStack(int addr){
int memory = numPages * PageSize; int memory = numPages * PageSize;
int bit = (memory - addr) / UserStackSize; int bit = ((memory - addr) / UserStackSize);
DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr); DEBUG('x', "Deallocate User Stack bit %d, addr:0x%x\n", bit, addr);
semAllocateUserStack->P(); semAllocateUserStack->P();
memoryMap->Clear(bit); memoryMap->Clear(bit);
semAllocateUserStack->P(); semAllocateUserStack->V();
} }
#endif #endif

View file

@ -26,7 +26,7 @@ class Semaphore;
#define UserStackSize 256 #define UserStackSize 256
#endif //CHANGED #endif //CHANGED
#define UserStacksAreaSize 1024 // increase this as necessary! #define UserStacksAreaSize 4096 // increase this as necessary!
class AddrSpace:public dontcopythis class AddrSpace:public dontcopythis
{ {

View file

@ -52,11 +52,12 @@ int do_ThreadCreate(int f, int arg){
args->arg = arg; args->arg = arg;
// Check if we can allocate Stack for our son // Check if we can allocate Stack for our son
int stackAddr = currentThread->space->AllocateUserStack(); args->stackAddr = currentThread->space->AllocateUserStack();
if ( stackAddr == -1 ) { if ( args->stackAddr == -1 ) {
fprintf(stderr, "Segmentation Fault - no space avaible on stack\n"); fprintf(stderr, "Segmentation Fault - no space avaible on stack\n");
Exit(1); Exit(1);
} }
DEBUG('x',"Father found stack address: 0x%x\n", args->stackAddr);
// create a new Thread and start it // create a new Thread and start it
Thread * newThread = new Thread("new thread"); Thread * newThread = new Thread("new thread");