README.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. mm/README.txt
  2. =============
  3. This directory contains the NuttX memory management logic. This include:
  4. 1) Standard Memory Management Functions:
  5. Standard Functions:
  6. The standard memory management functions as prototyped in stdlib.h as
  7. specified in the Base definitions volume of IEEE Std 1003.1-2001. This
  8. include the files:
  9. o Standard Interfaces: mm_malloc.c, mm_calloc.c, mm_realloc.c,
  10. mm_memalign.c, mm_free.c
  11. o Less-Standard Interfaces: mm_zalloc.c, mm_mallinfo.c
  12. o Internal Implementation: mm_initialize.c mm_sem.c mm_addfreechunk.c
  13. mm_size2ndx.c mm_shrinkchunk.c
  14. o Build and Configuration files: Kconfig, Makefile
  15. Memory Models:
  16. o Small Memory Model. If the MCU supports only 16-bit data addressing
  17. then the small memory model is automatically used. The maximum size
  18. of the heap is then 64K. The small memory model can also be forced
  19. MCUs with wider addressing by defining CONFIG_SMALL_MEMORY in the
  20. NuttX configuration file.
  21. o Large Memory Model. Otherwise, the allocator uses a model that
  22. supports a heap of up to 4G.
  23. This implementation uses a variable length allocator with the following
  24. properties:
  25. o Overhead: Either 8- or 4-bytes per allocation for large and small
  26. models, respectively.
  27. o Alignment: All allocations are aligned to 8- or 4-bytes for large
  28. and small models, respectively.
  29. Multiple Heaps:
  30. This allocator can be used to manage multiple heaps (albeit with some
  31. non-standard interfaces). A heap is represented by struct mm_heap_s
  32. as defined in the file include/nuttx/mm/mm.h. To create another heap
  33. instance, you would allocate a heap structure, most likely statically
  34. in memory:
  35. include <nuttx/mm/mm.h>
  36. static struct mm_heap_s g_myheap;
  37. Then initialize the heap using:
  38. mm_initialize(&g_myheap, myheap_start, myheap_size);
  39. Where mm_initialize() and all related interfaces are prototyped in the
  40. header file include/nuttx/mm/mm.h.
  41. After the new heap instance has been initialized, it can then be used
  42. with these almost familiar interfaces: mm_malloc(), mm_realloc(), mm_free(),
  43. etc. These are 'almost familiar' because they are analogous of the
  44. standard malloc(), realloc(), free(), etc. except that they expect a
  45. reference to the initialized heap structure as the first parameter.
  46. In fact, the standard malloc(), realloc(), free() use this same mechanism,
  47. but with a global heap structure called g_mmheap.
  48. User/Kernel Heaps
  49. This multiple heap capability is exploited in some of the more complex NuttX
  50. build configurations to provide separate kernel-mode and user-mode heaps.
  51. Sub-Directories:
  52. mm/mm_heap - Holds the common base logic for all heap allocators
  53. mm/umm_heap - Holds the user-mode memory allocation interfaces
  54. mm/kmm_heap - Holds the kernel-mode memory allocation interfaces
  55. 2) Granule Allocator.
  56. A non-standard granule allocator is also available in this directory The
  57. granule allocator allocates memory in units of a fixed sized block ("granule").
  58. Allocations may be aligned to a user-provided address boundary.
  59. The granule allocator interfaces are defined in nuttx/include/nuttx/mm/gran.h.
  60. The granule allocator consists of these files in this directory:
  61. mm_gran.h, mm_granalloc.c, mm_grancritical.c, mm_granfree.c
  62. mm_graninit.c
  63. The granule allocator is not used anywhere within the base NuttX code
  64. as of this writing. The intent of the granule allocator is to provide
  65. a tool to support platform-specific management of aligned DMA memory.
  66. NOTE: Because each granule may be aligned and each allocation is in
  67. units of the granule size, selection of the granule size is important:
  68. Larger granules will give better performance and less overhead but more
  69. losses of memory due to quantization waste. Additional memory waste
  70. can occur from alignment; Of course, heap alignment should no be
  71. used unless (a) you are using the granule allocator to manage DMA memory
  72. and (b) your hardware has specific memory alignment requirements.
  73. The current implementation also restricts the maximum allocation size
  74. to 32 granules. That restriction could be eliminated with some
  75. additional coding effort, but currently requires larger granule
  76. sizes for larger allocations.
  77. General Usage Example.
  78. This is an example using the GCC section attribute to position a DMA
  79. heap in memory (logic in the linker script would assign the section
  80. .dmaheap to the DMA memory.
  81. FAR uint32_t g_dmaheap[DMAHEAP_SIZE] __attribute__((section(.dmaheap)));
  82. The heap is created by calling gran_initialize. Here the granule size
  83. is set to 64 bytes and the alignment to 16 bytes:
  84. GRAN_HANDLE handle = gran_initialize(g_dmaheap, DMAHEAP_SIZE, 6, 4);
  85. Then the GRAN_HANDLE can be used to allocate memory:
  86. FAR uint8_t *dma_memory = (FAR uint8_t *)gran_alloc(handle, 47);
  87. The actual memory allocates will be 64 byte (wasting 17 bytes) and
  88. will be aligned at least to (1 << log2align).
  89. Sub-Directories:
  90. mm/mm_gran - Holds the granule allocation logic
  91. 3) Page Allocator
  92. The page allocator is an application of the granule allocator. It is a
  93. special purpose memory allocator intended to allocate physical memory
  94. pages for use with systems that have a memory management unit (MMU).
  95. Sub-Directories:
  96. mm/mm_gran - The page allocator cohabits the same directory as the
  97. granule allocator.
  98. 4) Shared Memory Management
  99. When NuttX is build in kernel mode with a separate, privileged, kernel-
  100. mode address space and multiple, unprivileged, user-mode address spaces,
  101. then shared memory regions must also be managed. Shared memory regions
  102. are user-accessible memory regions that can be attached into the user
  103. process address space for sharing between user process.
  104. Sub-Directories:
  105. mm/shm - The shared memory logic
  106. The shared memory management logic has its own README file that can be
  107. found at nuttx/mm/shm/README.txt.
  108. 5) I/O Buffers
  109. The iob subdirectory contains a simple allocator of I/O buffers. These
  110. I/O buffers, IOBs, are used extensively for networking but are generally
  111. available for usage by drivers. The I/O buffers have these properties:
  112. 1. Uses a pool of a fixed number of fixed fixed size buffers.
  113. 2. Free buffers are retained in free list: When a buffer is allocated
  114. it is removed from the free list; when a buffer is freed it is
  115. returned to the free list.
  116. 3. The calling application will wait if there are not free buffers.