/usr/include/ucommon/shared.h is in libucommon-dev 7.0.0-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | // Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* Common thread shared data types we will use. This is for specialized
* data types that include locking to be thread-safe.
* @file ucommon/shared.h
*/
#ifndef _UCOMMON_SHARED_H_
#define _UCOMMON_SHARED_H_
#ifndef _UCOMMON_CPR_H_
#include <ucommon/cpr.h>
#endif
#ifndef _UCOMMON_ATOMIC_H_
#include <ucommon/atomic.h>
#endif
#ifndef _UCOMMON_PROTOCOLS_H_
#include <ucommon/protocols.h>
#endif
#ifndef _UCOMMON_OBJECT_H_
#include <ucommon/object.h>
#endif
#ifndef _UCOMMON_TYPEREF_H_
#include <ucommon/typeref.h>
#endif
#ifndef _UCOMMON_THREAD_H_
#include <ucommon/thread.h>
#endif
#ifndef _UCOMMON_SOCKET_H_
#include <ucommon/socket.h>
#endif
namespace ucommon {
class __EXPORT SharedRef : protected TypeRef
{
private:
__DELETE_COPY(SharedRef);
protected:
Mutex lock;
SharedRef();
TypeRef get();
void get(TypeRef& object);
void put(TypeRef& object);
};
template<typename T>
class sharedref : private SharedRef
{
private:
__DELETE_COPY(sharedref);
public:
inline sharedref() : SharedRef() {};
inline operator typeref<T>() {
lock.acquire();
typeref<T> ptr(ref);
lock.release();
return ptr;
}
inline typeref<T> operator*() {
lock.acquire();
typeref<T> ptr(ref);
lock.release();
return ptr;
}
inline void put(typeref<T>& ptr) {
SharedRef::put(ptr);
}
inline sharedref& operator=(typeref<T> ptr) {
SharedRef::get(ptr);
return *this;
}
inline sharedref& operator=(T obj) {
typeref<T> ptr(obj);
SharedRef::get(ptr);
return *this;
}
};
class __EXPORT MappedPointer
{
private:
__DELETE_COPY(MappedPointer);
protected:
class __EXPORT Index : public LinkedObject
{
public:
explicit Index(LinkedObject **origin);
const void *key;
void *value;
};
condlock_t *lock;
LinkedObject *free, **list;
memalloc pager;
size_t paths;
MappedPointer(size_t indexes, condlock_t *locking = NULL, size_t paging = 0);
~MappedPointer();
LinkedObject *access(size_t path);
LinkedObject *modify(size_t path);
void release(void *obj);
void insert(const void *key, void *value, size_t path);
void replace(Index *ind, void *value);
void remove(Index *ind, size_t path);
public:
static size_t keypath(const uint8_t *addr, size_t size);
};
template<typename T>
inline size_t mapped_keypath(const T *addr)
{
if(!addr)
return 0;
return MappedPointer::keypath((const uint8_t *)addr, sizeof(T));
}
template<typename T>
inline bool mapped_keyequal(const T* key1, const T* key2)
{
if(!key1 || !key2)
return false;
return !memcmp(key1, key2, sizeof(T));
}
template<>
inline size_t mapped_keypath<char>(const char *addr)
{
if(!addr)
return 0;
return MappedPointer::keypath((const uint8_t *)addr, strlen(addr));
}
template<>
inline bool mapped_keyequal<char>(const char *k1, const char *k2)
{
if(!k1 || !k2)
return false;
return eq(k1, k2);
}
template<>
inline size_t mapped_keypath<struct sockaddr>(const struct sockaddr *addr)
{
if(!addr)
return 0;
return MappedPointer::keypath((const uint8_t *)addr, Socket::len(addr));
}
template<>
inline bool mapped_keyequal<struct sockaddr>(const struct sockaddr *s1, const struct sockaddr *s2)
{
if(!s1 || !s2)
return false;
return Socket::equal(s1, s2);
}
template<typename K, typename V>
class mapped_pointer : public MappedPointer
{
public:
inline mapped_pointer(size_t indexes = 37, condlock_t *locking = NULL, size_t paging = 0) : MappedPointer(indexes, locking, paging) {}
inline void release(V* object) {
MappedPointer::release(object);
}
void remove(const K* key) {
size_t path = mapped_keypath<K>(key);
linked_pointer<Index> ip = modify(path);
while(is(ip)) {
if(mapped_keyequal<K>((const K*)(ip->key), key)) {
MappedPointer::remove(*ip, path);
return;
}
ip.next();
}
lock->commit();
}
V* get(const K* key) {
linked_pointer<Index> ip = access(mapped_keypath<K>(key));
while(is(ip)) {
if(mapped_keyequal<K>((const K*)(ip->key), key)) {
return static_cast<V*>(ip->value);
}
ip.next();
}
lock->release();
return nullptr;
}
void set(const K* key, V* ptr) {
size_t path = mapped_keypath<K>(key);
linked_pointer<Index> ip = modify(path);
while(is(ip)) {
if(mapped_keyequal<K>((const K*)(ip->key), key)) {
replace(*ip, ptr);
return;
}
}
insert((const void *)key, (void *)ptr, path);
}
};
} // namespace
#endif
|