source: rtems/cpukit/rtems/src/regionreturnsegment.c @ 80f2885b

4.104.114.84.95
Last change on this file since 80f2885b was 80f2885b, checked in by Joel Sherrill <joel.sherrill@…>, on 05/20/05 at 19:15:41

2005-05-14 Sergei Organov <osv@…>

PR 746/rtems
Optimize realloc(). The problem is that realloc() can neither grow
nor shrink efficiently the current memory region without support
from underlying heap/region modules. The patch introduces one new
routine for each of heap and region modules, _Heap_Resize_block(),
and rtems_region_resize_segment(), respectively, and uses the
latter to optimize realloc().

The implementation of _Heap_Resize_block() lead to changing of the
heap allocation strategy: now the heap manager, when splits larger
free block into used and new free parts, makes the first part of
the block used, not the last one as it was before. Due to this new
strategy, _Heap_Resize_block() never needs to change the user
pointer.

Caveat: unlike previous heap implementation, first few bytes of
the contents of the memory allocated from the heap are now almost
never all zero. This can trigger bugs in client code that have not
been visible before this patch.

  • libcsupport/src/malloc.c (realloc): try to resize segment in place using new rtems_region_resize_segment() routine before falling back to the malloc()/free() method.
  • score/src/heap.c: (_Heap_Initialize): change initial heap layout to reflect new allocation strategy of using of the lower part of a previously free block when splitting it for the purpose of allocation. (_Heap_Block_allocate): when split, make the lower part used, and leave the upper part free. Return type changed from Heap_Block* to uint32_t.
  • score/include/rtems/score/heap.h: (Heap_Statistics): added 'resizes' field. (Heap_Resize_status): new enum. (_Heap_Resize_block): new routine. (_Heap_Block_allocate): return type changed from Heap_Block* to uint32_t.
  • score/src/heapwalk.c: reflect new heap layout in checks.
  • score/src/heapsizeofuserarea.c: more assertions added.
  • score/src/heapresizeblock.c: new file. (_Heap_Resize_block): new routine.
  • score/src/heapfree.c: reverse the checks _Heap_Is_block_in() and _Heap_Is_prev_used() on entry to be in this order.
  • score/src/heapallocate.c, score/src/heapallocatealigned.c: ignore return value of _Heap_Block_allocate().
  • score/Makefile.am (HEAP_C_FILES): added src/heapresizeblock.c.
  • rtems/include/rtems/rtems/region.h: (rtems_region_resize_segment): new interface routine. (_Region_Process_queue): new internal routine called from rtems_region_resize_segment() and rtems_region_return_segment().
  • rtems/src/regionreturnsegment.c: move queue management code into the new internal routine _Region_Process_queue() and call it.
  • rtems/src/regionresizesegment.c: new file. (rtems_region_resize_segment): new interface routine.
  • rtems/src/regionprocessqueue.c: new file. (_Region_Process_queue): new internal routine containing queue management code factored out from 'regionreturnsegment.c'.
  • rtems/Makefile.am (REGION_C_FILES): Added src/regionresizesegment.c, and src/regionprocessqueue.c.
  • ada/rtems.adb, ada/rtems.ads: Added Region_Resize_Segment.
  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 *  Region Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989-1999.
6 *  On-Line Applications Research Corporation (OAR).
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.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#ifdef RTEMS_REGION_SHRED_ON_FREE
20#include <string.h>
21
22#ifndef RTEMS_REGION_FREE_SHRED_PATTERN
23#define RTEMS_REGION_FREE_SHRED_PATTERN 0x00
24#endif
25#endif
26
27#include <rtems/system.h>
28#include <rtems/rtems/status.h>
29#include <rtems/rtems/support.h>
30#include <rtems/score/object.h>
31#include <rtems/rtems/options.h>
32#include <rtems/rtems/region.h>
33#include <rtems/score/states.h>
34#include <rtems/score/thread.h>
35#include <rtems/score/apimutex.h>
36
37/*PAGE
38 *
39 *  rtems_region_return_segment
40 *
41 *  This directive will return a segment to its region.
42 *
43 *  Input parameters:
44 *    id      - region id
45 *    segment - pointer to segment address
46 *
47 *  Output parameters:
48 *    RTEMS_SUCCESSFUL - if successful
49 *    error code       - if unsuccessful
50 */
51
52rtems_status_code rtems_region_return_segment(
53  Objects_Id  id,
54  void       *segment
55)
56{
57  register Region_Control *the_region;
58  Objects_Locations        location;
59#ifdef RTEMS_REGION_FREE_SHRED_PATTERN
60  uint32_t                 size;
61#endif
62  int                      status;
63
64  _RTEMS_Lock_allocator();
65  the_region = _Region_Get( id, &location );
66  switch ( location ) {
67
68    case OBJECTS_REMOTE:        /* this error cannot be returned */
69      _RTEMS_Unlock_allocator();
70      return RTEMS_INTERNAL_ERROR;
71
72    case OBJECTS_ERROR:
73      _RTEMS_Unlock_allocator();
74      return RTEMS_INVALID_ID;
75
76    case OBJECTS_LOCAL:
77
78      _Region_Debug_Walk( the_region, 3 );
79
80#ifdef RTEMS_REGION_FREE_SHRED_PATTERN
81      if ( _Heap_Size_of_user_area( &the_region->Memory, segment, &size ) ) {
82        memset( segment, (RTEMS_REGION_FREE_SHRED_PATTERN & 0xFF), size );
83      } else {
84        _RTEMS_Unlock_allocator();
85        return RTEMS_INVALID_ADDRESS;
86      }
87#endif
88
89      status = _Region_Free_segment( the_region, segment );
90
91      _Region_Debug_Walk( the_region, 4 );
92
93      if ( !status ) {
94        _RTEMS_Unlock_allocator();
95        return RTEMS_INVALID_ADDRESS;
96      }
97
98      the_region->number_of_used_blocks -= 1;
99
100      _Region_Process_queue(the_region); /* unlocks allocator internally */
101
102      return RTEMS_SUCCESSFUL;
103  }
104
105  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
106}
Note: See TracBrowser for help on using the repository browser.