mirror of
https://gitlab.rtems.org/rtems/rtos/rtems.git
synced 2026-09-22 01:56:28 +08:00
Added support functions for greedy heap allocation
Various tests must check program paths that result due to failed memory allocations from the heap. To avoid tinkering with internal heap structures throughout the test code these functions should be used.
This commit is contained in:
@@ -107,7 +107,8 @@ MALLOC_C_FILES = src/malloc_initialize.c src/calloc.c src/malloc.c \
|
||||
src/malloc_statistics_helpers.c src/posix_memalign.c \
|
||||
src/rtems_memalign.c src/malloc_deferred.c src/malloc_sbrk_helpers.c \
|
||||
src/malloc_dirtier.c src/malloc_p.h src/rtems_malloc.c \
|
||||
src/rtems_heap_extend.c
|
||||
src/rtems_heap_extend.c \
|
||||
src/rtems_heap_greedy.c
|
||||
|
||||
PASSWORD_GROUP_C_FILES = src/getpwent.c
|
||||
|
||||
|
||||
@@ -184,6 +184,24 @@ rtems_status_code rtems_heap_extend(
|
||||
uintptr_t area_size
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Greedy allocate that empties the heap.
|
||||
*
|
||||
* Afterward the heap has at most @a remaining_free_space free space left in
|
||||
* one free block. All other blocks are used.
|
||||
*
|
||||
* @see rtems_heap_greedy_free().
|
||||
*/
|
||||
void *rtems_heap_greedy_allocate( size_t remaining_free_space );
|
||||
|
||||
/**
|
||||
* @brief Frees space of a greedy allocation.
|
||||
*
|
||||
* The @a opaque argument must be the return value of
|
||||
* rtems_heap_greedy_allocate().
|
||||
*/
|
||||
void rtems_heap_greedy_free( void *opaque );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2012 embedded brains GmbH. All rights reserved.
|
||||
*
|
||||
* embedded brains GmbH
|
||||
* Obere Lagerstr. 30
|
||||
* 82178 Puchheim
|
||||
* Germany
|
||||
* <rtems@embedded-brains.de>
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rtems.com/license/LICENSE.
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include "malloc_p.h"
|
||||
|
||||
void *rtems_heap_greedy_allocate( size_t remaining_free_space )
|
||||
{
|
||||
void *opaque;
|
||||
|
||||
_RTEMS_Lock_allocator();
|
||||
opaque = _Heap_Greedy_allocate( RTEMS_Malloc_Heap, remaining_free_space );
|
||||
_RTEMS_Unlock_allocator();
|
||||
|
||||
return opaque;
|
||||
}
|
||||
|
||||
void rtems_heap_greedy_free( void *opaque )
|
||||
{
|
||||
_RTEMS_Lock_allocator();
|
||||
_Heap_Greedy_free( RTEMS_Malloc_Heap, opaque );
|
||||
_RTEMS_Unlock_allocator();
|
||||
}
|
||||
@@ -181,7 +181,8 @@ endif
|
||||
## HEAP_C_FILES
|
||||
libscore_a_SOURCES += src/heap.c src/heapallocate.c src/heapextend.c \
|
||||
src/heapfree.c src/heapsizeofuserarea.c src/heapwalk.c src/heapgetinfo.c \
|
||||
src/heapgetfreeinfo.c src/heapresizeblock.c src/heapiterate.c
|
||||
src/heapgetfreeinfo.c src/heapresizeblock.c src/heapiterate.c \
|
||||
src/heapgreedy.c
|
||||
|
||||
## OBJECT_C_FILES
|
||||
libscore_a_SOURCES += src/objectallocate.c src/objectclose.c \
|
||||
|
||||
@@ -548,6 +548,29 @@ void _Heap_Iterate(
|
||||
void *visitor_arg
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Greedy allocate that empties the heap.
|
||||
*
|
||||
* Afterward the heap has at most @a remaining_free_space free space left in
|
||||
* one free block. All other blocks are used.
|
||||
*
|
||||
* @see _Heap_Greedy_free().
|
||||
*/
|
||||
Heap_Block *_Heap_Greedy_allocate(
|
||||
Heap_Control *heap,
|
||||
uintptr_t remaining_free_space
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Frees blocks of a greedy allocation.
|
||||
*
|
||||
* The @a blocks must be the return value of _Heap_Greedy_allocate().
|
||||
*/
|
||||
void _Heap_Greedy_free(
|
||||
Heap_Control *heap,
|
||||
Heap_Block *blocks
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Returns information about used and free blocks for the heap @a heap
|
||||
* in @a info.
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2012 embedded brains GmbH. All rights reserved.
|
||||
*
|
||||
* embedded brains GmbH
|
||||
* Obere Lagerstr. 30
|
||||
* 82178 Puchheim
|
||||
* Germany
|
||||
* <rtems@embedded-brains.de>
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rtems.com/license/LICENSE.
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <rtems/score/heap.h>
|
||||
|
||||
Heap_Block *_Heap_Greedy_allocate(
|
||||
Heap_Control *heap,
|
||||
uintptr_t remaining_free_space
|
||||
)
|
||||
{
|
||||
void *free_space = remaining_free_space > 0 ?
|
||||
_Heap_Allocate( heap, remaining_free_space )
|
||||
: NULL;
|
||||
Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap );
|
||||
Heap_Block *current = _Heap_Free_list_first( heap );
|
||||
Heap_Block *blocks = NULL;
|
||||
|
||||
while ( current != free_list_tail ) {
|
||||
_Heap_Block_allocate(
|
||||
heap,
|
||||
current,
|
||||
_Heap_Alloc_area_of_block( current ),
|
||||
_Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE
|
||||
);
|
||||
|
||||
current->next = blocks;
|
||||
blocks = current;
|
||||
current = _Heap_Free_list_first( heap );
|
||||
}
|
||||
|
||||
_Heap_Free( heap, free_space );
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
void _Heap_Greedy_free(
|
||||
Heap_Control *heap,
|
||||
Heap_Block *blocks
|
||||
)
|
||||
{
|
||||
while ( blocks != NULL ) {
|
||||
Heap_Block *current = blocks;
|
||||
|
||||
blocks = blocks->next;
|
||||
_Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user