Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory allocator??

Started by OleKaiwalker Jun 2, 2004 at 4:42 PM 1 replies 300+ views
Original Post
OleKaiwalker
OleKaiwalker
Hey, I''m working on a memory allocator, but I''d run into some serious problems The allocator isn''t completed yet. The Malloc function is quite defective, but it will do.
class MallocWindow {

public:
	MallocWindow() {

		mem = (MallocBlock*)malloc(POOL_MAX);
		mem->blocks = (POOL_MAX >> 3); mem->free = true; mem->next = NULL;
		
		firstMem = mem;
	};

	~MallocWindow(){

		free(mem);
	}

	void *Malloc(unsigned int capacity){

		// To get the number of blocks required for the request, we

		// round the capacity up to the nearest multiple of 8, then

		// divide it by 8 and add a 8 bytes buffer for the header.

		unsigned int blocks = (((capacity + 7) & ~7) >> 3) + 1;
		MallocBlock *currBlock = firstMem;

		// Loop through all the blocks to see if there''s any free blocks

		// with enough capacity.

		while (currBlock != NULL){

			if (currBlock->free)
				if (currBlock->blocks >= blocks)
					break;

			currBlock = currBlock->next;
		}

		// When a block is found, it''s time to determine whether it

		// should be spilt or not. If the allocation will leave more

		// than 5 (40 bytes) unused blocks behind, the block is big

		// enough for splitting.

		if (currBlock->blocks - blocks > 5){

			MallocBlock *nextBlock = (currBlock + blocks);
			
			nextBlock->blocks = currBlock->blocks - blocks;
			nextBlock->free = true; nextBlock->next = currBlock->next;

			currBlock->blocks = blocks; currBlock->free = false;
			currBlock->next = nextBlock;
		}

		// If the block was to small, we just use the entire one.

		else
			currBlock->free = false;

		return (currBlock + 1);
	}

	void Free(void *ptr){

		MallocBlock *currBlock = ((MallocBlock*)ptr) - 1;

		currBlock->free = true;

		currBlock = firstMem;

		while (currBlock != NULL){

			if (currBlock->free)
				if (currBlock->next->free){
			
					currBlock->blocks = currBlock->blocks + currBlock->next->blocks;
					currBlock->next = currBlock->next->next;
				}

			currBlock = currBlock->next;
		}
	}

	void DumpAllocs(){

		MallocBlock *iterator = firstMem;
		ofstream log("Memory Log.txt", std::ios::trunc | std::ios::out);
		char buffer[1024]; unsigned int totalAllocs = 0;
		
		sprintf(buffer, "Memory pool: %d bytes -> %d blocks\n", POOL_MAX, POOL_MAX >> 3);
		log << buffer;
		sprintf(buffer, "------------------------------------------\n");
		log << buffer;

		while (iterator != NULL){

			sprintf(buffer, "Block #%d: %d blocks; free: %d\n",
					++totalAllocs, iterator->blocks, iterator->free);
			log << buffer;

			iterator = iterator->next;
		}

		log.close();
	}

private:

	// Information about a block of memory. 8 bytes.

	struct MallocBlock {

		unsigned short blocks, free;
		MallocBlock *next;
	};

	enum {POOL_MAX = 32768};
	
	MallocBlock *mem, *firstMem;
};


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, PSTR cmdLine, int show){

	////////// *** Detection Memory Leaks *** //////////

	#ifndef NDEBUG
	int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	flag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(flag);
	#endif
	////////// *** Detection Memory Leaks *** //////////


	MallocWindow m;
	char *pBytes = (char*)m.Malloc(100);
	pBytes = "hej";
	int *pInts = (int*)m.Malloc(100);
	pInts[0] = 1;
	double *pDouble = (double*)m.Malloc(512);
	m.Free(pBytes);

	m.DumpAllocs();

	MSG msg;
	AppWindow appWindow;

	ASSERT(appWindow.InitWindow(), "The Window Wrapper Could Not Initialize The Window");

	while (true){

		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){

			if (msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		else {

		}
	}

	appWindow.CloseWindow();
	ErrorLog::WriteOutputFile();

	return msg.wParam;
}
It''s the freeing function, which is caursing my pain. It works when I don''t use the pointer (*pBytes), but intantly when I uncooment the line "pBytes = "hej";" it breaks.?? What could be wrong?
Palidine
Palidine
i haven't read all your code but you can't do this:

char *pBytes = (char*)m.Malloc(100);
pBytes = "hej";



you have to do something like:

char *pBytes = (char*)m.Malloc(100);

//either this

sprintf(pBytes, "hej");

//or this

strcpy(pBytes, "hej");

//or some other way, perhaps using memset or something else



you can only set a char using = "string" during initialization:

//ok
char *myString = "myString";

-me

[edited by - Palidine on June 2, 2004 5:47:55 PM]
OleKaiwalker
OleKaiwalker
You''r right, but that wasn''t quite the problem. It seems like the pointer are giving error without being "used" before freed to. I really can''t see the problem?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.