diff vmalloc.c @ 76:9ddbb071142d

make hardware independent and port to 64bit
author Merten Sach <msach@mailbox.tu-berlin.de>
date Thu, 16 Jun 2011 14:41:15 +0200
parents 9c3107044f86
children 521c75d64cef
line diff
     1.1 --- a/vmalloc.c	Thu Jun 02 13:55:51 2011 +0200
     1.2 +++ b/vmalloc.c	Thu Jun 16 14:41:15 2011 +0200
     1.3 @@ -8,7 +8,9 @@
     1.4   */
     1.5  
     1.6  #include <malloc.h>
     1.7 +#include <inttypes.h>
     1.8  #include <stdlib.h>
     1.9 +#include <stdio.h>
    1.10  
    1.11  #include "VMS.h"
    1.12  #include "Histogram/Histogram.h"
    1.13 @@ -45,10 +47,10 @@
    1.14   *
    1.15   *Will find a
    1.16   */
    1.17 -void *
    1.18 -VMS__malloc( int32 sizeRequested )
    1.19 +void *VMS__malloc( size_t sizeRequested )
    1.20   { MallocProlog *foundElem = NULL, *currElem, *newElem;
    1.21 -   int32         amountExtra, foundElemIsTopOfHeap, sizeConsumed,sizeOfFound;
    1.22 +   ssize_t        amountExtra, sizeConsumed,sizeOfFound;
    1.23 +   uint32        foundElemIsTopOfHeap;
    1.24  
    1.25     //============================= MEASUREMENT STUFF ========================
    1.26     #ifdef MEAS__TIME_MALLOC
    1.27 @@ -63,7 +65,7 @@
    1.28  
    1.29     while( currElem != NULL )
    1.30      {    //check if size of currElem is big enough
    1.31 -      sizeOfFound=(int32)((char*)currElem->nextHigherInMem -(char*)currElem);
    1.32 +      sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
    1.33        amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
    1.34        if( amountExtra > 0 )
    1.35         {    //found it, get out of loop
    1.36 @@ -99,7 +101,7 @@
    1.37     if( amountExtra > 64 )
    1.38      {    //make new elem by adding to addr of curr elem then casting
    1.39        sizeConsumed = sizeof(MallocProlog) + sizeRequested;
    1.40 -      newElem = (MallocProlog *)( (char *)foundElem + sizeConsumed );
    1.41 +      newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
    1.42        newElem->nextHigherInMem   = foundElem->nextHigherInMem;
    1.43        newElem->nextLowerInMem    = foundElem;
    1.44        foundElem->nextHigherInMem = newElem;
    1.45 @@ -124,7 +126,7 @@
    1.46     //========================================================================
    1.47  
    1.48        //skip over the prolog by adding its size to the pointer return
    1.49 -   return (void *)((char *)foundElem + sizeof(MallocProlog));
    1.50 +   return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
    1.51   }
    1.52  
    1.53  
    1.54 @@ -137,7 +139,8 @@
    1.55  void
    1.56  VMS__free( void *ptrToFree )
    1.57   { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem;
    1.58 -   int32         lowerExistsAndIsFree, higherExistsAndIsFree, sizeOfElem;
    1.59 +   size_t         sizeOfElem;
    1.60 +   uint32         lowerExistsAndIsFree, higherExistsAndIsFree;
    1.61  
    1.62     //============================= MEASUREMENT STUFF ========================
    1.63     #ifdef MEAS__TIME_MALLOC
    1.64 @@ -152,8 +155,8 @@
    1.65        return;
    1.66      }
    1.67        //subtract size of prolog to get pointer to prolog, then cast
    1.68 -   elemToFree = (MallocProlog *)((char *)ptrToFree - sizeof(MallocProlog));
    1.69 -   sizeOfElem =(int32)((char*)elemToFree->nextHigherInMem-(char*)elemToFree);
    1.70 +   elemToFree = (MallocProlog *)((uintptr_t)ptrToFree - sizeof(MallocProlog));
    1.71 +   sizeOfElem =(size_t)((uintptr_t)elemToFree->nextHigherInMem-(uintptr_t)elemToFree);
    1.72  
    1.73     if( elemToFree->prevChunkInFreeList != NULL )
    1.74      { printf( "error: freeing same element twice!" ); exit(1);
    1.75 @@ -257,7 +260,7 @@
    1.76   * empty or not --
    1.77   */
    1.78  void *
    1.79 -VMS__malloc_in_ext( int32 sizeRequested )
    1.80 +VMS__malloc_in_ext( size_t sizeRequested )
    1.81   {
    1.82   /*
    1.83        //This is running in the master, so no chance for multiple cores to be
    1.84 @@ -323,14 +326,14 @@
    1.85        //Use this addr to free the heap when cleanup
    1.86     freeListHead->nextLowerInMem      = firstChunk;
    1.87        //to identify top-of-heap elem, compare this addr to elem's next higher
    1.88 -   freeListHead->nextHigherInMem     = (void*)( (char*)firstChunk +
    1.89 +   freeListHead->nextHigherInMem     = (void*)( (uintptr_t)firstChunk +
    1.90                                           MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
    1.91     freeListHead->nextChunkInFreeList = firstChunk;
    1.92  
    1.93     firstChunk->nextChunkInFreeList   = NULL;
    1.94     firstChunk->prevChunkInFreeList   = freeListHead;
    1.95        //next Higher has to be set to top of chunk, so can calc size in malloc
    1.96 -   firstChunk->nextHigherInMem       = (void*)( (char*)firstChunk +
    1.97 +   firstChunk->nextHigherInMem       = (void*)( (uintptr_t)firstChunk +
    1.98                                           MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
    1.99     firstChunk->nextLowerInMem        = NULL; //identifies as bott of heap
   1.100