Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/implem/alloc_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ void pos_allocs_init();

void alloc_init_l1(int cid);

#endif
/*
* pos_alloc_user_data and pos_free_user_data will allocate memory in the best
* location based on the available hardware. The preferred memories are (in
* order): FC_TCDM > L2_private_bank_0 > L2_private_bank_1 > L2_share_banks
*/
void *pos_alloc_user_data(int size);

void pos_free_user_data(void *_chunk, int size);

#endif
37 changes: 36 additions & 1 deletion kernel/alloc_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,41 @@ void pos_allocs_init()
}


void *pos_alloc_user_data(int size)
{
#if defined(ARCHI_HAS_FC_TCDM)
return pos_alloc(&pos_alloc_fc_tcdm, size);
#elif defined(ARCHI_HAS_L2_MULTI)
// We try all the available L2 banks.
for (int i=0; i<3; i++) {
void *result = pos_alloc(&pos_alloc_l2[i], size);
if (result != NULL) return result;
}
// No available memory in any bank.
return NULL;
#else // i.e. we have a single l2 bank for data.
return pos_alloc(&pos_alloc_l2[0], size);
#endif
}


void pos_free_user_data(void *_chunk, int size)
{
#if defined(ARCHI_HAS_FC_TCDM)
pos_free(&pos_alloc_fc_tcdm, _chunk, size);
#elif defined(ARCHI_HAS_L2_MULTI)
// We deduce which l2 bank the chunk came from by looking at its address.
pos_alloc_t *allocator;
unsigned int base = (unsigned int) _chunk;
if (base < (unsigned int) pos_l2_priv0_base() + pos_l2_priv0_size()) allocator = &pos_alloc_l2[0];
else if (base < (unsigned int) pos_l2_priv1_base() + pos_l2_priv1_size()) allocator = &pos_alloc_l2[1];
else allocator = &pos_alloc_l2[2];
pos_free(allocator, _chunk, size);
#else
pos_free(&pos_alloc_l2[0]);
#endif
}


#if defined(ARCHI_HAS_L1)
void alloc_init_l1(int cid)
Expand Down Expand Up @@ -126,4 +161,4 @@ void pi_fc_tcdm_free(void *_chunk, int size)
return pos_free(&pos_alloc_fc_tcdm, _chunk, size);
}

#endif
#endif
21 changes: 21 additions & 0 deletions lib/libc/minimal/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,24 @@ int pos_io_stop()

return 0;
}


void *malloc(size_t size)
{
// We will store the size of the chunk at the beginning of the allocated memory.
void *ptr = pos_alloc_user_data(size + sizeof(uint32_t));
if (ptr == NULL) return NULL;

*((uint32_t *)ptr) = size + sizeof(uint32_t);
void *user_ptr = (void *)(((uint32_t) ptr) + 1);

return user_ptr;
}


void free(void *ptr)
{
void *alloc_ptr = (void *)(((uint32_t *) ptr) - 1);
uint32_t size = *((uint32_t *) alloc_ptr);
pos_free_user_data(alloc_ptr, size);
}