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
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 #include <stdlib.h>
00092 #include <stdio.h>
00093
00094 #include "tdef.h"
00095 #include "uti.h"
00096 #include "xalloc.h"
00097
00098 #ifdef __XALLOC_XFUNCS
00099
00100
00101
00102 #if XALLOC_LVL==-2
00103 #undef XALLOC_LVL
00104 #define XALLOC_LVL 2
00105 #elif XALLOC_LVL==-1
00106 #undef XALLOC_LVL
00107 #define XALLOC_LVL 1
00108 #endif
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 #if XALLOC_LVL==1
00152
00153 #define MALLOC_ZEROxx
00154 #define MALLOC_NOMEM
00155 #define FREE_NULL
00156 #define NULL_TRAPxx
00157 #define BLK_CHECKxx
00158 #define LOGF_SAVExx
00159 #define HEAP_CHECKxx
00160 #define MEM_USEDxx
00161 #define HEAP_USEDxx
00162 #define HEAP_FREExx
00163
00164 #define LOGF_SAVE_DEF_STATE FALSE
00165 #define HEAP_CHECK_DEF_STATE FALSE
00166 #define MEM_USED_DEF_STATE FALSE
00167 #define HEAP_USED_DEF_STATE FALSE
00168 #define HEAP_FREE_DEF_STATE FALSE
00169
00170 #elif XALLOC_LVL==2
00171 #define MALLOC_ZEROxx
00172 #define MALLOC_NOMEM
00173 #define FREE_NULL
00174 #define NULL_TRAPxx
00175 #define BLK_CHECK
00176 #define LOGF_SAVE
00177 #define HEAP_CHECK
00178 #define MEM_USED
00179 #define HEAP_USED
00180 #define HEAP_FREE
00181
00182 #define LOGF_SAVE_DEF_STATE FALSE
00183 #define HEAP_CHECK_DEF_STATE FALSE
00184 #define MEM_USED_DEF_STATE FALSE
00185 #define HEAP_USED_DEF_STATE FALSE
00186 #define HEAP_FREE_DEF_STATE FALSE
00187
00188 #endif
00189
00190
00191
00192 #ifdef __XALLOC_SRCNAME
00193 #define IUFLARGS srcname, line
00194 #define UUFLARGS ,srcname, line
00195 #else
00196 #define IUFLARGS
00197 #define UUFLARGS
00198 #endif
00199
00200
00201
00202
00203 #ifdef __XALLOC_STDOVL
00204
00205 #undef malloc
00206 #undef calloc
00207 #undef free
00208 #undef strdup
00209 #undef farmalloc
00210 #undef farcalloc
00211 #undef farfree
00212 #undef _fstrdup
00213
00214 #endif
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 #define BLK_LIMITER_SIZE 4
00225
00226
00227 #define BLK_FILLER 159
00228
00229
00230 #define HEAP_FILLER 123
00231
00232
00233 #define LOGF_NAME "xalloc.log"
00234
00235
00236
00237 #define MEM_USED_X 72
00238 #define MEM_USED_Y 1
00239
00240
00241 #define HEAP_USED_X 1
00242 #define HEAP_USED_Y 25
00243
00244
00245 #define HEAP_FREE_X 72
00246 #define HEAP_FREE_Y 25
00247
00248
00249
00250
00251 #if __CC_BORLANDC__
00252
00253 #include <alloc.h>
00254 #include <string.h>
00255
00256 #if (defined(__COMPACT__)||defined(__LARGE__)||defined(__HUGE__))
00257 #define XPTR PTRH
00258 #define MALLOC farmalloc
00259 #define CALLOC farcalloc
00260 #define FREE farfree
00261 #define CORELEFT farcoreleft
00262 #define STRCPY _fstrcpy
00263 #define STRLEN _fstrlen
00264 #else
00265
00266 #define XPTR PTR
00267 #define MALLOC malloc
00268 #define CALLOC calloc
00269 #define FREE free
00270 #define CORELEFT coreleft
00271 #define STRCPY strcpy
00272 #define STRLEN strlen
00273 #endif
00274
00275 #define HEAPCHECK() heapcheck()
00276 #define HEAPCHECKFREE(value) heapcheckfree(value)
00277 #define HEAPFILLFREE(value) heapfillfree(value)
00278 #define HEAP_OK _HEAPOK
00279 #define HEAP_CORRUPT _HEAPCORRUPT
00280 #define HEAP_FREEWRITE _BADVALUE
00281
00282 #else
00283
00284 #include <string.h>
00285 #define XPTR PTRH
00286 #define MALLOC malloc
00287 #define CALLOC calloc
00288 #define FREE free
00289 #define CORELEFT() 0
00290 #define STRCPY strcpy
00291 #define STRLEN strlen
00292
00293
00294 #define HEAPCHECK() 0
00295 #define HEAPCHECKFREE(value) 0
00296 #define HEAPFILLFREE(value) {}
00297 #define HEAP_OK 0
00298 #define HEAP_CORRUPT 1
00299 #define HEAP_FREEWRITE 2
00300 #endif
00301
00302 #if __OS_BORLANDC__
00303 #define PRINT(msg,arg1) printf(msg,arg1)
00304 #else
00305 #define PRINT(msg,arg1) fprintf(stderr,msg,arg1)
00306 #endif
00307
00308
00309
00310 #if (defined(MEM_USED) || defined(HEAP_USED) || defined(HEAP_FREE))
00311
00312 #if __CC_BORLANDC__
00313
00314 #include <conio.h>
00315 #define GOTOXY(x,y) gotoxy(x,y)
00316 #define WHEREX() wherex()
00317 #define WHEREY() wherey()
00318
00319 #else
00320
00321 #define GOTOXY(x,y)
00322 #define WHEREX() 1
00323 #define WHEREY() 1
00324
00325 #endif
00326
00327 #endif
00328
00329
00330
00331
00332 #if (defined(BLK_CHECK)||defined(LOGF_SAVE)||defined(MEM_USED)||defined(HEAP_USED))
00333 #define MEM_LIM
00334 #endif
00335
00336
00337
00338 #ifdef MEM_LIM
00339
00340
00341 #ifdef __CC_MSVC__
00342 #pragma pack(push,1)
00343 #elif __CC_BORLANDC__
00344 typedef struct {CHAR8 x; INT2 y; } ____tmp_xalloc_align_test___;
00345 #if sizeof(____tmp_xalloc_align_test___)>3
00346 #error You must disable Word Alignment
00347 #endif
00348 #endif
00349
00350
00351 typedef struct {
00352 #ifdef BLK_CHECK
00353 #if (BLK_LIMITER_SIZE>0)
00354 INT8 s0[BLK_LIMITER_SIZE] MINALIGN;
00355 #endif
00356 #endif
00357 unsigned long blklen MINALIGN;
00358 #ifdef BLK_CHECK
00359 #if (BLK_LIMITER_SIZE>0)
00360 INT8 s1[BLK_LIMITER_SIZE] MINALIGN;
00361 #endif
00362 #endif
00363 #ifdef LOGF_SAVE
00364 long logf_count MINALIGN;
00365 #endif
00366 #ifdef BLK_CHECK
00367 #if (BLK_LIMITER_SIZE>0)
00368 INT8 s2[BLK_LIMITER_SIZE] MINALIGN;
00369 #endif
00370 unsigned long blklen1 MINALIGN;
00371 #if (BLK_LIMITER_SIZE>0)
00372 INT8 s3[BLK_LIMITER_SIZE] MINALIGN;
00373 #endif
00374 #endif
00375 } mem_limiter;
00376
00377 #define STARTER(blk) \
00378 ((mem_limiter XPTR)(((INT8 XPTR)(blk))-sizeof(mem_limiter)))
00379
00380 #define ENDER(blk,len) \
00381 ((mem_limiter XPTR)(((INT8 XPTR)(blk))+(len)))
00382
00383 #define XBPTR(blk,n) (((INT8 XPTR)(blk))+(n))
00384
00385 #ifdef __CC_MSVC__
00386 #pragma pack(pop)
00387 #endif
00388
00389 #endif
00390
00391
00392
00393
00394 #ifdef LOGF_SAVE
00395 PRIVATE BOOL logf_save_on = LOGF_SAVE_DEF_STATE;
00396 #endif
00397 #ifdef HEAP_CHECK
00398 PRIVATE BOOL heap_check_on = HEAP_CHECK_DEF_STATE;
00399 #endif
00400 #ifdef MEM_USED
00401 PRIVATE BOOL mem_used_on = MEM_USED_DEF_STATE;
00402 #endif
00403 #ifdef HEAP_USED
00404 PRIVATE BOOL heap_used_on = HEAP_USED_DEF_STATE;
00405 #endif
00406 #ifdef HEAP_FREE
00407 PRIVATE BOOL heap_free_on = HEAP_FREE_DEF_STATE;
00408 #endif
00409
00410
00411
00412
00413 #ifdef LOGF_SAVE
00414 PRIVATE long _logf_count=0;
00415 PRIVATE int _logf_append=0;
00416 #endif
00417
00418 #if (defined(MEM_USED) || defined(LOGF_SAVE))
00419 PRIVATE unsigned long _mem_used=0;
00420 #endif
00421
00422 #ifdef HEAP_USED
00423 PRIVATE unsigned long _heap_used=0;
00424 #endif
00425
00426 #ifdef HEAP_CHECK
00427 PRIVATE int _heap_filled=0;
00428 #endif
00429
00430
00431
00432 PRIVATE void _xalloc_error_handler( char * msg FLARGS )
00433 {
00434 #ifdef __XALLOC_SRCNAME
00435 die_beep("%s [%d] %s" UUFLARGS,msg);
00436 #else
00437 die_beep("%s",msg);
00438 #endif
00439 }
00440
00441
00442 #ifdef LOGF_SAVE
00443
00444 PRIVATE void _logf_log( long count, unsigned long size,
00445 int freeing FLARGS )
00446 {
00447 FILE * logfile;
00448
00449 if (_logf_append)
00450 logfile = fopen(LOGF_NAME,"a");
00451 else {
00452 logfile = fopen(LOGF_NAME,"w");
00453 _logf_append = 1;
00454 }
00455
00456 if (logfile) {
00457 #ifdef __XALLOC_SRCNAME
00458 if (freeing)
00459 fprintf(logfile,"$F%06ld %12s (%5d) -%7lu (%7lu->%7lu)\n",
00460 count UUFLARGS, size, _mem_used+size,_mem_used);
00461 else
00462 fprintf(logfile,"$M%06ld %12s (%5d) +%7lu (%7lu->%7lu)\n",
00463 count UUFLARGS, size, _mem_used-size,_mem_used);
00464 #else
00465 if (freeing)
00466 fprintf(logfile,"$F%06ld -%7lu (%7lu->%7lu)\n",
00467 count, size, _mem_used+size,_mem_used);
00468 else
00469 fprintf(logfile,"$M%06ld +%7lu (%7lu->%7lu)\n",
00470 count, size, _mem_used-size,_mem_used);
00471 #endif
00472 #ifdef __XALLOC_SRCNAME
00473 _assertheap_src(IUFLARGS);
00474 #else
00475 _assertheap();
00476 #endif
00477 fclose(logfile);
00478 fillheap();
00479 }
00480 else
00481 _xalloc_error_handler("error opening " LOGF_NAME UUFLARGS);
00482 }
00483
00484 #endif
00485
00486 #if (defined(MEM_USED) || defined(HEAP_USED) || defined(HEAP_FREE))
00487
00488 PRIVATE void _show_used( void )
00489 {
00490 int x,y;
00491
00492 BOOL show=FALSE;
00493
00494 #ifdef MEM_USED
00495 show = show || mem_used_on;
00496 #endif
00497 #ifdef HEAP_USED
00498 show = show || heap_used_on;
00499 #endif
00500 #ifdef HEAP_FREE
00501 show = show || heap_free_on;
00502 #endif
00503 if (!show) return;
00504
00505 x = WHEREX();
00506 y = WHEREY();
00507
00508 #ifdef MEM_USED
00509 GOTOXY(MEM_USED_X,MEM_USED_Y);
00510 PRINT("%7lu ",_mem_used);
00511 #endif
00512
00513 #ifdef HEAP_USED
00514 GOTOXY(HEAP_USED_X,HEAP_USED_Y);
00515 PRINT("%7lu ",_heap_used);
00516 #endif
00517
00518 #ifdef HEAP_FREE
00519 GOTOXY(HEAP_FREE_X,HEAP_FREE_Y);
00520 PRINT("%7lu ",(unsigned long)CORELEFT());
00521 #endif
00522
00523 GOTOXY(x,y);
00524 }
00525 #else
00526
00527 #define _show_used()
00528
00529 #endif
00530
00531
00532
00533
00534
00535
00536 PRIVATE void XPTR _prepare_block( INT8 XPTR p, unsigned long size FLARGS )
00537 {
00538 #ifdef MEM_LIM
00539 mem_limiter XPTR ps;
00540 #endif
00541 #ifdef BLK_CHECK
00542 #if (BLK_LIMITER_SIZE>0)
00543 int i;
00544 #endif
00545 #endif
00546
00547 if (p==NULL) {
00548 #ifdef MALLOC_NOMEM
00549 PRIVATE char msg[] = "Out of memory (123456789012)";
00550 _show_used();
00551 sprintf(strchr(msg,'(')+1,"%lu)",size);
00552 _xalloc_error_handler(msg UUFLARGS);
00553 #else
00554 _show_used();
00555 #endif
00556 return NULL;
00557 }
00558
00559 #if (defined(MEM_USED) || defined(LOGF_SAVE))
00560 _mem_used += size;
00561 #endif
00562 #ifdef HEAP_USED
00563 _heap_used += size;
00564 #ifdef MEM_LIM
00565 _heap_used += sizeof(mem_limiter);
00566 #endif
00567 #ifdef BLK_CHECK
00568 _heap_used += sizeof(mem_limiter);
00569 #endif
00570 #endif
00571
00572 _show_used();
00573
00574 #ifdef MEM_LIM
00575 ps = (mem_limiter XPTR)p;
00576 ps->blklen = size;
00577 p += sizeof(mem_limiter);
00578 #endif
00579
00580 #ifdef LOGF_SAVE
00581 if (logf_save_on) {
00582 ps->logf_count = _logf_count;
00583 _logf_log(_logf_count,size,0 UUFLARGS);
00584 }
00585 else
00586 ps->logf_count = -_logf_count;
00587 _logf_count++;
00588 #endif
00589
00590 #ifdef BLK_CHECK
00591 ps->blklen1 = ps->blklen;
00592 #if (BLK_LIMITER_SIZE>0)
00593 for (i=0; i<BLK_LIMITER_SIZE; i++)
00594 ps->s3[i] = (ps->s2[i] = (ps->s1[i] = (ps->s0[i] = BLK_FILLER+i)
00595 + BLK_LIMITER_SIZE) + BLK_LIMITER_SIZE) + BLK_LIMITER_SIZE;
00596 #endif
00597 *(mem_limiter XPTR)(p + size) = (*ps);
00598 #endif
00599
00600 #ifdef HEAP_CHECK
00601 #ifdef __XALLOC_SRCNAME
00602 _assertheap_src(IUFLARGS);
00603 #else
00604 _assertheap();
00605 #endif
00606 #endif
00607
00608 return (void XPTR)p;
00609 }
00610
00611
00612
00613
00614 #ifdef __XALLOC_SRCNAME
00615 void * _xmalloc_src( unsigned long size FLARGS )
00616 #else
00617 void * _xmalloc( unsigned long size )
00618 #endif
00619 {
00620 if (!size)
00621 #ifdef MALLOC_ZERO
00622 _xalloc_error_handler("xmalloc(0)" UUFLARGS);
00623 #else
00624 return NULL;
00625 #endif
00626
00627 return _prepare_block( (INT8 XPTR)MALLOC( size
00628 #ifdef MEM_LIM
00629 + sizeof(mem_limiter)
00630 #endif
00631 #ifdef BLK_CHECK
00632 + sizeof(mem_limiter)
00633 #endif
00634 ), size UUFLARGS);
00635 }
00636
00637
00638
00639
00640
00641 #ifdef __XALLOC_SRCNAME
00642 void * _xcalloc_src( unsigned long nitems, unsigned long size FLARGS)
00643 #else
00644 void * _xcalloc( unsigned long nitems, unsigned long size )
00645 #endif
00646 {
00647 size*=nitems;
00648
00649 if (!size)
00650 #ifdef MALLOC_ZERO
00651 _xalloc_error_handler("xcalloc(0)" UUFLARGS);
00652 #else
00653 return NULL;
00654 #endif
00655
00656 return _prepare_block( (INT8 XPTR)CALLOC( size
00657 #ifdef MEM_LIM
00658 + sizeof(mem_limiter)
00659 #endif
00660 #ifdef BLK_CHECK
00661 + sizeof(mem_limiter)
00662 #endif
00663 , 1), size UUFLARGS);
00664 }
00665
00666
00667
00668 void fillheap( void )
00669 {
00670 #ifdef HEAP_CHECK
00671 if (heap_check_on) HEAPFILLFREE(HEAP_FILLER);
00672 #endif
00673 }
00674
00675
00676
00677
00678 #ifdef __XALLOC_SRCNAME
00679 void _xfree_src( void * block FLARGS )
00680 #else
00681 void _xfree( void * block )
00682 #endif
00683 {
00684 #ifdef LOGF_SAVE
00685 long count;
00686 #endif
00687 #if (defined(MEM_USED) || defined(LOGF_SAVE) || defined(HEAP_USED))
00688 unsigned long size;
00689 #endif
00690
00691 #ifdef FREE_NULL
00692 if (block==NULL)
00693 _xalloc_error_handler("xfree(NULL)" UUFLARGS);
00694 #endif
00695
00696 #if (defined(MEM_USED) || defined(LOGF_SAVE) || defined(HEAP_USED))
00697 #ifdef __XALLOC_SRCNAME
00698 size = _xblksize_src(block UUFLARGS);
00699 #else
00700 size = _xblksize(block);
00701 #endif
00702 #elif BLK_CHECK
00703 _assertblk(block UUFLARGS);
00704 #elif HEAP_CHECK
00705 _assertheap(IUFLARGS);
00706 #endif
00707
00708 #ifdef LOGF_SAVE
00709 count = (STARTER(block))->logf_count;
00710 #endif
00711
00712 #ifdef MEM_LIM
00713 FREE((((INT8 XPTR)block)-sizeof(mem_limiter)));
00714 #else
00715 FREE(block);
00716 #endif
00717
00718 #ifdef HEAP_CHECK
00719 fillheap();
00720 #endif
00721
00722 #if (defined(MEM_USED) || defined(LOGF_SAVE))
00723 _mem_used -= size;
00724 #endif
00725 #ifdef HEAP_USED
00726 _heap_used -= size;
00727 #ifdef MEM_LIM
00728 _heap_used -= sizeof(mem_limiter);
00729 #endif
00730 #ifdef BLK_CHECK
00731 _heap_used -= sizeof(mem_limiter);
00732 #endif
00733 #endif
00734
00735 _show_used();
00736
00737 #ifdef LOGF_SAVE
00738 if (logf_save_on) {
00739 if (count>=0) _logf_log(count,size,1 UUFLARGS);
00740 }
00741 #endif
00742 }
00743
00744
00745
00746
00747
00748 unsigned long xusedmem( void )
00749 {
00750 #if (defined(MEM_USED) || defined(LOGF_SAVE))
00751 return _mem_used;
00752 #else
00753 return 0;
00754 #endif
00755 }
00756
00757
00758
00759
00760
00761 unsigned long xusedheap( void )
00762 {
00763 #ifdef HEAP_USED
00764 return _heap_used;
00765 #else
00766 return 0;
00767 #endif
00768 }
00769
00770
00771
00772
00773 #ifdef __XALLOC_SRCNAME
00774 char * _xstrdup_src( const char * str FLARGS )
00775 #else
00776 char * _xstrdup( const char * str )
00777 #endif
00778 {
00779 char * s;
00780
00781 #ifdef __XALLOC_SRCNAME
00782 s = (char *)_xmalloc_src(sizeof(char)*(STRLEN(str)+1) UUFLARGS);
00783 #else
00784 s = (char *)_xmalloc(sizeof(char)*(STRLEN(str)+1));
00785 #endif
00786
00787 if (s)
00788 STRCPY(s,str);
00789
00790 return s;
00791 }
00792
00793
00794
00795
00796
00797
00798
00799 #ifdef __XALLOC_SRCNAME
00800 unsigned long _xblksize_src( void * block FLARGS )
00801 #else
00802 unsigned long _xblksize( void * block )
00803 #endif
00804 {
00805 #ifdef __XALLOC_SRCNAME
00806 _assertblk_src(block UUFLARGS);
00807 #else
00808 _assertblk(block);
00809 #endif
00810
00811 #ifdef BLK_CHECK
00812 return (STARTER(block))->blklen;
00813 #else
00814 return 0;
00815 #endif
00816 }
00817
00818
00819 #ifdef BLK_CHECK
00820
00821 PRIVATE int _test_limiter( mem_limiter XPTR m )
00822 {
00823 #if (BLK_LIMITER_SIZE>0)
00824 int i;
00825
00826 for (i=0; i<BLK_LIMITER_SIZE; i++)
00827 if ((m->s0[i]!=(INT8)(BLK_FILLER+i))||
00828 (m->s1[i]!=(INT8)(BLK_FILLER+BLK_LIMITER_SIZE+i))||
00829 (m->s2[i]!=(INT8)(BLK_FILLER+BLK_LIMITER_SIZE*2+i))||
00830 (m->s3[i]!=(INT8)(BLK_FILLER+BLK_LIMITER_SIZE*3+i))||
00831 (m->blklen!=m->blklen1) )
00832 return XALLOC_BLK_CORRUPTED;
00833 #endif
00834
00835 return XALLOC_BLK_NOT_CORRUPTED;
00836 }
00837
00838 #endif
00839
00840
00841
00842
00843
00844 int testblk( void * block )
00845 {
00846 #ifdef BLK_CHECK
00847 mem_limiter XPTR ps;
00848 mem_limiter XPTR pe;
00849 #endif
00850
00851 if (block==NULL)
00852 #ifdef NULL_TRAP
00853 return XALLOC_BLK_NULLPTR;
00854 #else
00855 return XALLOC_BLK_NOT_CORRUPTED;
00856 #endif
00857
00858 #ifdef BLK_CHECK
00859 ps = STARTER(block);
00860 if (_test_limiter(ps)!=XALLOC_BLK_NOT_CORRUPTED)
00861 return XALLOC_BLK_CORRUPTED_START;
00862
00863 pe = ENDER(block,ps->blklen);
00864
00865 if (_test_limiter(pe)!=XALLOC_BLK_NOT_CORRUPTED)
00866 return XALLOC_BLK_CORRUPTED_END;
00867
00868 if (ps->blklen!=pe->blklen)
00869 return XALLOC_BLK_CORRUPTED;
00870 #endif
00871
00872 return XALLOC_BLK_NOT_CORRUPTED;
00873 }
00874
00875
00876
00877
00878 int testheap( void )
00879 {
00880 #ifdef HEAP_CHECK
00881 int i;
00882
00883 if (!heap_check_on) return XALLOC_HEAP_NOT_CORRUPTED;
00884
00885 if (!_heap_filled) {
00886 _heap_filled = 1;
00887 HEAPFILLFREE(HEAP_FILLER);
00888 }
00889
00890 if (HEAPCHECK()!=HEAP_OK) return XALLOC_HEAP_CORRUPTED;
00891
00892 i = HEAPCHECKFREE(HEAP_FILLER);
00893 if (i==HEAP_CORRUPT) return XALLOC_HEAP_CORRUPTED;
00894 else if (i==HEAP_FREEWRITE) return XALLOC_HEAP_WRITE_IN_FREE_MEM;
00895 else if (i!=HEAP_OK) return XALLOC_HEAP_VERY_CORRUPTED;
00896 #endif
00897
00898 return XALLOC_HEAP_NOT_CORRUPTED;
00899 }
00900
00901
00902
00903
00904
00905
00906 #ifdef __XALLOC_SRCNAME
00907 void _assertblk_src( void * block FLARGS )
00908 #else
00909 void _assertblk( void * block )
00910 #endif
00911 {
00912 #ifdef BLK_CHECK
00913 switch (testblk(block)) {
00914 case XALLOC_BLK_CORRUPTED_START :
00915 _xalloc_error_handler("out of range in memblock (begin)" UUFLARGS);
00916 case XALLOC_BLK_CORRUPTED_END :
00917 _xalloc_error_handler("out of range in memblock (end)" UUFLARGS);
00918 case XALLOC_BLK_CORRUPTED :
00919 _xalloc_error_handler("out of range in memblock (???)" UUFLARGS);
00920 case XALLOC_BLK_NULLPTR :
00921 _xalloc_error_handler("NULL pointer" UUFLARGS);
00922 }
00923 #endif
00924
00925 #ifdef HEAP_CHECK
00926 #ifdef __XALLOC_SRCNAME
00927 _assertheap_src(IUFLARGS);
00928 #else
00929 _assertheap();
00930 #endif
00931 #endif
00932 (void)block;
00933 #ifdef __XALLOC_SRCNAME
00934 (void)srcname;
00935 (void)line;
00936 #endif
00937 }
00938
00939
00940
00941 #ifdef __XALLOC_SRCNAME
00942 VOID _assertheap_src( IFLARGS )
00943 #else
00944 VOID _assertheap( VOID )
00945 #endif
00946 {
00947 #ifdef HEAP_CHECK
00948 switch (testheap()) {
00949 case XALLOC_HEAP_CORRUPTED :
00950 _xalloc_error_handler("corrupted heap" UUFLARGS);
00951 case XALLOC_HEAP_WRITE_IN_FREE_MEM :
00952 _xalloc_error_handler("free heap overwritten" UUFLARGS);
00953 case XALLOC_HEAP_VERY_CORRUPTED :
00954 _xalloc_error_handler("high corruption in heap!" UUFLARGS);
00955 }
00956 #elif defined(__XALLOC_SRCNAME)
00957 (void)srcname;
00958 (void)line;
00959 #endif
00960 }
00961
00962
00963
00964 void xalloc_logf(int b)
00965 {
00966 #ifdef LOGF_SAVE
00967 logf_save_on = (BOOL)b;
00968 #else
00969 (void)b;
00970 #endif
00971 }
00972
00973
00974
00975 void xalloc_heapcheck( int b )
00976 {
00977 #ifdef HEAP_CHECK
00978 heap_check_on = (BOOL)b;
00979 #else
00980 (void)b;
00981 #endif
00982 }
00983
00984
00985
00986 void xalloc_showmem(int b)
00987 {
00988 #ifdef MEM_USED
00989 mem_used_on = (BOOL)b;
00990 #else
00991 (void)b;
00992 #endif
00993 }
00994
00995
00996
00997 void xalloc_showheap(int b)
00998 {
00999 #ifdef HEAP_USED
01000 heap_used_on = (BOOL)b;
01001 #else
01002 (void)b;
01003 #endif
01004 }
01005
01006
01007
01008 void xalloc_showfree(int b)
01009 {
01010 #ifdef HEAP_FREE
01011 heap_free_on = (BOOL)b;
01012 #else
01013 (void)b;
01014 #endif
01015 }
01016
01017
01018 #endif
01019
01020
01021
01022
01023
01024
01025 #ifdef __XALLOC_SRCNAME
01026 void _xmem2disk_src( char * fname, void * block FLARGS )
01027 #else
01028 void _xmem2disk( char * fname, void * block )
01029 #endif
01030 {
01031 #ifndef MEM_LIM
01032 die_beep("error: xmem2disk not compiled in");
01033 (void)fname;
01034 (void)block;
01035 #if __XALLOC_SRCNAME
01036 (void)srcname;
01037 (void)line;
01038 #endif
01039
01040 #else
01041 FILE * f;
01042 unsigned long l, i;
01043
01044 #ifdef __XALLOC_SRCNAME
01045 l = _xblksize_src(block UUFLARGS);
01046 #else
01047 l = _xblksize(block);
01048 #endif
01049
01050 if ((f=fopen(fname,"wb"))==NULL)
01051 _xalloc_error_handler("error saving memblock (fopen)" UUFLARGS);
01052
01053 if (fwrite(STARTER(block),sizeof(mem_limiter),1,f)!=1)
01054 _xalloc_error_handler("error saving memblock (fwrite hdr)" UUFLARGS);
01055
01056 #define N 1000
01057 i=0;
01058 while (i+N<=l) {
01059 if (fwrite((void *)XBPTR(block,i),N,1,f)!=1)
01060 _xalloc_error_handler("error saving memblock (fwrite blk)" UUFLARGS);
01061 i+=N;
01062 }
01063 if (i<l) {
01064 if (fwrite((void *)XBPTR(block,i),(size_t)(l-i),1,f)!=1)
01065 _xalloc_error_handler("error saving memblock (fwrite blk)" UUFLARGS);
01066 }
01067 #undef N
01068
01069 #ifdef BLK_CHECK
01070 if (fwrite(ENDER(block,l),sizeof(mem_limiter),1,f)!=1)
01071 _xalloc_error_handler("error saving memblock (fwrite ehdr)" UUFLARGS);
01072 #endif
01073
01074 if (fclose(f)==EOF)
01075 _xalloc_error_handler("error saving memblock (fclose)" UUFLARGS);
01076
01077 #ifdef __XALLOC_SRCNAME
01078 _xfree_src(block UUFLARGS);
01079 #else
01080 _xfree(block);
01081 #endif
01082
01083 #endif
01084 }
01085
01086
01087
01088
01089
01090
01091 #ifdef __XALLOC_SRCNAME
01092 void * _xdisk2mem_src( char * fname FLARGS )
01093 #else
01094 void * _xdisk2mem( char * fname )
01095 #endif
01096 {
01097 #ifndef MEM_LIM
01098 die_beep("error: xdisk2mem not compiled in");
01099 (void)fname;
01100 #if __XALLOC_SRCNAME
01101 (void)srcname;
01102 (void)line;
01103 #endif
01104 return NULL;
01105
01106 #else
01107
01108 FILE * f;
01109 mem_limiter m;
01110 unsigned long l, i;
01111 void * p;
01112
01113 if ((f=fopen(fname,"rb"))==NULL)
01114 _xalloc_error_handler("error loading memblock (fopen)" UUFLARGS);
01115
01116 if (fread(&m,sizeof(mem_limiter),1,f)!=1)
01117 _xalloc_error_handler("error loading memblock (fread hdr)" UUFLARGS);
01118 #ifdef BLK_CHECK
01119 if (_test_limiter(&m)!=XALLOC_BLK_NOT_CORRUPTED)
01120 _xalloc_error_handler("error loading memblock (test hdr)" UUFLARGS);
01121 #endif
01122 l=m.blklen;
01123
01124 #ifdef __XALLOC_SRCNAME
01125 p=_xmalloc_src(l UUFLARGS);
01126 #else
01127 p=_xmalloc(l);
01128 #endif
01129
01130 #define N 1000
01131 i=0;
01132 while (i+N<=l) {
01133 if (fread((void *)XBPTR(p,i),N,1,f)!=1)
01134 _xalloc_error_handler("error loading memblock (fread blk)" UUFLARGS);
01135 i+=N;
01136 }
01137 if (i<l) {
01138 if (fread((void *)XBPTR(p,i),(size_t)(l-i),1,f)!=1)
01139 _xalloc_error_handler("error loading memblock (fread blk)" UUFLARGS);
01140 }
01141 #undef N
01142
01143 #ifdef BLK_CHECK
01144 if (fread(&m,sizeof(mem_limiter),1,f)!=1)
01145 _xalloc_error_handler("error loading memblock (fread ehdr)" UUFLARGS);
01146 #ifdef BLK_CHECK
01147 if (_test_limiter(&m)!=XALLOC_BLK_NOT_CORRUPTED)
01148 _xalloc_error_handler("error loading memblock (test ehdr)" UUFLARGS);
01149 #endif
01150 if (l!=m.blklen)
01151 _xalloc_error_handler("error loading memblock (comp hdr-ehdr)" UUFLARGS);
01152 #endif
01153
01154 #ifdef __XALLOC_SRCNAME
01155 _assertblk_src(p UUFLARGS);
01156 #else
01157 _assertblk(p);
01158 #endif
01159
01160 if (fclose(f)==EOF)
01161 _xalloc_error_handler("error loading memblock (fclose)" UUFLARGS);
01162
01163 return (p);
01164 #endif
01165 }
01166
01167