32 #define __INCLUDE_FROM_USB_DRIVER
35 #ifdef USB_CAN_BE_HOST
37 #include "../../../Common/Common.h"
45 #define TEST_NEW_ALLOC 0
49 typedef struct MemBlockInfo_t {
64 typedef struct MemBlockInfo_t {
74 #define ALIGN_FOUR_BYTES (4) // FIXME only m3 is 1 byte alignment
78 #define HEADER_SIZE (sizeof(sMemBlockInfo))
79 #define HEADER_POINTER(x) ((uint8_t *)x - sizeof(sMemBlockInfo))
80 #define NEXT_BLOCK(x) ((PMemBlockInfo) ( ((x)->next==0) ? 0 : ((uint32_t)head +(x)->next) ))
81 #define LINK_TO_THIS_BLOCK(x) (((uint32_t)(x))-((uint32_t)head))
88 PMemBlockInfo head = (PMemBlockInfo) USB_Mem_Buffer;
91 head->size = (Memory_Pool_Size & 0xfffffffc) -
HEADER_SIZE ;
93 head->type = MEM_FREE;
101 PMemBlockInfo freeBlock=
NULL, newBlock, blk_ptr =
NULL;
102 PMemBlockInfo head = (PMemBlockInfo) USB_Mem_Buffer;
105 for (blk_ptr = head; blk_ptr !=
NULL; blk_ptr =
NEXT_BLOCK(blk_ptr))
107 if (((blk_ptr->type == MEM_FREE)||(blk_ptr->type == MEM_PAD)) && (blk_ptr->size >= size))
113 blk_ptr->type = MEM_USED;
117 freeBlock->next = blk_ptr->next;
118 freeBlock->size = blk_ptr->size - (
HEADER_SIZE + size);
119 freeBlock->type = blk_ptr->type;
122 blk_ptr->size = size;
124 blk_ptr->type = MEM_USED;
127 if(num_aligned_bytes > 0)
130 if(pad_size == 0)
break;
132 if(blk_ptr->next != 0)
137 padBlkSize = num_aligned_bytes - pad_size -
HEADER_SIZE;
139 padBlkSize = 2*num_aligned_bytes - pad_size -
HEADER_SIZE;
141 if(padBlkSize > freeBlock->size)
143 blk_ptr->next = freeBlock->next;
144 blk_ptr->size = freeBlock->size + HEADER_SIZE + size;
145 blk_ptr->type = MEM_PAD;
149 newBlock = (PMemBlockInfo) (((
uint32_t) blk_ptr) + HEADER_SIZE + padBlkSize);
150 newBlock->next = freeBlock->next;
151 newBlock->type = freeBlock->type;
152 newBlock->size = freeBlock->size + HEADER_SIZE + size - padBlkSize;
154 blk_ptr->size = padBlkSize;
156 blk_ptr->type = MEM_PAD;
160 else if(blk_ptr->type == MEM_USED)
break;
164 if (blk_ptr ==
NULL) {
165 return ((uint8_t *)
NULL);
176 for (freeBlock = head; freeBlock !=
NULL; freeBlock =
NEXT_BLOCK(freeBlock))
178 if ((freeBlock->isFree == 1) && (freeBlock->size >= size))
185 if (blk_ptr ==
NULL) {
186 return ((uint8_t *)
NULL);
194 newBlock->isFree = 0;
197 freeBlock = (PMemBlockInfo) (((uint8_t *) blk_ptr) + size +
HEADER_SIZE);
198 freeBlock->next = blk_ptr->next;
199 freeBlock->size = blk_ptr->size - (
HEADER_SIZE + size);
200 freeBlock->isFree = 1;
204 newBlock->size = size;
206 newBlock->isFree = 0;
216 PMemBlockInfo head = (PMemBlockInfo) USB_Mem_Buffer;
217 PMemBlockInfo blk_ptr;
228 bool being_pad =
false;
229 if (blk_ptr->next != 0)
237 else being_pad =
true;
244 if ((prev->type == MEM_FREE)||(prev->type == MEM_PAD))
246 prev->size = prev->size + blk_ptr->size +
HEADER_SIZE;
247 prev->next = blk_ptr->next;
249 else if(being_pad ==
true)
250 blk_ptr->type = MEM_PAD;
257 if (blk_ptr->next != 0)
270 if (prev->isFree == 1)
272 prev->size = prev->size + blk_ptr->size +
HEADER_SIZE;
273 prev->next = blk_ptr->next;