00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <sys/types.h>
00042 #include <malloc.h>
00043 #include <bstring.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046
00047
00048 #define NO_MEM_POOL_WORKAROUND
00049 #include "workaround.h"
00050 #include "messg.h"
00051 #include "mempool.h"
00052 #include "tracing.h"
00053
00054
00055
00056
00057
00058
00059 #define MEM_POOL_INIT_IN_PROGRESS (-1)
00060
00061
00062
00063
00064
00065
00066 #define BLOCK_SIZE 0x2000
00067
00068
00069
00070
00071 #define MIN_LARGE_BLOCK_SIZE 0x800
00072
00073
00074
00075 #ifdef Is_True_On
00076 #define ZAP_ON_FREE (1)
00077 #endif
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 MEM_POOL MEM_local_pool;
00088 MEM_POOL MEM_src_pool;
00089 MEM_POOL MEM_pu_pool;
00090 MEM_POOL MEM_phase_pool;
00091
00092 MEM_POOL *MEM_local_pool_ptr = &MEM_local_pool;
00093 MEM_POOL *MEM_src_pool_ptr = &MEM_src_pool;
00094 MEM_POOL *MEM_pu_pool_ptr = &MEM_pu_pool;
00095 MEM_POOL *MEM_phase_pool_ptr = &MEM_phase_pool;
00096
00097
00098
00099
00100
00101 MEM_POOL MEM_local_nz_pool;
00102 MEM_POOL MEM_src_nz_pool;
00103 MEM_POOL MEM_pu_nz_pool;
00104 MEM_POOL MEM_phase_nz_pool;
00105
00106 MEM_POOL *MEM_local_nz_pool_ptr = &MEM_local_nz_pool;
00107 MEM_POOL *MEM_src_nz_pool_ptr = &MEM_src_nz_pool;
00108 MEM_POOL *MEM_pu_nz_pool_ptr = &MEM_pu_nz_pool;
00109 MEM_POOL *MEM_phase_nz_pool_ptr = &MEM_phase_nz_pool;
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 struct mem_block {
00122 size_t avail;
00123
00124
00125 MEM_PTR ptr;
00126
00127 MEM_BLOCK *rest;
00128
00129
00130 };
00131
00132 #define MEM_BLOCK_avail(x) ((x)->avail)
00133 #define MEM_BLOCK_ptr(x) ((x)->ptr)
00134 #define MEM_BLOCK_rest(x) ((x)->rest)
00135
00136 #define MEM_BLOCK_first_ptr(x) \
00137 (MEM_PTR) (((char *) (x)) + PAD_TO_ALIGN(sizeof(MEM_BLOCK)))
00138
00139
00140
00141
00142
00143
00144 typedef struct mem_large_block MEM_LARGE_BLOCK;
00145 struct mem_large_block {
00146 MEM_LARGE_BLOCK *next;
00147 MEM_LARGE_BLOCK *prev;
00148 MEM_POOL_BLOCKS *base;
00149 MEM_PTR ptr;
00150 };
00151
00152 #define MEM_LARGE_BLOCK_next(x) ((x)->next)
00153 #define MEM_LARGE_BLOCK_prev(x) ((x)->prev)
00154 #define MEM_LARGE_BLOCK_base(x) ((x)->base)
00155 #define MEM_LARGE_BLOCK_ptr(x) ((x)->ptr)
00156 #define MEM_LARGE_BLOCK_OVERHEAD (PAD_TO_ALIGN(sizeof(MEM_LARGE_BLOCK)))
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 static void
00170 MEM_LARGE_BLOCK_zap(MEM_LARGE_BLOCK *block)
00171 {
00172 MEM_LARGE_BLOCK_base(block) = NULL;
00173 MEM_LARGE_BLOCK_ptr(block) = NULL;
00174 }
00175
00176 static void
00177 MEM_LARGE_BLOCK_free(MEM_LARGE_BLOCK *block)
00178 {
00179 MEM_LARGE_BLOCK_zap(block);
00180 free(block);
00181 }
00182
00183 static MEM_LARGE_BLOCK *
00184 MEM_LARGE_BLOCK_realloc(MEM_LARGE_BLOCK *old_block, INT64 new_size)
00185 {
00186 MEM_POOL_BLOCKS * const base = MEM_LARGE_BLOCK_base(old_block);
00187 MEM_PTR const ptr = MEM_LARGE_BLOCK_ptr(old_block);
00188 MEM_LARGE_BLOCK * new_block;
00189
00190 MEM_LARGE_BLOCK_zap(old_block);
00191 new_block = (MEM_LARGE_BLOCK *)realloc(old_block, new_size);
00192 if (new_block != NULL)
00193 {
00194 MEM_LARGE_BLOCK_base(new_block) = base;
00195 MEM_LARGE_BLOCK_ptr(new_block) = ptr;
00196 }
00197 return new_block;
00198 }
00199
00200
00201
00202
00203
00204 typedef struct int_list INT_LIST;
00205
00206 struct int_list {
00207 INT32 first;
00208
00209 INT_LIST *rest;
00210
00211 };
00212
00213 #define INT_LIST_first(x) ((x)->first)
00214 #define INT_LIST_rest(x) ((x)->rest)
00215
00216 static INT_LIST *free_int_lists;
00217
00218
00219
00220
00221
00222
00223 struct mem_stat {
00224 const char *file;
00225
00226 INT32 line;
00227
00228 INT32 total;
00229
00230
00231 INT32 current;
00232
00233
00234 INT32 max_t;
00235
00236
00237
00238 size_t max_s;
00239
00240
00241
00242 INT32 last;
00243
00244
00245 INT32 last_grew;
00246
00247
00248
00249 INT32 last_shrank;
00250
00251
00252
00253 INT32 count;
00254
00255 MEM_STAT *hash_list_rest;
00256
00257
00258 MEM_STAT *pool_list_rest;
00259
00260
00261 INT_LIST *saved_current;
00262
00263
00264
00265 MEM_POOL *pool;
00266
00267 };
00268
00269 #define MEM_STAT_file(x) ((x)->file)
00270 #define MEM_STAT_line(x) ((x)->line)
00271 #define MEM_STAT_total(x) ((x)->total)
00272 #define MEM_STAT_current(x) ((x)->current)
00273 #define MEM_STAT_max_t(x) ((x)->max_t)
00274 #define MEM_STAT_max_s(x) ((x)->max_s)
00275 #define MEM_STAT_last(x) ((x)->last)
00276 #define MEM_STAT_last_grew(x) ((x)->last_grew)
00277 #define MEM_STAT_last_shrank(x) ((x)->last_shrank)
00278 #define MEM_STAT_count(x) ((x)->count)
00279 #define MEM_STAT_hash_list_rest(x) ((x)->hash_list_rest)
00280 #define MEM_STAT_pool_list_rest(x) ((x)->pool_list_rest)
00281 #define MEM_STAT_saved_current(x) ((x)->saved_current)
00282 #define MEM_STAT_pool(x) ((x)->pool)
00283
00284
00285
00286
00287
00288
00289
00290 struct mem_pool_blocks {
00291 MEM_BLOCK *block;
00292
00293 MEM_LARGE_BLOCK *large_block;
00294
00295 MEM_BLOCK *base_block;
00296
00297
00298 MEM_PTR *base_ptr;
00299
00300 size_t base_avail;
00301
00302
00303 MEM_POOL_BLOCKS *rest;
00304
00305
00306
00307
00308 };
00309
00310 #define MEM_POOL_BLOCKS_block(x) ((x)->block)
00311 #define MEM_POOL_BLOCKS_large_block(x) ((x)->large_block)
00312 #define MEM_POOL_BLOCKS_base_block(x) ((x)->base_block)
00313 #define MEM_POOL_BLOCKS_base_ptr(x) ((x)->base_ptr)
00314 #define MEM_POOL_BLOCKS_base_avail(x) ((x)->base_avail)
00315 #define MEM_POOL_BLOCKS_rest(x) ((x)->rest)
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 struct mem_pure_stack {
00360 MEM_PTR last_alloc;
00361 MEM_PURE_STACK *prev_stack;
00362
00363
00364 };
00365
00366 #define MEM_PURE_STACK_last_alloc(x) ((x)->last_alloc)
00367 #define MEM_PURE_STACK_prev_stack(x) ((x)->prev_stack)
00368 #define MEM_POOL_last_alloc(x) \
00369 MEM_PURE_STACK_last_alloc(MEM_POOL_pure_stack(x))
00370 #define MEM_POOL_prev_stack(x) \
00371 MEM_PURE_STACK_prev_stack(MEM_POOL_pure_stack(x))
00372 BOOL purify_pools = FALSE;
00373 static BOOL purify_pools_trace = FALSE;
00374 static BOOL purify_pools_trace_x = FALSE;
00375
00376 #define MAGIC_NUM 0xabcd
00377
00378
00379
00380
00381 #define MEM_POOL_name(x) ((x)->name)
00382 #define MEM_POOL_blocks(x) ((x)->blocks)
00383 #define MEM_POOL_bz(x) ((x)->bz)
00384 #define MEM_POOL_rest(x) ((x)->rest)
00385 #define MEM_POOL_pure_stack(x) ((x)->pure_stack)
00386 #define MEM_POOL_frozen(x) ((x)->frozen)
00387 #define MEM_POOL_magic_num(x) ((x)->magic_num)
00388 #define MEM_POOL_alloc_site_list(x) ((x)->alloc_site_list)
00389
00390 #define MEM_POOL_block(x) \
00391 MEM_POOL_BLOCKS_block(MEM_POOL_blocks(x))
00392 #define MEM_POOL_large_block(x) \
00393 MEM_POOL_BLOCKS_large_block(MEM_POOL_blocks(x))
00394
00395
00396
00397
00398
00399 static MEM_POOL_BLOCKS *free_mem_pool_blocks_list;
00400
00401
00402
00403 static MEM_POOL_BLOCKS overhead_blocks;
00404
00405
00406
00407 static MEM_POOL mem_overhead_pool =
00408
00409
00410 {
00411 "memory overhead",
00412 &overhead_blocks,
00413 NULL,
00414 NULL,
00415 TRUE,
00416 FALSE,
00417 MAGIC_NUM,
00418 NULL
00419 };
00420
00421 static MEM_POOL *The_Default_Mem_Pool;
00422
00423
00424
00425 #define PAD_TO_ALIGN(size) (((size) + 7) & (~0U << 3))
00426
00427
00428
00429
00430 static BOOL mem_tracing_enabled = FALSE;
00431 #ifdef Is_True_On
00432 static MEM_POOL *initialized_pools =
00433
00434
00435 &mem_overhead_pool;
00436 #endif
00437
00438 #define N_BUCKETS 503
00439
00440 static MEM_STAT *call_site_hash_tab[N_BUCKETS];
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 static UINT32
00456 Hash(
00457 INT32 line,
00458 const char *file
00459 )
00460 {
00461 const char *p;
00462 UINT32 result = line;
00463
00464
00465
00466
00467 for ( p = file; *p; ++p )
00468 result = ((result << 1) ^ (result >> 1) ^ *p) + *p;
00469
00470 return result % N_BUCKETS;
00471 }
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 static MEM_STAT*
00488 Hash_Get(
00489 UINT32 hn,
00490 MEM_POOL *pool,
00491 INT32 line,
00492 const char *file
00493 )
00494 {
00495 MEM_STAT *as;
00496
00497 for ( as = call_site_hash_tab[hn];
00498 as != NULL;
00499 as = MEM_STAT_hash_list_rest(as)
00500 ) {
00501 if ( MEM_STAT_line(as) == line
00502 && strcmp(MEM_STAT_file(as),file) == 0
00503 && MEM_STAT_pool(as) == pool
00504 ) {
00505 return as;
00506 }
00507 }
00508
00509 return NULL;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 static void
00528 Site_Account_Alloc(
00529 MEM_POOL *pool,
00530 size_t old_size,
00531 size_t new_size
00532 MEM_STAT_ARGS(line,file)
00533 )
00534 {
00535 INT32 size = new_size - old_size;
00536 UINT32 hn = Hash(line,file);
00537 MEM_STAT *ms = Hash_Get(hn,pool,line,file);
00538
00539 if ( ms == NULL ) {
00540 ms = (MEM_STAT *) calloc(1,sizeof(MEM_STAT));
00541
00542 MEM_STAT_pool(ms) = pool;
00543 MEM_STAT_line(ms) = line;
00544 MEM_STAT_file(ms) = file;
00545 MEM_STAT_hash_list_rest(ms) = call_site_hash_tab[hn];
00546 call_site_hash_tab[hn] = ms;
00547 MEM_STAT_pool_list_rest(ms) = MEM_POOL_alloc_site_list(pool);
00548 MEM_POOL_alloc_site_list(pool) = ms;
00549 }
00550
00551 MEM_STAT_current(ms) += size;
00552 MEM_STAT_total(ms) += size;
00553 if ( size > MEM_STAT_last(ms) )
00554 ++MEM_STAT_last_grew(ms);
00555 else if ( size < MEM_STAT_last(ms) )
00556 ++MEM_STAT_last_shrank(ms);
00557 MEM_STAT_max_t(ms) = Max(MEM_STAT_max_t(ms),MEM_STAT_current(ms));
00558 MEM_STAT_max_s(ms) = Max(MEM_STAT_max_s(ms),size);
00559 MEM_STAT_last(ms) = size;
00560 ++MEM_STAT_count(ms);
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 static void
00577 Site_Account_Pop(
00578 MEM_POOL *pool
00579 MEM_STAT_ARGS(line,file)
00580 )
00581 {
00582 MEM_STAT *ms;
00583
00584 for ( ms = MEM_POOL_alloc_site_list(pool);
00585 ms != NULL;
00586 ms = MEM_STAT_pool_list_rest(ms)
00587 ) {
00588 INT_LIST *tmp = MEM_STAT_saved_current(ms);
00589
00590 if ( tmp == NULL ) {
00591
00592
00593
00594 MEM_STAT_current(ms) = 0;
00595 }
00596 else {
00597 MEM_STAT_current(ms) = INT_LIST_first(tmp);
00598 MEM_STAT_saved_current(ms) = INT_LIST_rest(tmp);
00599 INT_LIST_rest(tmp) = free_int_lists;
00600 free_int_lists = tmp;
00601 }
00602 }
00603 }
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 static void
00617 Site_Account_Push(
00618 MEM_POOL *pool
00619 MEM_STAT_ARGS(line,file)
00620 )
00621 {
00622 MEM_STAT *ms;
00623
00624 for ( ms = MEM_POOL_alloc_site_list(pool);
00625 ms != NULL;
00626 ms = MEM_STAT_pool_list_rest(ms)
00627 ) {
00628 INT_LIST *il;
00629
00630 if ( free_int_lists == NULL )
00631 il = TYPE_MEM_POOL_ALLOC(INT_LIST,&mem_overhead_pool);
00632 else {
00633 il = free_int_lists;
00634
00635 free_int_lists = INT_LIST_rest(il);
00636 }
00637
00638 INT_LIST_rest(il) = MEM_STAT_saved_current(ms);
00639 MEM_STAT_saved_current(ms) = il;
00640 INT_LIST_first(il) = MEM_STAT_current(ms);
00641 }
00642 }
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 static INT32
00654 Field_Size(
00655 INT32 i
00656 )
00657 {
00658 char buff[100];
00659
00660
00661
00662
00663 sprintf(buff,"%d",i);
00664 return strlen(buff);
00665 }
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 typedef INT (*QSORT_FUNC) (const void *, const void *);
00681
00682 static INT
00683 MEM_STAT_Sort(
00684 MEM_STAT **as1p,
00685 MEM_STAT **as2p
00686 )
00687 {
00688 MEM_STAT *as1 = *as1p;
00689 MEM_STAT *as2 = *as2p;
00690
00691 return MEM_STAT_max_t(as2) - MEM_STAT_max_t(as1);
00692 }
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703 static BOOL
00704 MEM_STAT_In_List(
00705 MEM_POOL *list,
00706 MEM_POOL *pool
00707 )
00708 {
00709 for ( ;
00710 list != NULL;
00711 list = MEM_POOL_rest(list)
00712 ) {
00713 if ( list == pool )
00714 return ( TRUE );
00715 }
00716
00717 return ( FALSE );
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732 INT32
00733 MEM_POOL_Report(
00734 MEM_POOL *pool,
00735 INT32 used_total
00736 )
00737 {
00738 MEM_STAT *as;
00739 MEM_STAT **as_vec;
00740 INT32 i;
00741 INT32 total_current = 0;
00742 INT32 total_allocated = 0;
00743 INT32 max_allocated = 0;
00744 INT32 current_fs = 3;
00745 INT32 total_fs = 3;
00746 INT32 max_t_fs = 4;
00747 INT32 max_s_fs = 4;
00748 INT32 count_fs = 5;
00749 INT32 last_grew_fs = 4;
00750 INT32 last_shrank_fs = 6;
00751 INT32 site_count = 0;
00752
00753 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
00754 ("Report from un-initialized MEM_POOL %s\n", MEM_POOL_name(pool)));
00755
00756 fprintf(TFile,"----- %s callsites\n",MEM_POOL_name(pool));
00757
00758
00759
00760 for ( as = MEM_POOL_alloc_site_list(pool);
00761 as != NULL;
00762 as = MEM_STAT_pool_list_rest(as)
00763 ) {
00764 current_fs = Max(current_fs,Field_Size(MEM_STAT_current(as)));
00765 total_fs = Max(total_fs,Field_Size(MEM_STAT_total(as)));
00766 max_t_fs = Max(max_t_fs,Field_Size(MEM_STAT_max_t(as)));
00767 max_s_fs = Max(max_s_fs,Field_Size(MEM_STAT_max_s(as)));
00768 count_fs = Max(count_fs,Field_Size(MEM_STAT_count(as)));
00769 last_grew_fs = Max(last_grew_fs,
00770 Field_Size(MEM_STAT_last_grew(as)));
00771 last_shrank_fs = Max(last_shrank_fs,
00772 Field_Size(MEM_STAT_last_shrank(as)));
00773
00774 ++site_count;
00775 }
00776
00777
00778
00779 MEM_POOL_Push(&mem_overhead_pool);
00780 as_vec = TYPE_MEM_POOL_ALLOC_N(MEM_STAT *,&mem_overhead_pool,
00781 site_count);
00782
00783 for ( as = MEM_POOL_alloc_site_list(pool), i = 0;
00784 as != NULL;
00785 as = MEM_STAT_pool_list_rest(as), ++i
00786 ) {
00787 as_vec[i] = as;
00788 }
00789
00790 qsort((void*)as_vec,site_count,
00791 sizeof(MEM_STAT*),
00792 (QSORT_FUNC) MEM_STAT_Sort);
00793
00794
00795
00796 fprintf(TFile,"%*s %*s %*s %*s %*s %*s %*s Site\n",
00797 max_t_fs,
00798 "maxt",
00799 current_fs,
00800 "cur",
00801 total_fs,
00802 "tot",
00803 max_s_fs,
00804 "maxs",
00805 count_fs,
00806 "count",
00807 last_grew_fs,
00808 "grew",
00809 last_shrank_fs,
00810 "shrank");
00811
00812
00813
00814 for ( i = 0; i < site_count; ++i ) {
00815 as = as_vec[i];
00816
00817 fprintf(TFile,"%*d %*d %*d %*d %*d %*d %*d %s %d\n",
00818 max_t_fs,
00819 MEM_STAT_max_t(as),
00820 current_fs,
00821 MEM_STAT_current(as),
00822 total_fs,
00823 MEM_STAT_total(as),
00824 max_s_fs,
00825 MEM_STAT_max_s(as),
00826 count_fs,
00827 MEM_STAT_count(as),
00828 last_grew_fs,
00829 MEM_STAT_last_grew(as),
00830 last_shrank_fs,
00831 MEM_STAT_last_shrank(as),
00832 MEM_STAT_file(as),
00833 MEM_STAT_line(as));
00834 total_current += MEM_STAT_current(as);
00835 total_allocated += MEM_STAT_total(as);
00836 max_allocated += MEM_STAT_max_t(as);
00837 }
00838
00839 MEM_POOL_Pop(&mem_overhead_pool);
00840
00841 fprintf(TFile,"++++ Allocated for %s pool: total=%d, max=%d, current=%d (%d%%used)\n",
00842 MEM_POOL_name(pool),
00843 total_allocated,
00844 max_allocated,
00845 total_current,
00846 (INT) (100.0 * ( ((double) total_current)
00847 / ((double) used_total))));
00848 return total_allocated;
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860 void
00861 MEM_Trace(void)
00862 {
00863 #ifdef Is_True_On
00864
00865 #if defined(linux) || defined(MEM_STATS)
00866 MEM_POOL *pool;
00867 struct mallinfo mi = mallinfo();
00868 INT32 used_total = mi.usmblks + mi.uordblks;
00869 INT32 total_allocated = 0;
00870
00871 fprintf(TFile,"arena %10d\n",mi.arena);
00872 fprintf(TFile,"ordblks %10d\n",mi.ordblks);
00873 fprintf(TFile,"smblks %10d\n",mi.smblks);
00874 fprintf(TFile,"hblkhd %10d\n",mi.hblkhd);
00875 fprintf(TFile,"hblks %10d\n",mi.hblks);
00876 fprintf(TFile,"usmblks %10d\n",mi.usmblks);
00877 fprintf(TFile,"fsmblks %10d\n",mi.fsmblks);
00878 fprintf(TFile,"uordblks %10d\n",mi.uordblks);
00879 fprintf(TFile,"fordblks %10d\n",mi.fordblks);
00880 fprintf(TFile,"keepcost %10d\n",mi.keepcost);
00881
00882 for ( pool = initialized_pools;
00883 pool != NULL;
00884 pool = MEM_POOL_rest(pool)
00885 ) {
00886 total_allocated += MEM_POOL_Report(pool,used_total);
00887 }
00888 fprintf(TFile,"++++ Total Allocated = %d\n",total_allocated);
00889 #else
00890 fprintf(TFile,
00891 "MEM_Trace: Not available; compiler not compiled with MEM_STATS\n");
00892 #endif
00893 #else
00894 fprintf(TFile,
00895 "MEM_Trace: Not available; compiler not compiled with Is_True_On\n");
00896 #endif
00897 }
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908 void
00909 Trace_Memory_Allocation (
00910 const INT phase,
00911 const char *const pname )
00912 {
00913 if ( Get_Trace ( TKIND_ALLOC, phase ) ) {
00914 fprintf ( TFile,
00915 "\n%s%s\tMemory allocation information after %s\n%s%s\n",
00916 DBar, DBar, pname, DBar, DBar );
00917 MEM_Trace ();
00918 }
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930 void
00931 MEM_Tracing_Enable(void)
00932 {
00933 #ifdef Is_True_On
00934 mem_tracing_enabled = TRUE;
00935 #endif
00936 }
00937
00938
00939
00940 #if Is_True_On
00941 char *special_address = NULL;
00942 char *special_address_owner = "NOBODY";
00943 #endif
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956 static MEM_PTR
00957 Allocate_Block (MEM_POOL *pool)
00958 {
00959 MEM_BLOCK *block = (MEM_BLOCK *)
00960 malloc (BLOCK_SIZE + PAD_TO_ALIGN(sizeof(MEM_BLOCK)));
00961
00962 if (block == NULL)
00963 ErrMsg (EC_No_Mem, "Allocate_Block");
00964
00965 if ( MEM_POOL_bz(pool) )
00966 bzero (block, BLOCK_SIZE + PAD_TO_ALIGN(sizeof(MEM_BLOCK)));
00967
00968 #ifdef ZAP_ON_FREE
00969 else
00970 memset(((char *) block), 0xa5,
00971 BLOCK_SIZE + PAD_TO_ALIGN(sizeof(MEM_BLOCK)));
00972 #endif
00973
00974 MEM_BLOCK_avail(block) = BLOCK_SIZE;
00975 MEM_BLOCK_ptr(block) = MEM_BLOCK_first_ptr(block);
00976 MEM_BLOCK_rest(block) = MEM_POOL_block(pool);
00977 MEM_POOL_block(pool) = block;
00978
00979 #if Is_True_On
00980 if (special_address >= ((char *) MEM_BLOCK_ptr(block)) &&
00981 special_address < (((char *) MEM_BLOCK_ptr(block)) +
00982 MEM_BLOCK_avail(block))) {
00983 fprintf(TFile, "Pool %s given %llu bytes from 0x%p to 0x%p\n",
00984 MEM_POOL_name(pool), (UINT64)MEM_BLOCK_avail(block),
00985 (char *) MEM_BLOCK_ptr(block),
00986 ((char *) MEM_BLOCK_ptr(block)) + MEM_BLOCK_avail(block));
00987 special_address_owner = MEM_POOL_name(pool);
00988 }
00989 #endif
00990
00991 return block;
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006 static MEM_PTR
01007 Allocate_Large_Block (MEM_POOL *pool, INT32 size)
01008 {
01009 MEM_LARGE_BLOCK *block;
01010 size += MEM_LARGE_BLOCK_OVERHEAD;
01011 block = (MEM_LARGE_BLOCK *) malloc (size);
01012
01013 if (block == NULL)
01014 ErrMsg (EC_No_Mem, "Allocate_Large_Block");
01015
01016 if ( MEM_POOL_bz(pool) ) {
01017 bzero (block, size);
01018 }
01019
01020 #ifdef ZAP_ON_FREE
01021 else
01022 memset(((char *) block), 0xa5, size);
01023 #endif
01024
01025 MEM_LARGE_BLOCK_ptr(block) = (MEM_PTR)
01026 (((char *)block) + MEM_LARGE_BLOCK_OVERHEAD);
01027 MEM_LARGE_BLOCK_base(block) = MEM_POOL_blocks(pool);
01028 MEM_LARGE_BLOCK_next(block) = MEM_POOL_large_block(pool);
01029 MEM_LARGE_BLOCK_prev(block) = NULL;
01030 if (MEM_LARGE_BLOCK_next(block) != NULL)
01031 MEM_LARGE_BLOCK_prev(MEM_LARGE_BLOCK_next(block)) = block;
01032 MEM_POOL_large_block(pool) = block;
01033
01034 return MEM_LARGE_BLOCK_ptr(block);
01035 }
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051 static MEM_PTR
01052 Raw_Allocate(
01053 MEM_POOL *pool,
01054 INT32 size
01055 )
01056 {
01057 MEM_PTR *result;
01058
01059 if (size <= MIN_LARGE_BLOCK_SIZE) {
01060 MEM_BLOCK *b = MEM_POOL_block(pool);
01061
01062 if (b == NULL || MEM_BLOCK_avail(b) < size) {
01063 b = (MEM_BLOCK *)Allocate_Block (pool);
01064 }
01065
01066 result = (MEM_PTR*)MEM_BLOCK_ptr(b);
01067 MEM_BLOCK_ptr(b) = (MEM_PTR) (((char*) MEM_BLOCK_ptr(b)) + size);
01068 MEM_BLOCK_avail(b) -= size;
01069
01070 return result;
01071 } else
01072 return Allocate_Large_Block (pool, size);
01073 }
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087 MEM_PTR
01088 MEM_POOL_Alloc_P
01089 (
01090 MEM_POOL *pool,
01091 size_t size
01092 MEM_STAT_ARGS(line,file)
01093 )
01094 {
01095
01096 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01097 if (pool == Malloc_Mem_Pool) {
01098 MEM_PTR p = malloc(size);
01099 if (p == NULL)
01100 ErrMsg (EC_No_Mem, "MEM_POOL_Alloc");
01101 return p;
01102 }
01103
01104 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01105 ("Alloc from un-initialized MEM_POOL %s\n", MEM_POOL_name(pool)));
01106
01107 #ifdef JUST_USE_MALLOC
01108 return calloc(1,size);
01109 #endif
01110
01111 if (purify_pools) {
01112 MEM_PTR ret_val;
01113
01114
01115
01116 if (!MEM_POOL_blocks(pool)) {
01117 DevWarn("Allocation from %s before MEM_POOL_Push(%s)",
01118 MEM_POOL_name(pool), MEM_POOL_name(pool));
01119 MEM_POOL_blocks(pool) = (MEM_POOL_BLOCKS *) TRUE;
01120 }
01121
01122 ret_val = calloc(1,size+8);
01123 Is_True (ret_val, ("MEM_POOL_Alloc: calloc returned NULL"));
01124 Is_True (MEM_POOL_pure_stack(pool), ("MEM_POOL_Alloc %s: missing stack",
01125 MEM_POOL_name(pool)));
01126 *(MEM_PTR*)ret_val = MEM_POOL_last_alloc(pool);
01127 MEM_POOL_last_alloc(pool) = ret_val;
01128 if (purify_pools_trace)
01129 printf ("pool %s, alloc 0x%p, size %llu, (0x%p - 0x%p)\n",
01130 MEM_POOL_name(pool), (char *)ret_val+8, (UINT64)size,
01131 (char *)ret_val+8, (char *)ret_val+size);
01132 return ((MEM_PTR) ((size_t)ret_val+8));
01133 }
01134
01135 Is_True(MEM_POOL_blocks(pool) != NULL,
01136 ("Alloc with uninitialized MEM_POOL"));
01137 #ifdef Is_True_On
01138 if ( mem_tracing_enabled )
01139 Site_Account_Alloc(pool,0,size,line,file);
01140 #endif
01141
01142
01143
01144 size = PAD_TO_ALIGN(size);
01145
01146 return Raw_Allocate(pool,size);
01147 }
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159 MEM_PTR
01160 MEM_POOL_Realloc_P
01161 (
01162 MEM_POOL *pool,
01163 MEM_PTR old_block,
01164 size_t old_size,
01165 size_t new_size
01166 MEM_STAT_ARGS(line,file)
01167 )
01168 {
01169 MEM_PTR result;
01170
01171 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01172 if (pool == Malloc_Mem_Pool) {
01173 MEM_PTR p = realloc(old_block,new_size);
01174 if (p == NULL)
01175 ErrMsg (EC_No_Mem, "MEM_POOL_Realloc");
01176 return p;
01177 }
01178
01179 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01180 ("Realloc from un-initialized MEM_POOL %s\n", MEM_POOL_name(pool)));
01181
01182 #ifdef JUST_USE_MALLOC
01183 result = realloc(old_block,new_size);
01184 #ifdef KEY
01185 if (purify_pools_trace)
01186 if (result != old_block)
01187 printf ("pool %s, freed block 0x%p\n", MEM_POOL_name(pool), old_block);
01188 #endif
01189 if ( old_size < new_size )
01190 bzero((char*)result + old_size,new_size - old_size);
01191 return result;
01192 #endif
01193
01194 if (purify_pools) {
01195 MEM_PTR ret_val = NULL;
01196 BOOL foundit = FALSE;
01197
01198 if (!MEM_POOL_blocks(pool)) {
01199 DevWarn("Realloc from %s before MEM_POOL_Push(%s)",
01200 MEM_POOL_name(pool), MEM_POOL_name(pool));
01201 MEM_POOL_blocks(pool) = (MEM_POOL_BLOCKS *) TRUE;
01202 }
01203
01204
01205 if (old_block) {
01206
01207 MEM_PURE_STACK* tmp_stack = MEM_POOL_pure_stack(pool);
01208 MEM_PTR cur = (MEM_PTR) ((size_t) old_block - 8);
01209 Is_True (tmp_stack, ("MEM_POOL_Realloc %s: missing stack",
01210 MEM_POOL_name(pool)));
01211 while (tmp_stack) {
01212 MEM_PTR tmp = MEM_PURE_STACK_last_alloc(tmp_stack);
01213 MEM_PTR prev = NULL;
01214
01215 while (tmp && (tmp != cur)) {
01216 prev = tmp;
01217 tmp = *((MEM_PTR*) tmp);
01218 }
01219 if (tmp) {
01220 foundit = TRUE;
01221
01222 if (prev) *(MEM_PTR*) prev = *(MEM_PTR*) tmp;
01223 else MEM_PURE_STACK_last_alloc(tmp_stack) = *(MEM_PTR*) tmp;
01224 break;
01225 }
01226 tmp_stack = MEM_PURE_STACK_prev_stack(tmp_stack);
01227 }
01228 }
01229 if (old_block && !foundit) {
01230 DevWarn ("Realloc without a previous alloc, pool %s, 0x%p",
01231 MEM_POOL_name(pool), old_block);
01232
01233 ret_val = (MEM_PTR) malloc (new_size+8);
01234 bcopy(old_block, (MEM_PTR) (((size_t) ret_val)+8), old_size);
01235 }
01236 else {
01237
01238 ret_val = (MEM_PTR)
01239 realloc((MEM_PTR) (old_block ? (size_t) old_block-8 : 0),
01240 new_size+8);
01241 #ifdef KEY
01242 if (purify_pools_trace)
01243 if (ret_val != old_block && old_block != 0)
01244 printf ("pool %s, freed block 0x%p\n", MEM_POOL_name(pool), old_block);
01245 #endif
01246 }
01247 if (new_size > 0) {
01248 FmtAssert (ret_val, ("oops - realloc returned NULL, pool %s\n",
01249 MEM_POOL_name(pool)));
01250 *(MEM_PTR*) ret_val = MEM_POOL_last_alloc(pool);
01251 MEM_POOL_last_alloc(pool) = ret_val;
01252 ret_val = (MEM_PTR) ((size_t) ret_val + 8);
01253 if ( old_size < new_size )
01254 bzero((char*)ret_val + old_size,new_size - old_size);
01255 }
01256 if (purify_pools_trace)
01257 printf ("pool %s, realloc 0x%p, new size %llu, (0x%p - 0x%p)\n",
01258 MEM_POOL_name(pool), ret_val, (UINT64)new_size,
01259 ret_val, (char *)ret_val + new_size - 8);
01260 return ret_val;
01261 }
01262
01263 Is_True(MEM_POOL_blocks(pool) != NULL,
01264 ("Alloc with uninitialized MEM_POOL"));
01265 #ifdef Is_True_On
01266 if ( mem_tracing_enabled )
01267 Site_Account_Alloc(pool,old_size,new_size,line,file);
01268 #endif
01269
01270
01271
01272 old_size = PAD_TO_ALIGN(old_size);
01273 new_size = PAD_TO_ALIGN(new_size);
01274
01275
01276
01277 if ( new_size == old_size )
01278 return old_block;
01279
01280 #if 0
01281 #ifdef Is_True_On
01282 if (new_size < old_size)
01283 DevWarn ("MEMORY: shrinking an object in (%s) from %d to %d bytes",
01284 MEM_POOL_name(pool), old_size, new_size);
01285 else if (new_size < old_size * 1.5 && old_size > 256)
01286 DevWarn ("MEMORY: small grow from %d to %d bytes (mempool: %s)",
01287 old_size, new_size, MEM_POOL_name(pool));
01288 #endif
01289 #endif
01290
01291 if (old_size <= MIN_LARGE_BLOCK_SIZE) {
01292 if (new_size < old_size)
01293 return old_block;
01294 else {
01295 result = Raw_Allocate (pool, new_size);
01296 bcopy (old_block, result, old_size);
01297 return result;
01298 }
01299 } else {
01300 MEM_LARGE_BLOCK *large_block = (MEM_LARGE_BLOCK *)
01301 (((char *) old_block) - MEM_LARGE_BLOCK_OVERHEAD);
01302 if (MEM_LARGE_BLOCK_ptr(large_block) == (MEM_PTR) old_block &&
01303 MEM_LARGE_BLOCK_base(large_block) == MEM_POOL_blocks(pool)) {
01304
01305 if (new_size <= MIN_LARGE_BLOCK_SIZE) {
01306 result = Raw_Allocate (pool, new_size);
01307 bcopy (old_block, result, new_size);
01308 MEM_POOL_FREE (pool, old_block);
01309 return result;
01310 } else {
01311 MEM_LARGE_BLOCK *p =
01312 (MEM_LARGE_BLOCK *)(((char *)old_block) - MEM_LARGE_BLOCK_OVERHEAD);
01313
01314 large_block =
01315 MEM_LARGE_BLOCK_realloc(p, new_size + MEM_LARGE_BLOCK_OVERHEAD);
01316 #ifdef KEY
01317 if (purify_pools_trace)
01318 if (p != large_block && p != 0)
01319 printf ("pool %s, freed block 0x%p\n", MEM_POOL_name(pool), p);
01320 #endif
01321
01322 if (large_block == NULL)
01323 ErrMsg (EC_No_Mem, "MEM_POOL_Realloc");
01324 MEM_LARGE_BLOCK_ptr(large_block) = (MEM_PTR)
01325 (((char *)large_block) + MEM_LARGE_BLOCK_OVERHEAD);
01326 if (MEM_POOL_bz(pool)) {
01327 bzero (((char *) MEM_LARGE_BLOCK_ptr(large_block)) + old_size,
01328 new_size - old_size);
01329 }
01330 p = MEM_LARGE_BLOCK_prev(large_block);
01331 if (p == NULL)
01332 MEM_POOL_large_block(pool) = large_block;
01333 else
01334 MEM_LARGE_BLOCK_next(p) = large_block;
01335 p = MEM_LARGE_BLOCK_next(large_block);
01336 if (p)
01337 MEM_LARGE_BLOCK_prev(p) = large_block;
01338 return MEM_LARGE_BLOCK_ptr(large_block);
01339 }
01340 } else {
01341 result = Raw_Allocate (pool, new_size);
01342 if (new_size > old_size)
01343 bcopy (old_block, result, old_size);
01344 else
01345 bcopy (old_block, result, new_size);
01346 return result;
01347 }
01348 }
01349 }
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364 void
01365 MEM_POOL_Push_P
01366 (
01367 MEM_POOL *pool
01368 MEM_STAT_ARGS(line,file)
01369 )
01370 {
01371 MEM_POOL_BLOCKS *pb;
01372
01373 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01374 ("Push before Initialize in MEM_POOL %s\n", MEM_POOL_name(pool)));
01375
01376 FmtAssert(MEM_POOL_frozen(pool) == FALSE, ("Pushing a frozen pool - %s.",
01377 MEM_POOL_name(pool)));
01378 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01379 if (pool == Malloc_Mem_Pool) return;
01380 #ifdef JUST_USE_MALLOC
01381 return;
01382 #endif
01383
01384 if (purify_pools) {
01385 MEM_PURE_STACK* tmp = (MEM_PURE_STACK*) malloc(sizeof(MEM_PURE_STACK));
01386 Is_True (tmp, ("MEM_POOL_Push %s: malloc stack returned NULL",
01387 MEM_POOL_name(pool)));
01388 MEM_PURE_STACK_last_alloc(tmp) = NULL;
01389 MEM_PURE_STACK_prev_stack(tmp) = MEM_POOL_pure_stack(pool);
01390 MEM_POOL_pure_stack(pool) = tmp;
01391 if (purify_pools_trace_x) {
01392 if (MEM_POOL_blocks(pool) == (MEM_POOL_BLOCKS *) MEM_POOL_INIT_IN_PROGRESS) {
01393 (void) printf("MEM_POOL_Push %s 0x%p<-- free push (called by M_P_Initialize)\n",
01394 MEM_POOL_name(pool), pool);
01395 }
01396 else {
01397 (void) printf ("MEM_POOL_Push %s 0x%p\n", MEM_POOL_name(pool), pool);
01398 }
01399 }
01400
01401
01402
01403
01404 MEM_POOL_blocks(pool) = (MEM_POOL_BLOCKS *) TRUE;
01405 return;
01406 }
01407
01408 #ifdef Is_True_On
01409 if ( mem_tracing_enabled )
01410 Site_Account_Push(pool,line,file);
01411 #endif
01412
01413 if ( free_mem_pool_blocks_list != NULL ) {
01414
01415
01416 pb = free_mem_pool_blocks_list;
01417 free_mem_pool_blocks_list = MEM_POOL_BLOCKS_rest(pb);
01418 } else {
01419
01420
01421 pb = TYPE_MEM_POOL_ALLOC(MEM_POOL_BLOCKS,&mem_overhead_pool);
01422 }
01423
01424 MEM_POOL_BLOCKS_rest(pb) = MEM_POOL_blocks(pool);
01425 MEM_POOL_BLOCKS_large_block(pb) = NULL;
01426 if (MEM_POOL_BLOCKS_rest(pb) == NULL) {
01427 MEM_POOL_BLOCKS_block(pb) = NULL;
01428 MEM_POOL_BLOCKS_base_block(pb) = NULL;
01429 MEM_POOL_BLOCKS_base_ptr(pb) = NULL;
01430 MEM_POOL_BLOCKS_base_avail(pb) = 0;
01431 } else {
01432 MEM_POOL_BLOCKS *p = MEM_POOL_BLOCKS_rest(pb);
01433 MEM_POOL_BLOCKS_block(pb) = MEM_POOL_BLOCKS_block(p);
01434 MEM_POOL_BLOCKS_base_block(pb) = MEM_POOL_BLOCKS_block(p);
01435 if (MEM_POOL_BLOCKS_block(p)) {
01436 MEM_POOL_BLOCKS_base_ptr(pb) = (MEM_PTR*)MEM_BLOCK_ptr(MEM_POOL_BLOCKS_block(p));
01437 MEM_POOL_BLOCKS_base_avail(pb) =
01438 MEM_BLOCK_avail(MEM_POOL_BLOCKS_block(p));
01439 } else {
01440 MEM_POOL_BLOCKS_base_ptr(pb) = NULL;
01441 MEM_POOL_BLOCKS_base_avail(pb) = 0;
01442 }
01443 }
01444 MEM_POOL_blocks(pool) = pb;
01445 }
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460 void
01461 MEM_POOL_Push_Freeze_P
01462 (
01463 MEM_POOL *pool
01464 MEM_STAT_ARGS(line,file)
01465 )
01466 {
01467 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01468 ("Push_Freeze before Initialize in MEM_POOL %s\n", MEM_POOL_name(pool)));
01469 FmtAssert (MEM_POOL_frozen(pool) == FALSE,
01470 ("Cannot Push_Freeze a frozen pool - %s.", MEM_POOL_name(pool)));
01471 if (purify_pools_trace_x)
01472 printf ("MEM_POOL_Push_Freeze %s -- \n", MEM_POOL_name(pool));
01473 MEM_POOL_Push_P (pool, line, file);
01474 MEM_POOL_frozen(pool) = TRUE;
01475 }
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490 void
01491 MEM_POOL_Pop_P
01492 (
01493 MEM_POOL *pool
01494 MEM_STAT_ARGS(line,file)
01495 )
01496 {
01497 MEM_BLOCK *bp, *next_bp;
01498 MEM_LARGE_BLOCK *lbp, *next_lbp;
01499 MEM_POOL_BLOCKS *bsp;
01500
01501 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01502 ("Pop before Initialize in MEM_POOL %s\n", MEM_POOL_name(pool)));
01503
01504 FmtAssert(MEM_POOL_frozen(pool) == FALSE, ("Popping a frozen pool - %s.",
01505 MEM_POOL_name(pool)));
01506 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01507 if (pool == Malloc_Mem_Pool) return;
01508
01509 #ifdef JUST_USE_MALLOC
01510 return;
01511 #endif
01512 if (purify_pools) {
01513 MEM_PURE_STACK *tmp_stack;
01514 MEM_PTR tmp = NULL;
01515 MEM_PTR next = NULL;
01516 Is_True (MEM_POOL_pure_stack(pool),
01517 ("Pop, but no push stack on %s", MEM_POOL_name(pool)));
01518 if (purify_pools_trace_x)
01519 printf ("MEM_POOL_Pop %s 0x%p\n", MEM_POOL_name(pool), pool);
01520 tmp = MEM_POOL_last_alloc(pool);
01521 while (tmp) {
01522 next = (*(MEM_PTR*) tmp);
01523 if (purify_pools_trace) printf ("pool %s, pop-free 0x%p\n",
01524 MEM_POOL_name(pool), (char *)tmp+8);
01525 free (tmp);
01526 tmp = next;
01527 }
01528 tmp_stack = MEM_POOL_prev_stack(pool);
01529 free (MEM_POOL_pure_stack(pool));
01530 MEM_POOL_pure_stack(pool) = tmp_stack;
01531 return;
01532 }
01533
01534 bsp = MEM_POOL_blocks(pool);
01535
01536 FmtAssert(MEM_POOL_blocks(pool),("Freeing an uninitialized pool."));
01537
01538 #ifdef Is_True_On
01539 if ( mem_tracing_enabled )
01540 Site_Account_Pop(pool,line,file);
01541 #endif
01542
01543 for (bp = MEM_POOL_BLOCKS_block(bsp); bp; bp = next_bp) {
01544 next_bp = MEM_BLOCK_rest(bp);
01545
01546 #if Is_True_On
01547 if (special_address >= (char *) MEM_BLOCK_first_ptr(bp) &&
01548 special_address < ((char *) MEM_BLOCK_ptr(bp) +
01549 MEM_BLOCK_avail(bp))) {
01550 fprintf(TFile, "Pool %s freed %llu bytes from 0x%p to 0x%p\n",
01551 MEM_POOL_name(pool),
01552 (UINT64)(MEM_BLOCK_avail(bp) +
01553 (char *) MEM_BLOCK_ptr(bp) -
01554 (char *) MEM_BLOCK_first_ptr(bp)),
01555 (char *) MEM_BLOCK_first_ptr(bp),
01556 (char *) MEM_BLOCK_ptr(bp) + MEM_BLOCK_avail(bp));
01557 special_address_owner = "NOBODY";
01558 }
01559 #endif
01560
01561 if (bp == MEM_POOL_BLOCKS_base_block(bsp)) {
01562 MEM_BLOCK_ptr(bp) = MEM_POOL_BLOCKS_base_ptr(bsp);
01563 MEM_BLOCK_avail(bp) = MEM_POOL_BLOCKS_base_avail(bsp);
01564 if (MEM_POOL_bz(pool))
01565 bzero (MEM_BLOCK_ptr(bp), MEM_BLOCK_avail(bp));
01566 break;
01567 }
01568 free (bp);
01569 }
01570
01571 for (lbp = MEM_POOL_BLOCKS_large_block(bsp); lbp; lbp = next_lbp) {
01572 next_lbp = MEM_LARGE_BLOCK_next(lbp);
01573 MEM_LARGE_BLOCK_free(lbp);
01574 }
01575
01576
01577
01578
01579
01580
01581
01582
01583 if ( MEM_POOL_BLOCKS_rest(bsp) != NULL ) {
01584 MEM_POOL_blocks(pool) = MEM_POOL_BLOCKS_rest(bsp);
01585 MEM_POOL_BLOCKS_rest(bsp) = free_mem_pool_blocks_list;
01586 free_mem_pool_blocks_list = bsp;
01587 } else {
01588 bzero (bsp, sizeof(MEM_POOL_BLOCKS));
01589 }
01590 }
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605 void
01606 MEM_POOL_Pop_Unfreeze_P
01607 (
01608 MEM_POOL *pool
01609 MEM_STAT_ARGS(line,file)
01610 )
01611 {
01612 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01613 ("Pop_Unfreeze before Initialize in MEM_POOL %s\n", MEM_POOL_name(pool)));
01614 FmtAssert (MEM_POOL_frozen(pool) == TRUE,
01615 ("Cannot Pop_Unfreeze a non-frozen pool - %s.",
01616 MEM_POOL_name(pool)));
01617 MEM_POOL_frozen(pool) = FALSE;
01618 if (purify_pools_trace_x)
01619 printf ("MEM_POOL_Pop_Unfreeze %s -- \n", MEM_POOL_name(pool));
01620 MEM_POOL_Pop_P (pool, line, file);
01621 }
01622
01623 void MEM_POOL_Set_Default(MEM_POOL *pool)
01624 {
01625 The_Default_Mem_Pool = pool;
01626 }
01627
01628 void MEM_POOL_FREE(MEM_POOL *pool, void *data)
01629 {
01630 MEM_LARGE_BLOCK *large_block;
01631
01632 if (data == NULL)
01633 return;
01634
01635 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01636 if (pool == Malloc_Mem_Pool) {
01637 free(data);
01638 return;
01639 }
01640
01641 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01642 ("Free into un-initialized MEM_POOL %s\n", MEM_POOL_name(pool)));
01643
01644 #ifdef JUST_USE_MALLOC
01645 return;
01646 #endif
01647 if (data && purify_pools) {
01648 BOOL foundit = FALSE;
01649
01650 MEM_PURE_STACK* tmp_stack = MEM_POOL_pure_stack(pool);
01651 MEM_PTR cur = (MEM_PTR) ((size_t) data - 8);
01652 Is_True (tmp_stack, ("MEM_POOL_Realloc %s: missing stack",
01653 MEM_POOL_name(pool)));
01654 while (tmp_stack) {
01655 MEM_PTR tmp = MEM_PURE_STACK_last_alloc(tmp_stack);
01656 MEM_PTR prev = NULL;
01657
01658 while (tmp && (tmp != cur)) {
01659 prev = tmp;
01660 tmp = *((MEM_PTR*) tmp);
01661 }
01662 if (tmp) {
01663 foundit = TRUE;
01664
01665 if (prev) *(MEM_PTR*) prev = *(MEM_PTR*) tmp;
01666 else MEM_PURE_STACK_last_alloc(tmp_stack) = *(MEM_PTR*) tmp;
01667 break;
01668 }
01669 tmp_stack = MEM_PURE_STACK_prev_stack(tmp_stack);
01670 }
01671 if (purify_pools_trace)
01672 printf ("pool %s, free 0x%p\n", MEM_POOL_name(pool), data);
01673 if (!foundit) {
01674
01675 free (cur);
01676 FmtAssert(FALSE,("MEM_POOL_FREE: pool %s, could not find pointer 0x%p\n",
01677 MEM_POOL_name(pool), data));
01678 }
01679 free (cur);
01680 return;
01681 }
01682
01683 large_block = (MEM_LARGE_BLOCK *)
01684 (((char *) data) - MEM_LARGE_BLOCK_OVERHEAD);
01685 if (MEM_LARGE_BLOCK_ptr(large_block) == (MEM_PTR) data) {
01686 MEM_LARGE_BLOCK *prev;
01687 MEM_LARGE_BLOCK *next;
01688 if (MEM_LARGE_BLOCK_base(large_block) != MEM_POOL_blocks(pool))
01689 return;
01690
01691 prev = MEM_LARGE_BLOCK_prev(large_block);
01692 next = MEM_LARGE_BLOCK_next(large_block);
01693 if (prev == NULL)
01694 MEM_POOL_large_block(pool) = next;
01695 else
01696 MEM_LARGE_BLOCK_next(prev) = next;
01697
01698 if (next)
01699 MEM_LARGE_BLOCK_prev(next) = prev;
01700
01701 MEM_LARGE_BLOCK_free(large_block);
01702 }
01703
01704 }
01705
01706 #ifdef Is_True_On
01707 static void
01708 trace_initialized_pool (char *msg, char *pname)
01709 {
01710 MEM_POOL **listp = &initialized_pools;
01711 printf("<%s %s> initialized_pools: ", msg, pname);
01712 while (*listp != NULL) {
01713 printf(", %s", MEM_POOL_name(*listp));
01714 listp = &MEM_POOL_rest(*listp);
01715 }
01716 printf("\n");
01717 }
01718 #endif
01719
01720 void MEM_POOL_Delete(MEM_POOL *pool)
01721 {
01722 MEM_POOL_BLOCKS *bsp;
01723
01724 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01725 if (pool == Malloc_Mem_Pool) return;
01726
01727 #ifdef Is_True_On
01728
01729
01730
01731
01732
01733
01734
01735 if ( mem_tracing_enabled ) {
01736 MEM_POOL **listp = &initialized_pools;
01737
01738 if (*listp == pool) {
01739 initialized_pools = MEM_POOL_rest(pool);
01740 }
01741 else {
01742 while (*listp && MEM_POOL_rest(*listp)) {
01743 if (MEM_POOL_rest(*listp) == pool) break;
01744 listp = &MEM_POOL_rest(*listp);
01745 }
01746 if (*listp && MEM_POOL_rest(*listp)) {
01747
01748 MEM_POOL_rest(*listp) = MEM_POOL_rest(pool);
01749 }
01750 else {
01751 DevWarn("didn't find pool %s in initialized_pools", MEM_POOL_name(pool));
01752 }
01753 }
01754 Is_True(!MEM_STAT_In_List( initialized_pools, pool ),
01755 ("Pool still in initialized list"));
01756 }
01757 #endif
01758
01759 Is_True (MEM_POOL_magic_num(pool) == MAGIC_NUM,
01760 ("Deleting a pool that has not been initialized: %s\n",
01761 MEM_POOL_name(pool)));
01762
01763 if (purify_pools) {
01764
01765 #ifndef TODO_REMOVE_FREE_PUSH
01766 if (!MEM_POOL_pure_stack(pool)) {
01767 DevWarn("During MEM_POOL_Delete: Too many pops on %s.",
01768 MEM_POOL_name(pool));
01769 }
01770 else {
01771 MEM_POOL_Pop(pool);
01772 #endif
01773 if (MEM_POOL_pure_stack(pool)) {
01774 DevWarn("During MEM_POOL_Delete: Too few pops on %s.",
01775 MEM_POOL_name(pool));
01776 while (MEM_POOL_pure_stack(pool))
01777 MEM_POOL_Pop(pool);
01778 }
01779 #ifndef TODO_REMOVE_FREE_PUSH
01780 }
01781 #endif
01782 if (purify_pools_trace_x)
01783 printf ("MEM_POOL_Delete %s 0x%p\n", MEM_POOL_name(pool), pool);
01784 MEM_POOL_magic_num(pool) = 0;
01785 return;
01786 }
01787
01788
01789
01790
01791
01792 while (MEM_POOL_BLOCKS_rest(MEM_POOL_blocks(pool)) != NULL)
01793 MEM_POOL_Pop(pool);
01794 MEM_POOL_Pop(pool);
01795 bsp = MEM_POOL_blocks(pool);
01796 MEM_POOL_BLOCKS_rest(bsp) = free_mem_pool_blocks_list;
01797 free_mem_pool_blocks_list = bsp;
01798
01799 bzero (pool, sizeof(MEM_POOL));
01800 MEM_POOL_magic_num(pool) = 0;
01801 }
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819 void
01820 MEM_POOL_Initialize_P
01821 (
01822 MEM_POOL *pool,
01823 char *name,
01824 BOOL bzero
01825 MEM_STAT_ARGS(line,file)
01826 )
01827 {
01828 if (pool == Default_Mem_Pool) pool = The_Default_Mem_Pool;
01829 if (pool == Malloc_Mem_Pool) return;
01830 MEM_POOL_name(pool) = name;
01831 MEM_POOL_bz(pool) = bzero;
01832 MEM_POOL_blocks(pool) = NULL;
01833 MEM_POOL_frozen(pool) = FALSE;
01834 MEM_POOL_pure_stack(pool) = NULL;
01835
01836
01837 Is_True (MEM_POOL_magic_num(pool) != MAGIC_NUM,
01838 ("Initialization of an already initialized pool: %s\n",
01839 MEM_POOL_name(pool)));
01840 MEM_POOL_magic_num(pool) = MAGIC_NUM;
01841
01842 if (purify_pools_trace_x)
01843 printf ("MEM_POOL_Initialize %s 0x%p\n", MEM_POOL_name(pool), pool);
01844
01845 #ifdef Is_True_On
01846 MEM_POOL_alloc_site_list(pool) = NULL;
01847 if ( mem_tracing_enabled ) {
01848 if ( ! MEM_STAT_In_List( initialized_pools, pool ) ) {
01849
01850 MEM_POOL_rest(pool) = initialized_pools;
01851 initialized_pools = pool;
01852 }
01853 }
01854 #endif
01855
01856 if (purify_pools) {
01857 MEM_POOL_blocks(pool) = (MEM_POOL_BLOCKS *) MEM_POOL_INIT_IN_PROGRESS;
01858 }
01859
01860 #ifndef TODO_REMOVE_FREE_PUSH
01861 MEM_POOL_Push(pool);
01862 #endif
01863
01864 if (purify_pools) {
01865
01866
01867
01868 MEM_POOL_blocks(pool) = NULL;
01869 }
01870 }
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882 void
01883 MEM_Initialize(void)
01884 {
01885 char* ppools = getenv ("PURIFY_MEMPOOLS");
01886 if (ppools) {
01887 if (((ppools[0] == 'O') || (ppools[0] == 'o')) &&
01888 ((ppools[1] == 'N') || (ppools[1] == 'n'))) {
01889 purify_pools = TRUE;
01890 if ((ppools[2] == '-') &&
01891 ((ppools[3] == 'T') || (ppools[3] == 't')) &&
01892 ((ppools[4] == 'R') || (ppools[4] == 'r')) &&
01893 ((ppools[5] == 'A') || (ppools[5] == 'a')) &&
01894 ((ppools[6] == 'C') || (ppools[6] == 'c')) &&
01895 ((ppools[7] == 'E') || (ppools[7] == 'e'))) {
01896 purify_pools_trace = TRUE;
01897 if ((ppools[8] == '-') &&
01898 ((ppools[9] == 'X') || (ppools[9] == 'x'))) {
01899 purify_pools_trace_x = TRUE;
01900 if ((ppools[10] == '-') &&
01901 ((ppools[11] == 'O') || (ppools[11] == 'o')) &&
01902 ((ppools[12] == 'N') || (ppools[12] == 'n')) &&
01903 ((ppools[13] == 'L') || (ppools[13] == 'l')) &&
01904 ((ppools[14] == 'Y') || (ppools[14] == 'y'))) {
01905 purify_pools_trace = FALSE;
01906 DevWarn("Using purify memory pools, limited extended tracing ###");
01907 } else
01908 DevWarn ("Using purify memory pools, with extended tracing ###");
01909 }
01910 else
01911 DevWarn ("Using purify memory pools, with tracing ###");
01912 }
01913 else DevWarn ("Using purify memory pools ###");
01914 }
01915 else if (((ppools[0] == 'O') || (ppools[0] == 'o')) &&
01916 ((ppools[1] == 'F') || (ppools[1] == 'f')) &&
01917 ((ppools[2] == 'F') || (ppools[2] == 'f'))) {
01918 purify_pools = FALSE;
01919 }
01920 else DevWarn ("PURIFY_MEMPOOLS set to garbage, using regular pools");
01921 }
01922
01923 MEM_POOL_Initialize(&MEM_local_pool,"Local",TRUE);
01924 MEM_POOL_Initialize(&MEM_src_pool,"Source",TRUE);
01925 MEM_POOL_Initialize(&MEM_pu_pool,"Program unit",TRUE);
01926 MEM_POOL_Initialize(&MEM_phase_pool,"Phase",TRUE);
01927
01928 MEM_POOL_Push(&MEM_local_pool);
01929 MEM_POOL_Push(&MEM_src_pool);
01930 MEM_POOL_Push(&MEM_pu_pool);
01931 MEM_POOL_Push(&MEM_phase_pool);
01932
01933 MEM_POOL_Initialize(&MEM_local_nz_pool,"Local (nz)",FALSE);
01934 MEM_POOL_Initialize(&MEM_src_nz_pool,"Source (nz)",FALSE);
01935 MEM_POOL_Initialize(&MEM_pu_nz_pool,"Program unit (nz)",FALSE);
01936 MEM_POOL_Initialize(&MEM_phase_nz_pool,"Phase (nz)",FALSE);
01937
01938 MEM_POOL_Push(&MEM_local_nz_pool);
01939 MEM_POOL_Push(&MEM_src_nz_pool);
01940 MEM_POOL_Push(&MEM_pu_nz_pool);
01941 MEM_POOL_Push(&MEM_phase_nz_pool);
01942
01943 }
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976 MEM_PTR
01977 Realloc_Clear ( MEM_PTR ptr, INT32 new_size, INT32 old_size )
01978 {
01979 MEM_PTR result = (MEM_PTR) realloc ( ptr, new_size );
01980 #ifdef KEY
01981 if (purify_pools_trace)
01982 if (result != ptr && ptr != 0)
01983 printf ("pool UNKNOWN, freed 0x%p (size %d)\n", ptr, old_size);
01984 #endif
01985
01986 if ( result == NULL )
01987 ErrMsg ( EC_No_Mem, "Realloc_Clear" );
01988
01989
01990 if ( new_size > old_size ) {
01991 MEM_PTR start_of_new = (MEM_PTR) ( ((char *) result) + old_size );
01992 INT32 num_added_bytes = new_size - old_size;
01993 bzero ( start_of_new, num_added_bytes );
01994 }
01995
01996 return result;
01997 }
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026 MEM_PTR
02027 Re_Calloc ( MEM_PTR ptr, INT32 new_nelem, INT32 elsize, INT32 old_nelem )
02028 {
02029 return Realloc_Clear ( ptr, new_nelem * elsize, old_nelem * elsize );
02030 }