just testing to see if this algorithim works under unix using cc compiler. get the following error when i try to run: test.c:60: error: invalid operands to binary & . It is the following line: unsigned long bit = bPtr & mask; Also, the unsigned long decleration used to be DWORD, which does not work under unix.Code:struct block {
int size;
struct block *next;
char data[1];
};
struct heap {
int max;
struct block *free[1];
};
struct block *GetBlock(int size, struct heap *hPtr);
struct block *GetFree(struct block *addr, struct heap *hPtr, int size);
void FreeBlock(struct block *bPtr, struct heap *hPtr);
int main()
{
return 0;
}
struct block *GetBlock(int size, struct heap *hPtr) {
struct block *ret;
if (ret = hPtr->free[size]) {
hPtr->free[size] = hPtr->free[size]->next;
} else if (size < hPtr->max) {
if(ret = GetFlock(size + 1, hPtr)) {
struct block *bPtr = (struct block *)((char *)ret + (1 << size));
bPtr->next = hPtr->free[size];
hPtr->free[size] = bPtr;
}
}
return ret;
}
struct block *GetFree(struct block *addr, struct heap *hPtr, int size) {
struct block *prev = &(hPtr->free[size - 1]); // HACK!
struct block *current = hPtr->free[size];
while (current) {
if (current == addr) {
break;
}
prev = current;
current = current->next;
}
if (current) {
prev->next = current->next;
}
return current;
}
void FreeBlock(struct block *bPtr, struct heap *hPtr) {
int size = bPtr->size;
while (size < hPtr->max) {
unsigned long mask = 1 << size;
unsigned long bit = bPtr & mask;
struct block *base = (struct block *)((unsigned long)bPtr & ~mask);
struct block *buddy = (struct block *)((unsigned long)base | (~bit & mask));
if (GetFree(buddy, hPtr, size)) {
size = base->size++;
bPtr = base;
}
}
bPtr->next = hPtr->free[size];
hPtr->free[size] = bPtr;
}