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 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
|
/*************************************************************************
This project implements a complete(!) JPEG (Recommendation ITU-T
T.81 | ISO/IEC 10918-1) codec, plus a library that can be used to
encode and decode JPEG streams.
It also implements ISO/IEC 18477 aka JPEG XT which is an extension
towards intermediate, high-dynamic-range lossy and lossless coding
of JPEG. In specific, it supports ISO/IEC 18477-3/-6/-7/-8 encoding.
Note that only Profiles C and D of ISO/IEC 18477-7 are supported
here. Check the JPEG XT reference software for a full implementation
of ISO/IEC 18477-7.
Copyright (C) 2012-2018 Thomas Richter, University of Stuttgart and
Accusoft. (C) 2019-2020 Thomas Richter, Fraunhofer IIS.
This program is available under two licenses, GPLv3 and the ITU
Software licence Annex A Option 2, RAND conditions.
For the full text of the GPU license option, see README.license.gpl.
For the full text of the ITU license option, see README.license.itu.
You may freely select between these two options.
For the GPL option, please note the following:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/*
** Definition of the Environment.
**
** $Id: environment.cpp,v 1.11 2022/06/14 06:18:30 thor Exp $
**
** The environment holds structures for exception management without
** exceptions, and for memory management without a global new.
**
**
*/
/// Includes
#include "environment.hpp"
#include "interface/parameters.hpp"
#include "std/stdlib.hpp"
#include "std/stddef.hpp"
#include "std/string.hpp"
#include "tools/debug.hpp"
///
/// Defines
// Define this it get a histogram of the memory usage
//#define HIST 1
//
// Define whether we want to print allocations
//#define DEBUG_ALLOC
//
#if CHECK_LEVEL > 0 && !defined(USE_VALGRIND)
#define MUNGE_MEM 1
#endif
#if !defined(USE_VALGRIND)
#define USE_POOL 1
#endif
///
/// Externals
///
/// Memory counter
#if CHECK_LEVEL > 0
static ULONG totalmem = 0;
static ULONG maxmem = 0;
ULONG malloccount = 0;
#endif
#ifdef HIST
#include "std/stdio.hpp"
#define SMALL_MEM_LIMIT 16384
static ULONG memhist[32];
static ULONG smallmem[SMALL_MEM_LIMIT];
static ULONG lifetime[SMALL_MEM_LIMIT];
static ULONG allocstamp[SMALL_MEM_LIMIT];
static ULONG lifetimer = 0;
#endif
///
/// BreakPoint
#if DEBUG_LEVEL > 0 || defined(TIME_ME)
#include "std/stdio.hpp"
void BreakPoint(void)
{
}
#endif
///
/// Memory histogram support
#ifdef HIST
static void RecordMemAlloc(size_t bytesize)
{
unsigned long i = 0;
lifetimer++;
while(bytesize >= (1UL<<(i+1)))
++i;
#if HAVE_ATOMIC_ADDSUB
__sync_add_and_fetch(memhist+i,1);
#else
memhist[i]++;
#endif
if (bytesize<SMALL_MEM_LIMIT) {
#if HAVE_ATOMIC_ADDSUB
__sync_add_and_fetch(smallmem+bytesize,1);
#else
smallmem[bytesize]++;
#endif
allocstamp[bytesize] = lifetimer;
}
}
static void RecordMemFree(size_t bytesize)
{
lifetimer++;
if (bytesize < SMALL_MEM_LIMIT) {
ULONG t = lifetimer - lifetime[bytesize];
if (allocstamp[bytesize] > 0 && (lifetime[bytesize] == 0 || t < lifetime[bytesize])) {
lifetime[bytesize] = t;
allocstamp[bytesize] = 0;
}
}
}
static void PrintHist(void)
{
unsigned long i;
for(i=0;i<32;i++) {
printf("Allocations for memory of chunk size between %10lu and %10lu: %lu\n",
1UL<<i,1UL<<(i+1),(unsigned long)memhist[i]);
}
for(i=0;i<SMALL_MEM_LIMIT;i++) {
if (smallmem[i])
printf("Allocation of %3lu bytes are: %lu\n",i,(unsigned long)(smallmem[i]));
}
for(i=0;i<SMALL_MEM_LIMIT;i++) {
if (lifetime[i])
printf("Life time of %3lu bytes are: %lu\n",i,(unsigned long)(lifetime[i]));
}
}
#endif
///
/// Environ::Environ
// Tag-List constructor of the environment
Environ::Environ(struct JPG_TagItem *tags)
: m_First(), m_Root(&m_First), m_WarnRoot(&m_First),
m_pParent(NULL)
{
// Now fill in the hooks from the supplied tag list
if (tags) {
m_pAllocationHook = (struct JPG_Hook *)tags->GetTagPtr(JPGTAG_MIO_ALLOC_HOOK);
m_pReleaseHook = (struct JPG_Hook *)tags->GetTagPtr(JPGTAG_MIO_RELEASE_HOOK);
//
m_pExceptionHook = (struct JPG_Hook *)tags->GetTagPtr(JPGTAG_EXC_EXCEPTION_HOOK);
m_pWarningHook = (struct JPG_Hook *)tags->GetTagPtr(JPGTAG_EXC_WARNING_HOOK);
m_bSuppressMultiple = tags->GetTagData(JPGTAG_EXC_SUPPRESS_IDENTICAL)?true:false;
//
} else {
m_pAllocationHook = NULL;
m_pReleaseHook = NULL;
m_pExceptionHook = NULL;
m_pWarningHook = NULL;
m_bSuppressMultiple = true;
}
//
//
// Now fill in the tags for the allocator
m_AllocationTags[0].ti_Tag = JPGTAG_MIO_SIZE;
m_AllocationTags[1].ti_Tag = JPGTAG_MIO_TYPE;
m_AllocationTags[2].ti_Tag = JPGTAG_MIO_ALLOC_USERDATA;
m_AllocationTags[2].ti_Data.ti_pPtr = tags?tags->GetTagPtr(JPGTAG_MIO_ALLOC_USERDATA):NULL;
m_AllocationTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// now go for the tags for the memory release: Do not fill in the
// size tag unless we really "keep" it.
m_ReleaseTags[0].ti_Tag = JPGTAG_MIO_SIZE;
m_ReleaseTags[1].ti_Tag = JPGTAG_MIO_MEMORY;
m_ReleaseTags[2].ti_Tag = JPGTAG_MIO_RELEASE_USERDATA;
m_ReleaseTags[2].ti_Data.ti_pPtr = tags?tags->GetTagPtr(JPGTAG_MIO_RELEASE_USERDATA):NULL;
m_ReleaseTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// Fill in the exception tags
m_ExceptionTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_ExceptionTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_ExceptionTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_ExceptionTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_ExceptionTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_ExceptionTags[5].ti_Tag = JPGTAG_EXC_EXCEPTION_USERDATA;
m_ExceptionTags[5].ti_Data.ti_pPtr = tags?tags->GetTagPtr(JPGTAG_EXC_EXCEPTION_USERDATA):NULL;
m_ExceptionTags[6].ti_Tag = JPGTAG_TAG_DONE;
//
// and finally for the warning hook
m_WarningTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_WarningTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_WarningTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_WarningTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_WarningTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_WarningTags[5].ti_Tag = JPGTAG_EXC_WARNING_USERDATA;
m_WarningTags[5].ti_Data.ti_pPtr = tags?tags->GetTagPtr(JPGTAG_EXC_WARNING_USERDATA):NULL;
m_WarningTags[6].ti_Tag = JPGTAG_TAG_DONE;
//
//
CleanWarnQueue();
}
///
/// Assignment operator for the environment
class Environ &Environ::operator=(class Environ &env)
{
class ExceptionStack *es,*prev;
//
m_First = ExceptionStack();
m_Root = ExceptionRoot(&m_First);
m_WarnRoot = ExceptionRoot(&m_First);
//
// Copy the parent node over.
m_pParent = env.m_pParent;
//
// Now carry the active exception stack frames over
prev = NULL;
es = env.m_Root.m_pActive;
while(es->m_pPrevious) {
// repeat until we find the first dummy exception stack frame
prev = es;
if (m_Root.m_pActive->m_pPrevious == NULL)
m_Root.m_pActive = es;
es->m_pRoot = &(this->m_Root);
es = es->m_pPrevious;
}
// Now link the "prev" into our first if we have any
if (prev)
prev->m_pPrevious = &(m_First);
//
// Now fill in the hooks from the supplied tag list
m_pAllocationHook = env.m_pAllocationHook;
m_pReleaseHook = env.m_pReleaseHook;
//
m_pExceptionHook = env.m_pExceptionHook;
m_pWarningHook = env.m_pWarningHook;
//
// Now fill in the tags for the allocator
m_AllocationTags[0].ti_Tag = JPGTAG_MIO_SIZE;
m_AllocationTags[1].ti_Tag = JPGTAG_MIO_TYPE;
m_AllocationTags[2] = env.m_AllocationTags[2];
m_AllocationTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// now go for the tags for the memory release
m_ReleaseTags[0].ti_Tag = env.m_ReleaseTags[0].ti_Tag;
m_ReleaseTags[1].ti_Tag = JPGTAG_MIO_MEMORY;
m_ReleaseTags[2].ti_Tag = JPGTAG_MIO_RELEASE_USERDATA;
m_ReleaseTags[2] = env.m_ReleaseTags[2];
m_ReleaseTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// Fill in the exception tags
m_ExceptionTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_ExceptionTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_ExceptionTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_ExceptionTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_ExceptionTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_ExceptionTags[5] = env.m_ExceptionTags[5];
m_ExceptionTags[6].ti_Tag = JPGTAG_TAG_DONE;
//
// and finally for the warning hook
m_WarningTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_WarningTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_WarningTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_WarningTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_WarningTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_WarningTags[5].ti_Tag = JPGTAG_EXC_WARNING_USERDATA;
m_WarningTags[5] = env.m_WarningTags[5];
m_WarningTags[6].ti_Tag = JPGTAG_TAG_DONE;
// Clean the exception root as indicator
// whether we shall check the memory that remains allocated.
env.m_Root.m_pActive = NULL;
//
CleanWarnQueue();
//
return *this;
}
///
/// Environ::Environ
// Clone the exception from another exception to create an identically working
// copy for a side-thread, but with an empty exception stack.
Environ::Environ(class Environ *env)
: m_First(), m_Root(&m_First), m_WarnRoot(&m_First), m_pParent(env)
{
//
// Check whether we are creating environment trees, i.e. the
// parent is not the root. This is not supported as it complicates
// the warning/exception handling structure. Threads should only
// be created by the root.
assert(m_pParent);
assert(m_pParent->m_pParent == NULL);
//
// Now fill in the hooks from the supplied tag list
m_pAllocationHook = env->m_pAllocationHook;
m_pReleaseHook = env->m_pReleaseHook;
//
m_pExceptionHook = env->m_pExceptionHook;
m_pWarningHook = env->m_pWarningHook;
//
m_bSuppressMultiple = env->m_bSuppressMultiple;
//
// Now fill in the tags for the allocator
m_AllocationTags[0].ti_Tag = JPGTAG_MIO_SIZE;
m_AllocationTags[1].ti_Tag = JPGTAG_MIO_TYPE;
m_AllocationTags[2] = env->m_AllocationTags[2];
m_AllocationTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// now go for the tags for the memory release
m_ReleaseTags[0].ti_Tag = env->m_ReleaseTags[0].ti_Tag;
m_ReleaseTags[1].ti_Tag = JPGTAG_MIO_MEMORY;
m_ReleaseTags[2].ti_Tag = JPGTAG_MIO_RELEASE_USERDATA;
m_ReleaseTags[2] = env->m_ReleaseTags[2];
m_ReleaseTags[3].ti_Tag = JPGTAG_TAG_DONE;
//
// Fill in the exception tags
m_ExceptionTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_ExceptionTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_ExceptionTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_ExceptionTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_ExceptionTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_ExceptionTags[5] = env->m_ExceptionTags[5];
m_ExceptionTags[6].ti_Tag = JPGTAG_TAG_DONE;
//
// and finally for the warning hook
m_WarningTags[0].ti_Tag = JPGTAG_EXC_ERROR;
m_WarningTags[1].ti_Tag = JPGTAG_EXC_CLASS;
m_WarningTags[2].ti_Tag = JPGTAG_EXC_LINE;
m_WarningTags[3].ti_Tag = JPGTAG_EXC_SOURCE;
m_WarningTags[4].ti_Tag = JPGTAG_EXC_DESCRIPTION;
m_WarningTags[5].ti_Tag = JPGTAG_EXC_WARNING_USERDATA;
m_WarningTags[5] = env->m_WarningTags[5];
m_WarningTags[6].ti_Tag = JPGTAG_TAG_DONE;
//
CleanWarnQueue();
// Setup the pthread identifier
#ifdef PTHREAD_DEBUGGING
SetSelfId();
#endif
#ifdef RECORD_CB_SCHEDULING
m_iThreadId = m_pParent->m_iNextAvailId++;
#endif
}
///
/// Environ::~Environ
Environ::~Environ(void)
{
// Check whether this is a child environment. If so,
// check whether it accumulated any warnings. If it did,
// forward its warning now into the warning of the parent.
// This is thread-safe because the threads are only allocated
// and released within the master/supervisor thread.
if (m_pParent) {
// This CPU becomes now available again.
if (m_WarnRoot.m_Exception.ErrorOf()) {
m_pParent->m_WarnRoot.m_Exception = m_WarnRoot.m_Exception;
}
//
// Merge the warnings.
m_pParent->MergeWarningQueueFrom(this);
}
//
// Check if this was a copy that was made for a side-thread.
if (m_Root.m_pActive && m_pParent == NULL) {
//
//
#ifdef HIST
PrintHist();
#endif
//
//
#if CHECK_LEVEL > 0
// Check only on the final destruction.
printf("\n%ld bytes memory not yet released.\n"
"\n%ld bytes maximal required.\n"
"\n%ld allocations performed.\n",
long(totalmem),long(maxmem),long(malloccount));
# ifndef USE_VALGRIND
// All memory released?
# if defined(HAVE_ATOMIC_ADDSUB) || defined(USE_I386_XADD) || defined(USE_INTERLOCKED)
assert(totalmem == 0);
# else
printf("\nFinal memory check skipped,\n"
"unreliable due to missing locking primitive\n");
# endif
# endif
#endif
}
}
///
/// Environ::MergeWarningQueueFrom
// Merge the contents of our warning queue from the
// warning queue of the given environment.
void Environ::MergeWarningQueueFrom(class Environ *p)
{
int i;
//
// Merge the warnings.
if (m_bSuppressMultiple) {
for (i = 0;i < WarnQueueSize;i++) {
if (!p->m_WarnQueue[i].isEmpty()) {
// This will also enter the warning into the database.
isWarned(p->m_WarnQueue[i]);
p->m_WarnQueue[i].Reset();
}
}
}
}
///
/// Environ::Throw
void Environ::Throw(const LONG error,const char *what,const LONG line,
const char *where,
const char *description)
{
// Must have an error.
assert(error);
// Create an exception and forward to the thrower.
Throw(Exception(error,what,line,where,description));
}
//
///
/// Environ::Throw
void Environ::Throw(const class Exception &exc)
{
class ExceptionStack *es = m_Root.m_pActive;
//
assert(es);
assert(es->m_pPrevious);
assert(es->m_pRoot == &m_Root);
//
// There must be a reason code. Internal errors are obsolete.
assert(exc.ReasonOf());
//
m_Root.m_Exception = exc;
es->Unlink();
//
// Deliver this message to the exception hook
// FIX: No, don't. Have to do this manually in the outermost catch
/*
ForwardMessage(m_pExceptionHook,m_ExceptionTags,
error,what,line,where,description);
*/
//
// And now jump to the (hopefully collected) catch line.
longjmp(es->m_JumpDestination,1);
}
//
///
/// Environ::ReThrow
// Throw the last active exception again. This emulates a "throw;" in C++
void Environ::ReThrow(void)
{
class ExceptionStack *es = m_Root.m_pActive;
//
assert(es);
assert(es->m_pPrevious);
assert(m_Root.m_Exception.ErrorOf());
//
es->Unlink();
// The error was released to the hook already, hence we need not
// to forward it again.
longjmp(es->m_JumpDestination,1);
}
///
/// Environ::CleanWarnQueue
// Clean the warn queue/history, thus report all warnings from this
// point on again.
void Environ::CleanWarnQueue(void)
{
int i;
for(i = 0;i < WarnQueueSize;i++) {
m_WarnQueue[i].Reset();
}
}
///
/// Environ::isWarned
// Check whether the given warning (at the line and source file) is already
// in the warning database. In case it is, return false. Otherwise, enter
// it to the data base and return true.
bool Environ::isWarned(const class Exception &e)
{
int i;
// This algorithm here implements a simple move-to-front list
// where entries that re-arrive in the list bubble to the front, and
// new entries replace old at the end of the list. This should be
// sufficient.
//
for(i = 0;i < WarnQueueSize;i++) {
if (m_WarnQueue[i].isEmpty()) {
// Reached end of the queue, has not been warned about this entry.
// Now move the new entry to the front, placing the old front
// entry to here.
break;
} else {
// is already in the database? Note that the file name is used
// here as a key. No string comparison! This is good enough
// for our purposes.
if (m_WarnQueue[i] == e) {
// Found already. Ok, move this to front.
if (i > 0) {
m_WarnQueue[i] = m_WarnQueue[i-1];
m_WarnQueue[i-1] = e;
}
return true;
}
}
}
//
// Here the warning is not yet in the queue and should either
// replace the entry at offset i, which is free, or a new
// entry must be allocated at the end of the list.
if (i >= WarnQueueSize)
i = WarnQueueSize - 1;
m_WarnQueue[i] = e;
return false;
}
///
/// Environ::LowerToWarning
// Deliver the last caugt exception as warning only.
void Environ::LowerToWarning(void)
{
// Just grab the data from the exception root and place it into
// the warn root.
Warn(m_Root.m_Exception);
}
///
/// Environ::Warn
void Environ::Warn(const LONG error,const char *what,const LONG line,
const char *where, const char *description)
{
// Only if there is an error
if (error) {
Warn(Exception(error,what,line,where,description));
}
}
///
/// Environ::Warn
void Environ::Warn(const class Exception &exc)
{
m_WarnRoot.m_Exception = exc;
//
// Is the user warned about this exception already? If so,
// do not do this again.
if (!m_bSuppressMultiple || !isWarned(exc)) {
// Deliver this message to the exception hook
ForwardMessage(m_pWarningHook,m_WarningTags,m_WarnRoot.m_Exception);
//
// On debugging, print out the warning immediately
PrintWarning();
}
}
///
/// Environ::PostLastError
// Deliver the last error again over the exception hook
void Environ::PostLastError(void)
{
ForwardMessage(m_pExceptionHook,m_ExceptionTags,
m_Root.m_Exception);
}
///
/// Environ::PostLastWarning
// Deliver the last warning again over the warning hook
void Environ::PostLastWarning(void)
{
ForwardMessage(m_pWarningHook,m_WarningTags,
m_WarnRoot.m_Exception);
}
///
/// Environ::NextWarning
// In case several warnings are buffered, advance to the next buffered
// warning in this environment. Requires JPGTAG_EXC_SUPPRESS_IDENTICAL
// to be set.
void Environ::NextWarning(void)
{
if (m_bSuppressMultiple) {
int i,next = -1;
// Deactivate the current warning by going thru the warning queue.
for(i = 0;i < WarnQueueSize;i++) {
if (!m_WarnQueue[i].isEmpty()) {
if (m_WarnQueue[i] == m_WarnRoot.LastException()) {
m_WarnQueue[i].Reset();
} else {
next = i; // This will be the next warning.
}
}
}
if (next >= 0) {
m_WarnRoot.m_Exception = m_WarnQueue[next];
} else {
m_WarnRoot.m_Exception.Reset();
}
}
}
///
/// Environ::CoreAllocMem
inline void *Environ::CoreAllocMem(ULONG bytesize,ULONG reqments)
{
// This is only thread-safe only if the user supplied
// allocation hook is thread-safe. The HIST option is not,
// thus don't do that.
if (bytesize == 0) {
return NULL;
} else {
void *mem;
//
#if defined(HIST)
RecordMemAlloc(bytesize);
#endif
//
#ifdef MUNGE_MEM
bytesize += 2 * sizeof(Align);
#endif
//
if (m_pAllocationHook) {
// Fill in the tags by hand. This must be rather fast, so we
// do it the nasty way.
m_AllocationTags[0].ti_Data.ti_lData = bytesize;
m_AllocationTags[1].ti_Data.ti_lData = reqments;
mem = m_pAllocationHook->CallAPtr(m_AllocationTags);
} else {
#ifdef HAVE_MALLOC
mem = malloc(bytesize);
#if CHECK_LEVEL > 0
malloccount++;
#endif
#else
mem = NULL;
#endif
}
// In case no memory is here, throw an exception.
// Except for debugging....
assert(mem);
if (mem == NULL) {
class Environ *m_pEnviron = this; // for the macro.
JPG_THROW(OUT_OF_MEMORY,"Environ::AllocMem","Out of free memory, aborted");
}
//
#ifdef MUNGE_MEM
totalmem += bytesize;
if (totalmem > maxmem)
maxmem = totalmem;
{
ULONG s = bytesize;
ULONG *p = (ULONG *)mem;
while(s >= sizeof(ULONG)) {
*p++ = 0xdeadf00dUL;
s -= sizeof(ULONG);
}
UBYTE *q = (UBYTE *)p;
switch(s) {
case 3:
*q++ = 0xde;
/* fall through */
case 2:
*q++ = 0xad;
/* fall through */
case 1:
*q++ = 0xf0;
}
}
{
void *mmem = mem;
*(ULONG *)mmem = bytesize;
mmem = (void *)(((Align *)mmem)+1);
*(class Environ **)mmem = this;
mem = (void *)(((Align *)mem)+2);
}
#endif
return mem;
}
}
///
/// Environ::CoreFreeMem
// Free a memory block
inline void Environ::CoreFreeMem(void *mem,ULONG bytesize)
{
// This is only thread-safe only if the user supplied
// allocation hook is thread-safe. The HIST option is not,
// thus don't do that.
if (mem) {
#ifdef MUNGE_MEM
mem = (void *)(((Align *)mem)-2);
bytesize += 2*sizeof(Align);
//
// Allocation and release size match?
assert(bytesize == *(ULONG *)mem);
totalmem -= bytesize;
//
// Munge memory again
{
ULONG s = bytesize;
ULONG *p = (ULONG *)mem;
while(s >= sizeof(ULONG)) {
*p++ = 0xdeadbeefUL;
s -= sizeof(ULONG);
}
UBYTE *q = (UBYTE *)p;
switch(s) {
case 3:
*q++ = 0xde;
/* fall through */
case 2:
*q++ = 0xad;
/* fall through */
case 1:
*q++ = 0xbe;
}
}
#endif
//
#ifdef HIST
RecordMemFree(bytesize);
#endif
//
//
if (m_pReleaseHook) {
struct JPG_TagItem release[4];
// Fill in the tags by hand. This must be rather fast, so we
// do it the nasty way.
release[0] = m_ReleaseTags[0];
release[0].ti_Data.ti_lData = bytesize;
release[1] = m_ReleaseTags[1];
release[1].ti_Data.ti_pPtr = mem;
release[2] = m_ReleaseTags[2];
release[3] = m_ReleaseTags[3];
m_pReleaseHook->CallAPtr(release);
} else {
#ifdef HAVE_FREE
free(mem);
#else
JPG_FATAL("Cannot release memory, no free function and no release hook");
#endif
}
}
}
///
/// Environ::AllocVec
void *Environ::AllocVec(size_t bytesize,ULONG requirements)
{
size_t *mem;
// This is build directly on AllocMem
bytesize += sizeof(union Align);
mem = (size_t *)CoreAllocMem(ULONG(bytesize),requirements);
*mem = bytesize; // enter the bytesize
return (void *)(ptrdiff_t(mem) + sizeof(union Align));
}
///
/// Environ::AllocVec without reqments
void *Environ::AllocVec(size_t bytesize)
{
size_t *mem;
// This is build directly on AllocMem
bytesize += sizeof(union Align);
mem = (size_t *)CoreAllocMem(ULONG(bytesize),0);
*mem = bytesize; // enter the bytesize
return (void *)(ptrdiff_t(mem) + sizeof(union Align));
}
///
/// Environ::AllocMem
void *Environ::AllocMem(size_t bytesize,ULONG reqments)
{
return CoreAllocMem(ULONG(bytesize),reqments);
}
///
/// Environ::AllocMem without reqments
void *Environ::AllocMem(size_t bytesize)
{
return CoreAllocMem(ULONG(bytesize),0);
}
///
/// Environ::FreeVec
void Environ::FreeVec(void *mem)
{
if (mem) {
size_t *sptr = (size_t *)(ptrdiff_t(mem) - sizeof(union Align));
CoreFreeMem(sptr,ULONG(*sptr));
}
}
///
/// Environ::FreeMem
void Environ::FreeMem(void *mem,size_t bytesize)
{
CoreFreeMem(mem,ULONG(bytesize));
}
///
/// Environ::ForwardMessage
// Forward a warning or an exception to the supplied hook
void Environ::ForwardMessage(struct JPG_Hook *hook,struct JPG_TagItem *tags,
const Exception &exc)
{
// Check whether a hook is available. If not, ignore.
// Note that this is only thread-safe if the user supplied hook is.
if (hook) {
// Fill in the tags the dirty way
tags[0].ti_Data.ti_lData = exc.ErrorOf();
tags[1].ti_Data.ti_pPtr = const_cast<char *>(exc.ObjectOf());
tags[2].ti_Data.ti_lData = exc.LineOf();
tags[3].ti_Data.ti_pPtr = const_cast<char *>(exc.SourceOf());
tags[4].ti_Data.ti_pPtr = const_cast<char *>(exc.ReasonOf());
//
//
hook->CallAPtr(tags);
}
}
///
/// Environ::GetInformation
// Get information about the environment.
void Environ::GetInformation(struct JPG_TagItem *tags) const
{
struct JPG_TagItem *curtag;
//
// This can be only called from the root environment,
// and is thus thread-safe
//
for (curtag = tags; curtag != NULL; curtag = curtag->NextTagItem()) {
switch (curtag->ti_Tag) {
case JPGTAG_MIO_ALLOC_HOOK:
curtag->ti_Data.ti_pPtr = m_pAllocationHook;
curtag->SetTagSet();
break;
case JPGTAG_MIO_RELEASE_HOOK:
curtag->ti_Data.ti_pPtr = m_pReleaseHook;
curtag->SetTagSet();
break;
case JPGTAG_MIO_KEEPSIZE:
curtag->ti_Data.ti_lData = true; // always required now.
curtag->SetTagSet();
break;
case JPGTAG_EXC_EXCEPTION_HOOK:
curtag->ti_Data.ti_pPtr = m_pExceptionHook;
curtag->SetTagSet();
break;
case JPGTAG_EXC_WARNING_HOOK:
curtag->ti_Data.ti_pPtr = m_pWarningHook;
curtag->SetTagSet();
break;
}
}
}
///
/// Fatal
#if CHECK_LEVEL > 0
void Fatal(const char *msg,const char *file,int line)
{
char *x = NULL;
fprintf(stderr,"%s.\nFatal error at line %d, file %s, execution aborted.\n",msg,line,file);
*x = 0;
}
#endif
///
/// global new operator is now obsolete, at least for the check level
#if CHECK_LEVEL > 0 && !defined(GLOBAL_NEW_IS_OK)
void *operator new(size_t)
{
assert(false);
return (void *)(ptrdiff_t(0xdeadf00dUL));
}
void *operator new[](size_t)
{
assert(false);
return (void *)(ptrdiff_t(0xdeadf00dUL));
}
#endif
///
|