mm/mm_heap: define MM_MASK_BIT as significant bits mask

using MM_MASK_BIT to expand different bits of preceding field to store the
block status.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2022-10-25 22:52:02 +08:00 committed by Xiang Xiao
parent 5982cafbe0
commit 579e47347e
7 changed files with 17 additions and 15 deletions

View File

@ -114,6 +114,7 @@
*/ */
#define MM_ALLOC_BIT 0x1 #define MM_ALLOC_BIT 0x1
#define MM_MASK_BIT MM_ALLOC_BIT
#ifdef CONFIG_MM_SMALL #ifdef CONFIG_MM_SMALL
# define MMSIZE_MAX UINT16_MAX # define MMSIZE_MAX UINT16_MAX
#else #else

View File

@ -86,7 +86,7 @@ void mm_foreach(FAR struct mm_heap_s *heap, mmchunk_handler_t handler,
handler(node, arg); handler(node, arg);
DEBUGASSERT(prev == NULL || DEBUGASSERT(prev == NULL ||
prev->size == (node->preceding & ~MM_ALLOC_BIT)); prev->size == (node->preceding & ~MM_MASK_BIT));
prev = node; prev = node;
} }

View File

@ -107,12 +107,12 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
DEBUGASSERT(node->preceding & MM_ALLOC_BIT); DEBUGASSERT(node->preceding & MM_ALLOC_BIT);
node->preceding &= ~MM_ALLOC_BIT; node->preceding &= ~MM_MASK_BIT;
/* Check if the following node is free and, if so, merge it */ /* Check if the following node is free and, if so, merge it */
next = (FAR struct mm_freenode_s *)((FAR char *)node + node->size); next = (FAR struct mm_freenode_s *)((FAR char *)node + node->size);
DEBUGASSERT((next->preceding & ~MM_ALLOC_BIT) == node->size); DEBUGASSERT((next->preceding & ~MM_MASK_BIT) == node->size);
if ((next->preceding & MM_ALLOC_BIT) == 0) if ((next->preceding & MM_ALLOC_BIT) == 0)
{ {
FAR struct mm_allocnode_s *andbeyond; FAR struct mm_allocnode_s *andbeyond;
@ -140,7 +140,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
node->size += next->size; node->size += next->size;
andbeyond->preceding = node->size | andbeyond->preceding = node->size |
(andbeyond->preceding & MM_ALLOC_BIT); (andbeyond->preceding & MM_MASK_BIT);
next = (FAR struct mm_freenode_s *)andbeyond; next = (FAR struct mm_freenode_s *)andbeyond;
} }
@ -149,7 +149,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
*/ */
prev = (FAR struct mm_freenode_s *)((FAR char *)node - node->preceding); prev = (FAR struct mm_freenode_s *)((FAR char *)node - node->preceding);
DEBUGASSERT((node->preceding & ~MM_ALLOC_BIT) == prev->size); DEBUGASSERT((node->preceding & ~MM_MASK_BIT) == prev->size);
if ((prev->preceding & MM_ALLOC_BIT) == 0) if ((prev->preceding & MM_ALLOC_BIT) == 0)
{ {
/* Remove the node. There must be a predecessor, but there may /* Remove the node. There must be a predecessor, but there may
@ -166,7 +166,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
/* Then merge the two chunks */ /* Then merge the two chunks */
prev->size += node->size; prev->size += node->size;
next->preceding = prev->size | (next->preceding & MM_ALLOC_BIT); next->preceding = prev->size | (next->preceding & MM_MASK_BIT);
node = prev; node = prev;
} }

View File

@ -221,7 +221,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
* the allocated flag. * the allocated flag.
*/ */
next->preceding = remaining | (next->preceding & MM_ALLOC_BIT); next->preceding = remaining | (next->preceding & MM_MASK_BIT);
/* Add the remainder back into the nodelist */ /* Add the remainder back into the nodelist */

