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

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

View file

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

View file

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