RTEMS
mallocinitone.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 
11 /*
12  * Copyright (C) 2012, 2020 embedded brains GmbH (http://www.embedded-brains.de)
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _RTEMS_MALLOCINITONE_H
37 #define _RTEMS_MALLOCINITONE_H
38 
39 #include <rtems/malloc.h>
40 #include <rtems/score/assert.h>
41 #include <rtems/score/heapimpl.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
53 RTEMS_INLINE_ROUTINE Heap_Control *_Malloc_Initialize_with_one_area(
54  Heap_Control *heap
55 )
56 {
57  const Memory_Information *mem;
58  Memory_Area *area;
59  uintptr_t space_available;
60 
61  mem = _Memory_Get();
62  _Assert( _Memory_Get_count( mem ) == 1 );
63 
64  RTEMS_Malloc_Heap = heap;
65  area = _Memory_Get_area( mem, 0 );
66  space_available = _Heap_Initialize(
67  heap,
68  _Memory_Get_free_begin( area ),
69  _Memory_Get_free_size( area ),
71  );
72 
73  if ( space_available > 0 ) {
74  _Memory_Consume( area, _Memory_Get_free_size( area ) );
75  } else {
76  _Internal_error( INTERNAL_ERROR_NO_MEMORY_FOR_HEAP );
77  }
78 
79  return heap;
80 }
81 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif /* _RTEMS_MALLOCINITONE_H */
static __inline__ size_t _Memory_Get_count(const Memory_Information *information)
Get the memory area count.
Definition: memory.h:115
static __inline__ Memory_Area * _Memory_Get_area(const Memory_Information *information, size_t index)
Get a memory area by index.
Definition: memory.h:130
Heap Handler Implementation.
The memory information.
Definition: memory.h:80
static __inline__ uintptr_t _Memory_Get_free_size(const Memory_Area *area)
Get the size of the free memory area of the memory area.
Definition: memory.h:272
Information for the Assert Handler.
The memory area description.
Definition: memory.h:60
Control block used to manage a heap.
Definition: heap.h:318
uintptr_t _Heap_Initialize(Heap_Control *heap, void *area_begin, uintptr_t area_size, uintptr_t page_size)
Initializes the heap control block.
Definition: heap.c:207
Heap_Control * RTEMS_Malloc_Heap
C program heap control.
#define CPU_HEAP_ALIGNMENT
Definition: cpu.h:755
static __inline__ void * _Memory_Get_free_begin(const Memory_Area *area)
Get the begin of the free area of the memory area.
Definition: memory.h:246
void _Internal_error(Internal_errors_Core_list core_error) RTEMS_NO_RETURN
Terminates the system with an INTERNAL_ERROR_CORE fatal source and the specified core error code...
Definition: interr.c:51
static __inline__ void _Memory_Consume(Memory_Area *area, uintptr_t consume)
Consume the specified size from the free memory area of the memory area.
Definition: memory.h:285
#define RTEMS_INLINE_ROUTINE
Gives a hint to the compiler in a function declaration to inline this function.
Definition: basedefs.h:683
const Memory_Information * _Memory_Get(void)
Return the memory information of this platform.
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100