J
jacob navia
Hi
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;
size_t idx,byte;
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
heap->Used--;
heap->FreeCount++;
return 1;
}
Specifically it is OK to use size_t?
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;
size_t idx,byte;
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
heap->Used--;
heap->FreeCount++;
return 1;
}
Specifically it is OK to use size_t?