README.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. g_myheap = mm_initialize(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. Debugging:
  56. Please follow these steps to hook all memory related routines:
  57. 1.Add a new header file(e.g. xxx_malloc.h):
  58. ...
  59. #include <malloc.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <strings.h>
  63. #ifndef __ASSEMBLY__
  64. FAR void *xxx_malloc(FAR const char *file, int line, size_t size);
  65. void xxx_free(FAR const char *file, int line, FAR const void *ptr);
  66. FAR void *xxx_memcpy(FAR const char *file, int line,
  67. FAR void *dst, FAR const void *src, size_t len);
  68. ...
  69. #define malloc(s) xxx_malloc(__FILE__, __LINE__, s)
  70. #define free(p) xxx_free(__FILE__, __LINE__, p)
  71. #define memcpy(d, s, l) xxx_memcpy(__FILE__, __LINE__, d, s, l)
  72. ...
  73. #endif
  74. ...
  75. 2.Implement xxx_malloc, xxx_free, xxx_memcpy... in source code, you can:
  76. a.Modify some arguments(e.g. extend the allocation size for redzone)
  77. d.Check the critical arguments(e.g. pointer and length) in the range
  78. b.Forward to the original implementation(call malloc/free/memcpy)
  79. c.Attach the context info(e.g. file and line) before return
  80. 3.Enable the hook by either:
  81. a.Include xxx_malloc.h in your source code to hook one file
  82. b.Add -include xxx_malloc.h to CFLAGS to hook all source code
  83. 2) Granule Allocator.
  84. A non-standard granule allocator is also available in this directory The
  85. granule allocator allocates memory in units of a fixed sized block ("granule").
  86. Allocations may be aligned to a user-provided address boundary.
  87. The granule allocator interfaces are defined in nuttx/include/nuttx/mm/gran.h.
  88. The granule allocator consists of these files in this directory:
  89. mm_gran.h, mm_granalloc.c, mm_grancritical.c, mm_granfree.c
  90. mm_graninit.c
  91. The granule allocator is not used anywhere within the base NuttX code
  92. as of this writing. The intent of the granule allocator is to provide
  93. a tool to support platform-specific management of aligned DMA memory.
  94. NOTE: Because each granule may be aligned and each allocation is in
  95. units of the granule size, selection of the granule size is important:
  96. Larger granules will give better performance and less overhead but more
  97. losses of memory due to quantization waste. Additional memory waste
  98. can occur from alignment; Of course, heap alignment should no be
  99. used unless (a) you are using the granule allocator to manage DMA memory
  100. and (b) your hardware has specific memory alignment requirements.
  101. The current implementation also restricts the maximum allocation size
  102. to 32 granules. That restriction could be eliminated with some
  103. additional coding effort, but currently requires larger granule
  104. sizes for larger allocations.
  105. General Usage Example.
  106. This is an example using the GCC section attribute to position a DMA
  107. heap in memory (logic in the linker script would assign the section
  108. .dmaheap to the DMA memory.
  109. FAR uint32_t g_dmaheap[DMAHEAP_SIZE] __attribute__((section(.dmaheap)));
  110. The heap is created by calling gran_initialize. Here the granule size
  111. is set to 64 bytes and the alignment to 16 bytes:
  112. GRAN_HANDLE handle = gran_initialize(g_dmaheap, DMAHEAP_SIZE, 6, 4);
  113. Then the GRAN_HANDLE can be used to allocate memory:
  114. FAR uint8_t *dma_memory = (FAR uint8_t *)gran_alloc(handle, 47);
  115. The actual memory allocates will be 64 byte (wasting 17 bytes) and
  116. will be aligned at least to (1 << log2align).
  117. Sub-Directories:
  118. mm/mm_gran - Holds the granule allocation logic
  119. 3) Page Allocator
  120. The page allocator is an application of the granule allocator. It is a
  121. special purpose memory allocator intended to allocate physical memory
  122. pages for use with systems that have a memory management unit (MMU).
  123. Sub-Directories:
  124. mm/mm_gran - The page allocator cohabits the same directory as the
  125. granule allocator.
  126. 4) Shared Memory Management
  127. When NuttX is build in kernel mode with a separate, privileged, kernel-
  128. mode address space and multiple, unprivileged, user-mode address spaces,
  129. then shared memory regions must also be managed. Shared memory regions
  130. are user-accessible memory regions that can be attached into the user
  131. process address space for sharing between user process.
  132. Sub-Directories:
  133. mm/shm - The shared memory logic
  134. The shared memory management logic has its own README file that can be
  135. found at nuttx/mm/shm/README.txt.
  136. 5) I/O Buffers
  137. The iob subdirectory contains a simple allocator of I/O buffers. These
  138. I/O buffers, IOBs, are used extensively for networking but are generally
  139. available for usage by drivers. The I/O buffers have these properties:
  140. 1. Uses a pool of a fixed number of fixed fixed size buffers.
  141. 2. Free buffers are retained in free list: When a buffer is allocated
  142. it is removed from the free list; when a buffer is freed it is
  143. returned to the free list.
  144. 3. The calling application will wait if there are not free buffers.