lwp_memheap.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * File : lwp_memheap.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. */
  23. #ifndef __LWP_MEMHEAP_H__
  24. #define __LWP_MEMHEAP_H__
  25. #include <stdint.h>
  26. #include <rtthread.h>
  27. /**
  28. * memory item on the heap
  29. */
  30. struct rt_lwp_memheap_item
  31. {
  32. rt_uint32_t magic; /**< magic number for memheap */
  33. struct rt_lwp_memheap *pool_ptr; /**< point of pool */
  34. struct rt_lwp_memheap_item *next; /**< next memheap item */
  35. struct rt_lwp_memheap_item *prev; /**< prev memheap item */
  36. struct rt_lwp_memheap_item *next_free; /**< next free memheap item */
  37. struct rt_lwp_memheap_item *prev_free; /**< prev free memheap item */
  38. };
  39. /**
  40. * Base structure of memory heap object
  41. */
  42. struct rt_lwp_memheap
  43. {
  44. struct rt_object parent; /**< inherit from rt_object */
  45. void *start_addr; /**< pool start address and size */
  46. rt_uint32_t pool_size; /**< pool size */
  47. rt_uint32_t available_size; /**< available size */
  48. rt_uint32_t max_used_size; /**< maximum allocated size */
  49. struct rt_lwp_memheap_item *block_list; /**< used block list */
  50. struct rt_lwp_memheap_item *free_list; /**< free block list */
  51. struct rt_lwp_memheap_item free_header; /**< free block list header */
  52. struct rt_semaphore lock; /**< semaphore lock */
  53. rt_list_t mlist;
  54. };
  55. extern rt_err_t rt_lwp_memheap_init(struct rt_lwp_memheap *memheap, const char *name, void *start_addr, rt_uint32_t size);
  56. extern void *rt_lwp_memheap_alloc(struct rt_lwp_memheap *heap, rt_uint32_t size);
  57. extern void rt_lwp_memheap_free(void *ptr);
  58. extern void *rt_lwp_memheap_realloc(struct rt_lwp_memheap *heap, void *ptr, rt_size_t newsize);
  59. extern rt_bool_t rt_lwp_memheap_is_empty(struct rt_lwp_memheap *memheap);
  60. extern rt_bool_t rt_lwp_memheap_unavailable_size_get(void);
  61. #endif