I've just spent ~20 mins hacking together this function as part of my VM. It does it's job ok so far (I haven't extensively tested it yet) but it is very, very badly done - I get the feeling I've overdone it. Could somebody help me clean it up?
NOTE: I just added lim in as I was posting this because I realised it actually wasn't returning 0 if it couldn't find a hunk big enough. Yet to test it so slap me if it's not going to work.Code:// FindEmptyRAMHunk // // Searches in RAM for a hunk of free space // big enough to hold data of size (sz). // // Returns: A pointer to the start of the // free space, 0 if enough space // was not available // byte* CVirtualMachine::FindEmptyRAMHunk(size_t sz) { byte* search = &RAM[0]; byte* lookAhead = 0; byte* grabbed = new byte[sz]; bool found = false; unsigned lim = 0; // Find first free cell if (*search != 0xFF) while (*search != 0xFF) search++; lookAhead = search; while (! found) { reGrab: if (lim++ == ByteRAM) return 0; // Grab a chunk of RAM of size sz to examine for (size_t i = 0; i < sz; i++) *(grabbed + i) = ++*lookAhead; // Check grabbed chunk for (i = 0; i < sz; i++) if (*(grabbed + i) != 0xFF) goto reGrab; found = true; } delete[] grabbed; return lookAhead; }