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
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//-       Class: MemoryManager<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
//-       Owner: Jim Hipp
//- Description: MemoryManager provides object information and stack
//-              storage pointer management used in association with
//-              MemoryBlock and MemoryAllocation classes.
//- Checked By: 
//-    Version:
//  Modified 5/7/96 R W Ostensen:  Fixed operator_new function as per recommendation
//  from J Hipp.  Eliminated assignment of headOfFreeList to wild pointer loaction when
//  memAllocatnSize=1.


#include "MemoryBlock.hpp"
#include "MemoryManager.hpp"
#include "CubitMessage.hpp"
#include "ArrayBasedContainer.hpp"
#include "AppUtil.hpp"
#include <cassert>
#include <cstring>
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#endif


MemoryManager* MemoryManager::memoryManagerListHead = NULL;

// constructors
MemoryManager::MemoryManager() : useNew(false), objectName(NULL)
{
  // do not allow default constructor (must assign objectSize)
  assert(0);
}

MemoryManager::MemoryManager(const MemoryManager&) : useNew(false), objectName(NULL)
{
  // do not allow copy
  assert(0);
}

MemoryManager::MemoryManager(const char* name, size_t size, int mem_size,
                             int static_flag)
  : useNew(true)
{
    // allocate space for name and copy into buffer
  if (name) 
  {
    objectName = new char[std::strlen(name) + 1];
    std::strcpy(objectName, name);
  }
  else
    objectName = NULL;
  
    // initialize manager parameters
  staticManager   = static_flag;
  memBlockStack   = NULL;
  headOfFreeList  = NULL;
  
  assert(size >= sizeof(char*));
  objectSize = size;
  
  set_memory_allocation_increment(mem_size);
  
    // attach new manager to static stack
  next = memoryManagerListHead;
  memoryManagerListHead = this;
}

// destructor
MemoryManager::~MemoryManager()
{
  // delete objectName space and destroy block memory
  delete [] objectName;
  objectName = NULL;
  destroy_memory(staticManager);
  
  // find 'prev' manager pointer
  MemoryManager* prev        = NULL;
  MemoryManager* mem_manager = memoryManagerListHead;
  bool first = true;
  while (mem_manager && mem_manager != this)
  {
    if (!first && mem_manager == memoryManagerListHead)
    {
      // XXX: We aren't in the list!?
      std::cerr << "MemoryManager: corrupted list?" << std::endl;
      return;
    }
    first = false;
    prev        = mem_manager;
    mem_manager = mem_manager->next;
  }

  // remove memory manager from static list

  if (!next)
  {
    if (!prev)
    {
      // no memory managers left

      memoryManagerListHead = NULL;
    }
    else
    {
      // remove tail node

      prev->next = NULL;
    }
  }
  else
  {
    if (!prev)
    {
      // remove head node

      memoryManagerListHead = next;
    }
    else
    {
      // remove intermediate node

      prev->next = next;
    }
  }
}

// set memory block allocation increment
void MemoryManager::set_memory_allocation_increment(int mem_size)
{
  // set memory allocation increment

  if (mem_size <= 0)
  {
     memAllocatnSize = DEFAULT_MEMORY_ALLOC_SIZE;
  }
  else
  {
    memAllocatnSize = mem_size;
  }
}

// destroy allocated block memory
void MemoryManager::destroy_memory(int static_flag)
{
  // assert if this is not a static memory manager and some of it's objects
  // are still in use
  if (!static_flag) {
    assert (!get_used_objects());<--- Assert statement calls a function which may have desired side effects: 'get_used_objects'.
  }

  // else delete block memory if any was allocated
  if (memBlockStack) delete memBlockStack;

  // reset stack pointers to empty
  headOfFreeList = NULL;
  memBlockStack  = NULL;
}

