mirror of
https://github.com/bmartini/zynq-axis.git
synced 2024-09-05 19:19:27 +08:00
Add function to library to allocate CMA memory
Arrays that will be used to pass data between the host and fpga need to be page aligned and be slices of the DMA array the driver has reserved. These new functions allocate slices from the DMA array and ensures that the starting address is page aligned.
This commit is contained in:
parent
2f10fb29d2
commit
00541db077
@ -58,3 +58,48 @@ int cfg_read(unsigned int addr)
|
|||||||
|
|
||||||
return *reg;
|
return *reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *mem_alloc(const int length, const int byte_nb)
|
||||||
|
{
|
||||||
|
assert(mem);
|
||||||
|
|
||||||
|
// calculate start of next array
|
||||||
|
int next_offset = mem_offset + mem_alloc_size(length, byte_nb);
|
||||||
|
|
||||||
|
if (MEM_SIZE < next_offset) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"ERROR <Out Of Memory> attempted total memory allocation: %i bytes\n",
|
||||||
|
next_offset);
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
// pointer to new array
|
||||||
|
void *ptr = &mem[mem_offset];
|
||||||
|
|
||||||
|
// update memory pointer to the next free area
|
||||||
|
mem_offset = next_offset;
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mem_alloc_size(const int length, const int byte_nb)
|
||||||
|
{
|
||||||
|
assert(length);
|
||||||
|
assert(byte_nb);
|
||||||
|
|
||||||
|
int size = length * byte_nb;
|
||||||
|
int remainder = size % sysconf(_SC_PAGESIZE);
|
||||||
|
|
||||||
|
if (0 != remainder) {
|
||||||
|
size = size + sysconf(_SC_PAGESIZE) - remainder;
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mem_alloc_length(const int length, const int byte_nb)
|
||||||
|
{
|
||||||
|
assert(length);
|
||||||
|
assert(byte_nb);
|
||||||
|
|
||||||
|
return (mem_alloc_size(length, byte_nb) / byte_nb);
|
||||||
|
}
|
||||||
|
@ -18,6 +18,12 @@ extern "C" {
|
|||||||
|
|
||||||
int cfg_read(unsigned int addr);
|
int cfg_read(unsigned int addr);
|
||||||
|
|
||||||
|
// dma memory
|
||||||
|
void *mem_alloc(const int length, const int byte_nb);
|
||||||
|
|
||||||
|
int mem_alloc_size(const int length, const int byte_nb);
|
||||||
|
|
||||||
|
int mem_alloc_length(const int length, const int byte_nb);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user