2014-07-04 16:17:05 -04:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "platform/object.hpp"
|
|
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
namespace amd {
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
Atomic<ObjectMetadata::Key> ObjectMetadata::nextKey_ = 1;
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
ObjectMetadata::Destructor ObjectMetadata::destructors_[OCL_MAX_KEYS] = {NULL};
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
bool ObjectMetadata::check(Key key) { return key > 0 && key <= OCL_MAX_KEYS; }
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
ObjectMetadata::Key ObjectMetadata::createKey(Destructor destructor) {
|
|
|
|
|
Key key = nextKey_++;
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
if (!check(key)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
destructors_[key - 1] = destructor;
|
|
|
|
|
return key;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
ObjectMetadata::~ObjectMetadata() {
|
|
|
|
|
if (!values_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
for (size_t i = 0; i < OCL_MAX_KEYS; ++i) {
|
|
|
|
|
if (values_[i] && destructors_[i]) {
|
|
|
|
|
destructors_[i](values_[i]);
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
2017-04-13 13:56:38 -04:00
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
delete[] values_;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
void* ObjectMetadata::getValueForKey(Key key) const {
|
|
|
|
|
if (!values_ || !check(key)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
return values_[key - 1];
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
bool ObjectMetadata::setValueForKey(Key key, Value value) {
|
|
|
|
|
if (!check(key)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
while (!values_) {
|
|
|
|
|
Value* values = new Value[OCL_MAX_KEYS];
|
|
|
|
|
memset(values, '\0', sizeof(Value) * OCL_MAX_KEYS);
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
if (!values_.compareAndSet(NULL, values)) {
|
|
|
|
|
delete[] values;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
2017-04-13 13:56:38 -04:00
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
size_t index = key - 1;
|
|
|
|
|
Value prev = AtomicOperation::swap(value, &values_[index]);
|
|
|
|
|
if (prev && destructors_[index] != NULL) {
|
|
|
|
|
destructors_[index](prev);
|
|
|
|
|
}
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
return true;
|
2014-07-04 16:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
} // namespace amd
|