// return number of objects allocated
int MemoryManager::get_allocated_objects()
{
  // return total number of objects allocated for this memory manager

  if (memBlockStack)
  {
    return (memBlockStack->get_memory_allocation() / objectSize);
  }
  else
  {
    return 0;
  }
}

// return free objects
int MemoryManager::get_free_objects()
{
  // return total number of free objects (allocated but not used) for this
  // memory manager

  if (headOfFreeList)
  {
    int i = 0;
    char* list_ptr = headOfFreeList;
    while (list_ptr)
    {
      i++;
      list_ptr = *((char**) list_ptr);
    }

    return i;
  }
  else
  {
    return 0;
  }
}

// return used objects
int MemoryManager::get_used_objects()
{
  // return total number of used objects for this memory manager

  return (get_allocated_objects() - get_free_objects());
}

// print allocation information to the command line
void MemoryManager::show_object_memory(const char* name)<--- The function 'show_object_memory' is never used.
{
  int instance = 0;

  // find all instances of memory managers for object: name

  MemoryManager* mem_manager = memoryManagerListHead;
  while (mem_manager)
  {
    if (!std::strcmp(mem_manager->objectName, name))
    {
      // if found then print pertinent memory allocation information

      instance++;
      if (instance > 1)
      {
        PRINT_INFO("\nObject Name(%d): %s\n\n", instance, name);
      }
      else
      {
        PRINT_INFO("\nObject Name: %s\n\n", name);
      }

      int a_obj = mem_manager->get_allocated_objects();
      int f_obj = mem_manager->get_free_objects();
      int u_obj = a_obj - f_obj;

      PRINT_INFO("  Object Size: %lu     Allocation Increment: %d\n\n",
      (unsigned long)mem_manager->objectSize, mem_manager->memAllocatnSize);
      PRINT_INFO("  Allocated Objects: %d  (bytes) %d\n", a_obj,
		 a_obj * (int)(mem_manager->objectSize));
      if (a_obj)
      {
        PRINT_INFO("       Free Objects: %d  (bytes) %d (%d%%)\n", f_obj,
	  	   f_obj * (int)(mem_manager->objectSize),
		   (100*f_obj)/a_obj);
        PRINT_INFO("       Used Objects: %d  (bytes) %d (%d%%)\n", u_obj,
		   u_obj * (int)(mem_manager->objectSize),
		   (100*u_obj)/a_obj);
      }
    }

    // get next memory manager

    mem_manager = mem_manager->next;
  }

  // if none were found then announce

  if (!instance)
  {
    PRINT_INFO("\nObject: %s was not found ...\n",  name);
  }
}


