void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb) { if (q == NULL) return; if (pBufStore[headBuf][sock].data != NULL) { WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf][sock].data, headBuf); // to avoid memory leak free the oldest buffer freetDataIdx(headBuf, sock); } u8_t* p = (u8_t*)calloc(q->tot_len,sizeof(u8_t)); if(p != NULL) { if (pbuf_copy_partial(q, p, q->tot_len,0) != q->tot_len) { WARN("pbuf_copy_partial failed: src:%p, dst:%p, len:%d\n", q, p, q->tot_len); free(p); p = NULL; return; } pBufStore[headBuf][sock].data = p; pBufStore[headBuf][sock].len = q->tot_len; pBufStore[headBuf][sock].idx = 0; pBufStore[headBuf][sock].pcb = _pcb; headBuf++; if (headBuf == MAX_PBUF_STORED) headBuf = 0; if (headBuf == tailBuf) WARN("Overwriting data [%d-%d]!\n", headBuf, tailBuf); INFO_UTIL("Insert: %p:%d-%d [%d,%d]\n", p, q->tot_len, p[0], headBuf, tailBuf); } }
uint16_t clearBuf(uint8_t sock) { uint16_t len = 0; unsigned char index = tailBuf[sock]; do { if (pBufStore[index][sock].data != NULL) { freetDataIdx(index,sock); } ++index; if (index == MAX_PBUF_STORED) index = 0; }while (index!=headBuf[sock]); tailBuf[sock]=index; return len; }
uint8_t* insertBuf(uint8_t sock, uint8_t* buf, uint16_t len) { DUMP(buf,len); if (sock>= MAX_SOCK_NUM) { WARN("Sock out of range: sock=%d", sock); return NULL; } if (pBufStore[headBuf[sock]][sock].data != NULL) { WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf[sock]][sock].data, headBuf[sock]); // to avoid memory leak free the oldest buffer freetDataIdx(headBuf[sock], sock); } u8_t* p = (u8_t*)calloc(len,sizeof(u8_t)); if(p != NULL) { memcpy(p, buf, len); pBufStore[headBuf[sock]][sock].data = p; pBufStore[headBuf[sock]][sock].len = len; pBufStore[headBuf[sock]][sock].idx = 0; pBufStore[headBuf[sock]][sock].pcb = getTTCP(sock, TTCP_MODE_TRANSMIT); headBuf[sock]++; if (headBuf[sock] == MAX_PBUF_STORED) headBuf[sock] = 0; if (headBuf[sock] == tailBuf[sock]) { WARN("Avoid to Overwrite data [%d-%d]!\n", headBuf[sock], tailBuf[sock]); if (headBuf[sock] != 0) --headBuf[sock]; else headBuf[sock] = MAX_PBUF_STORED-1; } INFO_UTIL("Insert[%d]: %p:%d-%d [%d,%d]\n", sock, p, len, p[0], headBuf[sock], tailBuf[sock]); } return p; }