📄 debugmalloc.c
字号:
#include <stdlib.h>
#include <string.h>
#include "debugmalloc.h"
#include "dmhelper.h"
#include <stdio.h>
#define ALIGN 8
#define THEFENCE 0xDEADBEEF
#define HEADERSIZE sizeof(struct header)
/* currently 12 bytes */
struct header {
int checksum;
size_t size; /* 4 bytes
size does not include header and footer */
int fence;
};
#define FOOTERSIZE sizeof(struct footer)
/* currently 4 bytes */
struct footer {
int fence;
};
/* Global indicating # bytes allocated */
int allocatedSize = 0;
/* Taken from 15-213 Lab 1 */
int bitCount(int x) {
int n=0;
int i=0;
for(i=0;i<32;i++)
{
if((x&(0x1<<i))!=0) n++;
}
return n;
}
/* Wrappers for malloc and free */
void *MyMalloc(size_t size, char *filename, int linenumber) {
char *headptr, *footptr;
struct header h;
struct footer f;
int amount;
int temp;
h.fence = THEFENCE;
f.fence = THEFENCE;
h.size = size;
h.checksum = bitCount(size) + bitCount(h.fence);
/* round up size */
size += ALIGN - 1;
size -= size % ALIGN;
headptr = malloc(size + HEADERSIZE + FOOTERSIZE);
if (!headptr)
return NULL;
footptr = headptr + HEADERSIZE + size;
memcpy(headptr, &h, HEADERSIZE);
memcpy(footptr, &f, FOOTERSIZE);
/* if there is space after payload and before footer
because of alignment, fill in parts of THEFENCE */
amount = (ALIGN - (h.size % ALIGN));
temp = THEFENCE;
if (amount > 0) {strncpy(footptr - amount, (char *) &temp, amount);
if (amount <= 4) {
strncpy(footptr - amount, (char *) &temp, amount);
}
else {
strncpy(footptr - amount, (char *) &temp, 4);
strncpy(footptr - amount + 4, (char *) &temp, amount - 4);
}
}
allocatedSize += h.size;
return (void *) (headptr + HEADERSIZE);
}
void MyFree(void *ptr, char *filename, int linenumber) {
struct header *headptr = (struct header *) ptr - 1;
struct footer *footptr;
int size = headptr->size, amount;
int temp = THEFENCE;
if((headptr->fence != THEFENCE)&&(headptr->checksum != bitCount(headptr->size) + bitCount(headptr->fence)))
{
error(4,filename,linenumber);
return;
}
if (headptr->fence != THEFENCE) {
error(1, filename, linenumber);
return;
}
if (headptr->checksum != bitCount(headptr->size) + bitCount(headptr->fence)) {
error(3, filename, linenumber);
return;
}
/* round up size */
size += ALIGN - 1;
size -= size % ALIGN;
footptr = (struct footer *) ((char *) ptr + size);
if (footptr->fence != THEFENCE) {
error(2, filename, linenumber);
return;
}
/* check area before footer if not part of payload */
amount = (ALIGN - (headptr->size % ALIGN));
if (amount > 0) {
if (amount <= 4) {
if (strncmp((char *) footptr - amount, (char *) &temp, amount) != 0) {
error(2, filename, linenumber);
return;
}
}
else {
if (strncmp((char *) footptr - amount, (char *) &temp, 4) != 0
|| strncmp((char *) footptr - amount + 4, (char *) &temp, amount - 4) != 0) {
error(2, filename, linenumber);
return;
}
}
}
allocatedSize -= headptr->size;
free(headptr);
}
/* returns number of bytes allocated using MyMalloc/MyFree:
used as a debugging tool to test for memory leaks */
int AllocatedSize() {
return allocatedSize;
}
/* Optional functions */
/* Prints a list of all allocated blocks with the
filename/line number when they were MALLOC'd */
void PrintAllocatedBlocks() {
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -