Thread: Need to bring this function up to scratch

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Need to bring this function up to scratch

    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?

    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;
    }
    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.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ...why not just try to allocate it and let the OS tell you if there isn't enough memory?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To get rid of the goto and actually use the while loop you could put found = true above the "Check grabbed chunk" for loop and then replace goto reGrab; with
    Code:
    {
      found = false;
      break;
    }

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by major_small
    ...why not just try to allocate it and let the OS tell you if there isn't enough memory?
    I'm not sure what you mean.
    I'm working on a Virtual Machine, so...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Maybe kind of like this

    Code:
    #include <iostream>
    char * allocate(int size)
    {
    	return new char[size];
    }
    
    int main()
    {
    	char * test = allocate(6);
    	test = "sweet";
    	std::cout << test;
    }

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by MadCow257
    Maybe kind of like this

    Code:
    #include <iostream>
    char * allocate(int size)
    {
    	return new char[size];
    }
    
    int main()
    {
    	char * test = allocate(6);
    	test = "sweet";
    	std::cout << test;
    }
    that's pretty much what I had in mind
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	char * test = allocate(6);
    	test = "sweet";
    	std::cout << test;
    I'm a ..........

    Code:
    	char * test = allocate(6);
    	strcpy(test, "sweet");
    	std::cout << test;
    	delete[] test;

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think this is what you are trying to do. Although as mentioned, writing a virtual maching does not strictly require you to write your own memory allocator. This is also a very inefficient allocator. You might wish to do a search for 'memory allocation algorithm'.
    Code:
    byte* CVirtualMachine::FindEmptyRAMHunk(size_t sz)
    {
        size_t start         = 0;
        size_t count         = 0;
        const byte FREE_BYTE = 0xFF;
    
        while (i < RAM_SIZE)
        {
            if (RAM[i] == FREE_BYTE)
            {
                if (++count == sz)
                {
                    return &RAM[start];
                }
            }
            else
            {
                count = 0;
                start = i + 1;
            }
    
            i++;
        }
    
        return NULL; /* Large enough block not found or sz is zero. */
    }

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yeah I thought it wasn't very effective, I've got no experience writing this sort of thing (VM's) so I just wanted to do it for good practice.

    I'll do some searching later on when the caffeine kicks in
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM