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

(C) 如何写入/读取mmap返回的内存地址?,c,C,我已经阅读了一些关于如何提问的页面,所以我希望这符合标准 我们的教授希望我们建立一个自定义的malloc和free,一个使用伙伴分配的。他希望我们只使用mmap从操作系统请求1Gib的空间,而不是搅乱堆: MAX_MEM = 1 << 30. void * base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 因此,假设整个1 GiB是免费的。伪代码: Occupancy = 0;

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

我们的教授希望我们建立一个自定义的malloc和free,一个使用伙伴分配的。他希望我们只使用mmap从操作系统请求1Gib的空间,而不是搅乱堆:

MAX_MEM = 1 << 30.
void * base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
因此,假设整个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);
编辑说

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;

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

/* 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;
/*您需要告诉gcc在不填充的情况下打包结构,
*因为您希望指针以第二个字节开始存储,即未对齐。
*在这种情况下,这实际上很好,因为它们不会跨越缓存线边界。
*它们将位于页面的开头,来自mmap,因此是缓存线的开头。
*现代CPU能够快速处理缓存线中未对齐的负载。
*/
结构属性内存块{
无符号整数:1;
unsigned int size:7;//位域。不一定是个好主意。自己使用带符号的int可能更好。分配为正,免费为负。
结构mallocList{//嵌套定义。您可以用不同的方法执行此操作
结构mallocList*prev,*next;
}指针;
};  // 这里不需要类型名称。这将声明一个结构类型的变量。
int MAX_MEM=1大小=任意值;
mbp->pointers.prev=NULL;
mbp->pointers.next=NULL;

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

您需要为指针
two
分配内存,因为它目前尚未初始化,还请显示您的结构定义。请注意,
mymalloc.h
struct mallocList
定义也声明了该类型的全局变量。如果定义位于
typedef
中,则在结构定义之后使用标识符可以执行类似
typedef结构名称{int foo;}name\t
所以你不必到处写
struct name
,只要
name\u t
void *my_buddy_malloc(int size);
void my_free(void *ptr);

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

} mallocList;
/* 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;