24#ifndef _UCOMMON_SHARED_H_
25#define _UCOMMON_SHARED_H_
27#ifndef _UCOMMON_CPR_H_
31#ifndef _UCOMMON_ATOMIC_H_
35#ifndef _UCOMMON_PROTOCOLS_H_
39#ifndef _UCOMMON_OBJECT_H_
43#ifndef _UCOMMON_TYPEREF_H_
47#ifndef _UCOMMON_THREAD_H_
51#ifndef _UCOMMON_SOCKET_H_
57class __EXPORT SharedRef :
protected TypeRef
60 __DELETE_COPY(SharedRef);
69 void get(TypeRef&
object);
71 void put(TypeRef&
object);
75class sharedref :
private SharedRef
78 __DELETE_COPY(sharedref);
81 inline sharedref() : SharedRef() {};
83 inline operator typeref<T>() {
90 inline typeref<T> operator*() {
97 inline void put(typeref<T>& ptr) {
101 inline sharedref& operator=(typeref<T> ptr) {
106 inline sharedref& operator=(T obj) {
113class __EXPORT MappedPointer
116 __DELETE_COPY(MappedPointer);
119 class __EXPORT Index :
public LinkedObject
122 explicit Index(LinkedObject **origin);
130 LinkedObject *free, **list;
136 MappedPointer(
size_t indexes, condlock_t *locking = NULL,
size_t paging = 0);
139 LinkedObject *access(
size_t path);
141 LinkedObject *modify(
size_t path);
143 void release(
void *obj);
145 void insert(
const void *key,
void *value,
size_t path);
147 void replace(Index *ind,
void *value);
149 void remove(Index *ind,
size_t path);
152 static size_t keypath(
const uint8_t *addr,
size_t size);
156inline size_t mapped_keypath(
const T *
addr)
161 return MappedPointer::keypath((
const uint8_t *)
addr,
sizeof(T));
165inline bool mapped_keyequal(
const T* key1,
const T* key2)
169 return !memcmp(key1, key2,
sizeof(T));
173inline size_t mapped_keypath<char>(
const char *
addr)
178 return MappedPointer::keypath((
const uint8_t *)
addr, strlen(
addr));
182inline bool mapped_keyequal<char>(
const char *k1,
const char *k2)
191inline size_t mapped_keypath<struct sockaddr>(
const struct sockaddr *
addr)
200inline bool mapped_keyequal<struct sockaddr>(
const struct sockaddr *s1,
const struct sockaddr *s2)
207template<
typename K,
typename V>
208class mapped_pointer :
public MappedPointer
211 inline mapped_pointer(
size_t indexes = 37,
condlock_t *locking = NULL,
size_t paging = 0) : MappedPointer(indexes, locking, paging) {}
213 inline void release(V*
object) {
214 MappedPointer::release(
object);
217 void remove(
const K* key) {
218 size_t path = mapped_keypath<K>(key);
219 linked_pointer<Index> ip = modify(path);
221 if(mapped_keyequal<K>((
const K*)(ip->key), key)) {
222 MappedPointer::remove(*ip, path);
230 V* get(
const K* key) {
231 linked_pointer<Index> ip = access(mapped_keypath<K>(key));
233 if(mapped_keyequal<K>((
const K*)(ip->key), key)) {
234 return static_cast<V*
>(ip->value);
242 void set(
const K* key, V* ptr) {
243 size_t path = mapped_keypath<K>(key);
244 linked_pointer<Index> ip = modify(path);
246 if(mapped_keyequal<K>((
const K*)(ip->key), key)) {
251 insert((
const void *)key, (
void *)ptr, path);
A thread-safe atomic heap management system.
Atomic pointers and locks.
Abstract interfaces and support.
Common namespace for all ucommon objects.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
ConditionalLock condlock_t
Convenience type for using conditional locks.
bool is(T &object)
Convenience function to validate object assuming it is castable to bool.
void commit(void)
Commit changes / release a modify lock.
void release(void)
Release a shared lock.
static socklen_t len(const struct sockaddr *address)
Get the size of a socket address.
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Compare socket addresses.
A common object base class with auto-pointer support.
Common socket class and address manipulation.
Thread classes and sychronization objects.