View File

@ -184,7 +184,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
/* Reduce the size of the original chunk and mark it not allocated, */ /* Reduce the size of the original chunk and mark it not allocated, */
node->size = precedingsize; node->size = precedingsize;
node->preceding &= ~MM_ALLOC_BIT; node->preceding &= ~MM_MASK_BIT;
/* Fix the preceding size of the next node */ /* Fix the preceding size of the next node */

View File

@ -151,7 +151,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
} }
prev = (FAR struct mm_freenode_s *) prev = (FAR struct mm_freenode_s *)
((FAR char *)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT)); ((FAR char *)oldnode - (oldnode->preceding & ~MM_MASK_BIT));
if ((prev->preceding & MM_ALLOC_BIT) == 0) if ((prev->preceding & MM_ALLOC_BIT) == 0)
{ {
prevsize = prev->size; prevsize = prev->size;
@ -253,7 +253,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
newnode->size = oldsize + takeprev; newnode->size = oldsize + takeprev;
newnode->preceding = prev->size | MM_ALLOC_BIT; newnode->preceding = prev->size | MM_ALLOC_BIT;
next->preceding = newnode->size | next->preceding = newnode->size |
(next->preceding & MM_ALLOC_BIT); (next->preceding & MM_MASK_BIT);
/* Return the previous free node to the nodelist /* Return the previous free node to the nodelist
* (with the new size) * (with the new size)
@ -268,7 +268,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
newnode->size += oldsize; newnode->size += oldsize;
newnode->preceding |= MM_ALLOC_BIT; newnode->preceding |= MM_ALLOC_BIT;
next->preceding = newnode->size | next->preceding = newnode->size |
(next->preceding & MM_ALLOC_BIT); (next->preceding & MM_MASK_BIT);
} }
newmem = (FAR void *)((FAR char *)newnode + SIZEOF_MM_ALLOCNODE); newmem = (FAR void *)((FAR char *)newnode + SIZEOF_MM_ALLOCNODE);
@ -321,7 +321,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
DEBUGASSERT(newnode->size >= SIZEOF_MM_FREENODE); DEBUGASSERT(newnode->size >= SIZEOF_MM_FREENODE);
newnode->preceding = oldnode->size; newnode->preceding = oldnode->size;
andbeyond->preceding = newnode->size | andbeyond->preceding = newnode->size |
(andbeyond->preceding & MM_ALLOC_BIT); (andbeyond->preceding & MM_MASK_BIT);
/* Add the new free node to the nodelist (with the new size) */ /* Add the new free node to the nodelist (with the new size) */
@ -332,7 +332,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
/* Yes, just update some pointers. */ /* Yes, just update some pointers. */
andbeyond->preceding = oldnode->size | andbeyond->preceding = oldnode->size |
(andbeyond->preceding & MM_ALLOC_BIT); (andbeyond->preceding & MM_MASK_BIT);
} }
} }

View File

@ -95,7 +95,7 @@ void mm_shrinkchunk(FAR struct mm_heap_s *heap,
newnode->preceding = size; newnode->preceding = size;
node->size = size; node->size = size;
andbeyond->preceding = newnode->size | andbeyond->preceding = newnode->size |
(andbeyond->preceding & MM_ALLOC_BIT); (andbeyond->preceding & MM_MASK_BIT);
/* Add the new node to the freenodelist */ /* Add the new node to the freenodelist */
@ -121,7 +121,8 @@ void mm_shrinkchunk(FAR struct mm_heap_s *heap,
newnode->size = node->size - size; newnode->size = node->size - size;
newnode->preceding = size; newnode->preceding = size;
node->size = size; node->size = size;
next->preceding = newnode->size | MM_ALLOC_BIT; next->preceding = newnode->size |
(next->preceding & MM_MASK_BIT);
/* Add the new node to the freenodelist */ /* Add the new node to the freenodelist */