File: Based_Pointer_Repository.cpp

package info (click to toggle)
ace 6.4.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 48,640 kB
  • ctags: 41,204
  • sloc: cpp: 336,448; perl: 33,068; ansic: 20,676; sh: 3,735; exp: 787; python: 635; yacc: 511; xml: 330; lex: 158; lisp: 116; makefile: 80; csh: 20; tcl: 5
file content (123 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "ace/Map_Manager.h"
#include "ace/Based_Pointer_Repository.h"
#include "ace/Guard_T.h"
#include "ace/Null_Mutex.h"
#include "ace/Synch_Traits.h"
#include "ace/RW_Thread_Mutex.h"

#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

/**
 * @class ACE_Based_Pointer_Repository_Rep
 *
 * @brief Implementation for the ACE_Based_Pointer_Repository.
 *
 * Every memory pool in ACE binds it's mapping base address and
 * the mapped size to this repository every time it maps/remaps a
 * new chunk of memory successfully.
 */
class ACE_Based_Pointer_Repository_Rep
{
public:
  // Useful typedefs.
  typedef ACE_Map_Manager <void *, size_t, ACE_Null_Mutex> MAP_MANAGER;
  typedef ACE_Map_Iterator <void *, size_t, ACE_Null_Mutex> MAP_ITERATOR;
  typedef ACE_Map_Entry <void *, size_t> MAP_ENTRY;

  /// Keeps track of the mapping between addresses and their associated
  /// values.
  MAP_MANAGER addr_map_;

  /// Synchronize concurrent access to the map.
  ACE_SYNCH_MUTEX lock_;

  ACE_ALLOC_HOOK_DECLARE;
};

ACE_ALLOC_HOOK_DEFINE(ACE_Based_Pointer_Repository_Rep);

ACE_Based_Pointer_Repository::ACE_Based_Pointer_Repository (void)
{
  ACE_TRACE ("ACE_Based_Pointer_Repository::ACE_Based_Pointer_Repository");
  ACE_NEW (this->rep_,
           ACE_Based_Pointer_Repository_Rep);
}

ACE_Based_Pointer_Repository::~ACE_Based_Pointer_Repository (void)
{
  ACE_TRACE ("ACE_Based_Pointer_Repository::~ACE_Based_Pointer_Repository");
  delete this->rep_;
}

// Search for appropriate base address in repository

int
ACE_Based_Pointer_Repository::find (void *addr, void *&base_addr)
{
  ACE_TRACE ("ACE_Based_Pointer_Repository::find");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
  ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;

  for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
       iter.next (ce) != 0;
       iter.advance ())
    // Check to see if <addr> is within any of the regions.
    if (addr >= ce->ext_id_
        && addr < ((char *)ce->ext_id_ + ce->int_id_))
      {
        // Assign the base address.
        base_addr = ce->ext_id_;
        return 1;
      }

  // Assume base address 0 (e.g., if new'ed).
  base_addr = 0;
  return 0;
}

// Bind a new entry to the repository or update the size of an
// existing entry.

int
ACE_Based_Pointer_Repository::bind (void *addr, size_t size)
{
  ACE_TRACE ("ACE_Based_Pointer_Repository::bind");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);

  return this->rep_->addr_map_.rebind (addr, size);
}

// Unbind a base from the repository.

int
ACE_Based_Pointer_Repository::unbind (void *addr)
{
  ACE_TRACE ("ACE_Based_Pointer_Repository::unbind");
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->rep_->lock_, -1);
  ACE_Based_Pointer_Repository_Rep::MAP_ENTRY *ce = 0;

  // Search for service handlers that requested notification.

  for (ACE_Based_Pointer_Repository_Rep::MAP_ITERATOR iter (this->rep_->addr_map_);
       iter.next (ce) != 0;
       iter.advance ())
    {
      // Check to see if <addr> is within any of the regions and if
      // so, unbind the key from the map.
      if (addr >= ce->ext_id_
          && addr < ((char *)ce->ext_id_ + ce->int_id_))
        // Unbind base address.
        return this->rep_->addr_map_.unbind (ce->ext_id_);
    }

  return 0;
}

ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, ACE_Based_Pointer_Repository, ACE_SYNCH_RW_MUTEX);


ACE_END_VERSIONED_NAMESPACE_DECL