source: rtems/cpukit/libcsupport/src/free.c

Last change on this file was d90e2dc1, checked in by Joel Sherrill <joel@…>, on 03/30/22 at 22:28:06

cpukit/libcsupport/src/[a-f]*: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup libcsupport
7 *
8 * @brief calloc()
9 */
10
11/*
12 *  COPYRIGHT (c) 1989-2007.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#ifdef RTEMS_NEWLIB
42#include "malloc_p.h"
43#include <stdlib.h>
44
45#include <rtems/chain.h>
46
47static RTEMS_CHAIN_DEFINE_EMPTY( _Malloc_GC_list );
48
49RTEMS_INTERRUPT_LOCK_DEFINE( static, _Malloc_GC_lock, "Malloc GC" )
50
51static void *_Malloc_Get_deferred_free( void )
52{
53  rtems_interrupt_lock_context lock_context;
54  void *p;
55
56  rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context );
57  p = rtems_chain_get_unprotected( &_Malloc_GC_list );
58  rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context );
59
60  return p;
61}
62
63void _Malloc_Process_deferred_frees( void )
64{
65  rtems_chain_node *to_be_freed;
66
67  /*
68   *  If some free's have been deferred, then do them now.
69   */
70  while ( ( to_be_freed = _Malloc_Get_deferred_free() ) != NULL ) {
71    free( to_be_freed );
72  }
73}
74
75static void _Malloc_Deferred_free( void *p )
76{
77  rtems_interrupt_lock_context lock_context;
78  rtems_chain_node *node;
79
80  node = (rtems_chain_node *) p;
81
82  rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context );
83  rtems_chain_initialize_node( node );
84  rtems_chain_append_unprotected( &_Malloc_GC_list, node );
85  rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context );
86}
87
88void free(
89  void *ptr
90)
91{
92  if ( !ptr )
93    return;
94
95  /*
96   *  Do not attempt to free memory if in a critical section or ISR.
97   */
98  if ( _Malloc_System_state() != MALLOC_SYSTEM_STATE_NORMAL ) {
99      _Malloc_Deferred_free(ptr);
100      return;
101  }
102
103  if ( !_Protected_heap_Free( RTEMS_Malloc_Heap, ptr ) ) {
104    rtems_fatal( RTEMS_FATAL_SOURCE_INVALID_HEAP_FREE, (rtems_fatal_code) ptr );
105  }
106}
107#endif
Note: See TracBrowser for help on using the repository browser.