【问题标题】:Personal malloc function that uses a data structure to handle memory使用数据结构处理内存的个人 malloc 函数
【发布时间】:2011-03-13 23:40:21
【问题描述】:

我有这个函数,只有当它可用并且请求的字节数的大小适合我的小型托管内存时,它才会分配并发送回多个字节。 我的问题:

没有分配适当的数据结构,恐怕我不会得到正确的地址。有谁知道我如何在另一个程序中使用它作为库来测试这个函数?

数据结构

typedef struct memBlock{
struct memBlock* next;
unsigned long size;  // Size of this block
unsigned int is_used;  // bool 0 = not used 1 = used
} memBlock;

MALLOC 函数:

char *mm_alloc(unsigned long no_of_chars){

if (!has_initialized) {
    printf("No Memory has been intialized, PLEASE INITIALIZE THE MEMORY BEFORE calling This function\n");
    exit(1);
}


void *cur_location; // this is where we are currentl in our memory pool

memBlock *current_loc_mb; // the current mem block location

char *mem_location; // mem location we will return to the user

/* We are going to have to include the size of our data struct when we are searching for open memory*/
no_of_chars = no_of_chars + sizeof(struct memBlock);

mem_location = 0; // set to 0 until a proper size has been found

cur_location = managed_memory_start; // start at the beginning of our allocated memory

// go until there is no more memory left, allocate until we get to the end of our managed memory
while (managed_memory_start != NULL) {

    /*cur_location and cur_loc_mcb are at the same address initially,
but we use the current location as a pointer to move around our managed memory*/

    cur_loc_mcb = (memBlock *)cur_location; 

    // if our current location is not used
        if (!cur_loc_mcb->is_used) {

            if (cur_loc_mcb->size >= no_of_chars) {

                // we have found a size big enough or equal to what the user asks for
                cur_loc_mcb->is_used = 1; 
                mem_location = cur_location; 

                break;

            }
        }

// at this point we dont have a size big enough, move to the next one
cur_location = cur_location + cur_loc_mcb->size; 

}
/*Move the memory past or MCB and return*/

mem_location = mem_location + sizeof(struct memBlock);

return mem_location;
}

【问题讨论】:

  • "有谁知道我如何在另一个程序中使用它作为库来测试这个函数?"这意味着您也有另一个程序的测试套件。否则,您怎么知道您对他们的 功能以及您的 功能有很好的覆盖?更好的做法可能是对您自己的 malloc 库进行自动化测试,因为这样您就可以保证覆盖极端情况。如果你已经有了这个,那么你在这个问题中所问的可能是一个很好的第二步。
  • 你编译为C吗?演员表 (cur_loc_mcb = (memBlock *)cur_location;) 是虚假的,应该留给编译器去做。
  • @ Merlyn Morgan-Graham,我正在为我目前正在编写的这些功能寻找一些测试用例想法。
  • 无法判断您是否要求比最大块中可用的更多内存。如果这样做,您将永远不会离开 while 循环(直到程序段错误)
  • 所以 while(mem_start !=null) 将永远运行,即使我仔细检查了我的整个块。我怎么能遍历我的内存块,直到我没有留下任何东西

标签: c data-structures struct malloc allocation


【解决方案1】:

你在代码中的某个地方设置了 mem_location

            mem_location = cur_location;

然后,在返回它的值之前,你改变它

    mem_location = mem_location + sizeof(struct memBlock);

好像不太对劲……

【讨论】:

  • 看起来他的分配元数据与可用内存内联,直接位于每个已分配块和空闲块之前。他使用cur_location = cur_location + cur_loc_mcb->size; 逐步检查元数据条目,直到找到足够大的空闲空间。然后他只是跳过元数据来获得指向程序员可以写入的可用空间的指针。但是,是的,没有理由改变它两次。
  • 我所做的正是 Null Set 所说的。 mem_location = mem_location 只是在我将用户发送回分配的空间之前将其对齐
猜你喜欢
  • 2019-10-19
  • 2021-09-05
  • 2021-05-29
  • 2010-10-05
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
相关资源
最近更新 更多