/usr/include/pion/PionBlob.hpp is in libpion-common-dev 4.0.7+dfsg-3.1ubuntu4.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | // -----------------------------------------------------------------------
// pion-common: a collection of common libraries used by the Pion Platform
// -----------------------------------------------------------------------
// Copyright (C) 2007-2009 Atomic Labs, Inc. (http://www.atomiclabs.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_PIONBLOB_HEADER__
#define __PION_PIONBLOB_HEADER__
#include <string>
#include <boost/detail/atomic_count.hpp>
#include <boost/functional/hash.hpp>
#include <pion/PionConfig.hpp>
namespace pion { // begin namespace pion
///
/// PionBlob: a simple, reference-counting BLOB class
/// that uses PionPoolAllocator for memory management
///
template <typename CharType, typename AllocType>
class PionBlob {
protected:
/// structure used to store BLOB metadata; payload starts immediately following this
struct BlobData {
/// constructor takes allocator and size (in octets) of BLOB
BlobData(AllocType& blob_alloc, const std::size_t len) :
m_alloc_ptr(&blob_alloc), m_len(len), m_copies(0)
{
*((CharType*)(this) + sizeof(struct BlobData) + len) = '\0';
}
/// returns (const) reference to the BLOB payload
inline const CharType *get(void) const {
return ((CharType*)(this) + sizeof(struct BlobData));
}
/// returns (non-const) reference to the BLOB payload
inline CharType *get(void) {
return ((CharType*)(this) + sizeof(struct BlobData));
}
/// pointer to the allocator used by the BLOB
AllocType * const m_alloc_ptr;
/// size of the BLOB, in octets
const std::size_t m_len;
/// number of copies referencing this BLOB
boost::detail::atomic_count m_copies;
};
/// pointer to the BLOB metadata structure (payload follows the structure)
BlobData * m_blob_ptr;
/**
* creates a new BLOB reference object
*
* @param len size in octets to allocate for the BLOB
*
* @return BlobData* pointer to the new BLOB data object (with reference incremented)
*/
static inline BlobData *create(AllocType& blob_alloc, const std::size_t len) {
BlobData *blob_ptr = new (blob_alloc.malloc(len+sizeof(struct BlobData)+1))
BlobData(blob_alloc, len);
return blob_ptr;
}
/**
* releases pointer to the BLOB metadata structure, and frees memory if no more references
*/
inline void release(void) {
if (m_blob_ptr) {
if (m_blob_ptr->m_copies == 0) {
m_blob_ptr->m_alloc_ptr->free(m_blob_ptr, m_blob_ptr->m_len+sizeof(struct BlobData)+1);
} else {
--m_blob_ptr->m_copies;
}
m_blob_ptr = NULL;
}
}
/**
* grabs & returns reference pointer to this BLOB (increments references)
*/
inline BlobData *grab(void) const {
if (m_blob_ptr) {
++m_blob_ptr->m_copies;
return m_blob_ptr;
} else {
return NULL;
}
}
public:
/// data type used to initialize blobs in variants without copy construction
struct BlobParams {
/// constructor requires all parameters
BlobParams(AllocType& blob_alloc, const CharType *ptr, const std::size_t len)
: m_alloc(blob_alloc), m_ptr(ptr), m_len(len)
{}
// data parameters for constructing a PionBlob
AllocType& m_alloc;
const CharType * m_ptr;
std::size_t m_len;
};
/// virtual destructor
virtual ~PionBlob() {
release();
}
/// default constructor
PionBlob(void) :
m_blob_ptr(NULL)
{}
/**
* copy constructor
*
* @param blob grabs reference from this existing blob
*/
PionBlob(const PionBlob& blob) :
m_blob_ptr(blob.grab())
{}
/**
* constructs a BLOB using BlobParams
*
* @param p BlobParams contains all parameters used to initialize the BLOB
*/
PionBlob(const BlobParams& p) :
m_blob_ptr(NULL)
{
m_blob_ptr = create(p.m_alloc, p.m_len);
memcpy(m_blob_ptr->get(), p.m_ptr, p.m_len);
}
/**
* constructs a BLOB using existing memory buffer
*
* @param blob_alloc allocator used for memory management
* @param ptr pointer to a buffer of memory to copy into the BLOB
* @param len size in octets of the memory buffer to copy
*/
PionBlob(AllocType& blob_alloc, const CharType* ptr, const std::size_t len) :
m_blob_ptr(NULL)
{
m_blob_ptr = create(blob_alloc, len);
memcpy(m_blob_ptr->get(), ptr, len);
}
/**
* constructs a BLOB using existing string
*
* @param blob_alloc allocator used for memory management
* @param str existing std::string object to copy
*/
PionBlob(AllocType& blob_alloc, const std::string& str) :
m_blob_ptr(NULL)
{
m_blob_ptr = create(blob_alloc, str.size());
memcpy(m_blob_ptr->get(), str.c_str(), str.size());
}
/**
* assignment operator
*
* @param blob grabs reference from this existing blob
*
* @return reference to this BLOB
*/
inline PionBlob& operator=(const PionBlob& blob) {
release();
m_blob_ptr = blob.grab();
return *this;
}
/**
* assigns BLOB to existing memory buffer via BlobParams
*
* @param p BlobParams contains all parameters used to initialize the BLOB
*/
inline void set(const BlobParams& p) {
release();
m_blob_ptr = create(p.m_alloc, p.m_len);
memcpy(m_blob_ptr->get(), p.m_ptr, p.m_len);
}
/**
* assigns BLOB to use an existing memory buffer
*
* @param blob_alloc allocator used for memory management
* @param ptr pointer to a buffer of memory to copy into the BLOB
* @param len size in octets of the memory buffer to copy
*/
inline void set(AllocType& blob_alloc, const CharType* ptr, const std::size_t len) {
release();
m_blob_ptr = create(blob_alloc, len);
memcpy(m_blob_ptr->get(), ptr, len);
}
/**
* assigns BLOB to use an existing string
*
* @param blob_alloc allocator used for memory management
* @param str existing std::string object to copy
*/
inline void set(AllocType& blob_alloc, const std::string& str) {
release();
m_blob_ptr = create(blob_alloc, str.size());
memcpy(m_blob_ptr->get(), str.c_str(), str.size());
}
/**
* reserves memory for a new blob without assigning memory
*
* @param blob_alloc allocator used for memory management
* @param len size in octets of the new memory buffer to allocate
*
* @return reference to the new BLOB payload (scope should expire before Blob is copied/shared)
*/
inline CharType *reserve(AllocType& blob_alloc, const std::size_t len) {
release();
m_blob_ptr = create(blob_alloc, len);
return m_blob_ptr->get();
}
/// returns (const) reference to the BLOB payload
inline const CharType *get(void) const {
return (m_blob_ptr ? m_blob_ptr->get() : "");
}
/// returns size of the BLOB in octets
inline std::size_t size(void) const {
return (m_blob_ptr ? (m_blob_ptr->m_len) : 0);
}
/// returns length of the BLOB in octets (alias for size())
inline std::size_t length(void) const {
return size();
}
/// returns true if the BLOB is empty (undefined or size == 0)
inline bool empty(void) const {
return (m_blob_ptr == NULL || m_blob_ptr->m_len == 0);
}
/// returns the number of reference to this BLOB (or 0 if this is null)
inline long use_count(void) const {
return (m_blob_ptr == NULL ? 0 : m_blob_ptr->m_copies + 1);
}
/// returns true if this is a unique instance or if this is null
inline bool unique(void) const {
return (m_blob_ptr == NULL || m_blob_ptr->m_copies == 0);
}
/// alias for release() -> switch to empty state
inline void clear(void) { release(); }
/// alias for release() -> switch to empty state
inline void reset(void) { release(); }
/// returns true if str is equal to this (BLOB matches string)
inline bool operator==(const PionBlob& blob) const {
if (size() != blob.size())
return false;
return (empty() || m_blob_ptr==blob.m_blob_ptr || memcmp(get(), blob.get(), m_blob_ptr->m_len)==0);
}
/// returns true if str is equal to this (BLOB matches string)
inline bool operator==(const std::string& str) const {
if (size() != str.size())
return false;
return (empty() || memcmp(get(), str.c_str(), m_blob_ptr->m_len)==0);
}
/// returns true if blob is not equal to this (two BLOBs do not match)
inline bool operator!=(const PionBlob& blob) const {
return ! (this->operator==(blob));
}
/// returns true if str is not equal to this (BLOB does not match string)
inline bool operator!=(const std::string& str) const {
return ! (this->operator==(str));
}
/// returns true if this is less than blob
inline bool operator<(const PionBlob& blob) const {
const std::size_t len = (size() < blob.size() ? size() : blob.size());
if (len > 0) {
const int val = memcmp(get(), blob.get(), len);
if (val < 0)
return true;
if (val > 0)
return false;
}
return (size() < blob.size());
}
/// returns true if this is greater than blob
inline bool operator>(const PionBlob& blob) const {
const std::size_t len = (size() < blob.size() ? size() : blob.size());
if (len > 0) {
const int val = memcmp(get(), blob.get(), len);
if (val > 0)
return true;
if (val < 0)
return false;
}
return (size() > blob.size());
}
/// returns true if this is less than str
inline bool operator<(const std::string& str) const {
const std::size_t len = (size() < str.size() ? size() : str.size());
if (len > 0) {
const int val = memcmp(get(), str.c_str(), len);
if (val < 0)
return true;
if (val > 0)
return false;
}
return (size() < str.size());
}
/// returns true if this is greater than str
inline bool operator>(const std::string& str) const {
const std::size_t len = (size() < str.size() ? size() : str.size());
if (len > 0) {
const int val = memcmp(get(), str.c_str(), len);
if (val > 0)
return true;
if (val < 0)
return false;
}
return (size() > str.size());
}
};
/// returns hash value for a PionBlob object (used by boost::hash_map)
template <typename CharType, typename AllocType>
static inline std::size_t hash_value(const PionBlob<CharType,AllocType>& blob) {
return (blob.empty() ? 0 : boost::hash_range(blob.get(), blob.get() + blob.size()));
}
/// optimized hash function object for PionBlob objects which contain PionId string representations (bb49b9ca-e733-47c0-9a26-0f8f53ea1660)
struct HashPionIdBlob {
/// helper for hex->int conversion
inline unsigned long getValue(unsigned char c) const {
unsigned long result;
switch(c) {
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
result = (c - 48);
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
result = (c - 87);
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
result = (c - 55);
break;
default:
result = 0;
break;
}
return result;
}
/// returns hash value for the blob provided
template <typename CharType, typename AllocType>
inline std::size_t operator()(const PionBlob<CharType,AllocType>& blob) const {
if (blob.size() != 36) // sanity check
return hash_value(blob);
const char * const data = blob.get();
unsigned long n;
std::size_t seed = 0;
// calculate first ulong value
n = (getValue(data[0]) << 28);
n |= (getValue(data[1]) << 24);
n |= (getValue(data[2]) << 20);
n |= (getValue(data[3]) << 16);
n |= (getValue(data[4]) << 12);
n |= (getValue(data[5]) << 8);
n |= (getValue(data[6]) << 4);
n |= getValue(data[7]);
boost::hash_combine(seed, n);
// calculate second ulong value
n = (getValue(data[9]) << 28);
n |= (getValue(data[10]) << 24);
n |= (getValue(data[11]) << 20);
n |= (getValue(data[12]) << 16);
n |= (getValue(data[14]) << 12);
n |= (getValue(data[15]) << 8);
n |= (getValue(data[16]) << 4);
n |= getValue(data[17]);
boost::hash_combine(seed, n);
// calculate third ulong value
n = (getValue(data[19]) << 28);
n |= (getValue(data[20]) << 24);
n |= (getValue(data[21]) << 20);
n |= (getValue(data[22]) << 16);
n |= (getValue(data[24]) << 12);
n |= (getValue(data[25]) << 8);
n |= (getValue(data[26]) << 4);
n |= getValue(data[27]);
boost::hash_combine(seed, n);
// calculate third ulong value
n = (getValue(data[28]) << 28);
n |= (getValue(data[29]) << 24);
n |= (getValue(data[30]) << 20);
n |= (getValue(data[31]) << 16);
n |= (getValue(data[32]) << 12);
n |= (getValue(data[33]) << 8);
n |= (getValue(data[34]) << 4);
n |= getValue(data[35]);
boost::hash_combine(seed, n);
return seed;
}
#ifdef _MSC_VER
//This code is needed for stdext::hash_map
enum {
bucket_size = 4, // 0 < bucket_size
min_buckets = 8 // min_buckets = 2 ^^ N, 0 < N
};
template <typename CharType, typename AllocType>
bool operator()(const PionBlob<CharType, AllocType>& _Keyval1, const PionBlob<CharType, AllocType>& _Keyval2) const {
return _Keyval1 < _Keyval2;
}
#endif
};
} // end namespace pion
#endif
|