【发布时间】:2013-12-01 20:28:19
【问题描述】:
我是一名 CS 专业的学生,我的班级有一个实验室来创建一个保存内存信息(位置、大小等)的链表,以模拟 C 语言中的简单垃圾收集器。我们需要做的一件事就是找到我们系统的基本指针。问题是我们几乎没有人能够做到,教授暗示这些概念将在决赛中。
实验室已经结束,所以不要担心这是否值得一个分数或任何东西,我正在努力掌握这个想法,以便为我的期末考试做好准备。
我不确定“根”是否是一个常用术语,但我们的想法是我们保存主函数基的位置,然后当我们调用根函数时,我们立即保存它的位置函数,并遍历两者之间的所有内存,寻找指向我们链表的指针。做的指针被认为是“根”。
这是我实验室的代码。也许只是看它比尝试解释它更容易。我意识到它可能不是很好的代码,它并没有真正做任何事情,但我只是按照他们告诉我的去做。
*我的问题是我的链接列表中的“开始”和“结束”范围从未被指向 当我遍历堆栈时,所以我相信我的指针一定做错了。我的 &start 和 &fin 总是非常接近迭代器,但从不重叠,我不明白为什么。
两个文件都保存为 .c 文件,编译它应该像 gcc -g *.c 一样简单
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
//#include "gc_lib.h"
typedef struct hnode{
bool used;
long size;
void* loc;
struct hnode* next;
struct hnode* prev;
}hnode;
//Globals
long HEAP_SIZE = 0;
bool AUTO_FREE = false;
void* MAIN_BASE = NULL;
void* MY_HEAP = NULL;
hnode* head = 0;
bool gc_init( long heapsize, void* main_base, bool autofree ){
if( heapsize <= 0 )
return false;
HEAP_SIZE = heapsize;
AUTO_FREE = autofree;
MAIN_BASE = main_base;
if( ( MY_HEAP = malloc( HEAP_SIZE ) ) == NULL )
return false;
return true;
}
void* gc_malloc( unsigned long size ){
if( size <= 0 )
return NULL;
//first malloc
if( !head ){
head = malloc( sizeof( hnode ) );
head -> size = size;
head -> loc = MY_HEAP;
head -> used = true;
head -> prev = 0;
hnode* hMem = malloc( sizeof( hnode ) );
hMem -> size = HEAP_SIZE - size;
hMem -> loc = (void*)((char*)(MY_HEAP) + size);
hMem -> used = false;
hMem -> next = 0;
hMem -> prev = head;
head -> next = hMem;
return head -> loc;
}
hnode* findSpot = head;
void* tempLoc = MY_HEAP;
int tempS = 0;
while( findSpot ){
//Used node
if( findSpot -> used == true ){
tempS += findSpot -> size;
tempLoc = (void*)((char*)(MY_HEAP) + tempS);
findSpot = findSpot -> next;
}
//Empty node; fits perfectly
else if( ( findSpot -> used == false ) && ( findSpot -> size == size ) ){
findSpot -> used = true;
return findSpot -> loc;
}
//Empty node; fits imperfectly
else if( ( findSpot -> used == false ) && ( findSpot -> size > size ) ){
int splitSize = ( findSpot -> size ) - size;
findSpot -> used = true;
findSpot -> size = size;
hnode* newNode = malloc ( sizeof( hnode ) );
newNode -> prev = findSpot;
newNode -> next = findSpot -> next;
newNode -> size = splitSize;
newNode -> used = false;
if( findSpot -> next )
findSpot -> next -> prev = newNode;
findSpot -> next = newNode;
tempS += findSpot -> size;
tempLoc = (void*)((char*)(MY_HEAP) + tempS);
newNode -> loc = tempLoc;
return findSpot -> loc;
}
//Empty node; too small
else if( ( findSpot -> used == false ) && ( findSpot -> size < size ) ){
tempS += findSpot -> size;
tempLoc = (void*)((char*)(MY_HEAP) + tempS);
findSpot = findSpot -> next;
}
}
return NULL;
}
void print_roots( void ){
register void* base asm("ebp");
void* iter = base;
printf( "Roots:\n" );
if( head ){
void* mBase = MAIN_BASE;
hnode* nTemp = head;
void* start = 0;
void* fin = 0;
while( iter != mBase ){
if( nTemp )
start = nTemp -> loc;
while( nTemp && nTemp -> used)
nTemp = nTemp -> next;
fin = nTemp -> loc + nTemp -> size;
if( iter >= start && iter <= fin )
fprintf( stdout, ">>>>%p\n", iter );
printf("iter: %p\n", (iter)++ );
}
printf("MAIN_BASE: %p\n", MAIN_BASE );
printf("base: %p\n", base );
printf("\tstart: %p\n", &start );
printf("\tfin: %p\n", &fin );
}
}
这是我们提供的测试文件:
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "gc_lib.h"
int main(int argc, char** argv){
register void* base asm("ebp");
gc_init(100, base, false);
void* x1 = gc_malloc(8);
assert(x1 != NULL);
void* x2 = gc_malloc(8);
assert(x2 != NULL);
assert((x2 == x1 + 8) || (x2 == x1 - 8));
printf("%p\n", x1);
printf("%p\n", x2);
print_roots();
return 0;
}
【问题讨论】:
标签: c pointers cpu-registers dereference