[r6]: trunk / src / xmalloc.c Maximize Restore History

Download this file

xmalloc.c    1488 lines (1167 with data), 32.6 kB

   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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
#if !defined( lint )
static char rcsid[] = "$Id: xmalloc.c,v 1.5 2012-02-02 09:13:58 jullien Exp $";
#endif /* lint */
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
/*
* @(#)xmalloc.c :
*
* This is a very fast storage allocator. It allocates blocks
* of a small number of different sizes, and keeps free lists
* of each size. Blocks that don't exactly fit are passed up to
* the next larger size. In this implementation, the available
* sizes are (2^n)-4 (or -16) bytes long. This is designed for
* use in a program that uses vast quantities of memory, but
* bombs when it runs out.
*
* nextfree[i] is the pointer to the next free memory block of
* size 2 ^ (i+MINBLOCK). The smallest allocatable block size
* is SMALLEST_SIZE bytes. The overhead information will go in
* the first int of the block, and the returned pointer will
* point to the second.
*
* nmalloc[i] is the difference between the number of mallocs
* and frees for a given block size. It is defined only when
* _XALLOX_STATS is on.
*
* These routines are also tuned to C++ in that free(0) is a
* noop and a failed malloc automatically calls
* (*_new_handler)(). If your compiler supports the separate
* redefinitions of new and delete for arrays that is new[] and
* delete[] operators, you must set _NEW_ARRAY_OPERATOR.
*
* Note : this file use protypes so that it can compile with
* ISO/ANSI C compiles as well as C++ compilers. You must
* remove declarations if you still use Old-C compilers.
*/
#if !defined( _XALLOC_DEFAULT )
#if defined(_WIN32) || \
defined(_WIN64) || \
defined(__MINGW32__) || \
defined(__MINGW64__)
#define _WINDOWS_SOURCE
#if !defined( _CRT_SECURE_NO_DEPRECATE )
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#if !defined( _CRT_NONSTDC_NO_DEPRECATE )
#define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
#endif /* _WIN32 || _WIN64 || __MINGW32__ || __MINGW64__ */
#if defined( _WINDOWS ) || defined( _WIN32 ) || defined( _WIN64 )
#include <windows.h>
#endif /* _WINDOWS */
#if defined( unix ) || defined( _POSIX_SOURCE ) || defined( _XOPEN_SOURCE )
#include <unistd.h>
#endif /* unix || _POSIX_SOURCE || _XOPEN_SOURCE */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined( _DLL )
#if !defined( _XIMPORT )
#define _XIMPORT __declspec(dllexport)
#endif /* _XIMPORT */
#else /* _DLL */
#if !defined( _XIMPORT )
#define _XIMPORT
#endif /* _XIMPORT */
#endif /* _DLL */
#include "xmem.h"
#if defined( __cplusplus )
extern "C" {
#endif /* __cplusplus */
#define NO_XALLOC_DEBUG /* defined when debugging is wanted */
#define NO_XALLOC_STATS /* defined when stats wanted */
#define NO_XALLOC_RANGE_CHECK /* defined when range checking wanted */
#define NO_XALLOC_TYPE_CHECK /* defined when array checking wanted */
#define NO_XALLOC_TRACE /* defined when trace __FILE__,__LINE__ on */
#if !defined( _XALLOC_ALIGN )
#define _XALLOC_ALIGN 8 /* allocation alignment */
#endif /* _XALLOC_ALIGN */
#if defined( _XALLOC_REPLACE )
#if defined( free )
#undef free
#endif /* free */
#if defined( malloc )
#undef malloc
#endif /* malloc */
#if defined( realloc )
#undef realloc
#endif /* realloc */
#if defined( calloc )
#undef calloc
#endif /* calloc */
_XIMPORT void free(void *mem) { _xfree(mem); }
_XIMPORT void * malloc(size_t size) { return( _xmalloc(size) ); }
_XIMPORT void * calloc(size_t nelem, size_t size) { return( _xcalloc(nelem, size) ); }
_XIMPORT void * realloc(void *mem, size_t nbytes) { return( _xrealloc(mem, nbytes) ); }
#endif /* _XALLOC_REPLACE */
static void *sysgetmem( size_t size );
static int morecore( int bucket );
static void getpool( void );
static int gotpool = 0; /* flag initial free blocks */
#if defined( _XALLOC_TRACE ) || defined( _XALLOC_STATS )
int _traceflag = 0;
static void _ftrace( const char* o, size_t s, void* p );
static FILE* _tracefd = NULL;
static FILE* _logfd = (FILE *)NULL;
#define INSURE_LOG if( _logfd == (FILE *)NULL ) _logfd = stderr
#define trace( o, s, p ) _ftrace( o, s, p )
#define traceon() _traceflag = 1
#define traceoff() _traceflag = 0
static void
_ftrace( const char* o, size_t s, void* p )
{
/*
* WARNING!! we use C printf functions to avoid confusion
* with operator<< that may use new or delete internally.
*/
if( !gotpool )
getpool();
if( _traceflag ) {
if( _tracefd == NULL )
_tracefd = fopen( "newdel.log", "w" );
if( s >= 0 )
(void)fprintf(_tracefd, "%-9s: %p (%d bytes)\n", o, p, s);
else (void)fprintf(_tracefd, "%-9s: %p\n", o, p);
}
}
#else /* _XALLOC_TRACE || _XALLOC_STATS */
#define trace( o, s, p )
#define traceon()
#define traceoff()
#endif /* _XALLOC_TRACE || _XALLOC_STATS */
typedef unsigned char _byte; /* small int in the range 0,255 */
typedef unsigned int _int4; /* 32 bit integer */
#define ISALLOC ((_byte)0xF7) /* magic byte for allocation */
#define ISFREE ((_byte)0x54) /* magic byte for free block */
#define ISTAGED ((_byte)0xD9) /* magic byte for tagged block */
#define MAXBLOCK 30 /* max number of blocks */
#define MINBLOCK 3 /* min block size (1<<3) == 8 */
#define BLOCKSIZE 8 /* i.e. 1<<MINBLOCK bytes */
#define SMALLEST_SIZE 8 /* smallest allocation size */
#if defined( _M_IA64 ) || defined( _IA64 ) || defined( _WIN64 )
typedef unsigned __int64 XULPTR;
#else
typedef unsigned long XULPTR;
#endif /* _M_IA64 || _IA64 || _WIN64 */
#define ALIGN(x) (((XULPTR)(x)+(_XALLOC_ALIGN-1))&~(_XALLOC_ALIGN-1))
#define ISALIGN(x) (((XULPTR)(x) & (_XALLOC_ALIGN-1)) == 0)
#if defined( _XALLOC_STATS )
static long nmalloc[ MAXBLOCK ]; /* stats between mallocs/frees */
static long allocount; /* number of allocations */
static long freecount; /* number of frees */
#endif /* _XALLOC_STATS */
/*
* If range checking is not turned on, all we have is a flag
* indicating whether memory is allocated, an index in
* nextfree[], and a size field; to xrealloc() memory we copy
* either size bytes or 1<<(index+MINBLOCK) bytes depending on
* whether the former can hold the exact size (given the value
* of 'index'). If range checking is on, we always need to know
* how much space is allocated, so the 'size' field is never
* used.
*/
struct mhead {
_byte mh_alloc; /* ISALLOC or ISFREE */
_byte mh_index; /* index in nextfree[] */
#if defined( _XALLOC_TYPE_CHECK )
_byte mh_type; /* allocation type */
_byte mh_pad1; /* padded byte (not used) */
#else /* _XALLOC_TYPE_CHECK */
_byte mh_pad0; /* padded byte (not used) */
_byte mh_pad1; /* padded byte (not used) */
#endif /* _XALLOC_TYPE_CHECK */
size_t mh_size; /* size, if < 0x10000 */
#if defined( _XALLOC_TRACE )
struct mhead * mh_prev; /* prev allocated block */
struct mhead * mh_next; /* next allocated block */
const char * mh_file; /* file that make allocation */
_int4 mh_line; /* line that make allocation */
#endif /* _XALLOC_TRACE */
#if defined( _XALLOC_RANGE_CHECK ) || defined( _XALLOC_TRACE )
size_t mh_nbytes; /* number of bytes allocated */
_int4 mh_magic4; /* should be == MAGIC4 */
#endif /* _XALLOC_RANGE_CHECK || _XALLOC_TRACE */
};
typedef struct mhead MHEAD;
typedef struct mhead * PMHEAD;
static PMHEAD nextfree[MAXBLOCK]; /* free list of size 2**(i+MINBLOCK) */
#define HEADSIZE (ALIGN( sizeof( MHEAD ) ))
#define HEADPTR( ptr ) ((PMHEAD)((char *)ptr - HEADSIZE))
#define ALLOC_NEW_ARRAY ((_byte)0xA7)
#define ALLOC_NEW_NOARRAY ((_byte)0xB7)
#define ALLOC_MALLOC ((_byte)0xC7)
/*
* Access free-list pointer of a block. It is stored at
* block+4. This is not a field in the mhead structure because
* sizeof( struct mhead ) must describe the overhead for when
* the block is in use, and we do not want the free-list
* pointer to count in that.
*/
#define CHAIN( a ) (*(PMHEAD *)(sizeof( char * ) + (char *)( a )))
static int xabort( void );
#define DONT_ABORT_ON_ERRORS
static int
xabort( void )
{
#if defined( _ABORT_ON_ERRORS )
abort();
#endif /* _ABORT_ON_ERRORS */
return( 0 );
}
static void _xmalloc_error( const char *e, MHEAD* p, const char *f, unsigned int l );
#if defined( __LCC__ )
static void * _xmalloc_unused( void* p );
static void *
_xmalloc_unused( void* p )
{
return( p );
}
#else
#define _xmalloc_unused( p ) ((void)p)
#endif
static void
_xmalloc_error( const char *e, MHEAD *p, const char *f, unsigned int l )
{
#if defined( _XALLOC_TRACE )
if( p && p->mh_file ) {
INSURE_LOG;
if( p->mh_alloc == ISFREE || p->mh_alloc == ISALLOC )
(void)fprintf(
_logfd,
"Block from %s(%d)\n\r",
p->mh_file,
p->mh_line
);
else (void)fprintf( _logfd, "Block at address %p\n\r", p );
}
#else /* _XALLOC_TRACE */
_xmalloc_unused( &p );
#endif /* _XALLOC_TRACE */
#if defined( _XALLOC_DEBUG )
INSURE_LOG;
(void)fprintf( _logfd, "Assertion '%s' fails: %s(%d)\n\r", e, f, l );
(void)fflush( _logfd ); /* just in case user buffered it */
#else /* _XALLOC_DEBUG */
_xmalloc_unused( &e );
_xmalloc_unused( &f );
_xmalloc_unused( &l );
#endif /* _XALLOC_DEBUG */
(void)xabort();
}
#define XALLOC_ERROR( e, p ) _xmalloc_error( e, p, __FILE__, __LINE__ )
#if defined( _XALLOC_DEBUG )
#define ASSERT(c,p) (void)((c)||(_xmalloc_error(#c,p,__FILE__,__LINE__),0))
#else /* _XALLOC_DEBUG */
#define ASSERT(c,p) (void)((c)||xabort())
#endif /* _XALLOC_DEBUG */
#if defined( _XALLOC_RANGE_CHECK )
/*
* To implement range checking, we write magic values in at the
* beginning and end of each allocated block, and make sure
* they are undisturbed whenever a xfree or a xrealloc occurs.
*/
#define MAGIC1 0x55 /* 4 bytes following the block's real space */
#define MAGIC4 0x55555555 /* 4 bytes before the block's real space */
#define EXTRA 4 /* 4 bytes extra for MAGIC1s */
#else /* _XALLOC_RANGE_CHECK */
#define EXTRA 0 /* 0 bytes extra for MAGIC1s */
#endif /* _XALLOC_RANGE_CHECK */
/*
* Ask system for more memory size index. The first time, it
* allocate two block of each size up to 1k bytes and a block
* of the requested size.
*/
static int
morecore( int nu )
{
char *cp;
int nblks;
size_t size;
/*
* On initial startup, get two blocks of each size up
* to 1k bytes
*/
if( !gotpool )
getpool();
/*
* Take at least 2k, and figure out how many blocks of
* the desired size we're about to get
*/
nblks = 1;
size = nu;
if( size < BLOCKSIZE ) {
size = BLOCKSIZE;
nblks = 1 << (BLOCKSIZE - nu);
}
if( (cp = (char *)sysgetmem( 1 << (size + MINBLOCK))) == NULL )
/*
* No more room!
*/
return( -1 );
if( ISALIGN( cp ) ) {
/*
* shouldn't happen, but just in case ...
*/
cp = (char *)ALIGN( cp );
nblks--;
}
/*
* save new header and link the nblks blocks together
*/
nextfree[ nu ] = (PMHEAD)cp;
size = (1 << (nu + MINBLOCK));
for( ;; ) {
((PMHEAD)cp)->mh_alloc = ISFREE;
((PMHEAD)cp)->mh_index = (_byte)nu;
if( --nblks <= 0 )
break;
CHAIN( (PMHEAD)cp ) = (PMHEAD)( cp + size );
cp += size;
}
CHAIN( (PMHEAD)cp ) = NULL;
return( 0 );
}
#if (_XALLOC_ALIGN <= 64)
/*
* Build initial free list for the first blocks. Get one block
* of each size up to 1K bytes. It's called twice at startup to
* allocate 2 blocks of each size: 8, 16, 32, 64, 128, 256, 512.
*/
static void
getpool( void )
{
int nu;
char *cp;
int i;
ASSERT( ((_XALLOC_ALIGN % 4) == 0), 0 ); /* power of 2 >= 4 */
ASSERT( (SMALLEST_SIZE == 8), 0 ); /* smallest size == 8 */
ASSERT( (sizeof( _int4 ) == 4), 0 ); /* _int4 is 4 bytes long */
for( i = 0 ; i < 2 ; i++ ) {
/*
* Get initial block for storage
*/
if( (cp = (char *)sysgetmem(1<<(BLOCKSIZE+MINBLOCK))) == NULL )
return;
/*
* Divide it into an initial 8-word block plus
* one block of size 2**nu for nu=MINBLOCK..10.
*/
CHAIN( cp ) = nextfree[ 0 ];
nextfree[ 0 ] = (PMHEAD)cp;
((PMHEAD)cp)->mh_alloc = ISFREE;
((PMHEAD)cp)->mh_index = 0;
cp = (char *)ALIGN( cp + SMALLEST_SIZE );
/*
* Block from 8 to 512 bytes.
*/
for( nu = 0 ; nu < (BLOCKSIZE-1) ; nu++ ) {
CHAIN( cp ) = nextfree[ nu ];
nextfree[ nu ] = (PMHEAD)cp;
((PMHEAD)cp)->mh_alloc = ISFREE;
((PMHEAD)cp)->mh_index = (_byte)nu;
cp = (char *)ALIGN( cp + (SMALLEST_SIZE << nu) );
}
}
gotpool = 1;
}
#else /* _XALLOC_ALIGN <= 64 */
static void
getpool( void )
{
gotpool = 1;
}
#endif /* _XALLOC_ALIGN <= 64 */
#if defined( _XALLOC_TRACE )
/*
* The following functions are used to save trace (as file +
* line) of the calling routine. When trace is ON, headers are
* 4 pointers bigger to save __FILE__, __LINE__ and doubly
* linked list informations.
*/
static void xmalloc_remove_node( PMHEAD ptr, int index );
static void xmalloc_add_node( PMHEAD ptr, int index );
static void xmalloc_tag_used( void );
static PMHEAD nextused[MAXBLOCK]; /* used list of size 2**(i+MINBLOCK) */
static const char* _trace_file;
static int _trace_line;
_XIMPORT int
_xmalloc_set_tag( const char* file, int line )
{
_trace_file = file;
_trace_line = line;
return( line );
}
_XIMPORT void *
_xmalloc_tag_block( void* mem, const char* file, int line )
{
PMHEAD p;
if( mem == NULL )
return( NULL );
p = HEADPTR( mem );
ASSERT( (p->mh_alloc == ISALLOC), p );
if( _trace_file ) {
p->mh_file = _trace_file;
p->mh_line = _trace_line;
_trace_file = (const char*)0;
_trace_line = 0;
} else {
p->mh_file = file;
p->mh_line = (_int4)line;
}
return( mem );
}
/*
* Traced malloc defined as a macro in xmem.h
*/
_XIMPORT void *
_xmalloc_trace_malloc( size_t size, const char *file, int line )
{
return( _xmalloc_tag_block( _xmalloc( size ), file, line ) );
}
/*
* Traced calloc defined as a macro in xmem.h
*/
_XIMPORT void *
_xmalloc_trace_calloc( size_t nelem, size_t size, const char *file, int line )
{
return( _xmalloc_tag_block( _xcalloc( nelem, size ), file, line ) );
}
/*
* Traced realloc defined as a macro in xmem.h
*/
_XIMPORT void *
_xmalloc_trace_realloc( void *mem, size_t size, const char *file, int line )
{
return( _xmalloc_tag_block( _xrealloc( mem, size ), file, line ) );
}
/*
* Add a new allocated node in nextused array. The node is
* always inserted on top. Take care of the doubly linked-list.
*/
static void
xmalloc_add_node( PMHEAD ptr, int index )
{
PMHEAD p = nextused[ index ];
ptr->mh_next = p;
ptr->mh_prev = 0;
ptr->mh_pad1 = (_byte)0;
if( p )
p->mh_prev = ptr;
nextused[ index ] = ptr;
}
/*
* Remove an allocated node from nextused array. The node may
* be everywhere in the doubly linked-list. To prevent bug if
* the node is deleted twice, the link must be broken.
*/
static void
xmalloc_remove_node( PMHEAD ptr, int index )
{
PMHEAD prev = ptr->mh_prev;
PMHEAD next = ptr->mh_next;
if( prev )
prev->mh_next = next;
else nextused[ index ] = next;
if( next )
next->mh_prev = prev;
ptr->mh_next = 0;
ptr->mh_prev = 0;
ptr->mh_pad1 = (_byte)0;
}
/*
* Tag all allocated blocks so that when we find an untagged
* block, it's a leak candidate.
*/
static void
xmalloc_tag_used( void )
{
int i;
for( i = 0 ; i < MAXBLOCK ; ++i ) {
PMHEAD p;
for( p = nextused[i] ; p ; p = p->mh_next )
p->mh_pad1 = (_byte)1;
}
}
/*
* Find untagged block and display file and line origine.
*/
static void
xmalloc_find_untagged( void )
{
static const char unknown[] = "'unknown module' ";
PMHEAD p;
int i;
for( i = 0 ; i < MAXBLOCK ; ++i )
for( p = nextused[i] ; p ; p = p->mh_next )
if( p->mh_pad1 == (_byte)0 ) {
INSURE_LOG;
if( p->mh_file )
(void)fprintf(
_logfd,
"%9d bytes leaks from %s(%d)\n\r",
p->mh_nbytes,
p->mh_file,
p->mh_line
);
else (void)fprintf(
_logfd,
"%8d bytes leaks from %s\n\r",
p->mh_nbytes,
unknown
);
}
}
#endif /* _XALLOC_TRACE */
/*
* get a block of n bytes.
*/
_XIMPORT void *
_xmalloc( size_t n )
{
PMHEAD p;
size_t nbytes;
size_t shiftr;
int nunits;
#if defined( _XALLOC_RANGE_CHECK )
char *m;
#endif /* _XALLOC_RANGE_CHECK */
/*
* Figure out how many bytes are required, rounding up
* to the nearest multiple of _XALLOC_ALIGN, then
* figure out which nestf[] area to use. Both the
* beginning of the header and the beginning of the
* block should be on an eight byte boundary.
*/
nbytes = (size_t)ALIGN(n + HEADSIZE + EXTRA);
shiftr = (nbytes - 1) >> 2;
/*
* Find the bucket by shifting right until 0.
*/
for( nunits = 0 ; (shiftr >>= 1) != 0 ; ++nunits )
continue;
/*
* If there are no blocks of the appropriate size, go
* get some
*/
if( nextfree[ nunits ] == 0 )
if( morecore( nunits ) == -1 )
return( NULL );
/*
* Get one block off the list, and set the new list head
*/
if( (p = nextfree[ nunits ]) == 0 )
return( NULL );
nextfree[ nunits ] = CHAIN( p );
/*
* Check for free block clobbered. If not for this
* check, we would gobble a clobbered free chain ptr
* and bomb out on the NEXT allocate of this size block
*/
if( p->mh_alloc != ISFREE || p->mh_index != (_byte)nunits ) {
XALLOC_ERROR( "_xmalloc: block on free list clobbered", p );
return( 0 );
}
/*
* Fill in the info, and if range checking, set up the
* magic numbers
*/
p->mh_alloc = ISALLOC;
#if defined( _XALLOC_TYPE_CHECK )
p->mh_type = ALLOC_MALLOC;
#endif /* _XALLOC_TYPE_CHECK */
#if defined( _XALLOC_RANGE_CHECK )
p->mh_nbytes = n;
p->mh_magic4 = MAGIC4;
/*
* Get the location n after the beginning of the user's
* space.
*/
m = (char *)p + HEADSIZE + n;
*m++ = MAGIC1;
*m++ = MAGIC1;
*m++ = MAGIC1;
*m = MAGIC1;
#else /* _XALLOC_RANGE_CHECK */
p->mh_size = n;
#endif /* _XALLOC_RANGE_CHECK */
#if defined( _XALLOC_TRACE )
p->mh_file = (const char*)0;
p->mh_line = 0;
#endif /* _XALLOC_TRACE */
#if defined( _XALLOC_STATS )
nmalloc[ nunits ]++;
allocount++;
#endif /* _XALLOC_STATS */
#if defined( _XALLOC_TRACE )
xmalloc_add_node( p, nunits );
#endif /* _XALLOC_TRACE */
#if defined( _XALLOC_DEBUG ) && defined( _CHECK_ALIGN )
if( !ISALIGN( (void *)((char *)p + HEADSIZE)) )
xabort();
#endif /* _XALLOC_DEBUG && _CHECK_ALIGN */
return( (void *)((char *)p + HEADSIZE) );
}
/*
* Get a cleared block.
*/
_XIMPORT void *
_xcalloc( size_t nelem, size_t size )
{
void* mem = _xmalloc( size *= nelem );
#if defined( _BSD )
(void)bzero( mem, size );
#else /* _BSD */
(void)memset( mem, 0, size );
#endif /* _BSD */
return( mem );
}
_XIMPORT void
_xfree( void *mem )
{
PMHEAD p;
char *ap = (char *)mem;
int nunits;
if( ap == NULL )
return;
p = HEADPTR( ap );
#if defined( _XALLOC_TYPE_CHECK )
ASSERT( (p->mh_type == ALLOC_MALLOC), p );
#endif /* _XALLOC_TYPE_CHECK */
#if !defined( _XALLOC_RANGE_CHECK )
if( p->mh_alloc != ISALLOC )
xabort();
#else /* _XALLOC_RANGE_CHECK */
if( p->mh_alloc != ISALLOC ) {
if( p->mh_alloc == ISFREE )
XALLOC_ERROR("xfree: block on free list clobbered", p);
else XALLOC_ERROR("xfree: invalid block",p);
return;
}
/*
* check if user has written before it's address space
*/
ASSERT( (p->mh_magic4 == MAGIC4), p );
/*
* check if user has written after it's address space
*/
ap += p->mh_nbytes;
ASSERT( (*ap++ == MAGIC1), p );
ASSERT( (*ap++ == MAGIC1), p );
ASSERT( (*ap++ == MAGIC1), p );
ASSERT( (*ap == MAGIC1), p );
#endif /* _XALLOC_RANGE_CHECK */
nunits = (int)(p->mh_index);
ASSERT( (nunits < MAXBLOCK), p );
p->mh_alloc = ISFREE;
/*
* Put this block on the free list.
*/
CHAIN( p ) = nextfree[ nunits ];
nextfree[nunits] = p;
#if defined( _XALLOC_TRACE )
xmalloc_remove_node( p, nunits );
#endif /* _XALLOC_TRACE */
#if defined( _XALLOC_STATS )
nmalloc[ nunits ]--;
freecount++;
#endif /* _XALLOC_STATS */
}
_XIMPORT void *
_xrealloc( void *mem, size_t n )
{
PMHEAD p;
size_t tocopy;
size_t nbytes;
size_t nunits;
char *newmem;
if( mem == NULL )
return( _xmalloc( n ) );
if( n == 0 ) {
_xfree( mem );
return( NULL );
}
p = HEADPTR( mem );
nunits = (size_t)(p->mh_index);
ASSERT( (p->mh_alloc == ISALLOC), p );
#if defined( _XALLOC_RANGE_CHECK )
newmem = (char *)mem + (tocopy = p->mh_nbytes);
ASSERT( (p->mh_magic4 == MAGIC4), p );
ASSERT( (*newmem++ == MAGIC1), p );
ASSERT( (*newmem++ == MAGIC1), p );
ASSERT( (*newmem++ == MAGIC1), p );
ASSERT( (*newmem == MAGIC1), p );
#else /* _XALLOC_RANGE_CHECK */
if( p->mh_index >= 13 )
/*
* 64 k or more
*/
tocopy = (size_t)((1<<(int)(p->mh_index+MINBLOCK))-HEADSIZE);
else tocopy = p->mh_size;
#endif /* _XALLOC_RANGE_CHECK */
/*
* See if desired size rounds to same power of 2 as actual size.
*/
nbytes = (size_t)ALIGN( n + HEADSIZE + EXTRA );
/*
* If ok, use the same block, just marking its size as changed.
*/
if( (nbytes > ((unsigned)4<<nunits)) &&
(nbytes <= ((unsigned)8<<nunits)) ) {
#if defined( _XALLOC_RANGE_CHECK )
char *m;
m = (char *)mem + tocopy;
*m++ = 0;
*m++ = 0;
*m++ = 0;
*m = 0;
p->mh_nbytes = n;
m = (char *)mem + n;
*m++ = MAGIC1;
*m++ = MAGIC1;
*m++ = MAGIC1;
*m = MAGIC1;
#else /* _XALLOC_RANGE_CHECK */
p->mh_size = n;
#endif /* _XALLOC_RANGE_CHECK */
return( mem );
}
if( n < tocopy )
tocopy = n;
if( (newmem = (char *)_xmalloc( n ) ) == NULL )
return( 0 );
#if defined( _BSD )
(void)bcopy( mem, newmem, tocopy );
#else /* _BSD */
(void)memcpy( newmem, mem, tocopy );
#endif /* _BSD */
_xfree( mem );
return( newmem );
}
#if defined( _XALLOC_VALLOC )
_XIMPORT void *
xvalloc( size_t i );
{
int valsiz = getpagesize();
int j;
void *cp = malloc( i + (valsiz-1) );
j = ((int)cp + (valsiz-1)) &~ (valsiz-1);
return( (void *)j );
}
#endif /* _VALLOC */
#if defined( _XALLOC_STATS )
/*
* Return statistics describing allocation of blocks of size
* 2**n.
*/
struct xmalloc_stats {
unsigned long blocksize;
unsigned long nfree;
unsigned long nused;
};
typedef struct xmalloc_stats XMALLOC_STATS;
/*
* Compute the stats for block N.
*/
static XMALLOC_STATS xmalloc_stats( int size );
static XMALLOC_STATS
xmalloc_stats( int size )
{
XMALLOC_STATS v;
PMHEAD p;
if( size < 0 || size >= MAXBLOCK ) {
v.blocksize = 0L;
v.nused = 0L;
v.nfree = 0L;
return( v );
}
v.nfree = 0;
v.blocksize = 1 << (size + MINBLOCK);
v.nused = nmalloc[ size ];
for( p = nextfree[ size ] ; p ; p = CHAIN( p ) )
v.nfree++;
return( v );
}
_XIMPORT void
xmalloc_stats_print()
{
XMALLOC_STATS v;
int i;
INSURE_LOG;
(void)fprintf(_logfd," | Size | Free | Used |\n\r");
for( i = 0 ; i < (MAXBLOCK-1) ; i++ ) {
v = xmalloc_stats( i );
if( i < 10 || v.nfree != 0L || v.nused != 0L )
(void)fprintf(
_logfd,
"Block %2d | %8lu | %8ld | %8ld |\n\r",
i,
v.blocksize,
v.nfree,
v.nused
);
}
(void)fprintf(
_logfd,
"\n%d bytes used (%d alloc / %d free).\n\r",
xmalloc_mem_used(),
allocount,
freecount
);
}
_XIMPORT long
xmalloc_mem_used()
{
int i;
long size_used = 0L;
for( i = 0 ; i < MAXBLOCK ; ++i ) {
int allocation_size = (1 << (i + MINBLOCK));
size_used += nmalloc[ i ] * allocation_size;
}
return( size_used );
}
_XIMPORT long
xmalloc_mem_free( void )
{
int i;
long size_unused = 0L;
for( i = 0; i < MAXBLOCK ; i++ ) {
int allocation_size = (1 << (i + MINBLOCK));
PMHEAD p;
for( p = nextfree[ i ] ; p ; p = CHAIN( p ) )
size_unused += allocation_size;
}
return( size_unused );
}
_XIMPORT void
xmalloc_set_log( const char *file )
{
INSURE_LOG;
if( _logfd != stderr ) {
(void)fflush( _logfd );
(void)fclose( _logfd );
}
if( file != (const char *)NULL )
_logfd = fopen( file, "w" );
else _logfd = (FILE*)NULL;
INSURE_LOG;
}
_XIMPORT void
xmalloc_set_logfd( FILE *fd )
{
_logfd = fd;
}
static unsigned long _memory_saved; /* last saved memory state */
static unsigned long _alloc_saved; /* last allocation state */
static unsigned long _free_saved; /* last free state */
static const char* _file_saved; /* last file state */
static int _line_saved; /* last line state */
_XIMPORT void
_xmalloc_save_state( const char* file, int line )
{
#if defined( _XALLOC_TRACE )
xmalloc_tag_used();
#endif /* _XALLOC_TRACE */
_memory_saved = xmalloc_mem_used();
_alloc_saved = allocount;
_free_saved = freecount;
_file_saved = file;
_line_saved = line;
}
_XIMPORT void
_xmalloc_check_leaks( const char *file, int line )
{
unsigned long diffs = xmalloc_mem_used() - _memory_saved;
if( diffs ) {
INSURE_LOG;
(void)fprintf(
_logfd,
"LEAKS: %s(%d) found %lu bytes leaks",
file,
line,
diffs
);
(void)fprintf(
_logfd,
" from saved state %s(%d).\n\r",
_file_saved,
_line_saved
);
#if defined( _XALLOC_TRACE )
xmalloc_find_untagged();
#endif /* _XALLOC_TRACE */
(void)fprintf(
_logfd,
" in %lu alloc / %lu free.\n\r\n\r",
allocount - _alloc_saved,
freecount - _free_saved
);
(void)fflush( _logfd ); /* just in case user buffered it */
}
}
#endif /* _XALLOC_STATS */
/*
* Real memory request. This is system dependant.
*/
#if (defined(unix) || defined(__GNUC__) || defined(__SC__)) && !defined(_SYSGETMEM)
#define _SYSGETMEM
#if defined( __SC__ )
#include <unistd.h>
#endif /* __SC__ */
#if defined( _MUST_DECLARE_SBRK )
#if defined( __cplusplus )
extern "C" void *sbrk( int incr );
#else /* __cplusplus */
extern void *sbrk( int incr );
#endif /* __cplusplus */
#endif /* _MUST_DECLARE_SBRK */
static void *
sysgetmem( size_t n )
{
void *p = sbrk( n + _XALLOC_ALIGN );
if( p == (char *)-1 )
return( NULL );
return( (void *)ALIGN( p ) );
}
#endif /* unix || __SC__ */
#if (defined( _WIN32 ) || defined( _WIN64 )) && !defined( _SYSGETMEM )
#define _SYSGETMEM
static HANDLE _allocHeap;
#define INITIAL_SIZE 0x4000 /* 16k bytes */
static void *
sysgetmem( size_t n )
{
HANDLE hMem;
DWORD memFlag;
if( _allocHeap == 0 ) {
_allocHeap = HeapCreate( 0, INITIAL_SIZE, 0 );
if( _allocHeap == 0 )
return( NULL );
}
memFlag = HEAP_ZERO_MEMORY;
hMem = HeapAlloc( _allocHeap, memFlag, (DWORD)n + _XALLOC_ALIGN );
if( hMem == (HANDLE)0 )
return( NULL );
return( (void *)ALIGN( hMem ) );
}
#endif /* _WIN32 || _WIN64 */
#if defined( _WINDOWS ) && !defined( _SYSGETMEM )
#define _SYSGETMEM
static HANDLE _allocHeap;
static void *
sysgetmem( size_t n )
{
HANDLE hMem = GlobalAlloc( GMEM_FIXED, (DWORD)n + _XALLOC_ALIGN );
if( hMem == (HANDLE)0 )
return( NULL );
return( (void *)ALIGN( GlobalLock( hMem ) ) );
}
#endif /* _WINDOWS */
#if !defined( _SYSGETMEM )
#define _SYSGETMEM
static void *
sysgetmem( size_t n )
{
void *p = malloc( n );
/*
* Assumes that real malloc is correctly aligned.
*/
return( p );
}
#endif /* _SYSGETMEM */
#if defined( __cplusplus )
}
#endif /* __cplusplus */
/*
* C++ new/delete
*/
#if defined( __cplusplus )
#if defined( new )
#undef new /* the real new please !! */
#endif /* new */
#if !defined( _HAVE_NEW_HANDLER )
typedef void (_CCALL *new_handler)();
#endif /* _HAVE_NEW_HANDLER */
new_handler __new_handler;
#if defined( _HAVE_NAMESPACE )
namespace std {}
using namespace std;
#endif /* _HAVE_NAMESPACE */
#if defined( _HAVE_NOTHROW_TYPE )
#if !defined( __GNUC__ )
static nothrow_t empty_nothrow;
extern _XIMPORT const nothrow_t nothrow = empty_nothrow;
#endif /* __GNUC__ */
#endif /* _HAVE_NOTHROW_TYPE */
_XIMPORT extern new_handler set_new_handler( new_handler new_p ) THROW_ALL;
static void* __raw_alloc( size_t size ) THROW_ALL;
#if !defined( _HAVE_LIBRARY_HANDLER )
_XIMPORT new_handler
set_new_handler( new_handler new_p ) THROW_ALL
{
new_handler old_handler = __new_handler;
__new_handler = new_p;
return( old_handler );
}
#endif /* _HAVE_LIBRARY_HANDLER */
static void*
__raw_alloc( size_t size ) THROW_ALL
{
void* ptr;
if( size == 0 )
size++;
for( ;; ) {
ptr = _xmalloc( size );
if( ptr != 0 || __new_handler == 0 )
break;
(*__new_handler)();
}
return( ptr );
}
void* _CCALL
operator new( size_t size ) THROW_BAD_ALLOC
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
void* ptr = __raw_alloc( size );
trace( "new", size, ptr );
#if defined( _HAVE_EXCEPTIONS )
if( ptr == 0 )
throw bad_alloc();
#endif /* _HAVE_EXCEPTIONS */
#if defined( _XALLOC_TYPE_CHECK )
p = HEADPTR( ptr );
p->mh_type = ALLOC_NEW_NOARRAY;
#endif /* _XALLOC_TYPE_CHECK */
#if defined( _XALLOC_TRACE )
_xmalloc_tag_block( ptr, 0, 0 );
#endif /* _XALLOC_TRACE */
return( ptr );
}
#if defined( _HAVE_NOTHROW_TYPE )
void* _CCALL
operator new( size_t size, const nothrow_t& )
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
void* ptr = __raw_alloc( size );
trace( "new (nothrow)", size, ptr );
if( ptr == 0 )
return 0;
#if defined( _XALLOC_TYPE_CHECK )
p = HEADPTR( ptr );
p->mh_type = ALLOC_NEW_NOARRAY;
#endif /* _XALLOC_TYPE_CHECK */
#if defined( _XALLOC_TRACE )
_xmalloc_tag_block( ptr, 0, 0 );
#endif /* _XALLOC_TRACE */
return( ptr );
}
void* _CCALL
operator new[]( size_t size, const nothrow_t& )
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
void* ptr = __raw_alloc( size );
trace( "new(nothrow&)[]", size, ptr );
#if defined( _XALLOC_TYPE_CHECK )
p = (PMHEAD)((char *)ptr - HEADSIZE);
p->mh_type = ALLOC_NEW_ARRAY;
#endif /* _XALLOC_TYPE_CHECK */
#if defined( _XALLOC_TRACE )
_xmalloc_tag_block( ptr, 0, 0 );
#endif /* _XALLOC_TRACE */
return( ptr );
}
#endif /* _HAVE_NOTHROW_TYPE */
void _CCALL
operator delete( void* ptr ) THROW_ALL
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
trace( "delete", -1, ptr );
if( ptr ) {
#if defined( _XALLOC_TYPE_CHECK )
p = (PMHEAD)((char *)ptr - HEADSIZE);
ASSERT( (p->mh_type == ALLOC_NEW_NOARRAY), p );
p->mh_type = ALLOC_MALLOC;
#endif /* _XALLOC_TYPE_CHECK */
_xfree( ptr );
}
}
#if defined( _HAVE_ARRAY_OPERATOR )
void* _CCALL
operator new[]( size_t size ) THROW_BAD_ALLOC
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
void* ptr = __raw_alloc( size );
trace( "new[]", size, ptr );
#if defined( _HAVE_EXCEPTIONS )
if( ptr == 0 )
throw bad_alloc();
#endif /* _HAVE_EXCEPTIONS */
#if defined( _XALLOC_TYPE_CHECK )
p = (PMHEAD)((char *)ptr - HEADSIZE);
p->mh_type = ALLOC_NEW_ARRAY;
#endif /* _XALLOC_TYPE_CHECK */
#if defined( _XALLOC_TRACE )
_xmalloc_tag_block( ptr, 0, 0 );
#endif /* _XALLOC_TRACE */
return( ptr );
}
void _CCALL
operator delete[]( void* ptr ) THROW_ALL
{
#if defined( _XALLOC_TYPE_CHECK )
PMHEAD p;
#endif /* _XALLOC_TYPE_CHECK */
trace( "delete[]", -1, ptr );
if( ptr ) {
#if defined( _XALLOC_TYPE_CHECK )
p = (PMHEAD)((char *)ptr - HEADSIZE);
ASSERT( (p->mh_type == ALLOC_NEW_ARRAY), p );
p->mh_type = ALLOC_MALLOC;
#endif /* _XALLOC_TYPE_CHECK */
_xfree( ptr );
}
}
#endif /* _HAVE_ARRAY_OPERATOR */
#if defined( _XALLOC_TRACE )
/*
* C++ checked new and delete operators
*/
void* _CCALL
operator new( size_t size, const char *file, int line ) THROW_BAD_ALLOC
{
return( _xmalloc_tag_block( operator new( size ), file, line ) );
}
#if defined( _HAVE_ARRAY_OPERATOR )
void* _CCALL
operator new[]( size_t size, const char *file, int line ) THROW_BAD_ALLOC
{
return( _xmalloc_tag_block( operator new[]( size ), file, line ) );
}
#endif /* _HAVE_ARRAY_OPERATOR */
#endif /* _XALLOC_TRACE */
#endif /* __cplusplus */
#endif /* _XALLOC_DEFAULT */