void hfree(void *ptr) { unsigned long addr = (unsigned long)ptr; unsigned long index = (addr - heap_start) / HALLOC_CHUNK_SIZE; int e = (int)(index / BITS_PER_ENTRY); int b = (int)(index % BITS_PER_ENTRY); kthread_mutex_lock(&heap_mutex); bit_clear(e, b); if (cur_last_bitmap_entry > e) { goto done; } // Resize the heap for (; e; e--) { if (entry_inuse(e)) { cur_last_bitmap_entry = e; break; } } resize_heap(); done: atomic_membar(); kthread_mutex_unlock(&heap_mutex); }
int mheap_init(maxHeap* hp) { hp->size = 0; hp->cap = 0; hp->heap = NULL; hp->state = HP_BUILD; return resize_heap(hp,HP_STARTCAP); }
//Insert element into heap void insert(int data){ if(h->count==h->capacity) h=resize_heap(); //Increase heap size h->count++; //Place data at last h->a[h->count-1]=data; percolate_up(h->count-1); }
void fill_heap(int p[],int n){ int i; //Create space for allocation all array elements while(n>h->capacity) h=resize_heap(); for(i=0;i<n;i++) h->a[i]=p[i]; h->count=n; //Head to first non-leaf node for(i=(n-2)/2;i>=0;i--) percolate_down(i); }
int mheap_insert(maxHeap* hp,g3Float key,void* entity) { if (hp->size == hp->cap) { if (!resize_heap(hp,hp->cap*2)) { hp->state = HP_INVALID; return 0; } } hp->heap[hp->size].entity = entity; hp->heap[hp->size].key = -FHUGE; hp->size++; return inc_key(hp,(hp->size - 1),key); }
void *halloc() { int e; int b; int found = 0; int prev_entry; unsigned long addr; void *result = NULL; kthread_mutex_lock(&heap_mutex); // Find the first avail entry for (e = 0; e < BITMAP_ENTRY_COUNT; e++) { if (entry_avail(e)) { found = 1; break; } } if (!found) { goto done; } // Find and set the first avail bit b = find_first(e, 0); bit_set(e, b); // Resize the heap prev_entry = cur_last_bitmap_entry; if (e > cur_last_bitmap_entry) { cur_last_bitmap_entry = e; } if (!resize_heap()) { bit_clear(e, b); cur_last_bitmap_entry = prev_entry; goto done; } // Calculate the final address addr = heap_start + HALLOC_CHUNK_SIZE * (BITS_PER_ENTRY * e + b); result = (void *)addr; done: atomic_membar(); kthread_mutex_unlock(&heap_mutex); return result; }
void mheap_free(maxHeap* hp) { resize_heap(hp,0); free(hp); }