我是一名 CS 专业的学生,我的类(class)有一个实验室来创建一个链表来保存内存信息(位置、大小等),以在 C 中模拟一个简单的垃圾收集器。我们需要做的一件事是找到我们系统的基本指针。问题是我们几乎没有人能做到,教授已经暗示概念会在期末。
实验结束了,所以不要担心这是否值得打分或其他任何事情,我正在努力掌握这个想法,以便为我的期末考试做好准备。
我不确定“root”是否是一个常用术语,但我们的想法是我们保存 main 函数基址的位置,然后当我们调用 roots 函数时,我们立即保存它的位置函数,并遍历两者之间的所有内存,寻找指向我们链表的指针。做的指针被认为是“根”。
这是我在实验室中的代码。也许只看它比尝试解释它更容易。我意识到这可能不是很好的代码,而且它实际上做任何事情,但我只是按照他们告诉我的去做。
*我的问题是我的链表中的“开始”和“结束”范围从未被指向 当我遍历堆栈时,所以我相信我的指针一定做错了。我的 &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;
}
最佳答案
如果我是对的,你想知道为什么永远不会满足以下条件,并且相应的 printf 永远不会执行:
if( iter >= start && iter <= fin )
fprintf( stdout, ">>>>%p\n", iter );
据我所知,代码register void* base asm("ebp");
放置 base
EBP 寄存器中的变量。虽然,这似乎只是编译器将其放置在那里的建议,因此可以忽略 - 来自 gcc docs :
This option does not guarantee that GCC generates code that has this variable in the register you specify at all times.
因此,无法保证获得 EBP 值。
但这里好像不是这样。
iter
从 base
开始值,即指向 void print_roots( void )
所在位置的指针被调用自(我在这里可能是错的,但这并不重要——它指向堆栈中的某个地方)。它通过增加它的值进行迭代,直到它等于 MAIN_BASE
。 ,它指向一个堆栈,其中 int main(int argc, char** argv)
函数存储一些关于它自己的东西。在这两个值之间,main
的局部变量预期会找到函数( x1
和 x2
),它指向堆中的某个位置(其中一些 hnode->loc
指向)。
以下代码定义了 start
的值和 fin
变量:
nTemp = head;
if( nTemp )
start = nTemp -> loc;
while( nTemp && nTemp -> used)
nTemp = nTemp -> next;
fin = nTemp -> loc + nTemp -> size;
所以,start
和 fin
指向堆(因为列表中的任何 hnode
都是指向堆的指针),而 iter
指向堆栈。这就是为什么条件iter >= start && iter <= fin
永远不会满足。
关于c - 在堆栈上查找函数的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316868/