// show allocation for all memory manager objects
void MemoryManager::show_all_object_memory()<--- The function 'show_all_object_memory' is never used.
{

#if defined(MACOSX)
	
  pid_t PID = getpid();
  char command1[60];
  unsigned long rss=0, vm=0;

  FILE *pipe;
  char buf[1024];
  
  //get size of real memory
  sprintf(command1,"ps -o rss -p %d | grep -v RSS",PID);
  pipe = popen(command1, "r");
  if (pipe) 
  {
    fgets(buf, 1024, pipe);
    rss = strtoul(buf, NULL, 0);
    pclose(pipe);
  }

  //get size of virtual memory
  sprintf(command1,"ps -o vsz -p %d | grep -v VSZ",PID);
  pipe = popen(command1, "r");
  if (pipe) 
  {
    fgets(buf, 1024, pipe);
    vm = strtoul(buf, NULL, 0);
    pclose(pipe);
  }

  PRINT_INFO("Total memory = %lu\n", (unsigned long)vm );
  PRINT_INFO("Resident memory = %lu\n", (unsigned long)rss );


/*
  struct rusage my_rusage;
  int ret_val = getrusage( RUSAGE_CHILDREN, &my_rusage ); 

  if( ret_val == 0 )
  {
    PRINT_INFO("It was a success\n");
    PRINT_INFO("Memory size = %d\n", my_rusage.ru_maxrss );
    PRINT_INFO("Unshared data size = %d\n", my_rusage.ru_idrss);
    PRINT_INFO("Integeral unshared data size = %d\n", my_rusage.ru_isrss);
    PRINT_INFO("more values: %d %d %d %d %d %d %d %d %d %d %d \n",
		my_rusage.ru_ixrss, my_rusage.ru_minflt, my_rusage.ru_majflt, my_rusage.ru_nswap, 
                my_rusage.ru_inblock, my_rusage.ru_oublock, my_rusage.ru_msgsnd, my_rusage.ru_msgrcv, 
                my_rusage.ru_nsignals, my_rusage.ru_nvcsw, my_rusage.ru_nivcsw ); 
  }
  else
    PRINT_INFO("It was a failure\n");
 */  

 
/* 
  int i, mib[4];
  size_t len;
  struct kinfo_proc kp;

  len = 4;
  sysctlnametomib("kern.proc.pid", mib, &len);
  len = sizeof(kp);
  int pid = getpid();
  mib[3] = pid;
  if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
  {
    perror("sysctl");
    PRINT_INFO("Got problems\n");
  }
  else if (len > 0)
  {
    PRINT_INFO("The call was successful!!!\n");
  }
*/

/*
  int i, mib[4];
  size_t len;
  struct kinfo_proc kp;
 
  len = 4; 
  sysctlnametomib("kern.proc.pid", mib, &len);

  for (i = 0; i < 100; i++) 
  {         
    mib[3] = i;         
    len = sizeof(kp);         
    if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
      perror("sysctl");         
    else if (len > 0)   
      PRINT_INFO("Call was successful!\n"); 
  }
*/ 

#endif


#if defined(CUBIT_LINUX)
  unsigned long vm, rss;
  process_mem_usage( vm, rss );

  unsigned long read, write;
  process_file_io( read, write );

  PRINT_INFO("Total memory = %lu\n", vm );
  PRINT_INFO("Resident memory = %lu\n", rss );
  PRINT_INFO("Bytes read = %lu\n", read );
  PRINT_INFO("Bytes written = %lu\n", write );
#endif

  /*
  long int a_obj_byte_total = 0;
  long int f_obj_byte_total = 0;
  long int u_obj_byte_total = 0;

  // loop over all memory managers

  PRINT_INFO("\nDynamic Memory Allocation per Object\n\n");

  MemoryManager* mem_manager = memoryManagerListHead;
  while (mem_manager) {
    long int a_obj = mem_manager->get_allocated_objects();
    long int f_obj = mem_manager->get_free_objects();
    long int u_obj = a_obj - f_obj;
    if (a_obj) {
      // sum total allocated memory parameters (bytes)
      a_obj_byte_total += a_obj * mem_manager->objectSize;
      f_obj_byte_total += f_obj * mem_manager->objectSize;
      u_obj_byte_total += u_obj * mem_manager->objectSize;
    }
    mem_manager = mem_manager->next;
  }

  mem_manager = memoryManagerListHead;
  while (mem_manager)
  {
    // print pertinent memory allocation information 

    long int a_obj = mem_manager->get_allocated_objects();
    long int f_obj = mem_manager->get_free_objects();
    long int u_obj = a_obj - f_obj;

    if (a_obj)
    {
      PRINT_INFO("\nObject Name: %s\n\n", mem_manager->objectName);

      PRINT_INFO("  Object Size: %d     Allocation Increment: %d\n\n",
	         mem_manager->objectSize, mem_manager->memAllocatnSize);
      if (a_obj_byte_total != 0)
      {
         PRINT_INFO("  Allocated Objects: %ld  (bytes) %ld (%d%% of Total)\n",
                    a_obj, a_obj * mem_manager->objectSize,
                    int((100.0*a_obj * mem_manager->objectSize)/a_obj_byte_total));
         PRINT_INFO("       Free Objects: %ld  (bytes) %ld (%d%%)\n", f_obj,
                    f_obj * mem_manager->objectSize,
                    int((100.0*f_obj)/a_obj));
         PRINT_INFO("       Used Objects: %ld  (bytes) %ld (%d%%)\n", u_obj,
                    u_obj * mem_manager->objectSize,
                    int((100.0*u_obj)/a_obj));
      }
      else
      {
         PRINT_INFO("  Allocated Objects: %ld  (bytes) %ld (100%% of Total)\n",
                    a_obj, a_obj * mem_manager->objectSize);
         PRINT_INFO("       Free Objects: %ld  (bytes) %ld (100%%)\n", f_obj,
                    f_obj * mem_manager->objectSize);
         PRINT_INFO("       Used Objects: %ld  (bytes) %ld (100%%)\n", u_obj,
                    u_obj * mem_manager->objectSize);
      }
    }

    mem_manager = mem_manager->next;
  }

  // print total memory allocation information

  char sizechar;

  PRINT_INFO("\nTotal Memory Allocation Information\n\n");
  int divisor;
  if (a_obj_byte_total > 10000000) {
    sizechar = 'M';
    divisor = 1000000;
  }
  else {
    sizechar = 'K';
    divisor = 1000;
  }
  
  PRINT_INFO("  Allocated Memory: %ld%c (%ld bytes)\n", a_obj_byte_total/divisor, 
             sizechar, a_obj_byte_total);

  if (a_obj_byte_total)
  {
    PRINT_INFO("       Free Memory: %ld%c (%d%%)\n", f_obj_byte_total/divisor, sizechar,
	       int((100.0*f_obj_byte_total)/a_obj_byte_total));
    PRINT_INFO("       Used Memory: %ld%c (%d%%)\n", u_obj_byte_total/divisor, 
               sizechar,
	       int((100.0*u_obj_byte_total)/a_obj_byte_total));
  }

#ifndef JANUS
#ifndef _WIN32
  struct rusage r_usage;
  AppUtil::instance()->apputil_getrusage(r_usage);
  PRINT_INFO("       (System reports %ld%c used, incl. executable)\n", 
              r_usage.ru_maxrss*getpagesize()/divisor, sizechar);
#else
  PRINT_INFO("\n");
#endif // _WIN32
#endif // JANUS

  // print DLList non-Pool allocation information
  PRINT_INFO("\nTotal non-pool ArrayBasedContainer memory allocation  = %u%c\n"
	       "Maximum non-pool ArrayBasedContainer memory allocated = %u%c\n",
             ArrayBasedContainer::current_allocated_memory()/divisor, sizechar, 
             ArrayBasedContainer::maximum_allocated_memory()/divisor, sizechar);
*/



#if 0
  // print HOOPS memory usage
  long allocated = 0;
  long in_use = 0;
    //  DrawingTool::instance()->show_memory(allocated, in_use);
  if (allocated != 0)
     PRINT_INFO("\nGraphics subsystem memory: Allocated = %u%c (%d bytes)\n"
                "                           In-Use    = %u%c (%d%%)\n",
                allocated/divisor, sizechar, allocated, 
                in_use/divisor, sizechar, int((100.0*in_use)/allocated));
  else
     PRINT_INFO("\nGraphics subsystem memory: Allocated = %u%c (%d bytes)\n"
                "                           In-Use    = %u%c (100%%)\n",
                allocated/divisor, sizechar, allocated, 
                in_use/divisor, sizechar);
#endif
}

