mm_gran.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /****************************************************************************
  2. * mm/mm_gran/mm_gran.h
  3. *
  4. * Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __MM_MM_GRAN_MM_GRAN_H
  36. #define __MM_MM_GRAN_MM_GRAN_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <stdint.h>
  42. #include <arch/types.h>
  43. #include <nuttx/mm/gran.h>
  44. #include <nuttx/semaphore.h>
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. /* Sizes of things */
  49. #define SIZEOF_GAT(n) \
  50. ((n + 31) >> 5)
  51. #define SIZEOF_GRAN_S(n) \
  52. (sizeof(struct gran_s) + sizeof(uint32_t) * (SIZEOF_GAT(n) - 1))
  53. /* Debug */
  54. #ifdef CONFIG_DEBUG_GRAM
  55. # define granerr _err
  56. # define granwarn _warn
  57. # define graninfo _info
  58. #else
  59. # define granerr merr
  60. # define granwarn mwarn
  61. # define graninfo minfo
  62. #endif
  63. /****************************************************************************
  64. * Public Types
  65. ****************************************************************************/
  66. /* This structure represents the state of one granule allocation */
  67. struct gran_s
  68. {
  69. uint8_t log2gran; /* Log base 2 of the size of one granule */
  70. uint16_t ngranules; /* The total number of (aligned) granules in the heap */
  71. #ifdef CONFIG_GRAN_INTR
  72. irqstate_t irqstate; /* For exclusive access to the GAT */
  73. #else
  74. sem_t exclsem; /* For exclusive access to the GAT */
  75. #endif
  76. uintptr_t heapstart; /* The aligned start of the granule heap */
  77. uint32_t gat[1]; /* Start of the granule allocation table */
  78. };
  79. /****************************************************************************
  80. * Public Function Prototypes
  81. ****************************************************************************/
  82. /****************************************************************************
  83. * Name: gran_enter_critical and gran_leave_critical
  84. *
  85. * Description:
  86. * Critical section management for the granule allocator.
  87. *
  88. * Input Parameters:
  89. * priv - Pointer to the gran state
  90. *
  91. * Returned Value:
  92. * gran_enter_critical() may return any error reported by
  93. * nxsem_wait_uninterruptible()
  94. *
  95. ****************************************************************************/
  96. int gran_enter_critical(FAR struct gran_s *priv);
  97. void gran_leave_critical(FAR struct gran_s *priv);
  98. /****************************************************************************
  99. * Name: gran_mark_allocated
  100. *
  101. * Description:
  102. * Mark a range of granules as allocated.
  103. *
  104. * Input Parameters:
  105. * priv - The granule heap state structure.
  106. * alloc - The address of the allocation.
  107. * ngranules - The number of granules allocated
  108. *
  109. * Returned Value:
  110. * None
  111. *
  112. ****************************************************************************/
  113. void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
  114. unsigned int ngranules);
  115. #endif /* __MM_MM_GRAN_MM_GRAN_H */