diff --git a/host.h b/host.h index 2a15c3a..6e4fe87 100644 --- a/host.h +++ b/host.h @@ -95,6 +95,7 @@ struct Transfer_struct { uint32_t unused[4]; }; +void init_Device_Pipe_Transfer_memory(void); Device_t * allocate_Device(void); void free_Device(Device_t *q); Pipe_t * allocate_Pipe(void); diff --git a/k66_usbhost.ino b/k66_usbhost.ino index 79b34b6..35fed59 100644 --- a/k66_usbhost.ino +++ b/k66_usbhost.ino @@ -109,6 +109,7 @@ void setup() } print(" reset waited ", count); + init_Device_Pipe_Transfer_memory(); for (int i=0; i < 32; i++) { periodictable[i] = 1; } @@ -648,41 +649,69 @@ void print(const char *s, int num) } -// 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) { - 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) { - 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) { - 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; }