File: ObjectHandle.cpp

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (122 lines) | stat: -rw-r--r-- 2,712 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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "ObjectHandle.h"
#include <map>
#include <stack>

namespace ospray {

static std::map<int64, memory::RefCount *> objectByHandle;
static std::stack<int64> freedHandles;

//! next unassigned ID on this node
/*! we start numbering with 1 to make sure that "0:0" is an
  invalid handle (so we can typecast between (64-bit) handles
  and (64-bit)OSPWhatEver pointers */
static int32 nextFreeLocalID = 1;

void ObjectHandle::free()
{
  freedHandles.push((int64) * this);
}

ObjectHandle::ObjectHandle() : i64(NULL_HANDLE) {}

ObjectHandle::ObjectHandle(int64 i) : i64(i) {}

ObjectHandle::ObjectHandle(const ObjectHandle &other) : i64(other.i64) {}

ObjectHandle &ObjectHandle::operator=(const ObjectHandle &other)
{
  if (this != &other)
    i64 = other.i64;
  return *this;
}

/*! define the given handle to refer to given object */
void ObjectHandle::assign(const ObjectHandle &handle, memory::RefCount *object)
{
  objectByHandle[handle] = object;
}

void ObjectHandle::assign(memory::RefCount *object) const
{
  objectByHandle[i64] = object;
}

void ObjectHandle::freeObject() const
{
  auto it = objectByHandle.find(i64);
  if (it != objectByHandle.end()) {
    // let internal device handle this -> it->second->refDec();
    objectByHandle.erase(it);
  }
}

int32 ObjectHandle::ownerRank() const
{
  return i32.owner;
}

int32 ObjectHandle::objID() const
{
  return i32.ID;
}

ospray::ObjectHandle::operator int64() const
{
  return i64;
}

bool ObjectHandle::defined() const
{
  return objectByHandle.find(i64) != objectByHandle.end();
}

ObjectHandle ObjectHandle::allocateLocalHandle()
{
  ObjectHandle handle;
  if (freedHandles.empty()) {
    handle.i32.ID = nextFreeLocalID++;
    handle.i32.owner = 0;
  } else {
    handle.i64 = freedHandles.top();
    freedHandles.pop();
  }
  return handle;
}

memory::RefCount *ObjectHandle::lookup() const
{
  if (i64 == 0)
    return nullptr;

  auto it = objectByHandle.find(i64);
  if (it == objectByHandle.end()) {
#ifndef NDEBUG
    // iw - made this into a warning only; the original code had
    // this throw an actual exceptoin, but that may be overkill
    std::cout << "#osp: WARNING: ospray is trying to look up object handle "
            + std::to_string(i64) + " that isn't defined!"
              << std::endl;
#endif
    return nullptr;
  }

  return it->second;
}

ObjectHandle ObjectHandle::lookup(memory::RefCount *object)
{
  for (auto it = objectByHandle.begin(); it != objectByHandle.end(); it++) {
    if (it->second == object)
      return (ObjectHandle(it->first));
  }

  return (nullHandle);
}

const ObjectHandle nullHandle(0);

} // namespace ospray