Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief Greedy Allocate that Empties the Heap
0007  *  @ingroup MallocSupport
0008  */
0009 
0010 /*
0011  * Copyright (c) 2012 embedded brains GmbH & Co. KG
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include "malloc_p.h"
0040 
0041 #define SBRK_ALLOC_SIZE (128 * 1024UL * 1024UL)
0042 
0043 void *rtems_heap_greedy_allocate(
0044   const uintptr_t *block_sizes,
0045   size_t block_count
0046 )
0047 {
0048   Heap_Control *heap = RTEMS_Malloc_Heap;
0049   void *opaque;
0050 
0051   rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
0052 
0053   _RTEMS_Lock_allocator();
0054   opaque = _Heap_Greedy_allocate( RTEMS_Malloc_Heap, block_sizes, block_count );
0055   _RTEMS_Unlock_allocator();
0056 
0057   return opaque;
0058 }
0059 
0060 void *rtems_heap_greedy_allocate_all_except_largest(
0061   uintptr_t *allocatable_size
0062 )
0063 {
0064   Heap_Control *heap = RTEMS_Malloc_Heap;
0065   void *opaque;
0066 
0067   rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
0068 
0069   _RTEMS_Lock_allocator();
0070   opaque = _Heap_Greedy_allocate_all_except_largest(
0071     heap,
0072     allocatable_size
0073   );
0074   _RTEMS_Unlock_allocator();
0075 
0076   return opaque;
0077 }
0078 
0079 void rtems_heap_greedy_free( void *opaque )
0080 {
0081   _RTEMS_Lock_allocator();
0082   _Heap_Greedy_free( RTEMS_Malloc_Heap, opaque );
0083   _RTEMS_Unlock_allocator();
0084 }