RTEMS
malloc_deferred.c
Go to the documentation of this file.
1 
8 /*
9  * Process free requests deferred because they were from ISR
10  * or other critical section.
11  *
12  * COPYRIGHT (c) 1989-2007.
13  * On-Line Applications Research Corporation (OAR).
14  *
15  * The license and distribution terms for this file may be
16  * found in the file LICENSE in this distribution or at
17  * http://www.rtems.org/license/LICENSE.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #ifdef RTEMS_NEWLIB
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "malloc_p.h"
29 
30 #include <rtems/score/sysstate.h>
32 
33 Malloc_System_state _Malloc_System_state( void )
34 {
36 
37  if ( _System_state_Is_up( state ) ) {
39  return MALLOC_SYSTEM_STATE_NORMAL;
40  } else {
41  return MALLOC_SYSTEM_STATE_NO_ALLOCATION;
42  }
43  } else if ( _System_state_Is_before_multitasking( state ) ) {
44  return MALLOC_SYSTEM_STATE_NORMAL;
45  } else {
46  return MALLOC_SYSTEM_STATE_NO_PROTECTION;
47  }
48 }
49 
50 RTEMS_WEAK void _Malloc_Process_deferred_frees( void )
51 {
52  /*
53  * Do nothing by default. If free() is used by the application, then a
54  * strong implementation of this function will be provided.
55  */
56 }
57 
59  size_t size,
60  uintptr_t alignment,
61  uintptr_t boundary
62 )
63 {
65  void *p;
66 
67  switch ( _Malloc_System_state() ) {
68  case MALLOC_SYSTEM_STATE_NORMAL:
69  _RTEMS_Lock_allocator();
70  _Malloc_Process_deferred_frees();
72  heap,
73  size,
74  alignment,
75  boundary
76  );
77  _RTEMS_Unlock_allocator();
78  break;
79  case MALLOC_SYSTEM_STATE_NO_PROTECTION:
81  heap,
82  size,
83  alignment,
84  boundary
85  );
86  break;
87  default:
88  /*
89  * Do not attempt to allocate memory if not in correct system state.
90  */
91  return NULL;
92  }
93 
94  if ( p == NULL && alignment == 0 && boundary == 0 ) {
95  p = (*rtems_malloc_extend_handler)( heap, size );
96  }
97 
98  /*
99  * If the user wants us to dirty the allocated memory, then do it.
100  */
101  if ( p != NULL && rtems_malloc_dirty_helper != NULL )
102  (*rtems_malloc_dirty_helper)( p, size );
103 
104  return p;
105 }
106 
107 void *rtems_malloc( size_t size )
108 {
109  if ( size == 0 ) {
110  return NULL;
111  }
112 
113  return rtems_heap_allocate_aligned_with_boundary( size, 0, 0 );
114 }
115 
116 void *rtems_calloc( size_t nelem, size_t elsize )
117 {
118  size_t length;
119  void *p;
120 
121  length = nelem * elsize;
122  p = rtems_malloc( length );
124  if ( RTEMS_PREDICT_FALSE( p == NULL ) ) {
125  return p;
126  }
127 
128  return memset( p, 0, length );
129 }
130 #endif
#define RTEMS_WEAK
Tells the compiler in a function definition that this function should be weak.
Definition: basedefs.h:909
System_state_Codes
System states.
Definition: sysstate.h:40
static __inline__ bool _System_state_Is_up(System_state_Codes state)
Checks if the state is up.
Definition: sysstate.h:133
void * rtems_malloc(size_t size) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_WARN_UNUSED_RESULT
Allocates a memory area of the specified size from the heap.
#define RTEMS_PREDICT_FALSE(_exp)
Returns the value of the specified integral expression and tells the compiler that the predicted valu...
Definition: basedefs.h:747
Constants and Structures Related with Thread Dispatch.
void * rtems_calloc(size_t nelem, size_t elsize) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE_2(1
Allocates a memory area for the specified count of elements from the heap.
static __inline__ bool _System_state_Is_before_multitasking(System_state_Codes state)
Checks if the state is before multitasking.
Definition: sysstate.h:118
static __inline__ bool _Thread_Dispatch_is_enabled(void)
Indicates if the executing thread is inside a thread dispatch critical section.
Control block used to manage a heap.
Definition: heap.h:318
Heap_Control * RTEMS_Malloc_Heap
C program heap control.
#define RTEMS_OBFUSCATE_VARIABLE(_var)
Obfuscates the variable so that the compiler cannot perform optimizations based on the variable value...
Definition: basedefs.h:729
static __inline__ System_state_Codes _System_state_Get(void)
Gets the current system state.
Definition: sysstate.h:90
System State Handler API.
void * rtems_heap_allocate_aligned_with_boundary(size_t size, uintptr_t alignment, uintptr_t boundary) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_ALLOC_ALIGN(2) RTEMS_WARN_UNUSED_RESULT
Allocates a memory area of size size bytes from the heap.
void * _Heap_Allocate_aligned_with_boundary(Heap_Control *heap, uintptr_t size, uintptr_t alignment, uintptr_t boundary)
Allocates an aligned memory area with boundary constraint.
Definition: heapallocate.c:192