Thread: Custom Memory Allocation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    48

    Custom Memory Allocation

    I recently discovered that dynamic memory allocation is pretty slow, so I've been thinking of a way to get around that. I've been thinking about going into low level video game engine programming after college/uni (studying Maths/Physics in college, then Computer Science at Uni), and it occurs to me that dynamic memory allocation is a fairly important thing in the engine and therefore, if it's slow, it's a potential bottleneck.

    I imagine one of the problems with dynamic memory allocation is that you have to ask the OS for the memory, then wait for it to find some and give it to you (please feel free to correct me if I'm mistaken). So I came up with the idea of allocating the memory to a buffer during initialisation, then I can use my own custom methods to manage that memory throughout the application. Since I have already allocated the memory, I'm simply passing around addresses from my buffer rather than asking the OS for more memory. I can't imagine this is something new, though, so I'd appreciate it if anyone can give me some advice, show me some material to look at, or point out any problems with this idea (e.g. is there actually a significant performance boost using this method?).

    This is something I quickly threw together as an example, it is by no means supposed to be a real world implementation:

    Code:
    #include <iostream>
    
    // Allocate 1GiB of memory
    char* g_buffer = new char[1024 * 1024 * 1024];
    
    typedef struct
    {
    	float x;
    	float y;
    	float z;
    	float w;
    } test;
    
    // Allocates a block of memory depending on the size requested
    void* AssignMemory(int size)
    {
    	static int offset = 0;
    	offset += size;
    	return (void*)&g_buffer[offset-size];
    }
    
    int main()
    {
    	int* p = (int*)AssignMemory(sizeof(int));
    	double* p2 = (double*)AssignMemory(sizeof(double));
    	float* p3 = (float*)AssignMemory(sizeof(float));
    
    	short* p4 = (short*)AssignMemory(sizeof(short));
    	test* p5 = (test*)AssignMemory(sizeof(test));
    
    	*p = 20;
    	*p2 = 33.23;
    	*p3 = 2.2f;
    	*p4 = 223;
    	
    	p5->x = 2.0f;
    	p5->y = 3.0f;
    	p5->z = 4.0f;
    	p5->w = 5.0f;
    
    	std::cout << "p: " << p << "\t*p: " << *p << std::endl;
    	std::cout << "p2: " << p2 << "\t*p2: " << *p2 << std::endl;
    	std::cout << "p3: " << p3 << "\t*p3: " << *p3 << std::endl;
    	std::cout << "p4: " << p4 << "\t*p4: " << *p4 << std::endl;
    	
    	std::cout << "p5: " << p5 << std::endl;
    	std::cout << "p5->x: " << p5->x << "\tp5->y: " << p5->y << std::endl;
    	std::cout << "p5->z: " << p5->z << "\tp5->w: " << p5->w << std::endl;
    
    	std::cin.get();
    
    	delete [] g_buffer;
    	g_buffer = 0;
    	p = 0;
    	p2 = 0;
    	p3 = 0;
    	p4 = 0;
    	p5 = 0;
    
    	return 0;
    }
    So far, this all seems to work with no apparent issues (each pointer contains the expected address (relative to the first address), and each value is correct).

    EDIT: Arrays are actually just like using malloc(...) in C:

    Code:
    int main()
    {	
    	// ...
    
    	int arrSize = 10;
    	int* arr = (int*)AssignMemory(sizeof(int) * arrSize);
    		
    	for(int i = 0; i < arrSize; ++i)
    		arr[i] = i;
    	
    	for(int i = 0; i < arrSize; ++i)
    		std::cout << "arr[" << i << "]: " << arr[i] << std::endl;
    
    	// ...
    }
    Last edited by Ushakal; 08-22-2011 at 10:35 AM. Reason: Added array example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  2. Memory allocation
    By kernelio in forum C Programming
    Replies: 6
    Last Post: 10-11-2007, 10:19 AM
  3. Custom Allocation
    By SevenThunders in forum C Programming
    Replies: 17
    Last Post: 04-09-2007, 04:10 PM
  4. memory allocation
    By mbooka in forum C Programming
    Replies: 3
    Last Post: 02-28-2006, 03:13 PM
  5. Memory Allocation
    By Marcelo in forum C Programming
    Replies: 1
    Last Post: 10-26-2001, 08:43 AM