// compress memory for the requested object
int MemoryManager::compress_object_memory(const char* name)<--- The function 'compress_object_memory' is never used.
{
  // find all instances of memory managers for object: name

  int found        = 0;
  int saved_memory = 0;
  MemoryManager* mem_manager = memoryManagerListHead;
  while (mem_manager)
  {
    if (!std::strcmp(mem_manager->objectName, name))
    {
      // if found then compress memory

      saved_memory += mem_manager->compress_memory();<--- Variable 'saved_memory' is assigned a value that is never used.
      found         = 1;
    }

    mem_manager = mem_manager->next;
  }

  return found;
}

// compress all object memory
int MemoryManager::compress_all_object_memory()<--- The function 'compress_all_object_memory' is never used.
{
  // find all instances of memory managers
  int saved_memory = 0;
  MemoryManager* mem_manager = memoryManagerListHead;
  MemoryManager* block_manager = NULL;
  while (mem_manager)
  {
    if (!std::strcmp(mem_manager->objectName, "MemoryBlock"))
    {
      // save block_manager until end

      block_manager = mem_manager;
    }
    else
    {
      // compress memory

      saved_memory += mem_manager->compress_memory();
    }

    mem_manager = mem_manager->next;
  }

  if (block_manager)
  {
    saved_memory += block_manager->compress_memory();
  }

  return saved_memory;
}
      
