__alloc

 avatar
user_0531932
c_cpp
a year ago
1.3 kB
2
Indexable
Never
/*__alloc - allocate a memory region
 *@caller: caller
 *@vmaid: ID vm area to alloc memory region
 *@rgid: memory region ID (used to identify variable in symbole table)
 *@size: allocated size
 *@alloc_addr: address of allocated memory region
 */
int __alloc(struct pcb_t *caller, int vmaid, int rgid, int size, int *alloc_addr)
{
  /* Allocate at the toproof */
  struct vm_rg_struct rgnode;

  if (get_free_vmrg_area(caller, vmaid, size, &rgnode) == 0)
  {
    caller->mm->symrgtbl[rgid].rg_start = rgnode.rg_start;
    caller->mm->symrgtbl[rgid].rg_end = rgnode.rg_end;

    *alloc_addr = rgnode.rg_start;

    return 0;
  }

  /*Attempt to increase limit to get space */
  struct vm_area_struct *cur_vma = get_vma_by_num(caller->mm, vmaid);\
  int gap_size = cur_vma->vm_end - cur_vma->sbrk;
  if(gap_size >= size){
      cur_vma->sbrk += size;
      return 0;
  }

  int inc_sz = PAGING_PAGE_ALIGNSZ(size - gap_size);
  if (inc_vma_limit(caller, vmaid, inc_sz) < 0) return -1;


  int old_sbrk;

  old_sbrk = cur_vma->sbrk;


  /*Successful increase limit */
  caller->mm->symrgtbl[rgid].rg_start = old_sbrk;
  caller->mm->symrgtbl[rgid].rg_end = old_sbrk + size - gap_size;
  
  cur_vma->sbrk = old_sbrk + size - gap_size;

  *alloc_addr = old_sbrk;
  
  return 0;
}