0
votes
#define ALIGNMENT 8

#define CHUNK_SIZE (1<<12) //Extend heap by this amount (bytes)
#define WSIZE 4
#define DSIZE 8
#define MIN_BLOCK_SIZE (4*WSIZE) //Should it require at least 1 word of payload?

#define MAX(x,y) ((x) > (y) ? (x) : (y))

//Pack a size and allocated bit into a word
//#define PACK(size, alloc) ((size << 1 ) | (alloc))

//Read and write a word at address p
#define GET(p) (*(unsigned int*)(p))
#define PUT(p,val) (*(unsigned int *)(p) = (val))

//Read the size and allocated fields from address p
#define GET_SIZE(hp) (GET(hp)) //alli has doubts
//#define GET_ALLOC(hp) (GET(hp) & 0x1) 
//Get next and prev block pointers from a block
#define GET_NEXT(hp) ((char*)(hp)+2*WSIZE)
#define GET_PREV(hp) ((char*)(hp)+1*WSIZE)

//Given block ptr bp, compute address of its header and footer
//Should we ever reference a block ptr in an explicit list?
#define HDRP(bp) ((char*)(bp) - 3 * WSIZE))
#define FTRP(bp) ((char*)(bp) + GET_SIZE(HDRP(bp)))
#define BP(hp) ((char*)(hp) + (3 * WSIZE))

/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)


#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))

I am getting

error: expected statement before ‘)’ token in C macro

One place where I use PUT is:

static int extend_heap(size_t words) {
char* hp;
size_t size;

//This returns a ptr to the HEADER of the list allocatable block
void* last_block = find_end_of_free_list();
void* tail = GET_NEXT(last_block);



 //Only allocate an even number of words to maintain alignment
  size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
  if((long)(hp = mem_sbrk(size)) == -1)
    return -1;

  //Maybe we should make a function (create header/create footer when given an address)
  PUT(hp, size); //Free block header
  PUT(FTRP(BP(hp)), size); //Free block footer

  PUT(GET_PREV(hp), last_block); //Make prev of new block point to end of old list
  PUT(GET_NEXT(hp), tail); //Make next of new block point to tail

  PUT(GET_NEXT(last_block), hp); // Make next of old list point to new list
  return 0;
}

I am getting the error on PUT.
In extend_heap I get another error:

mm.c:121:3: note: in expansion of macro ‘PUT’ PUT(FTRP(BP(hp)), size); //Free block footer ^ mm.c:53:49: error: expected statement before ‘)’ token #define PUT(p,val) (*(unsigned int *)(p) = (val))

I included the code above PUT because I thought the error might be due to syntax of something above it.

1
Show us the call to PUT in your code. It might help to identify which compiler you're using; does it recognize C99/C++ comments marked by // to end of line? Also, I'm fairly sure you've not minimized the code; the PUT macro doesn't use any of the others, so you should be able to eliminate all but the last line of the code you show. Are you sure there are spaces before the open parentheses in CHUNK_SIZE and MIN_BLOCK_SIZE? - Jonathan Leffler
The code as you've shown it (in isolation) is acceptable to my C preprocessor. As Jonathan indicated, we need to see the rest of your code. - Jonathon Reinhart
Edited with an example of calling PUT. - michaelAdam
OK; so now you need to show us what the FTRP macro is about. Are you sure it isn't a misspelling of FPTR? And the BP macro. And you need to worry about why on earth your code uses so many macros. - Jonathan Leffler
I'm using so many macros because my assignment is to build malloc from scratch using nothing but primitives and pointers. - michaelAdam

1 Answers

1
votes

The bug is in line:

#define HDRP(bp) ((char*)(bp) - 3 * WSIZE))

This has a extra ) at the end.Which should be removed. Since in PUT you are using FTRP() macro and before this macro is HDRP() we see this error. Removing the extra ) will solve your problem.