/usr/include/dbstl_dbt.h is in libdb5.3-stl-dev 5.3.28-9+deb8u1.
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 | /*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#ifndef _DB_STL_DBT_H
#define _DB_STL_DBT_H
#include <assert.h>
#include <string>
#include "dbstl_common.h"
#include "dbstl_exception.h"
#include "dbstl_utility.h"
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// DataItem class template definition
//
// 1. DataItem is a Dbt wrapper, it provides both typed data to/from memory
// chunk mapping as well as iostream support. Note that iostream functionality
// is not yet implemented.
// 2. DataItem is used inside dbstl to provide consistent Dbt object memory
// management.
// 3. DataItem is not only capable of mapping fixed size objects, but also
// varying length objects and objects not located in a consecutive chunk of
// memory, with the condition that user configures the required methods in
// DbstlElemTraits.
// 4. DataItem can not be a class template because inside it, the "member
// function template override" support is needed.
//
START_NS(dbstl)
using std::string;
#ifdef HAVE_WSTRING
using std::wstring;
#endif
class DataItem
{
private:
typedef DataItem self;
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//
// DataItem memory management
//
// The dbt_ member is the current dbt, data is stored in the dbt's
// referenced memory, it may
// deep copy from constructor and from other Dbt, depending on
// the constructors "onstack" parameter --- if true, this object
// is only used as a stack object inside a function,
// so do shallow copy; otherwise do deep copy.
// There is always a DB_DBT_USERMEM flag set to the dbt,
// its ulen data member stores the length of referenced memory,
// its size data member stores the actual size of data;
// If onstack is true, its dlen is INVALID_DLEN, and freemem()
// will not free such memory because this object only reference
// other object's memory, its the referenced object's responsibility
// to free their memory.
//
// A DataItem object is not used everywhere, so it is impossible for
// such an object to have two kinds of usages as above at the same
// time, so we are safe doing so.
Dbt dbt_;
// Free dbt_'s referenced memory if that memory is allocated in heap
// and owned by dbt_.
inline void freemem()
{
void *buf = dbt_.get_data();
if (buf != NULL && (dbt_.get_flags() & DB_DBT_USERMEM) != 0
&& dbt_.get_dlen() != INVALID_DLEN)
free(buf);
memset(&dbt_, 0, sizeof(dbt_));
}
public:
// Deep copy, because dbt2.data pointed memory may be short lived.
inline void set_dbt(const DbstlDbt&dbt2, bool onstack)
{
void *buf;
u_int32_t s1, s2;
DBT *pdbt2, *pdbt;
pdbt2 = (DBT *)&dbt2;
pdbt = (DBT *)&dbt_;
if (!onstack) {
buf = pdbt->data;
s1 = pdbt->ulen;
s2 = pdbt2->size;
if(s2 > s1) {
buf = DbstlReAlloc(buf, s2);
pdbt->size = s2;
pdbt->data = buf;
pdbt->ulen = s2;
pdbt->flags |= DB_DBT_USERMEM;
} else
pdbt->size = s2;
memcpy(buf, pdbt2->data, s2);
} else {
freemem();
dbt_ = (const Dbt)dbt2;
pdbt->dlen = (INVALID_DLEN);
}
}
// Deep copy, because dbt2.data pointed memory may be short lived.
inline void set_dbt(const Dbt&dbt2, bool onstack)
{
void *buf;
u_int32_t s1, s2;
DBT *pdbt2, *pdbt;
pdbt2 = (DBT *)&dbt2;
pdbt = (DBT *)&dbt_;
if (!onstack) {
buf = pdbt->data;
s1 = pdbt->ulen;
s2 = pdbt2->size;
if(s2 > s1) {
buf = DbstlReAlloc(buf, s2);
pdbt->size = s2;
pdbt->data = buf;
pdbt->ulen = s2;
pdbt->flags |= DB_DBT_USERMEM;
} else
pdbt->size = s2;
memcpy(buf, pdbt2->data, s2);
} else {
freemem();
dbt_ = dbt2;
pdbt->dlen = (INVALID_DLEN);
}
}
inline void set_dbt(const DBT&dbt2, bool onstack)
{
void *buf;
u_int32_t s1, s2;
DBT *pdbt = (DBT *)&dbt_;
if (!onstack) {
buf = pdbt->data;
s1 = pdbt->ulen;
s2 = dbt2.size;
if(s2 > s1) {
buf = DbstlReAlloc(buf, s2);
pdbt->size = s2;
pdbt->data = buf;
pdbt->ulen = s2;
pdbt->flags |= DB_DBT_USERMEM;
} else
pdbt->size = s2;
memcpy(buf, dbt2.data, s2);
} else {
freemem();
// The following is right because Dbt derives from
// DBT with no extra members or any virtual functions.
memcpy(&dbt_, &dbt2, sizeof(dbt2));
pdbt->dlen = INVALID_DLEN;
}
}
// Return to the initial state.
inline void reset()
{
void *buf = dbt_.get_data();
if (buf) {
memset(buf, 0, dbt_.get_ulen());
dbt_.set_size(0);
}
}
inline Dbt& get_dbt()
{
return dbt_;
}
// Return data of this object. If no data return -1, if it has data
// return 0.
//
// !!!XXX Note that the type parameter T can only be in this function
// because "template type parameter overload" applies only to a
// functions template argument list, rather than that of classes.
// If you put the "template<Typename T>" to this class's declaration,
// making it a class template, then when T is any of Dbt, DBT, or
// DataItem<T>, there will be two copies of this function. One will be
// this function's instantiated version, the other one is one of the
// three functions defined below.
//
template <Typename T>
inline int get_data(T& data) const
{
int ret;
typedef DbstlElemTraits<T> EM;
typename EM::ElemRstoreFunct restore;
void *pdata = NULL;
if ((pdata = dbt_.get_data()) != NULL) {
if ((restore = EM::instance()->
get_restore_function()) != NULL)
restore(data, pdata);
else
data = *((T*)pdata);
ret = 0;
} else
ret = -1;
return ret;
}
////////////////////////////////////////////////////////////////////
//
// Begin functions supporting direct naked string storage.
//
// Always store the data, rather than the container object.
//
// The returned string lives no longer than the next iterator
// movement call.
//
inline int get_data(char*& data) const
{
data = (char*)dbt_.get_data();
return 0;
}
inline int get_data(string &data) const
{
data = (string::pointer) dbt_.get_data();
return 0;
}
inline int get_data(wchar_t*& data) const
{
data = (wchar_t*)dbt_.get_data();
return 0;
}
#ifdef HAVE_WSTRING
inline int get_data(wstring &data) const
{
data = (wstring::pointer) dbt_.get_data();
return 0;
}
#endif
////////////////////////////////////////////////////////////////////
// Supporting storing arbitrary type of sequence.
template <Typename T>
inline int get_data(T*& data) const
{
data = (T*)dbt_.get_data();
return 0;
}
inline int get_data(DataItem& data) const
{
int ret;
if (dbt_.get_data()) {
data.set_dbt(dbt_, false);
ret = 0;
} else
ret = -1;
return ret;
}
////////////////////////////////////////////////////////////////////
//
// Begin functions supporting Dbt storage.
//
// This member function allows storing a Dbt type, so that user can
// store the varying length data into Dbt.
//
// This method is required to copy a data element's bytes to another
// Dbt object, used inside by dbstl.
// If there is no data return -1, if it has data return 0.
//
inline int get_data(Dbt& data) const
{
int ret;
void *addr;
u_int32_t sz;
DBT *pdbt = (DBT *)&dbt_, *pdata = (DBT *)&data;
if (pdbt->data) {
addr = pdata->data;
sz = pdbt->size;
if (pdata->ulen < sz) {
pdata->data = DbstlReAlloc(addr, sz);
pdata->size = sz;
pdata->ulen = sz;
pdata->flags |= DB_DBT_USERMEM;
} else
pdata->size = sz;
memcpy(pdata->data, pdbt->data, sz);
ret = 0;
} else
ret = -1;
return ret;
}
inline int get_data(DBT& data) const
{
int ret;
void*addr;
u_int32_t sz;
if (dbt_.get_data()) {
addr = data.data;
if (data.ulen < (sz = dbt_.get_size())) {
data.data = DbstlReAlloc(addr, sz);
// User need to free this memory
data.flags = data.flags | DB_DBT_USERMEM;
data.size = sz;
data.ulen = sz;
} else
data.size = sz;
memcpy(data.data, dbt_.get_data(), sz);
ret = 0;
} else
ret = -1;
return ret;
}
inline int get_data(DbstlDbt& data) const
{
int ret;
void *addr;
u_int32_t sz;
DBT *pdbt = (DBT *)&dbt_, *pdata = (DBT *)&data;
if (pdbt->data) {
addr = pdata->data;
sz = pdbt->size;
if (pdata->ulen < sz) {
pdata->data = DbstlReAlloc(addr, sz);
pdata->size = sz;
pdata->ulen = sz;
pdata->flags |= DB_DBT_USERMEM;
} else
pdata->size = sz;
memcpy(pdata->data, pdbt->data, sz);
ret = 0;
} else
ret = -1;
return ret;
}
////////////////////////////////////////////////////////////////////
// Deep copy in assignment and copy constructor.
inline const DbstlDbt& operator=(const DbstlDbt& t2)
{
set_dbt(t2, false);
return t2;
}
// Deep copy in assignment and copy constructor.
inline const Dbt& operator=(const Dbt& t2)
{
set_dbt(t2, false);
return t2;
}
// Deep copy in assignment and copy constructor.
inline const DBT& operator=(const DBT& t2)
{
set_dbt(t2, false);
return t2;
}
// Deep copy in assignment and copy constructor.
template <Typename T>
inline const T& operator = (const T&dt)
{
make_dbt(dt, false);
return dt;
}
// Generic way of storing an object or variable. Note that DataItem
// is not a class template but a class with function templates.
// Variable t locates on a consecutive chunk of memory, and objects
// of T have the same size.
//
template <Typename T>
void make_dbt(const T& dt, bool onstack)
{
typedef DbstlElemTraits<T> EM;
u_int32_t sz;
typename EM::ElemSizeFunct sizef;
typename EM::ElemCopyFunct copyf;
DBT *pdbt = (DBT *)&dbt_;
if ((sizef = EM::instance()->get_size_function()) != NULL)
sz = sizef(dt);
else
sz = sizeof(dt);
copyf = EM::instance()->get_copy_function();
if (onstack && copyf == NULL) {
freemem();
pdbt->data = ((void*)&dt);
// We have to set DB_DBT_USERMEM for DB_THREAD to work.
pdbt->flags = (DB_DBT_USERMEM);
pdbt->size = (sz);
pdbt->ulen = (sz);
// This is a flag that this memory can't be freed
// because it is on stack.
pdbt->dlen = (INVALID_DLEN);
return;
}
// Not on stack, allocate enough space and "copy" the object
// using shall copy or customized copy.
if (pdbt->ulen < sz) {
pdbt->data = (DbstlReAlloc(pdbt->data, sz));
assert(pdbt->data != NULL);
pdbt->size = (sz);
pdbt->ulen = (sz);
pdbt->flags = (DB_DBT_USERMEM);
} else
pdbt->size = (sz);
if (copyf != NULL)
copyf(pdbt->data, dt);
else
memcpy(pdbt->data, &dt, sz);
}
inline const char*&operator = (const char*&dt)
{
make_dbt(dt, false);
return dt;
}
inline const wchar_t*&operator = (const wchar_t*&dt)
{
make_dbt(dt, false);
return dt;
}
inline const string &operator=(const string &dt)
{
make_dbt(dt, false);
return dt;
}
#ifdef HAVE_WSTRING
inline const wstring &operator=(const wstring &dt)
{
make_dbt(dt, false);
return dt;
}
#endif
template <Typename T>
inline const T*&operator = (const T*&dt)
{
make_dbt(dt, false);
return dt;
}
inline const self& operator=(const self&dbt1)
{
ASSIGNMENT_PREDCOND(dbt1)
this->set_dbt(dbt1.dbt_, false);
return dbt1;
}
// Deep copy.
inline DataItem(const self&dbt1)
{
set_dbt(dbt1.dbt_, false);
}
inline DataItem(u_int32_t sz)
{
void *buf;
DBT *pdbt = (DBT *)&dbt_;
buf = NULL;
buf = DbstlMalloc(sz);
memset(buf, 0, sz);
pdbt->size = sz;
pdbt->ulen = sz;
pdbt->data = buf;
pdbt->flags = DB_DBT_USERMEM;
}
// Deep copy. The onstack parameter means whether the object referenced
// by this DataItem is on used with a function call where this DataItem
// object is used. If so, we don't deep copy the object, simply refer
// to its memory location. The meaining is the same for this parameter
// in constructors that follow.
inline DataItem(const Dbt&dbt2, bool onstack)
{
set_dbt(dbt2, onstack);
}
inline DataItem(const DbstlDbt&dbt2, bool onstack)
{
set_dbt(dbt2, onstack);
}
inline DataItem(const DBT&dbt2, bool onstack)
{
set_dbt(dbt2, onstack);
}
// Deep copy. There is a partial specialization for char*/wchar_t*/
// string/wstring.
template<Typename T>
inline DataItem(const T& dt, bool onstack)
{
make_dbt(dt, onstack);
}
inline ~DataItem(void)
{
freemem();
}
protected:
// Store a char*/wchar_t* string. Need four versions for char*
// and wchar_t* respectively to catch all
// possibilities otherwise the most generic one will be called.
// Note that the two const decorator matters when doing type
// matching.
inline void make_dbt_chars(const char *t, bool onstack)
{
DBT *d = (DBT *)&dbt_;
u_int32_t sz;
sz = ((t == NULL) ?
sizeof(char) :
(u_int32_t)((strlen(t) + 1) * sizeof(char)));
if (!onstack) {
if (d->ulen < sz) {
d->flags |= DB_DBT_USERMEM;
d->data = DbstlReAlloc(d->data, sz);
d->ulen = sz;
}
d->size = sz;
if (t != NULL)
strcpy((char*)d->data, t);
else
memset(d->data, '\0', sizeof(char));
} else {
freemem();
d->data = ((t == NULL) ? (void *)"" : (void *)t);
d->size = sz;
d->ulen = sz;
d->flags = (DB_DBT_USERMEM);
d->dlen = (INVALID_DLEN);
}
}
inline void make_dbt_wchars(const wchar_t *t, bool onstack)
{
DBT *d = (DBT *)&dbt_;
u_int32_t sz;
sz = ((t == NULL) ?
sizeof(wchar_t) :
(u_int32_t)((wcslen(t) + 1) * sizeof(wchar_t)));
if (!onstack) {
if (d->ulen < sz) {
d->flags |= DB_DBT_USERMEM;
d->data = DbstlReAlloc(d->data, sz);
d->ulen = sz;
}
d->size = sz;
if (t != NULL)
wcscpy((wchar_t*)d->data, t);
else
memset(d->data, L'\0', sizeof(wchar_t));
} else {
freemem();
d->data = ((t == NULL) ? (void *)L"" : (void *)t);
d->size = sz;
d->ulen = sz;
d->flags = (DB_DBT_USERMEM);
d->dlen = (INVALID_DLEN);
}
}
inline void make_dbt(const char*& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline void make_dbt(const char* const& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline void make_dbt(char*& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline void make_dbt(char* const& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline void make_dbt(const string& t, bool onstack)
{
make_dbt_chars(t.c_str(), onstack);
}
inline void make_dbt(const wchar_t*& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline void make_dbt(const wchar_t* const& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline void make_dbt(wchar_t*& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline void make_dbt(wchar_t* const& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
#ifdef HAVE_WSTRING
inline void make_dbt(const wstring& t, bool onstack)
{
make_dbt_wchars(t.c_str(), onstack);
}
#endif
template <Typename T>
void make_dbt_internal(const T*t, bool onstack)
{
typedef DbstlElemTraits<T> EM;
u_int32_t i, sz, totalsz, sql;
DBT *pdbt = (DBT *)&dbt_;
typename EM::ElemSizeFunct szf = NULL;
typename EM::SequenceLenFunct seqlenf = NULL;
typename EM::SequenceCopyFunct seqcopyf = NULL;
szf = EM::instance()->get_size_function();
seqlenf = EM::instance()->get_sequence_len_function();
seqcopyf = EM::instance()->get_sequence_copy_function();
assert(seqlenf != NULL);
sql = sz = (u_int32_t)seqlenf(t);
if (szf)
for (i = 0, totalsz = 0; i < sz; i++)
totalsz += szf(t[i]);
else
totalsz = sz * sizeof(T);
sz = totalsz;
if (onstack && seqcopyf == NULL) {
freemem();
pdbt->data = (void *)t;
pdbt->size = sz;
pdbt->ulen = sz;
pdbt->flags = DB_DBT_USERMEM;
pdbt->dlen = INVALID_DLEN; // onstack flag;
} else {
// ulen stores the real length of the pointed memory.
if (pdbt->ulen < sz) {
pdbt->data = DbstlReAlloc(pdbt->data, sz);
pdbt->ulen = sz;
pdbt->flags |= DB_DBT_USERMEM;
}
pdbt->size = sz;
EM::instance()->copy((T *)pdbt->data, t, sql);
}
}
// Store a sequence of base type T. Need four versions to catch all
// possibilities otherwise the most generic one will be called.
template <Typename T>
inline void make_dbt(const T*const&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template <Typename T>
inline void make_dbt(T*const&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template <Typename T>
inline void make_dbt(T*&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template <Typename T>
inline void make_dbt(const T*&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
public:
inline DataItem(const char*& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline DataItem(const char* const& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline DataItem(char*& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline DataItem(char* const& t, bool onstack)
{
make_dbt_chars(t, onstack);
}
inline DataItem(const string& t, bool onstack)
{
make_dbt_chars(t.c_str(), onstack);
}
inline DataItem(const wchar_t*& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline DataItem(const wchar_t* const& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline DataItem(wchar_t*& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
inline DataItem(wchar_t* const& t, bool onstack)
{
make_dbt_wchars(t, onstack);
}
#ifdef HAVE_WSTRING
inline DataItem(const wstring& t, bool onstack)
{
make_dbt_wchars(t.c_str(), onstack);
}
#endif
template<Typename T>
inline DataItem(T*&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template<Typename T>
inline DataItem(const T*&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template<Typename T>
inline DataItem(T*const&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
template<Typename T>
inline DataItem(const T*const&tt, bool onstack)
{
make_dbt_internal((const T*)tt, onstack);
}
}; // DataItem<>
bool operator==(const Dbt&d1, const Dbt&d2);
bool operator==(const DBT&d1, const DBT&d2);
END_NS
#endif // !_DB_STL_DBT_H
|