示例#1
0
文件: heap.c 项目: iamif3000/forfun
Heap *createHeap(Heap_cmp_func cmp_func_p, int64_t capacity)
{
  int error = NO_ERROR;
  Heap *heap_p = NULL;

  assert(cmp_func_p != NULL);

  if (capacity < LEAST_HEAP_SIZE) {
    capacity = LEAST_HEAP_SIZE;
  }

  heap_p = (Heap*)malloc(sizeof(Heap));
  if (heap_p == NULL) {
    error = ER_GENERIC_OUT_OF_VIRTUAL_MEMORY;
    goto end;
  }

  initHeap(heap_p);

  error = expandHeap(heap_p, capacity);
  if (error != NO_ERROR) {
    goto end;
  }

  heap_p->base_size = capacity;
  heap_p->cmp_func_p = cmp_func_p;

end:

  if (error != NO_ERROR && heap_p != NULL) {
    destroyHeapAndSetNull(heap_p);
  }

  return heap_p;
}
/**
 * allocate a pointer of size on the kernel heap
 *
 * @param size:		size of the space to be allocated
 * @return the allocated pointer
 */
void *KernelHeap::allocate(uint32_t size)
{
	// check ready flag
	if (!kernelHeapInitialized) EvaKernel::panic("%! tried to use uninitialized kernel heap", "kernheap");

	// allocate with collector
	void *allocated = allocator.allocate(size);

	// check success
	if (allocated)
	{
		usedMemoryAmount = usedMemoryAmount + size;
		return allocated;
	}

	// no memory avaible, expand the heap
	else if (expandHeap()) return allocate(size);

	// memory avaible for expansion, fail
	EvaKernel::panic("%! could not expand kernel heap", "kernheap");
	return 0;
}
示例#3
0
文件: heap.c 项目: iamif3000/forfun
int addToHeap(Heap *heap_p, HeapValue value)
{
  int error = NO_ERROR;
  int64_t next_idx = 0;

  assert(heap_p != NULL);

  next_idx = heap_p->base_used_length;
  if (next_idx >= heap_p->base_size) {
    error = expandHeap(heap_p, heap_p->base_size * EXPAND_RATIO);
    if (error != NO_ERROR) {
      goto end;
    }
  }

  heap_p->base_p[next_idx] = value;
  ++heap_p->base_used_length;

  ajustHeapAppend(heap_p);

end:

  return error;
}