pktbuf.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
  2. #include <esp8266.h>
  3. #include "pktbuf.h"
  4. #ifdef PKTBUF_DBG
  5. //static void ICACHE_FLASH_ATTR
  6. //PktBuf_Print(PktBuf *buf) {
  7. // os_printf("PktBuf:");
  8. // for (int i=-16; i<0; i++)
  9. // os_printf(" %02X", ((uint8_t*)buf)[i]);
  10. // os_printf(" %p", buf);
  11. // for (int i=0; i<16; i++)
  12. // os_printf(" %02X", ((uint8_t*)buf)[i]);
  13. // os_printf("\n");
  14. // os_printf("PktBuf: next=%p len=0x%04x\n",
  15. // ((void**)buf)[-4], ((uint16_t*)buf)[-6]);
  16. //}
  17. #endif
  18. PktBuf * ICACHE_FLASH_ATTR
  19. PktBuf_New(uint16_t length) {
  20. PktBuf *buf = os_zalloc(length+sizeof(PktBuf));
  21. buf->next = NULL;
  22. buf->filled = 0;
  23. //os_printf("PktBuf_New: %p l=%d->%d d=%p\n",
  24. // buf, length, length+sizeof(PktBuf), buf->data);
  25. return buf;
  26. }
  27. PktBuf * ICACHE_FLASH_ATTR
  28. PktBuf_Push(PktBuf *headBuf, PktBuf *buf) {
  29. if (headBuf == NULL) {
  30. //os_printf("PktBuf_Push: %p\n", buf);
  31. return buf;
  32. }
  33. PktBuf *h = headBuf;
  34. while (h->next != NULL) h = h->next;
  35. h->next = buf;
  36. //os_printf("PktBuf_Push: %p->..->%p\n", headBuf, buf);
  37. return headBuf;
  38. }
  39. PktBuf * ICACHE_FLASH_ATTR
  40. PktBuf_Unshift(PktBuf *headBuf, PktBuf *buf) {
  41. buf->next = headBuf;
  42. //os_printf("PktBuf_Unshift: %p->%p\n", buf, buf->next);
  43. return buf;
  44. }
  45. PktBuf * ICACHE_FLASH_ATTR
  46. PktBuf_Shift(PktBuf *headBuf) {
  47. PktBuf *buf = headBuf->next;
  48. headBuf->next = NULL;
  49. //os_printf("PktBuf_Shift: (%p)->%p\n", headBuf, buf);
  50. return buf;
  51. }
  52. PktBuf * ICACHE_FLASH_ATTR
  53. PktBuf_ShiftFree(PktBuf *headBuf) {
  54. PktBuf *buf = headBuf->next;
  55. //os_printf("PktBuf_ShiftFree: (%p)->%p\n", headBuf, buf);
  56. os_free(headBuf);
  57. return buf;
  58. }