简陋的内存管理单元

最近翻开了在学校曾经学过的ssd7 系统级编程的课件和实验材料。

用单向链表实现了一个丑陋的内存管理单元。

 

太tmd丑陋了

#include <stdlib.h>
#include <string.h>
#include "debugmalloc.h"
#include "dmhelper.h"
#include <stdio.h>

#define BARRIER 0xCCDEADCC

struct memory
{
	int size; //the size of the memory
	int checkCode;
	int lineNo;
	char *filename;
	struct memory *next;
};
struct memory *p = NULL;     //the link list to save the memory information

struct memory * deleteNode(struct memory * head, struct memory * node)
{
	struct memory * before = NULL;
	struct memory * temp = NULL;
	struct memory * deleted = NULL;
	before = head;

	if(head != NULL)
	{
		while(before->next != NULL && before->next != node)
		{
			before = before->next;
		}
		if( before->next != NULL)
		{
			printf("delete lineNo %d\n", node->lineNo);
			temp = before->next->next;
			deleted = before->next;
			before->next = temp;
			free(deleted);
		}
	}
	return head;
}

struct memory * addNode(struct memory * head, int size, int checkCode, int lineNo, char * filename)
{
	struct memory * next = NULL;
	struct memory * node = (struct memory *)malloc(sizeof(struct memory));

	if (NULL == head)
	{
		head = (struct memory *)malloc(sizeof(struct memory));
		head->size = 0;
		head->checkCode = 0;
		head->lineNo = 0;
		head->filename = NULL;
		head->next = NULL;
	}
	next = head;

		
	while(NULL != next->next)
	{
		next = next->next;
	}

	node->size = size;
	node->checkCode = checkCode;
	node->lineNo = lineNo;
	node->filename = filename;
	node->next = NULL;

	next->next = node;
	p = head;
	return next->next;
}



/* Wrappers for malloc and free */

/* code/info  /size /barrier/place /barrier */
/* int /struct memory * /int  /int	/place /int */

void *MyMalloc(size_t size, char *filename, int linenumber) 
{
	int totalSize = size + 4*sizeof(int) + sizeof(struct memory *);
	
	void * head;

	int * intHead;

	void * ret = NULL;
	
	int checkCode = 11;

	struct memory * node = NULL;

	char * info = malloc(sizeof(char) * (strlen(filename + linenumber) +1));

	strcpy(info, filename + linenumber);

	node = addNode(p, size, checkCode, linenumber,info);

	head = (void *)(malloc(totalSize));
	
	intHead = (int *)head;
	
	*intHead = checkCode;

	intHead ++;

	*intHead = node;

	intHead ++;

	*intHead = size;

	intHead++;

	*intHead = BARRIER;
	intHead++;

	head = (void *)(intHead);
	ret = head;

	head = (char *)head + size;

	intHead = (int *)head;

	*intHead = BARRIER;

	return ret;
}

void MyFree(void *ptr, char *filename, int linenumber)
 {
	char * head = NULL;
	int * intPtr = NULL;
	int * barrierRight = NULL;
	struct memory * node = NULL;
	int size = 0;

	intPtr = ((int *)ptr - 1);
	if(* intPtr != BARRIER)
	{
		printf("left barrier overflow\n");
		return;
	}
	
	intPtr--;
	size = * intPtr;

	intPtr--;

	node = *intPtr;

	intPtr = (char *)ptr + size;
	if(* intPtr != BARRIER)
	{
		printf("right barrier overflow\n");
		return;
	}	

	head = ((char *)ptr - 3*sizeof(int)) - sizeof(struct memory *) ;
	free((void *)head);	
	deleteNode(p, node);
}

/* returns number of bytes allocated using MyMalloc/MyFree:
	used as a debugging tool to test for memory leaks */
int AllocatedSize()
{
	int size = 0;
	struct memory * node;
	node = p;
	while(node != NULL)
	{
		size += node->size;
		node = node->next;
	}
	return size;
}



/* Optional functions */

/* Prints a list of all allocated blocks with the
	filename/line number when they were MALLOC'd */
void PrintAllocatedBlocks() {
	struct memory *node = p;
	while (node != NULL)
	{
		printf("Allocated:%d\nfilename:%s\nlineNo:%d\nsize:%d\n",
			node->checkCode, node->filename, node->lineNo, node->size);
		node = node->next;
	}
	return;
}

/* Goes through the currently allocated blocks and checks
	to see if they are all valid.
	Returns -1 if it receives an error, 0 if all blocks are
	okay.
*/
int HeapCheck() {
	return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值