Skip to content

Instantly share code, notes, and snippets.

@TyOverby
Created March 4, 2013 06:44
Show Gist options
  • Select an option

  • Save TyOverby/5080454 to your computer and use it in GitHub Desktop.

Select an option

Save TyOverby/5080454 to your computer and use it in GitHub Desktop.
These are some tools that I wrote for lab 5. They all involve debugging by printf, which I don't heavily encourage, but is sometimes necessary for quick iterations. All work is in the public domain. If a segfault comes out of this code, it is because you incorrectly linked your list.
Location: 0x7ff89bb23018
size : 32
used : 1
prev use: 1
tags : 35
Location: 0x7ff89bb23018
size : 32
used : 1
prev use: 1
tags : 35
// Given a pointer returned by mm_malloc, this
// function will print the details of the block.
void inspectAlloc(void* ptr){
BlockInfo* bi = (BlockInfo*) POINTER_SUB(ptr, WORD_SIZE);
printf("Location: %p\n", bi);
printf("size : %d\n", SIZE(bi->sizeAndTags));
printf("used : %d\n", bi->sizeAndTags & 1);
printf("prev use: %d\n", (bi->sizeAndTags & 2) >>1);
printf("tags : %d\n\n", bi->sizeAndTags);
}
// If you want to write your own tests with fine-grained control,
// you can use this skeleton to programatically test and inspect
// your memory allocator while it is running.
// you might want to place the functions provided in the other files
// in here to test.
// If you need to get some data from mm.c, remember that in order to
// access it from in here, you'll need to modify the header file.
// I suggest making your own header file for your own tests.
#include "mm.h" // This could change if you need more data from mm.c
#include "memlib.h"
#include <stdio.h>
#define POINTER_SUB(p,x) ((char*)(p) - (x))
#define POINTER_ADD(p,x) ((char*)(p) + (x))
#define WORD_SIZE sizeof(void*)
struct BlockInfo {
size_t sizeAndTags;
struct BlockInfo* next;
struct BlockInfo* prev;
};
typedef struct BlockInfo BlockInfo;
int main(void){
mem_init();
mm_init();
// Example test
void* one = mm_malloc(5);
void* two = mm_malloc(5);
void* three = mm_malloc(5);
void* four = mm_malloc(5);
void* five = mm_malloc(5);
mm_free(one);
mm_free(two);
mm_free(three);
mm_free(four);
mm_free(five);
mm_malloc(5);
return 0;
}
______________
|0x7f6341c01018| <- this is the position of the block
+----------------+
| sizeAndTags: |
| 32 |
| prev used |
| mine unused |
+----------------+
| next: |
| 0x7f6341c01038 | <- pointer to the next one in the list
+----------------+
| prev: |
| (nil) | <- this means that it is the HEAD
+----------------+
| ... |
| ... |
+----------------+
______________
|0x7f6341c01038|
+----------------+
| sizeAndTags: |
| 32 |
| prev unused |
| mine unused |
+----------------+
| next: |
| 0x7f6341c01078 |
+----------------+
| prev: |
| 0x7f6341c01018 |
+----------------+
| ... |
| ... |
+----------------+
______________
|0x7f6341c01078|
+----------------+
| sizeAndTags: |
| 4032 |
| prev used |
| mine unused |
+----------------+
| next: |
| (nil) |
+----------------+
| prev: |
| 0x7f6341c01038 |
+----------------+
| ... |
| ... |
+----------------+
+--------------+
|-end of list--|
+--------------+
// This function, when passed a pointer to a block, will print the details about the block
void printBlock(BlockInfo* block){
char* start = " ______________ \n";
char* heading = " |%14p| \n";
char* seperator = "+----------------+\n";
char* size = "| sizeAndTags: |\n| %14d |\n| prev %9s |\n| mine %9s |\n";
char* next = "| next: |\n| %14p |\n";
char* prev = "| prev: |\n| %14p |\n";
char* more = "| ... |\n";
char* tail = "| tailAndTags: |\n| %14d |\n| %14d |\n";
printf(start);
printf(heading, block);
printf(seperator);
int sat = block->sizeAndTags;
char* status = (sat & 1) ? "used" : "unused";
char* pprev = (sat & 2) ? "used" : "unused";
printf(size, SIZE(sat), pprev, status);
printf(seperator);
printf(next, block->next);
printf(seperator);
printf(prev, block->prev);
printf(seperator);
printf(more);
printf(more);
printf(seperator);
}
// When passed a pointer to a block, will recursively call
// into the list and print the entire list using printBlock (defined above).
void printList(BlockInfo* firstBlock){
printf("\n");
if(firstBlock){
printBlock(firstBlock);
printList(firstBlock->next);
}
else{
printf(" +--------------+\n");
printf(" |-end of list--|\n");
printf(" +--------------+\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment