简体   繁体   中英

(C) How to write to/read from memory address returned by mmap?

I've read some of the pages regarding how to ask questions, so I hope this is up to standard.

Our professor wants us to build a custom malloc and free, one that uses buddy allocation. Instead of messing with the heap, he wants us to just use mmap to request 1 GiB of space from the OS:

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

Each chunk of memory should have a header, and if the memory is empty, pointers to the next and previous free chunks via linked list.

I don't know how to say "I want to put this specific data in this specific place." I would imagine a free chunk to look like this in the memory:

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

So let's say that the whole 1 GiB is free. Pseudo Code:

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

How would I create these variables at the address I want them in?

I tried this,

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);

which compiled and ran without issue, but I realized trying to access the values,

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

the compiler says,

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

So I figure something's either wrong with my method of storing the data or retrieving it, and I'd greatly appreciate any help that could be offered.

Here's a header file mymalloc.h:

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

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

} mallocList;

Your compiler error explains the main problem: you can't dereference a void* . Cast the pointer to char* and store whatever bytes you want, or cast it to a struct yourstruct * and store to struct fields with 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;

This might not compile, sorry, but the basic idea about casting pointers is solid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM