summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/keyfreememory.c
blob: ff8fc82f6f4194a998e6776acafec4fbc0e0798f (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
61
62
63
64
/**
 * @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.com/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 search_node;
  POSIX_Keys_Key_value_pair *p;
  RBTree_Node *iter, *next;
  Objects_Id key_id;

  key_id = the_key->Object.id;
  search_node.key = key_id;
  search_node.thread_id = 0;
  iter = _RBTree_Find_unprotected( &_POSIX_Keys_Key_value_lookup_tree, &search_node.Key_value_lookup_node );
  if ( !iter )
    return;
  /**
   * find the smallest thread_id node in the rbtree.
   */
  next = _RBTree_Next_unprotected( iter, RBT_LEFT );
  p = _RBTree_Container_of( next, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
  while ( next != NULL && p->key == key_id) {
    iter = next;
    next = _RBTree_Next_unprotected( iter, RBT_LEFT );
    p = _RBTree_Container_of( next, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
  }

  /**
   * delete all nodes belongs to the_key from the rbtree and chain.
   */
  p = _RBTree_Container_of( iter, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
  while ( iter != NULL && p->key == key_id ) {
    next = _RBTree_Next_unprotected( iter, RBT_RIGHT );
    _RBTree_Extract_unprotected( &_POSIX_Keys_Key_value_lookup_tree, iter );
    _Chain_Extract_unprotected( &p->Key_values_per_thread_node );
    _POSIX_Keys_Key_value_pair_free( p );

    iter = next;
    p = _RBTree_Container_of( iter, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
  }
}