Browse Source

Memory management functions

main
PaulStoffregen 7 years ago
parent
commit
7997b07157
2 changed files with 46 additions and 16 deletions
  1. +1
    -0
      host.h
  2. +45
    -16
      k66_usbhost.ino

+ 1
- 0
host.h View File

uint32_t unused[4]; uint32_t unused[4];
}; };


void init_Device_Pipe_Transfer_memory(void);
Device_t * allocate_Device(void); Device_t * allocate_Device(void);
void free_Device(Device_t *q); void free_Device(Device_t *q);
Pipe_t * allocate_Pipe(void); Pipe_t * allocate_Pipe(void);

+ 45
- 16
k66_usbhost.ino View File

} }
print(" reset waited ", count); print(" reset waited ", count);


init_Device_Pipe_Transfer_memory();
for (int i=0; i < 32; i++) { for (int i=0; i < 32; i++) {
periodictable[i] = 1; periodictable[i] = 1;
} }
} }




// Memory allocation... for now, just simplest leaky way to get started

// Memory allocation

static Device_t memory_Device[3];
static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64)));
static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64)));

Device_t * free_Device_list = NULL;
Pipe_t * free_Pipe_list = NULL;
Transfer_t * free_Transfer_list = NULL;

void init_Device_Pipe_Transfer_memory(void)
{
Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t);
for (Device_t *device = memory_Device; device < end_device; device++) {
free_Device(device);
}
Pipe_t *end_pipe = memory_Pipe + sizeof(memory_Pipe)/sizeof(Pipe_t);
for (Pipe_t *pipe = memory_Pipe; pipe < end_pipe; pipe++) {
free_Pipe(pipe);
}
Transfer_t *end_transfer = memory_Transfer + sizeof(memory_Transfer)/sizeof(Transfer_t);
for (Transfer_t *transfer = memory_Transfer; transfer < end_transfer; transfer++) {
free_Transfer(transfer);
}
}


Device_t * allocate_Device(void) Device_t * allocate_Device(void)
{ {
static Device_t mem[3];
static size_t count=0;
if (count >= sizeof(mem)/sizeof(Device_t)) return NULL;
return &mem[count++];
Device_t *device = free_Device_list;
if (device) free_Device_list = *(Device_t **)device;
return device;
} }


void free_Device(Device_t *q)
void free_Device(Device_t *device)
{ {
*(Device_t **)device = free_Device_list;
free_Device_list = device;
} }


Pipe_t * allocate_Pipe(void) Pipe_t * allocate_Pipe(void)
{ {
static Pipe_t mem[6] __attribute__ ((aligned(64)));
static size_t count=0;
if (count >= sizeof(mem)/sizeof(Pipe_t)) return NULL;
return &mem[count++];
Pipe_t *pipe = free_Pipe_list;
if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
return pipe;
} }


void free_Pipe(Pipe_t *q)
void free_Pipe(Pipe_t *pipe)
{ {
*(Pipe_t **)pipe = free_Pipe_list;
free_Pipe_list = pipe;
} }


Transfer_t * allocate_Transfer(void) Transfer_t * allocate_Transfer(void)
{ {
static Transfer_t mem[22] __attribute__ ((aligned(64)));
static size_t count=0;
if (count >= sizeof(mem)/sizeof(Transfer_t)) return NULL;
return &mem[count++];
Transfer_t *transfer = free_Transfer_list;
if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
return transfer;
} }


void free_Transfer(Transfer_t *q)
void free_Transfer(Transfer_t *transfer)
{ {
*(Transfer_t **)transfer = free_Transfer_list;
free_Transfer_list = transfer;
} }



Loading…
Cancel
Save