source: rtems/cpukit/score/src/freechain.c @ 0daa8ab

5
Last change on this file since 0daa8ab was 059529e, checked in by Sebastian Huber <sebastian.huber@…>, on 07/21/16 at 08:15:02

score: Add debug support to chains

This helps to detect

  • double insert, append, prepend errors, and
  • get from empty chain errors.
  • Property mode set to 100644
File size: 1.7 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup ScoreFreechain
5 *
6 * @brief Freechain Handler Implementation
7 */
8
9/*
10 * Copyright (c) 2013 Gedare Bloom.
11 *
12 * The license and distribution terms for this file may be
13 * found in the file LICENSE in this distribution or at
14 * http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18  #include "config.h"
19#endif
20
21#include <rtems/score/freechain.h>
22#include <rtems/score/assert.h>
23#include <rtems/score/chainimpl.h>
24
25void _Freechain_Initialize(
26  Freechain_Control   *freechain,
27  Freechain_Allocator  allocator,
28  size_t               number_nodes,
29  size_t               node_size
30)
31{
32  void *starting_address;
33
34  if ( number_nodes > 0 ) {
35    starting_address = ( *allocator )( number_nodes * node_size );
36    number_nodes *= ( starting_address != NULL );
37  } else {
38    starting_address = NULL;
39  }
40
41  _Chain_Initialize(
42    &freechain->Free,
43    starting_address,
44    number_nodes,
45    node_size
46  );
47}
48
49void *_Freechain_Get(
50  Freechain_Control   *freechain,
51  Freechain_Allocator  allocator,
52  size_t               number_nodes_to_extend,
53  size_t               node_size
54)
55{
56  _Assert( node_size >= sizeof( Chain_Node ) );
57
58  if ( _Chain_Is_empty( &freechain->Free ) && number_nodes_to_extend > 0 ) {
59    void *starting_address;
60
61    starting_address = ( *allocator )( number_nodes_to_extend * node_size );
62    number_nodes_to_extend *= ( starting_address != NULL );
63
64    _Chain_Initialize(
65      &freechain->Free,
66      starting_address,
67      number_nodes_to_extend,
68      node_size
69    );
70  }
71
72  return _Chain_Get_unprotected( &freechain->Free );
73}
74
75void _Freechain_Put( Freechain_Control *freechain, void *node )
76{
77  if ( node != NULL ) {
78    _Chain_Initialize_node( node );
79    _Chain_Prepend_unprotected( &freechain->Free, node );
80  }
81}
Note: See TracBrowser for help on using the repository browser.