Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OleKaiwalker

Memory allocator??

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!