使用数据结构来处理内存的个人 malloc 函数

发布于 2024-10-21 11:28:11 字数 2059 浏览 4 评论 0原文

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

没有分配适当的数据结构,我担心我将无法取回正确的地址。有谁知道如何在另一个程序中使用它作为库来测试这个函数?

数据结构

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;
}

I have this function that will take in a number of bytes to allocate and send back only if it is available and the size of the requested number of bytes fit into my small managed memory.
My question:

The appropriate data structure is not being allocated for and I'm afraid I will not get back correct addresses. Does anyone know how I can test this function using it as a library in another program?

THE DATA STRUCTURE

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

THE MALLOC FUNCTION:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-10-28 11:28:11

在代码中的某个位置设置了 mem_location

            mem_location = cur_location;

,然后,在返回其值之前,您更改了它,

    mem_location = mem_location + sizeof(struct memBlock);

这似乎不正确......

Somewhere in your code you set mem_location

            mem_location = cur_location;

and later, just before returning its value, you change it

    mem_location = mem_location + sizeof(struct memBlock);

it doesn't seem right ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文