// generic operator new call
void* MemoryManager::operator_new(size_t size)
{

  if (useNew) return malloc(size);

  // send requests of "wrong" size to ::new
  
  try
  { 
    if (size != objectSize) return ::new char[size];
  }
  catch(...) 
  {
    return (void*) NULL;
  }
  // get new element from head of free list

  char* p = headOfFreeList;

  try
  {
    if(!p)
        {
        // allocate new block

        int block_size  = memAllocatnSize * size;
        char* new_block = ::new char[block_size];
        if (!new_block) return (void*) NULL;

        // link new elements to form the free list

        int fill_limit = (memAllocatnSize - 1) * size;
        for (int j = 0; j < fill_limit; j += size)
        {
          *((char**) &new_block[j]) = &new_block[j + size];
        }
        *((char**) &new_block[fill_limit]) = (char*) NULL;

        // assign new element

        p = new_block;

        // save new block to memory block stack

        memBlockStack = new MemoryBlock(memBlockStack, new_block, block_size);
      }
  }
  catch(...)
  {
    return (void*) NULL;
  }
  //assign head of free list and return p

  headOfFreeList = *((char**) p);
  return (void*) p;
}

// generic operator delete call
void MemoryManager::operator_delete(void *deadObject, size_t size)
{
  if (useNew) 
  {
    free(deadObject);
    return;
  }

  // requests of "wrong" size to ::delete

  if (size != objectSize)
  {
    ::delete [] ((char*) deadObject);
    return;
  }

  // attach dead element to head of free list

  char* delete_object = (char*) deadObject;
  *((char**) delete_object) = headOfFreeList;
  headOfFreeList = delete_object;
}

