用户实现的内存管理 [英] User-implemented memory management

查看:74
本文介绍了用户实现的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给我一​​个C项目任务,要求我实现一个简单的内存管理库.它的工作方式是它具有init函数,该函数将指向另一个c程序分配的内存块的指针(void *)连同该块的大小一起使用,并具有两个其他函数,用于从所述c分配请求的大小的块传回指向它的指针时,对块进行重新分配并取消分配.

I am given a C project assignment in which I am asked to implement a simple memory management library. The way it works is that it has init function which takes a pointer(void*) to a chunk of memory allocated by another c program along with the size of the chunk and has two other functions to allocate a block of requested size from the said chunk and deallocate a block when passed back the pointer pointing to it.

问题是,我被要求将我的管理结构保留在该块本身中,而我对此却一无所知.我曾考虑过将块划分为多个帧,但是如何在不使用块外部的情况下跟踪分配哪些帧呢?

Problem is that I am asked to keep my management structures inside the chunk itself and I have zero idea on how to do that. I thought about dividing the chunk into frames but how can I keep track of which frames are allocated without using anything from outside the chunk?

初始化函数的用法如下.有一个程序会调用我要编写的库.它将使用malloc或calloc分配一块内存.然后它将从库中调用init函数,并将指向该内存块的指针以及该块的大小传递给该内存块.

Init function is used like this. There is this program which will call the library I am going to write. It will allocate a chunk of memory using either malloc or calloc. Then it will call the init function from the library and pass the pointer to that memory chunk along with the size of the chunk to it.

我的库将对该块执行的操作是根据需要分配该块中的块.因此,我的库的allocate函数实际上是一个从块中请求一个内存块(大小作为参数传递)的调用.它将返回一个(void *)指针,指向已分配的内存块.

What my library will do with that chunk is to allocate blocks from it on demand. So my library's allocate function actually is a call to request a block of memory(size is passed as an argument) from the chunk. And it will return a (void *) pointer pointing to the allocated memory block.

Edit2:为使情况更加清楚,我的库必须能够分配和解除分配,这意味着漏洞将出现在它正在管理的数据块中,并将采用最适合",最适合"或最不适合"的方式.

To make the situation more clear, my library has to be able to allocate and deallocate which means holes will appear in the chunk it is managing and it will employ either first-fit, best-fit or worst-fit.

Edit3:有没有办法将内存地址转换为long int?

Is there a way to convert memory addresses into long int?

推荐答案

以下是您需要做什么的粗略想法:

Here's a rough idea of what you would need to do:

应将内存段构造为块的链接列表.每个块都以您的管理结构的副本开头,然后是您分配的内存.

The memory segment should be structured as a linked list of blocks. Each block starts with a copy of your management structure followed by the memory that you allocate.

在初始化时,将链接列表的开头指向给定内存段的开头.将大小设置为段的大小减去管理结构的大小,然后将下一个指针设置为NULL.

On initialization, point the head of the linked list to the start of the given memory segment. Set the size to the size of the segment minus the size of the management structure, and set the next pointer to NULL.

发出第一个分配请求时,将头块的大小设置为请求的大小,然后在此之后立即将下一个指针设置为内存.将新块的大小设置为旧磁头大小减去请求的大小以及管理结构的大小.

When the first allocation request is made, set the size of the head block to the requested size, then set the next pointer to the memory immediately after that. Set the size of the new block to the old head size minus the requested size and the size of the management struct.

对于释放,您需要在要释放的块之前找到块.将前一个块的大小更改为已释放块的大小再加上结构大小,然后将下一个指针更改为已释放块的下一个指针.

For a deallocation, you would need to find the block prior to the one you're about to release. Change the size of the prior block to the size of the freed block plus the struct size, then change the next pointer to the freed block's next pointer.

这应该足以让您入门.

这篇关于用户实现的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