📄 debugmalloc_sol2.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 20 bytes */
struct header {
int checksum;
char *filename; /* 4 bytes */
int linenumber; /* 4 bytes */
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) {
/* Sum 8 groups of 4 bits each */
int m1 = 0x11 | (0x11 << 8);
int mask = m1 | (m1 << 16);
int s = x & mask;
s += x>>1 & mask;
s += x>>2 & mask;
s += x>>3 & mask;
/* Now combine high and low order sums */
s = s + (s >> 16);
/* Low order 16 bits now consists of 4 sums,
each ranging between 0 and 8.
Split into two groups and sum */
mask = 0xF | (0xF << 8);
s = (s & mask) + ((s >> 4) & mask);
return (s + (s>>8)) & 0x3F;
}
/* 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;
/* round up size */
size += ALIGN - 1;
size -= size % ALIGN;
headptr = malloc(size + HEADERSIZE + FOOTERSIZE);
if (!headptr)
return NULL;
h.filename = (char *) malloc(strlen(filename) + 1);
if (!h.filename) {
free(headptr);
return NULL;
}
strcpy(h.filename, filename);
h.linenumber = linenumber;
h.checksum = bitCount(h.size) + bitCount((int) h.filename) + bitCount(h.linenumber);
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) {
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->checksum != bitCount(headptr->size) + bitCount((int) headptr->filename) + bitCount(headptr->linenumber)) {
error(3, filename, linenumber);
return;
}
if (headptr->fence != THEFENCE) {
errorfl(1, headptr->filename, headptr->linenumber, filename, linenumber);
return;
}
/* round up size */
size += ALIGN - 1;
size -= size % ALIGN;
footptr = (struct footer *) ((char *) ptr + size);
if (footptr->fence != THEFENCE) {
errorfl(2, headptr->filename, headptr->linenumber, 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) {
errorfl(2, headptr->filename, headptr->linenumber, filename, linenumber);
return;
}
}
else {
if (strncmp((char *) footptr - amount, (char *) &temp, 4) != 0
|| strncmp((char *) footptr - amount + 4, (char *) &temp, amount - 4) != 0) {
errorfl(2, headptr->filename, headptr->linenumber, filename, linenumber);
return;
}
}
}
allocatedSize -= headptr->size;
headptr->size = 0;
headptr->linenumber = 0;
free(headptr->filename);
headptr->filename = NULL;
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 + -