【发布时间】:2014-12-05 02:12:50
【问题描述】:
所以我需要一个简单的分配器来分配(有时使用归零),然后从映射内存池中释放 4K 块。然而,在实现这个之后,在测试时我发现释放一两个块后,如果我试图分配一个块,程序会SEGFAULT。
奇怪的是,当我连续释放多个块时,似乎没有任何问题。
从其他文件中收集的一些重要定义:
#define xmattr_constant __attribute__((const))
#define xmattr_malloc __attribute__((malloc))
#define xmattr_pure __attribute__((pure))
#define xm_likely(x) __builtin_expect(!!(x), 1)
#define xm_unlikely(x) __builtin_expect(!!(x), 0)
#define ABLKLEN 4096 // 4K pagesize
typedef struct {
uint8_t magic[16]; // "sfDB5" "vX.XXXXXXX" '\0'
uint8_t *freelist;
uint64_t size;
uint64_t bounds;
} arenaheader;
分配代码:
void *pd_arena;
void pd_init (size_t len, uint8_t *map) {
int x;
size_t const block = len / 256; // arena physical size
size_t const size = (block / ABLKLEN) * ABLKLEN; // arena useable size
arenaheader *header;
for (x = 0; x < 256; x++) {
header = (void *) &(map[x * block]);
header->freelist = NULL; // no free blocks because all are free
header->size = size; // useable size
header->bounds = ABLKLEN; // current bounds
}
return;
}
xmattr_malloc void *pd_mallocBK (void) {
arenaheader *header = pd_arena;
uint8_t *ptr;
if (xm_unlikely (header->freelist)) { // there's a sitting free block
ptr = header->freelist; // return the free block
void **next = ptr;
header->freelist = *next; // update the free list
} else if (xm_likely (header->bounds < header->size)) { // no free blocks
ptr = pd_arena;
ptr += header->size;
header->size += ABLKLEN;
} else { // no more blocks
ptr = NULL;
}
return ptr;
}
xmattr_malloc void *pd_callocBK (void) {
void *ptr = pd_mallocBK ();
if (xm_likely (ptr)) // allocation was successful
memset (ptr, 0, ABLKLEN);
return ptr;
}
void pd_freeBK (void *ptr) {
arenaheader *header = pd_arena;
if (xm_likely (ptr)) { // non-NULL ptr
void *next = header->freelist; // get current top of stack
void **this = ptr;
*this = next; // move address of current top of stack to ptr
header->freelist = ptr; // push ptr to stack
}
return;
}
测试代码:
#define F_LEN (1024 * 1024 * 1024) // 1 GB
#define A_LEN (F_LEN / 256)
int main (int argc, char **argv) {
int x, y;
// setup
int fd;
uint8_t *map;
assert (fd = open ("./pd_single.testout", O_CREAT | O_RDWR | O_EXCL));
if (ftruncate (fd, F_LEN)) {
perror ("ftruncate failed: ");
return 1;
}
assert (map = mmap (NULL, F_LEN, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0));
uint8_t *arena[256];
for (x = 0; x < 256; x++)
arena[x] = map + (x * A_LEN);
// test
volatile int *var;
void *list[512];
int lcnt = 0;
pd_init (F_LEN, map);
// per arena test
for (x = 0; x < 256; x++) {
pd_arena = arena[x];
// allocate and write a few times
for (y = 0; y < 256; y++) {
assert ((list[lcnt] = pd_mallocBK ()));
var = list[lcnt];
*var = (x + 1) * (y + 1);
}
// free some but not all
for (y = 0; y < 64; y++)
pd_freeBK (list[lcnt]);
// now reallocate some and write some
for (y = 0; y < 16; y++) {
assert ((list[lcnt] = pd_mallocBK()));
var = list[lcnt];
*var = 16;
}
}
// cleanup
munmap (map, F_LEN);
close (fd);
return 0;
}
通过gdb运行程序后,发现SEGFAULTs在pd_mallocBK()内;具体来说,在这一行:
header->freelist = *next; // update the free list
但是,我似乎无法理解该行有什么问题和/或如何解决它。
所以,有两个问题,真的(按重要性从高到低排列):
- 所选行有什么问题,如何解决?
- 是否有任何其他分配器,我可以简单地分配一个映射内存区域以供使用,而不必实现它?
【问题讨论】:
-
ABLKLEN 是否设置为 4096?你不能展示一个
main()程序,它发出一系列触发故障的调用吗?你用valgrind跑了吗?了解如何创建 MCVE (Minimal, Complete, Verifiable Example) 或 SSCCE (Short, Self-Contained, Correct Example)。 -
@JonathanLeffler 添加了我使用的测试程序和
ABLKLEN的定义。 -
几乎可以编译:我必须添加 8 个标题。
A_LEN和F_LEN是什么?为什么你认为void **next = ptr;行应该编译?它不适合我(Mac OS X 10.9.5 上的 GCC 4.9.1,带有编译器选项gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Werror xma.c -o xma,这是我用于编译的默认选项集)。 -
修复了
LENs的部分;我在 Ubuntu 14.04.1 上使用了带有 GCC 4.8.2 的选项gcc -g xma.c -o xma。 -
警告不够安全! :D 我将不得不研究你认为赋值是什么,但编译器认为它是可疑的,所以这是首先要注意的事情。顺便说一句,只有一个警告(在我为您定义的各种函数提供原型之后)也不错。
标签: c memory-management segmentation-fault