summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/keyfreememory.c
blob: 0e2c517cc532b6683c1c216ad5669a9eca85ea95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * @file
 *
 * @brief POSIX Function Keys Free Memory
 * @ingroup POSIXAPI
 */

/*
 * Copyright (c) 2012 Zhongwei Yao.
 * COPYRIGHT (c) 1989-2010.
 * On-Line Applications Research Corporation (OAR).
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/posix/keyimpl.h>
#include <rtems/score/chainimpl.h>

void _POSIX_Keys_Free_memory(
  POSIX_Keys_Control *the_key
)
{
  POSIX_Keys_Key_value_pair *p;
  RBTree_Node *iter, *next;
  Objects_Id key_id;

  key_id = the_key->Object.id;
  iter = _POSIX_Keys_Find( key_id, 0 );
  if ( !iter )
    return;
  /**
   * find the smallest thread_id node in the rbtree.
   */
  next = _RBTree_Predecessor( iter );
  p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( next );
  while ( next != NULL && p->key == key_id) {
    iter = next;
    next = _RBTree_Predecessor( iter );
    p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( next );
  }

  /**
   * delete all nodes belongs to the_key from the rbtree and chain.
   */
  p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( iter );
  while ( iter != NULL && p->key == key_id ) {
    next = _RBTree_Successor( iter );

    _POSIX_Keys_Free_key_value_pair( p );

    iter = next;
    p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( iter );
  }
}