0

I'm having difficulty with coding my realloc function.

I have it working through standard memcpy procedure, but I can't get it optimized. I know there are two other cases I need to accommodate for: expanding the current block forward, and checking if the current sized block is large enough (and if too large, split it to free memory).

However, I can't seem to get it right. I always get errors. To clarify, these are not compile errors... these are heap integrity checks that fail through a trace driver. If I do it without splitting, I I run out of memory, and if I try to split, it says it "failed to preserve the original block/data."

Below is my normal memcpy code. The commented section in the middle is my attempt to expand, but I think I need to split because it's causing a ton of fragmentation. This is leading to me running out of memory and erroring out during (one) of the realloc tests. If I do it without the comment block, it works fine, but there is zero optimization.

My attempts to split always fail; commented code at the bottom is my attempt. What am I doing wrong here?

I would very much appreciate any assistance, thank you. :)

#define PACK(size, alloc) ((size) | (alloc))
#define GET_SIZE(p) (GET(p) & ~0x7)
#define GET_ALLOC(p) (GET(p) & 0x1)
#define HDRP(bp) ((char *)(bp) - WSIZE)
#define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))

void *mm_realloc(void *oldptr, size_t size)
{
    void *newptr;
    size_t copySize;
    copySize = GET_SIZE(HDRP(oldptr));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(oldptr)));
//  if (copySize > size) return oldptr;

    /*if (!next_alloc) { 
        if ((GET_SIZE(HDRP(oldptr)) + GET_SIZE(HDRP(NEXT_BLKP(oldptr))))>size) {
        copySize += GET_SIZE(HDRP(NEXT_BLKP(oldptr)));
        PUT(HDRP(oldptr), PACK(copySize,1));
        PUT(FTRP(oldptr), PACK(copySize,1));
        return oldptr;
        }
    }*/

    newptr = mm_malloc(size);
    if (newptr == NULL)
      return NULL;
    if (size < copySize)
        copySize = size;
    memcpy(newptr, oldptr, copySize);
    PUT(newptr,GET(oldptr));
    mm_free(oldptr);
    return newptr;
}

//      int total_avail = (GET_SIZE(HDRP(oldptr)) + GET_SIZE(HDRP(NEXT_BLKP(oldptr))));
//      copySize -= (total_avail - size);
  • Welcome to Stack Overflow! Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, and then construct a [minimal test-case](http://sscce.org). – Oliver Charlesworth Mar 03 '13 at 02:39
  • The code compiles fine. The "errors" that exist are heap integrity failure checks through a trace driver. – user2127970 Mar 03 '13 at 02:40
  • 1
    Does it fail on the first use of `mm_realloc()`? What is the pointer that you pass in? The code doesn't look as if it is protected from a null pointer as the pointer. It doesn't look as if you ensure that the pointer passed in is appropriately aligned. Both are tests that could be added. You've not shown how you construct your lists of memory allocations. In short, you've not shown all the important code. Compact is good, but resolvable is good too. – Jonathan Leffler Mar 03 '13 at 02:46
  • The existing memcpy code works perfectly for all pointer tests, besides having poor memory utilization. If I run the commented block, it will fail near the end of the test (the last few very large words), due to running out of memory. I'm not sure what other code would be useful to see; the rest of the code is functional for all tests (malloc, etc). It's just the realloc tests that fail as described above. The splitting code at the bottom fails on the first few tests of realloc. The "tests" being run are traces created by my professor to ensure my code is functional. I am struggling. – user2127970 Mar 03 '13 at 02:49
  • May we ask why you are reimplementing functions in the standard C library? What is wrong with using one of the existing open source C libraries, or cribbing the functions you want? – vonbrand Mar 03 '13 at 02:54
  • This is a training exercise in school. I've spent the last many many hours trying to get this working. – user2127970 Mar 03 '13 at 02:56

0 Answers0