(C)如何写入/读取mmap返回的内存地址?

时间:2015-11-15 04:28:31

标签: c

我已经阅读了一些关于如何提问的网页,所以我希望这符合标准。

我们的教授希望我们建立一个自定义的malloc和free,一个使用好友分配的。他没有乱搞堆,而是希望我们只使用mmap从操作系统请求1 GiB的空间:

MAX_MEM = 1 << 30.
void * base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);

每个内存块应该有一个标头,如果内存为空,则通过链表指向下一个和前一个空闲块。

我不知道怎么说&#34;我想把这些具体数据放在这个特定的地方。&#34;我想象一个免费的块在内存中看起来像这样:

[Occupancy (1 bit)][Size (7 bits)][prev pointer (8 bytes)][next pointer (8bytes)][junk]

所以,让我们说整个1 GiB是免费的。伪代码:

Occupancy = 0; // 0 if empty, 1 if allocated
Size = 0011110; // where size in bytes = 2^Size
next = NULL;
prev = NULL; //note that these are part of a struct called mallocList

我如何在我想要的地址创建这些变量?

我试过了,

int MAX_MEM = 1 << 30;
base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);

*((unsigned char*) base) = 0x1E;
struct mallocList* temp;
temp->prev = NULL;
temp->next = NULL;
void* tempaddr = base + 1;

*((struct mallocList*) tempaddr) = *temp;

munmap(base, 1 <<30);

编译并运行没有问题,但我意识到尝试访问值,

printf("%c", *base); //line 37
struct mallocList* two;
two->prev = NULL;
two->next = NULL;
tempaddr->next = *two; //line 41

编译器说,

3.c:37: warning: dereferencing ‘void *’ pointer
3.c:37: error: invalid use of void expression
3.c:41: warning: dereferencing ‘void *’ pointer
3.c:41: error: request for member ‘next’ in something not a structure or union

所以我认为我的存储数据或检索数据的方法有问题,我非常感谢可以提供的任何帮助。

这是一个头文件mymalloc.h:

void *my_buddy_malloc(int size);
void my_free(void *ptr);

struct mallocList
{
  struct mallocList *prev;
  struct mallocList *next;

} mallocList;

1 个答案:

答案 0 :(得分:0)

您的编译器错误解释了主要问题:您无法取消引用void*。将指针转换为char*并存储您想要的任何字节,或将其转换为struct yourstruct *并存储到p->field的结构字段。

/* You need to tell gcc to pack the struct without padding,
 * because you want the pointers stored starting with the second byte,     i.e. unaligned.
 * That's actually fine in *this* case, since they won't cross a cache-line boundary.
 * They'll be at the beginning of a page, from mmap, and thus the beginning of a cache line.  
 * Modern CPUs are fast at handling misaligned loads within a cache line.
 */

struct __attribute__ ((__packed__)) mem_block {
    unsigned int occupied:1;
    unsigned int size:7;   // bitfields.  Not necessarily a good idea.  Just using a signed int yourself might be better.  positive for allocated, negative for free.
    struct mallocList { // nested definition.  You can do this differently
        struct mallocList *prev, *next;
    } pointers;
};  // don't need the type-name here.  That would declare a variable of the struct type.


int MAX_MEM = 1 << 30;
void *base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);

char *cp = base;
cp[0] = size << 1 | 1;  // pack the size and occupied bits into a byte
struct mallocList *mlp = (struct mallocList*)(cp+1);  // This avoids needing a compiler-specific way to pack your struct.

// or
struct mem_block *mbp = base;
mbp->occupied = 1;
mbp->size=whatever;
mbp->pointers.prev = NULL;
mbp->pointers.next = NULL;

这可能无法编译,抱歉,但关于转换指针的基本想法是可靠的。