// compress memory blocks
int MemoryManager::compress_memory()
{
  // if free objects exist then begin compression algorithm

  if (headOfFreeList)
  {
    // find total number of memory blocks attached to stack
 
    int n_blocks = 0;
    MemoryBlock* mem_block = memBlockStack;
    while (mem_block)
    {
      n_blocks++;
      mem_block = mem_block->next_block();
    }

    if (n_blocks == 0)
    {
       // no available memory to free ... return 0
       // this is here for safety ... n_blocks should never be zero if
       // headOfFreeList is not Null

       return 0;
    }
    else
    {
      // first determine if all objects are free ... if so then perform
      // the easy compression routine

      if (!get_used_objects())
      {
        // all objects are free ... delete all blocks

        int n_bytes = memBlockStack->get_memory_allocation();
        destroy_memory(staticManager);

        // return freed memory

        return n_bytes;
      }

      // else perform the complex routine to remove those memory blocks that
      // have all free elements

      // begin by constructing an integer array for each memory block to
      // tally the number of free objects that each block contains

        // if there are a lot of blocks, we can save a huge amount of
        // time by looking in the last few blocks that contained an
        // element.
      const int use_cache = n_blocks > 8;
      int i, j, k;
      i = j = k = 0;
      const int cache_size = 4;
      MemoryBlock* mem_block_sav[cache_size];
      int i_sav[cache_size];
      for ( i = cache_size; i--; )
      {
        mem_block_sav[i] = NULL;
        i_sav[i] = 0;
      }
      int found = 0;
      
      mem_block = NULL;
      char* list_ptr = NULL;
      
      unsigned int* free_tally = new unsigned int [n_blocks];
      for (i = 0; i < n_blocks; i++) free_tally[i] = 0;

      // loop through free list tallying free elements

      list_ptr = headOfFreeList;
      while (list_ptr)
      {
        // find memory block that owns this element
        
          // check last few blocks for speed
        found = CUBIT_FALSE;
        if ( use_cache )
        {
          for ( i = 0; i < cache_size; i++ )
          {
            mem_block = mem_block_sav[i];
            if ( mem_block &&
                 list_ptr >= mem_block->get_block() &&
                 list_ptr < (mem_block->get_block() + 
                             mem_block->block_size()) )
            {
              k = i_sav[i];
              free_tally[k]++;
              found = CUBIT_TRUE;
              break;
            }
          }
        }
        if ( !found )
        {
            // search through all blocks
          mem_block = memBlockStack;
          for (i = 0; i < n_blocks; i++)
          {
            if ((list_ptr >= mem_block->get_block()) &&
                (list_ptr < (mem_block->get_block() + mem_block->block_size())))
            {
                // increment tally and exit
              
              free_tally[i]++;
                // save
              if ( use_cache && mem_block_sav[j] != mem_block )
              {
                mem_block_sav[j] = mem_block;
                i_sav[j] = i;
                if ( ++j >= cache_size )
                  j = 0;
              }
              break;
            }
            
              //  get next memory block
            mem_block = mem_block->next_block();
          }
        }

        // get next element
        list_ptr = *((char**) list_ptr);
      }

      // zero tally for memory blocks that cannot be removed ... those that
      // have some used elements

      int all_blocks = 0;
      mem_block = memBlockStack;
      for (i = 0; i < n_blocks; i++)
      {
        if (free_tally[i] != (mem_block->block_size() / objectSize))
	{
          free_tally[i] = 0;
          all_blocks++;
        }

        mem_block = mem_block->next_block();
      }

      if (all_blocks == n_blocks)
      {
        // no memory can be saved ... all blocks have some used elements
        // return 0

        delete [] free_tally;
        return 0;
      }

      // adjust free list pointers to remove those that belong to
      // memory blocks that can be deleted
      char* prev_ptr = NULL;
      list_ptr = headOfFreeList;
      while (list_ptr)
      {
        // find memory block that owns this element
          // check last few blocks for speed
        found = CUBIT_FALSE;
        if ( use_cache )
        {
          for ( i = 0; i < cache_size; i++ )
          {
            mem_block = mem_block_sav[i];
            if ( mem_block &&
                 list_ptr >= mem_block->get_block() &&
                 list_ptr < (mem_block->get_block() + 
                             mem_block->block_size()) )
            {
              k = i_sav[i];
              found = CUBIT_TRUE;
              break;
            }
          }
        }
        if ( !found )
        {
          mem_block = memBlockStack;
          for (i = 0; i < n_blocks; i++)
          {
            if ((list_ptr >= mem_block->get_block()) &&
                (list_ptr < (mem_block->get_block() + mem_block->block_size())))
            {
              k = i;
                // save
              if ( use_cache && mem_block_sav[j] != mem_block )
              {
                mem_block_sav[j] = mem_block;
                i_sav[j] = i;
                if ( ++j >= cache_size )
                  j = 0;
              }
              break;
            }
              // get next memory block
            mem_block = mem_block->next_block();
          }
        }
        
        if (free_tally[k])
        {
            // remove element
          
          if (prev_ptr)
          {
            *((char**) prev_ptr) = *((char**) list_ptr);
          }
          else
          {
            headOfFreeList = *((char**) list_ptr);
          }
        }
        else
        {
            // advance prev_ptr  
          prev_ptr = list_ptr;
        }

        // get next element
        list_ptr = *((char**) list_ptr);
      }

      // delete all memory blocks that have free_tally[i] > 0

      i = 0;
      int save_bytes = 0;
      MemoryBlock* prev_block = NULL;
      mem_block               = memBlockStack;
      while (mem_block)
      {
        if (free_tally[i])
        {
          // set previous MemoryBlocks next pointer to skip this block

          if (prev_block)
          {
            prev_block->next_block(mem_block->next_block());
          }
          else
          {
            memBlockStack = mem_block->next_block();
          }

          // set MemoryBlock next pointer to NULL to avoid recusive delete
          // update saved memory and delete mem_block

          mem_block->next_block((MemoryBlock*) NULL);
          save_bytes += mem_block->block_size();
          delete mem_block;

          // update mem_block to point to new current MemoryBlock

          if (prev_block)
          {
            mem_block = prev_block->next_block();
          }
          else
          {
            mem_block = memBlockStack;
          }
        }
        else
        {
          // if block wasn't removed then update previous and current blocks

          prev_block = mem_block;
          mem_block = mem_block->next_block();
        }

        // increment to next block (used by free_tally array)

        ++i;
      }

      // return freed memory (bytes)

      delete [] free_tally;
      return save_bytes;
    }
  }
  else
  {
    // no memory allocated ... return 0

    return 0;
  }
}


