USBDevice memory leaks fixes

Fixes memory leaks in USBMSD, USBSerial (CircBuffer) and the KL25Z USB
HAL.
Original author: Erik Olieman, a few changes by Bogdan Marinescu.
pull/62/merge
Erik Olieman 2013-09-11 16:58:34 +03:00 committed by Bogdan Marinescu
parent 94ff741989
commit c255ca2959
4 changed files with 44 additions and 6 deletions

View File

@ -169,6 +169,16 @@ void USBHAL::disconnect(void) {
USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK; USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
// Pull up disable // Pull up disable
USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK; USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
//Free buffers if required:
for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
free(endpoint_buffer[i]);
endpoint_buffer[i] = NULL;
}
free(endpoint_buffer_iso[2]);
endpoint_buffer_iso[2] = NULL;
free(endpoint_buffer_iso[0]);
endpoint_buffer_iso[0] = NULL;
} }
void USBHAL::configureDevice(void) { void USBHAL::configureDevice(void) {
@ -200,18 +210,22 @@ bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flag
if ((flags & ISOCHRONOUS) == 0) { if ((flags & ISOCHRONOUS) == 0) {
handshake_flag = USB_ENDPT_EPHSHK_MASK; handshake_flag = USB_ENDPT_EPHSHK_MASK;
if (IN_EP(endpoint)) { if (IN_EP(endpoint)) {
endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )] = (uint8_t *) malloc (64*2); if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0]; endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64*2);
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
} else { } else {
endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )] = (uint8_t *) malloc (64*2); if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0]; endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64*2);
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
} }
} else { } else {
if (IN_EP(endpoint)) { if (IN_EP(endpoint)) {
endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2); if (endpoint_buffer_iso[2] == NULL)
endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
buf = &endpoint_buffer_iso[2][0]; buf = &endpoint_buffer_iso[2][0];
} else { } else {
endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2); if (endpoint_buffer_iso[0] == NULL)
endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
buf = &endpoint_buffer_iso[0][0]; buf = &endpoint_buffer_iso[0][0];
} }
} }

View File

@ -67,8 +67,12 @@ USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release
stage = READ_CBW; stage = READ_CBW;
memset((void *)&cbw, 0, sizeof(CBW)); memset((void *)&cbw, 0, sizeof(CBW));
memset((void *)&csw, 0, sizeof(CSW)); memset((void *)&csw, 0, sizeof(CSW));
page = NULL;
} }
USBMSD::~USBMSD() {
disconnect();
}
// Called in ISR context to process a class specific request // Called in ISR context to process a class specific request
@ -117,6 +121,7 @@ bool USBMSD::connect() {
if (BlockCount > 0) { if (BlockCount > 0) {
BlockSize = MemorySize / BlockCount; BlockSize = MemorySize / BlockCount;
if (BlockSize != 0) { if (BlockSize != 0) {
free(page);
page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t)); page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
if (page == NULL) if (page == NULL)
return false; return false;
@ -130,6 +135,12 @@ bool USBMSD::connect() {
return true; return true;
} }
void USBMSD::disconnect() {
//De-allocate MSD page size:
free(page);
page = NULL;
USBDevice::disconnect();
}
void USBMSD::reset() { void USBMSD::reset() {
stage = READ_CBW; stage = READ_CBW;

View File

@ -74,6 +74,15 @@ public:
*/ */
bool connect(); bool connect();
/**
* Disconnect the USB MSD device.
*/
void disconnect();
/**
* Destructor
*/
~USBMSD();
protected: protected:

View File

@ -29,6 +29,10 @@ public:
buf = (T *)malloc(size * sizeof(T)); buf = (T *)malloc(size * sizeof(T));
}; };
~CircBuffer() {
free(buf);
}
bool isFull() { bool isFull() {
return ((write + 1) % size == read); return ((write + 1) % size == read);
}; };