source: rtems/cpukit/score/src/heapallocate.c @ fa6b0f5

4.104.114.84.95
Last change on this file since fa6b0f5 was a8eed23, checked in by Ralf Corsepius <ralf.corsepius@…>, on 01/27/05 at 05:57:05

Include config.h.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/*
2 *  Heap Handler
3 *
4 *  COPYRIGHT (c) 1989-1999.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <rtems/system.h>
19#include <rtems/score/sysstate.h>
20#include <rtems/score/heap.h>
21
22/*PAGE
23 *
24 *  _Heap_Allocate
25 *
26 *  This kernel routine allocates the requested size of memory
27 *  from the specified heap.
28 *
29 *  Input parameters:
30 *    the_heap  - pointer to heap header.
31 *    size      - size in bytes of the memory block to allocate.
32 *
33 *  Output parameters:
34 *    returns - starting address of memory block allocated
35 */
36
37void *_Heap_Allocate(
38  Heap_Control        *the_heap,
39  uint32_t             size
40)
41{
42  uint32_t  the_size;
43  uint32_t  search_count;
44  Heap_Block *the_block;
45  void       *ptr = NULL;
46  Heap_Statistics *const stats = &the_heap->stats;
47  Heap_Block *const tail = _Heap_Tail(the_heap);
48
49  the_size =
50    _Heap_Calc_block_size(size, the_heap->page_size, the_heap->min_block_size);
51  if(the_size == 0)
52    return NULL;
53
54  /* Find large enough free block. */
55  for(the_block = _Heap_First(the_heap), search_count = 0;
56      the_block != tail;
57      the_block = the_block->next, ++search_count)
58  {
59    /* As we always coalesce free blocks, prev block must have been used. */
60    _HAssert(_Heap_Is_prev_used(the_block));
61
62    /* Don't bother to mask out the HEAP_PREV_USED bit as it won't change the
63       result of the comparison. */
64    if(the_block->size >= the_size) {
65      the_block = _Heap_Block_allocate(the_heap, the_block, the_size );
66
67      ptr = _Heap_User_area(the_block);
68
69      stats->allocs += 1;
70      stats->searches += search_count + 1;
71
72      _HAssert(_Heap_Is_aligned_ptr(ptr, the_heap->page_size));
73      break;
74    }
75  }
76
77  if(stats->max_search < search_count)
78    stats->max_search = search_count;
79
80  return ptr;
81}
Note: See TracBrowser for help on using the repository browser.