Finding an index from an address
- From: jacob navia <jacob@xxxxxxxxxxxx>
- Date: Sun, 20 May 2012 23:40:42 +0200
I have a heap of n objects of size Heap->ElementSize;
I receive from the client program an element to free, so
I have to put it in the free list. Since the heap is
just an array of n * ElementSize bytes, I find out
the index of the element to free from its address, then set
the bit to 1 to mark it as a free element.
Do you see anything wrong with this code?
static int AddToFreeList(ContainerHeap *heap,void *element)
char *p = heap->Heap;
char *elem = element;
idx = elem-p; // get the distance in bytes
if (idx < 0) return -1; // element not in the heap
idx /= heap->ElementSize; // Now in element size units
if (idx > l->Capacity) return -1; // element is not in the heap
byte = idx/8; // The byte in the bit map
// Set the bit in the bitmap to 1.
l->FreeMap[byte] |= (1 << (idx%8));
// Update counters
Specifically it is OK to use size_t?
- Prev by Date: Re: Queries about new C paper work C11
- Next by Date: Re: condition true or false? -> (-1 < sizeof("test"))
- Previous by thread: Queries about new C paper work C11
- Next by thread: Re: Finding an index from an address