void MemoryManager::process_mem_usage(unsigned long &vm_usage, unsigned long &resident_set)
{

#if defined(CUBIT_LINUX)
  using std::ios_base;
  using std::ifstream;
  using std::string;

  vm_usage     = 0;
  resident_set = 0;

  // 'file' stat seems to give the most reliable results
  //
  ifstream stat_stream("/proc/self/stat",ios_base::in);
  
  // dummy vars for leading entries in stat that we don't care about
  //
  string pid, comm, state, ppid, pgrp, session, tty_nr;
  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
  string utime, stime, cutime, cstime, priority, nice;
  string O, itrealvalue, starttime;
  
  // the two fields we want...virtual memory size and resident memory size
  //
  unsigned long vsize;
  long rss;
  
  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
              >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
              >> utime >> stime >> cutime >> cstime >> priority >> nice
              >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
              
  long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
  vm_usage     = vsize / 1024;
  resident_set = rss * page_size_kb;
#else
  vm_usage = 0;
  resident_set = 0;
#endif
}
  
 
void MemoryManager::process_file_io(unsigned long &read, unsigned long &write )
{
#if defined(CUBIT_LINUX)
  using std::ios_base;
  using std::ifstream;
  using std::string;

  read = 0;
  write = 0;

  // 'file' stat seems to give the most reliable results
  //
  ifstream stat_stream("/proc/self/io",ios_base::in);
  
  // dummy vars for leading entries in stat that we don't care about
  //
  string char1, char2;

  //----------------------Getting two numbers out of this file 
  // I/O counter: chars read
  //The number of bytes which this task has caused to be read from storage. This
  //is simply the sum of bytes which this process passed to read() and pread().
  //It includes things like tty IO and it is unaffected by whether or not actual
  //physical disk IO was required (the read might have been satisfied from
  //pagecache)
  
  // I/O counter: chars written
  //The number of bytes which this task has caused, or shall cause to be written
  //to disk. Similar caveats apply here as with rchar.
  
  unsigned long tmp_read, tmp_write;
  
  stat_stream >> char1 >> tmp_read >> char2 >> tmp_write; //don't care about the rest 
  
  read = tmp_read;
  write = tmp_write;
#else
  read = 0;
  write = 0;
#endif
}