source: rtems/cpukit/score/src/heapfree.c @ 93b4e6ef

4.104.114.84.95
Last change on this file since 93b4e6ef was 93b4e6ef, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 21:05:17

Split Heap and Time of Day Handlers.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 *  Heap Handler
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15
16#include <rtems/system.h>
17#include <rtems/score/sysstate.h>
18#include <rtems/score/heap.h>
19
20/*PAGE
21 *
22 *  _Heap_Free
23 *
24 *  This kernel routine returns the memory designated by the
25 *  given heap and given starting address to the memory pool.
26 *
27 *  Input parameters:
28 *    the_heap         - pointer to heap header
29 *    starting_address - starting address of the memory block to free.
30 *
31 *  Output parameters:
32 *    TRUE  - if starting_address is valid heap address
33 *    FALSE - if starting_address is invalid heap address
34 */
35
36boolean _Heap_Free(
37  Heap_Control        *the_heap,
38  void                *starting_address
39)
40{
41  Heap_Block        *the_block;
42  Heap_Block        *next_block;
43  Heap_Block        *new_next_block;
44  Heap_Block        *previous_block;
45  Heap_Block        *temporary_block;
46  unsigned32         the_size;
47
48  the_block = _Heap_User_block_at( starting_address );
49
50  if ( !_Heap_Is_block_in( the_heap, the_block ) ||
51        _Heap_Is_block_free( the_block ) ) {
52      return( FALSE );
53  }
54
55  the_size   = _Heap_Block_size( the_block );
56  next_block = _Heap_Block_at( the_block, the_size );
57
58  if ( !_Heap_Is_block_in( the_heap, next_block ) ||
59       (the_block->front_flag != next_block->back_flag) ) {
60      return( FALSE );
61  }
62
63  if ( _Heap_Is_previous_block_free( the_block ) ) {
64    previous_block = _Heap_Previous_block( the_block );
65
66    if ( !_Heap_Is_block_in( the_heap, previous_block ) ) {
67        return( FALSE );
68    }
69
70    if ( _Heap_Is_block_free( next_block ) ) {      /* coalesce both */
71      previous_block->front_flag += next_block->front_flag + the_size;
72      temporary_block             = _Heap_Next_block( previous_block );
73      temporary_block->back_flag  = previous_block->front_flag;
74      next_block->next->previous  = next_block->previous;
75      next_block->previous->next  = next_block->next;
76    }
77    else {                     /* coalesce prev */
78      previous_block->front_flag =
79      next_block->back_flag      = previous_block->front_flag + the_size;
80    }
81  }
82  else if ( _Heap_Is_block_free( next_block ) ) { /* coalesce next */
83    the_block->front_flag     = the_size + next_block->front_flag;
84    new_next_block            = _Heap_Next_block( the_block );
85    new_next_block->back_flag = the_block->front_flag;
86    the_block->next           = next_block->next;
87    the_block->previous       = next_block->previous;
88    next_block->previous->next = the_block;
89    next_block->next->previous = the_block;
90
91    if (the_heap->first == next_block)
92        the_heap->first = the_block;
93  }
94  else {                          /* no coalesce */
95    next_block->back_flag     =
96    the_block->front_flag     = the_size;
97    the_block->previous       = _Heap_Head( the_heap );
98    the_block->next           = the_heap->first;
99    the_heap->first           = the_block;
100    the_block->next->previous = the_block;
101  }
102
103  return( TRUE );
104}
105
Note: See TracBrowser for help on using the repository browser.