简体   繁体   English

C中的内存分配器 - 如何利用sbrk()的ed空间

[英]Memory allocator in C — how to utilize sbrk()'ed space

I've been writing an implementation of malloc and was wondering if someone could help me with this problem. 我一直在编写malloc的实现,并想知道是否有人可以帮我解决这个问题。

Basically, I would like to reuse memory after allocating it using sbrk(), and having made certain that the memory is free. 基本上,我想在使用sbrk()分配内存后重用内存,并确保内存是空闲的。

So essentially, imagine my memory is like this 从本质上讲,想象一下我的记忆是这样的

|------------------------------|

...and I do some allocations. ......我做了一些分配。 When I allocate memory, each bit has a head (h) and data (d). 当我分配内存时,每个位都有一个头(h)和数据(d)。

|hddddddhddd---hdd--hddd-------|

Now I've got these holes, and if I want to use say, the first gap in my diagram, how do I set it up so that it's got a head (h) and a body (dd) also? 现在我已经有了这些漏洞,如果我想使用say,我的图表中的第一个空白,我如何设置它以便它还有一个头(h)和一个体(dd)?

I've gotten to the point where now I've got a pointer to the memory location I want. 我已经到了现在我有一个指向我想要的内存位置的指针。 In C, its pointed to by a pointer. 在C中,它指向一个指针。 The pointer has a custom type, where "meta" is a struct I defined. 指针有一个自定义类型,其中“meta”是我定义的结构。 So now I have 所以现在我有

metaStruct * mypointer = the memory address.

But when I try to do 但是当我尝试做的时候

mypointer->size = 30;

Or 要么

mypointer->buddy = 1;

I get a segfault. 我得到了一个段错误。

The question: how do I set it up so that the memory address, which has been allocated via sbrk(), will have the form of my struct? 问题是:如何设置它以便通过sbrk()分配的内存地址将具有我的结构形式? Obviously I can't just go myPointer = malloc(sizeof(metaStruct)), because I am writing malloc itself. 显然我不能只是去myPointer = malloc(sizeof(metaStruct)),因为我正在编写malloc本身。 I'm also not interested in sbrk()'ing more space, but rather, utilizing the existing space that I'm pointing to (I want to disregard its junk data and use the space). 我也对sbrk()更多的空间感兴趣,而是利用我指向的现有空间(我想忽略它的垃圾数据并使用空间)。

How do I go about doing that? 我该怎么做呢?

As far as I know, p=sbrk(n) enlarges the available address space of (at least) n bytes and returns the base address of the new allocated area in "p". 据我所知,p = sbrk(n)扩大(至少)n个字节的可用地址空间,并以“p”的形式返回新分配区域的基地址。 So you now have a block of memory starting at "p" and n bytes long (probably more than n, it depends on the system). 所以你现在有一块内存从“p”开始,n字节长(可能大于n,这取决于系统)。

So I suppose that your "metaStruct" contains a "size" field, a "next free area" field, and a "data" field, 所以我想你的“metaStruct”包含一个“大小”字段,一个“下一个空闲区域”字段和一个“数据”字段,

metaStruct * m ;
p=sbrk(sizeof(metaStruct)+ data_size);
m = (metaStruct *)p;
m->size = data_size;
m->next = NULL;
memcpy(m->data, ...,data_size);

The code is not perfect, on some systems the sbrk function (indeed it's often a function, not a basic system call - and of course you should check if sbrk fails) doesn't return aligned pointers, so you have to align the pointer manually. 代码并不完美,在某些系统上sbrk函数(实际上它通常是函数,而不是基本的系统调用 - 当然你应该检查sbrk是否失败)不返回对齐的指针,所以你必须手动对齐指针。 Also, you can obtain the actual allocated size by calling sbrk(0) after sbrk(n) and calculating the difference between the two pointers. 此外,您可以通过在sbrk(n)之后调用sbrk(0)并计算两个指针之间的差异来获得实际分配的大小。 In general, you should mainatin a collection of "free blocks" and try to use them fist, then call sbrk only if none of them is large enough. 一般来说,你应该维护一个“免费积木”的集合,并尝试用它们拳头,然后只有当它们都不够大时才调用sbrk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM