问题描述
我正在尝试实现我自己的堆内存分配器,方法是通过 sbrk()
系统调用分配内存并管理返回缓冲区。 void *sbrk(intptr_t increment);
返回值:
来自man sbrk
:
成功时,sbrk() 返回前一个程序中断。 (如果 break 被增加,那么这个值是一个指向开始的指针 新分配的内存)。出错时, (void *) -1 重新 转,并且 errno 设置为 ENOMEM
#include <gnu/libc-version.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
/*
puts (gnu_get_libc_version ());
*/
#define VP_ALIGNMENT (4096)
#define ALIGN(size,alignment) ( ((size) + (alignment-1)) & ~((alignment-1)) )
static void *extend_heap(int nbytes)
{
void *sp = sbrk(ALIGN(nbytes,VP_ALIGNMENT));
if (sp == (void*)-1) {
perror("fails to retrieve program brk");
}
return sp;
}
struct S{
char m[4096];
};
int main() {
/* get current brk */
void *cbrk = sbrk(0);
printf("current prog brk: %p\n",cbrk);
/*allocate 4096 bytes and get pointer to the start of the buffer*/
struct S *buf = (struct S*)extend_heap(4000);
printf("prog brk after allocation: %p\n",sbrk(0));
int c=4096;
do {
buf->m[4096-c] = 'a';
} while(--c);
c=4096;
do {
assert(buf->m[4096-c] == 'a');
} while(--c);
return 0;
}
使用 gcc 和 gcc 标志编译:{{1}} 程序按预期运行,但我不明白为什么 linux 在我只要求 4096 时分配了大量内存,2200hex 字节?
添加 gcc 标志 cc main.c -pedantic -Wconversion -Werror
或 -ansi
之一会产生以下编译错误:
在函数‘extend_heap’中:main.c:15:20:错误:初始化
‘int’中的‘void *’使指针从整数而不进行强制转换
[-Werror=int-conversion] 15 | void *sp = sbrk(ALIGN(nbytes,VP_ALIGNMENT));
| ^~~~ main.c:在函数‘main’中:main.c:31:22:错误:从‘int’初始化‘void *’使得
没有强制转换的整数指针 [-Werror=int-conversion] 31 |
无效 *cbrk = sbrk(0);
| ^~~~
-std=c89
返回 sbrk
。
为什么我收到上面的错误?
我的 glibc 版本 = 2.31,gcc 版本 9.3.0
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)