【发布时间】:2014-05-18 02:07:01
【问题描述】:
我实现了一个自定义内存分配器。我在这个内部文件memory.c 的所有主要代码我在这个文件中创建了一个主函数来测试函数。一切正常。但是当我将这些测试代码移动到另一个文件时(调用main.c 并运行它。我遇到了分段错误。
int main (int argc, char *argv []) {
allocator_init();
char* ptr = (char*) allocate(4096); // csutom function. that on `memory.c`
strcpy(ptr, "this is the test one");// segmentation fault here
printf("print: %s\n", ptr);
deallocate(ptr);
}
这里是主要代码:
volatile Memory memory;
/* allocated memory to memory variable by assign /dev/zero to memory */
void allocator_init() {
fd = open("/dev/zero", O_RDWR);
if(fd == -1) {
perror("File open failed");
exit(0);
}
// page size can different on different platform. customize again to optimize
PAGE_SIZE = getPageSize();
// fd = open("ZEROES", O_RDWR);
if(fd == -1) {
perror("File open failed");
exit(0);
}
// Initialize the region list
memory.region = NULL;
int i;
/// Initialize the caches
/// size of each cache is 16 * 2^i => 16, 32, 64, 128, 256, 512, 1024, 2048
for (i=0; i<8; i++) {
memory.cache[i].size = 16<<i;
memory.cache[i].S = NULL;
}
return;
}
void *allocate_region (unsigned int size) {
Region *region, *temp;
temp = memory.region;
void *mapped_addr = mmap(NULL, size + REGION_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if(mapped_addr == MAP_FAILED) {
perror("Mapping failed");
exit(0);
}
/* create region from mapped address */
region = mapped_addr;
region->size = size;
region->addr = mapped_addr + REGION_SIZE;
printf("allocated region: %p\n", region->addr);
/* assign this region to memory */
memory.region = region;
/* just simple append algorithm in linked list. so new region will be appended to head of linked list */
region->next = temp;
return region->addr;
}
/* allocate : if size < 2048 : allocate cache. else allocate region */
void *allocate(unsigned int size) {
size = ALIGN(size);
return allocate_region(size);
}
这是memory.h 定义我使用过的所有结构:
#ifndef MEMORY_H
#define MEMORY_H
#define MAX_SIZE (1024*1024)
#define REGION_SIZE sizeof(Region)
#define SLAB_SIZE sizeof(Slab)
#define WORD_SIZE 32
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 10000
// Caches are:
// 16, 32, 64, 128, 256, 512, 1024, 2048
#include "bits.h"
int PAGE_SIZE;
// File descriptor for zeroes file
int fd;
void allocator_init();
int deallocate_cache(void* ptr);
void *allocate(unsigned int size);
typedef struct __Region {
void *addr; /// started address can use for caller (can calc by allocated address + Region size)
int size; /// size of this allocated Region (not include size Region)
struct __Region *next;
} Region;
/// size of Slab equals to size of System Page
typedef struct __Slab {
void *addr; /// address of this slab (exclude header)
char *bitmap; /// started address can use for caller (can calc by allocated address + slab size)
int size;
int slots; /// number of slots in maximum has been used
int cache_size; /// size of cache that contains this slab
int bitmap_size; /// size of bitmap. so, can count address bit by bitmap + bitmap_size
int currentSize; /// current allocated elements of this slab. currentSize = 0. deallocated this slab
int bit; /// bit to marked which part of slab has been used
struct __Slab * next;
} Slab;
typedef struct __Cache {
int size;
Slab *S;
} Cache;
typedef struct __Memory {
Region *region;
Cache cache[8];
} Memory;
#endif // MEMORY_H
上面的代码可以正常工作,allocate 函数返回一个地址。我只是在移动到另一个文件时遇到错误。在我的memory.c 中,我有一些全局变量来控制分配的地址和内存。当我将代码移动到新文件时它会影响吗?我无法解释这一点。
谢谢:)
【问题讨论】:
-
您是否正确链接/包含文件?尝试删除强制转换,如果这让编译器抱怨它可能意味着它看不到
allocate的声明/定义。不要忘记使用-Wall进行编译 -
是的。我试图删除演员,但仍然遇到这个错误。 on
memory.c包含一个全局变量来控制我分配的内存。搬到外面时会影响我的代码吗?谢谢:) -
只要该全局变量仅在内部
memory.c使用,就没有关系。你没有回答我关于你是否包含allocate的原型以及是否链接到memory.c的问题。 -
@Kninnug 是的。我有一个
memory.h文件,但它只包含一些结构。我没有在标题中包含allocate或deallocate函数。所以,现在,我尝试将这些函数作为原型添加到标题中,但我仍然遇到上述错误。谢谢:) -
@Kninnug 我已经调试并看到从 allcate() 函数返回时的地址在监视窗口上标记为“超出范围”。这很奇怪吗?谢谢)
标签: c segmentation-fault