void *malloc_realloc(malloc_t *heap, void *ptr, size_t n)
{
    void *new_ptr;

    if (n > MAX_SIZE)
        return NULL;

    if (n == 0)
    {
        malloc_free(heap, ptr);
        return NULL;
    }

    if (ptr == NULL)
        return malloc_alloc(heap, n);

    if (malloc_size(heap, ptr) >= n)
        return ptr;

    new_ptr = malloc_alloc(heap, n);
    if (new_ptr == NULL)
        return NULL;

    memcpy(new_ptr, ptr, malloc_size(heap, ptr));

    malloc_free(heap, ptr);
    return new_ptr;
}
Example #2
0
void task_init() {
  active_tasks = (struct list_t *)malloc_alloc(sizeof(struct list_t));
  list_init(active_tasks);

  inactive_tasks = (struct list_t *)malloc_alloc(sizeof(struct list_t));
  list_init(inactive_tasks);

  struct task_t *task = (struct task_t *)malloc_alloc(sizeof(struct task_t));
  task->sp    = 0;
  task->msg   = 0;

  list_add_first(active_tasks, task);
  current_task_it = list_first(active_tasks);
}
Example #3
0
int
init_process(struct pcb_s *pcb, int stack_size, func_t* f, int period, int calcul)
{	
  /* Function and args */
  pcb->entry_point = f;

  /* Stack allocation */
  pcb->size=stack_size;
  pcb->stack_base = malloc_alloc(stack_size);
  if(!pcb->stack_base)
    return 0;

  /* State and context */
  pcb->state = NEW;
  pcb->sp = ((uint32_t*) (pcb->stack_base + stack_size)) - 1;

  /* Fill in the stack with CPSR and PC */
  *(pcb->sp) = 0x53;
  pcb->sp --;
  *(pcb->sp) = (unsigned int) &start_current_process;
  
  /* SET RMS attributes */
  pcb->calcul = calcul;
  pcb->period = period;
  pcb->calcul_remaining = 0;
  pcb->period_remaining = 0;
  
  return 1;
}
Example #4
0
void sem_down(struct sem_s* sem) {
    DISABLE_IRQ();

	if (sem->val <= 0) {
		// Initializing "wait_proc"
		struct waiting_process* wait_proc = (struct waiting_process*) malloc_alloc(sizeof(struct waiting_process));
		wait_proc->process = current_process;
		wait_proc->next =  0;

		// If the waiting queue is empty, we put our process as the first (and only) waiting element
		if (sem->waiting == 0) {
			sem->last_process = wait_proc;
			sem->queue = wait_proc;
		}
		// Otherwise we put it at the end of the queue
		else {
			sem->last_process->next = wait_proc;
			sem->last_process = wait_proc;
		}

		sem->waiting += 1;

		current_process->state = WAITING;
		ENABLE_IRQ();
        while(current_process->state== WAITING){
            //DO NOTHING
        }
	}
	else {
		sem->val -= 1;
		ENABLE_IRQ();
	}

}
Example #5
0
struct task_t *task_start(task_func_t *task_func) {
  struct task_t *task = (struct task_t *)malloc_alloc(sizeof(struct task_t));
  char *p = malloc_alloc(0x800);
  p += 0x800;
  task->sp = (uint32 *)(p); // 2k stack
  task->msg = 0;
  task->state = TASK_STATE_ACTIVE;
  list_init(&task->input_channels);
  list_init(&task->output_channels);
  task_create(&task->sp, task_func);
  list_add_last(active_tasks, task);
  print_buf("task start:");
  print_buf(" t=");
  print_ptr(task);
  print_buf(" t->sp=");
  print_ptr(task->sp);
  print_buf("\n");
  return task;
}
Example #6
0
struct mutex_s * mutex_create() {
    struct mutex_s * mutex = (struct mutex_s *) malloc_alloc(
            sizeof(struct mutex_s));
    if (mutex == NULL) {
        return NULL;
    }

    if (mutex_init(mutex) == -1) {
        malloc_free(mutex);
        return NULL;
    }
    return mutex;
}
Example #7
0
void *calloc(size_t count, size_t size)
{
    size_t n = count * size;
    void *ptr;

    if ((uintmax_t)count * size > (uintmax_t)SIZE_MAX)
        return NULL;

    ptr = malloc_alloc(&g_heap, n);
    if (ptr == NULL)
        return NULL;

    cgc_memset(ptr, 0, n);
    return ptr;
}
Example #8
0
int
create_process(func_t* f, unsigned size, int period, int calcul)
{
  struct pcb_s *pcb;
  pcb = (struct pcb_s*) malloc_alloc(sizeof(struct pcb_s));

  if(!pcb)
    return 0;

  if (! ready_queue) {/* First process */
    ready_queue = pcb;
  } else {
    pcb->next = ready_queue->next;
  }
  
  ready_queue->next = pcb;
  return init_process(pcb,size,f,period,calcul);
}
Example #9
0
void sem_down(struct sem_s* sem) {
    DISABLE_IRQ();
	
	if (sem->val <= 0) {
		// Initializing "wait_proc"
		struct waiting_process* wait_proc = (struct waiting_process*) malloc_alloc(sizeof(struct waiting_process));
		wait_proc->process = current_process;
		wait_proc->next =  0;

		// If the waiting queue is empty, we put our process as the first (and only) waiting element
		if (sem->waiting == 0) {
			sem->last_process = wait_proc;
			sem->queue = wait_proc;
		}
		// Otherwise we put it at the end of the queue
		else {
			sem->last_process->next = wait_proc;
			sem->last_process = wait_proc;
		}

		sem->waiting += 1;
        
        // TODO : Marche pas si c'est le dernier process de sa queue
        // ... 'va juste changer la valeur de current_process->schedule_queue
        move_process(current_process, &current_process->schedule_queue, waiting)
        
		current_process->state = WAITING;
		ENABLE_IRQ();
		
        // TODO : Schedule instead of doing a while
        while(current_process->state== WAITING){
            //DO NOTHING
        }
	}
	else {
	    sem->val -= 1;
		ENABLE_IRQ();
	}

}
Example #10
0
void mtx_init(struct mtx_s** mutex){
	(*mutex) = (struct mtx_s*) malloc_alloc(sizeof(struct mtx_s));
	(*mutex)->owner = 0;
    sem_init( &( (*mutex)->sem_mtx ), 1);
	//mutex->pOwnerPid=getpid();
}
Example #11
0
void sem_init(struct sem_s** sem, unsigned int val){
	*sem = (struct sem_s*) malloc_alloc(sizeof(struct sem_s));
	(*sem)->val = val;
	(*sem)->waiting = 0;
	(*sem)->queue = 0;
}