1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief Malloc Deferred Support |
---|
7 | * @ingroup libcsupport |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Process free requests deferred because they were from ISR |
---|
12 | * or other critical section. |
---|
13 | * |
---|
14 | * COPYRIGHT (c) 1989-2007. |
---|
15 | * On-Line Applications Research Corporation (OAR). |
---|
16 | * |
---|
17 | * Redistribution and use in source and binary forms, with or without |
---|
18 | * modification, are permitted provided that the following conditions |
---|
19 | * are met: |
---|
20 | * 1. Redistributions of source code must retain the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer. |
---|
22 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer in the |
---|
24 | * documentation and/or other materials provided with the distribution. |
---|
25 | * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
36 | * POSSIBILITY OF SUCH DAMAGE. |
---|
37 | */ |
---|
38 | |
---|
39 | #ifdef HAVE_CONFIG_H |
---|
40 | #include "config.h" |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifdef RTEMS_NEWLIB |
---|
44 | #include <stdlib.h> |
---|
45 | #include <string.h> |
---|
46 | |
---|
47 | #include "malloc_p.h" |
---|
48 | |
---|
49 | #include <rtems/score/sysstate.h> |
---|
50 | #include <rtems/score/threaddispatch.h> |
---|
51 | |
---|
52 | Malloc_System_state _Malloc_System_state( void ) |
---|
53 | { |
---|
54 | System_state_Codes state = _System_state_Get(); |
---|
55 | |
---|
56 | if ( _System_state_Is_up( state ) ) { |
---|
57 | if ( _Thread_Dispatch_is_enabled() ) { |
---|
58 | return MALLOC_SYSTEM_STATE_NORMAL; |
---|
59 | } else { |
---|
60 | return MALLOC_SYSTEM_STATE_NO_ALLOCATION; |
---|
61 | } |
---|
62 | } else if ( _System_state_Is_before_multitasking( state ) ) { |
---|
63 | return MALLOC_SYSTEM_STATE_NORMAL; |
---|
64 | } else { |
---|
65 | return MALLOC_SYSTEM_STATE_NO_PROTECTION; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | RTEMS_WEAK void _Malloc_Process_deferred_frees( void ) |
---|
70 | { |
---|
71 | /* |
---|
72 | * Do nothing by default. If free() is used by the application, then a |
---|
73 | * strong implementation of this function will be provided. |
---|
74 | */ |
---|
75 | } |
---|
76 | |
---|
77 | void *rtems_heap_allocate_aligned_with_boundary( |
---|
78 | size_t size, |
---|
79 | uintptr_t alignment, |
---|
80 | uintptr_t boundary |
---|
81 | ) |
---|
82 | { |
---|
83 | Heap_Control *heap = RTEMS_Malloc_Heap; |
---|
84 | void *p; |
---|
85 | |
---|
86 | switch ( _Malloc_System_state() ) { |
---|
87 | case MALLOC_SYSTEM_STATE_NORMAL: |
---|
88 | _RTEMS_Lock_allocator(); |
---|
89 | _Malloc_Process_deferred_frees(); |
---|
90 | p = _Heap_Allocate_aligned_with_boundary( |
---|
91 | heap, |
---|
92 | size, |
---|
93 | alignment, |
---|
94 | boundary |
---|
95 | ); |
---|
96 | _RTEMS_Unlock_allocator(); |
---|
97 | break; |
---|
98 | case MALLOC_SYSTEM_STATE_NO_PROTECTION: |
---|
99 | p = _Heap_Allocate_aligned_with_boundary( |
---|
100 | heap, |
---|
101 | size, |
---|
102 | alignment, |
---|
103 | boundary |
---|
104 | ); |
---|
105 | break; |
---|
106 | default: |
---|
107 | /* |
---|
108 | * Do not attempt to allocate memory if not in correct system state. |
---|
109 | */ |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | |
---|
113 | if ( p == NULL && alignment == 0 && boundary == 0 ) { |
---|
114 | p = (*rtems_malloc_extend_handler)( heap, size ); |
---|
115 | } |
---|
116 | |
---|
117 | /* |
---|
118 | * If the user wants us to dirty the allocated memory, then do it. |
---|
119 | */ |
---|
120 | if ( p != NULL && rtems_malloc_dirty_helper != NULL ) |
---|
121 | (*rtems_malloc_dirty_helper)( p, size ); |
---|
122 | |
---|
123 | return p; |
---|
124 | } |
---|
125 | |
---|
126 | void *rtems_malloc( size_t size ) |
---|
127 | { |
---|
128 | if ( size == 0 ) { |
---|
129 | return NULL; |
---|
130 | } |
---|
131 | |
---|
132 | return rtems_heap_allocate_aligned_with_boundary( size, 0, 0 ); |
---|
133 | } |
---|
134 | #endif |
---|