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 #include "postgres.h"
00066
00067 #include "utils/memutils.h"
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #define ALLOC_MINBITS 3
00099 #define ALLOCSET_NUM_FREELISTS 11
00100 #define ALLOC_CHUNK_LIMIT (1 << (ALLOCSET_NUM_FREELISTS-1+ALLOC_MINBITS))
00101
00102 #define ALLOC_CHUNK_FRACTION 4
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 #define ALLOC_BLOCKHDRSZ MAXALIGN(sizeof(AllocBlockData))
00117 #define ALLOC_CHUNKHDRSZ MAXALIGN(sizeof(AllocChunkData))
00118
00119 typedef struct AllocBlockData *AllocBlock;
00120 typedef struct AllocChunkData *AllocChunk;
00121
00122
00123
00124
00125
00126 typedef void *AllocPointer;
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 typedef struct AllocSetContext
00138 {
00139 MemoryContextData header;
00140
00141 AllocBlock blocks;
00142 AllocChunk freelist[ALLOCSET_NUM_FREELISTS];
00143
00144 Size initBlockSize;
00145 Size maxBlockSize;
00146 Size nextBlockSize;
00147 Size allocChunkLimit;
00148 AllocBlock keeper;
00149 } AllocSetContext;
00150
00151 typedef AllocSetContext *AllocSet;
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 typedef struct AllocBlockData
00166 {
00167 AllocSet aset;
00168 AllocBlock next;
00169 char *freeptr;
00170 char *endptr;
00171 } AllocBlockData;
00172
00173
00174
00175
00176
00177
00178
00179 typedef struct AllocChunkData
00180 {
00181
00182 void *aset;
00183
00184 Size size;
00185 #ifdef MEMORY_CONTEXT_CHECKING
00186
00187
00188 Size requested_size;
00189 #endif
00190 } AllocChunkData;
00191
00192
00193
00194
00195
00196 #define AllocPointerIsValid(pointer) PointerIsValid(pointer)
00197
00198
00199
00200
00201
00202 #define AllocSetIsValid(set) PointerIsValid(set)
00203
00204 #define AllocPointerGetChunk(ptr) \
00205 ((AllocChunk)(((char *)(ptr)) - ALLOC_CHUNKHDRSZ))
00206 #define AllocChunkGetPointer(chk) \
00207 ((AllocPointer)(((char *)(chk)) + ALLOC_CHUNKHDRSZ))
00208
00209
00210
00211
00212 static void *AllocSetAlloc(MemoryContext context, Size size);
00213 static void AllocSetFree(MemoryContext context, void *pointer);
00214 static void *AllocSetRealloc(MemoryContext context, void *pointer, Size size);
00215 static void AllocSetInit(MemoryContext context);
00216 static void AllocSetReset(MemoryContext context);
00217 static void AllocSetDelete(MemoryContext context);
00218 static Size AllocSetGetChunkSpace(MemoryContext context, void *pointer);
00219 static bool AllocSetIsEmpty(MemoryContext context);
00220 static void AllocSetStats(MemoryContext context, int level);
00221
00222 #ifdef MEMORY_CONTEXT_CHECKING
00223 static void AllocSetCheck(MemoryContext context);
00224 #endif
00225
00226
00227
00228
00229 static MemoryContextMethods AllocSetMethods = {
00230 AllocSetAlloc,
00231 AllocSetFree,
00232 AllocSetRealloc,
00233 AllocSetInit,
00234 AllocSetReset,
00235 AllocSetDelete,
00236 AllocSetGetChunkSpace,
00237 AllocSetIsEmpty,
00238 AllocSetStats
00239 #ifdef MEMORY_CONTEXT_CHECKING
00240 ,AllocSetCheck
00241 #endif
00242 };
00243
00244
00245
00246
00247 #define LT16(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
00248
00249 static const unsigned char LogTable256[256] =
00250 {
00251 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
00252 LT16(5), LT16(6), LT16(6), LT16(7), LT16(7), LT16(7), LT16(7),
00253 LT16(8), LT16(8), LT16(8), LT16(8), LT16(8), LT16(8), LT16(8), LT16(8)
00254 };
00255
00256
00257
00258
00259
00260 #ifdef HAVE_ALLOCINFO
00261 #define AllocFreeInfo(_cxt, _chunk) \
00262 fprintf(stderr, "AllocFree: %s: %p, %d\n", \
00263 (_cxt)->header.name, (_chunk), (_chunk)->size)
00264 #define AllocAllocInfo(_cxt, _chunk) \
00265 fprintf(stderr, "AllocAlloc: %s: %p, %d\n", \
00266 (_cxt)->header.name, (_chunk), (_chunk)->size)
00267 #else
00268 #define AllocFreeInfo(_cxt, _chunk)
00269 #define AllocAllocInfo(_cxt, _chunk)
00270 #endif
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 static inline int
00281 AllocSetFreeIndex(Size size)
00282 {
00283 int idx;
00284 unsigned int t,
00285 tsize;
00286
00287 if (size > (1 << ALLOC_MINBITS))
00288 {
00289 tsize = (size - 1) >> ALLOC_MINBITS;
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 t = tsize >> 8;
00301 idx = t ? LogTable256[t] + 8 : LogTable256[tsize];
00302
00303 Assert(idx < ALLOCSET_NUM_FREELISTS);
00304 }
00305 else
00306 idx = 0;
00307
00308 return idx;
00309 }
00310
00311 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00312
00313
00314
00315
00316
00317
00318
00319 static void
00320 randomize_mem(char *ptr, size_t size)
00321 {
00322 static int save_ctr = 1;
00323 int ctr;
00324
00325 ctr = save_ctr;
00326 while (size-- > 0)
00327 {
00328 *ptr++ = ctr;
00329 if (++ctr > 251)
00330 ctr = 1;
00331 }
00332 save_ctr = ctr;
00333 }
00334 #endif
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 MemoryContext
00353 AllocSetContextCreate(MemoryContext parent,
00354 const char *name,
00355 Size minContextSize,
00356 Size initBlockSize,
00357 Size maxBlockSize)
00358 {
00359 AllocSet context;
00360
00361
00362 context = (AllocSet) MemoryContextCreate(T_AllocSetContext,
00363 sizeof(AllocSetContext),
00364 &AllocSetMethods,
00365 parent,
00366 name);
00367
00368
00369
00370
00371
00372
00373 initBlockSize = MAXALIGN(initBlockSize);
00374 if (initBlockSize < 1024)
00375 initBlockSize = 1024;
00376 maxBlockSize = MAXALIGN(maxBlockSize);
00377 if (maxBlockSize < initBlockSize)
00378 maxBlockSize = initBlockSize;
00379 context->initBlockSize = initBlockSize;
00380 context->maxBlockSize = maxBlockSize;
00381 context->nextBlockSize = initBlockSize;
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 context->allocChunkLimit = ALLOC_CHUNK_LIMIT;
00398 while ((Size) (context->allocChunkLimit + ALLOC_CHUNKHDRSZ) >
00399 (Size) ((maxBlockSize - ALLOC_BLOCKHDRSZ) / ALLOC_CHUNK_FRACTION))
00400 context->allocChunkLimit >>= 1;
00401
00402
00403
00404
00405 if (minContextSize > ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ)
00406 {
00407 Size blksize = MAXALIGN(minContextSize);
00408 AllocBlock block;
00409
00410 block = (AllocBlock) malloc(blksize);
00411 if (block == NULL)
00412 {
00413 MemoryContextStats(TopMemoryContext);
00414 ereport(ERROR,
00415 (errcode(ERRCODE_OUT_OF_MEMORY),
00416 errmsg("out of memory"),
00417 errdetail("Failed while creating memory context \"%s\".",
00418 name)));
00419 }
00420 block->aset = context;
00421 block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
00422 block->endptr = ((char *) block) + blksize;
00423 block->next = context->blocks;
00424 context->blocks = block;
00425
00426 context->keeper = block;
00427 }
00428
00429 return (MemoryContext) context;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 static void
00445 AllocSetInit(MemoryContext context)
00446 {
00447
00448
00449
00450
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 static void
00465 AllocSetReset(MemoryContext context)
00466 {
00467 AllocSet set = (AllocSet) context;
00468 AllocBlock block;
00469
00470 AssertArg(AllocSetIsValid(set));
00471
00472 #ifdef MEMORY_CONTEXT_CHECKING
00473
00474 AllocSetCheck(context);
00475 #endif
00476
00477
00478 MemSetAligned(set->freelist, 0, sizeof(set->freelist));
00479
00480 block = set->blocks;
00481
00482
00483 set->blocks = set->keeper;
00484
00485 while (block != NULL)
00486 {
00487 AllocBlock next = block->next;
00488
00489 if (block == set->keeper)
00490 {
00491
00492 char *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ;
00493
00494 #ifdef CLOBBER_FREED_MEMORY
00495
00496 memset(datastart, 0x7F, block->freeptr - datastart);
00497 #endif
00498 block->freeptr = datastart;
00499 block->next = NULL;
00500 }
00501 else
00502 {
00503
00504 #ifdef CLOBBER_FREED_MEMORY
00505
00506 memset(block, 0x7F, block->freeptr - ((char *) block));
00507 #endif
00508 free(block);
00509 }
00510 block = next;
00511 }
00512
00513
00514 set->nextBlockSize = set->initBlockSize;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 static void
00526 AllocSetDelete(MemoryContext context)
00527 {
00528 AllocSet set = (AllocSet) context;
00529 AllocBlock block = set->blocks;
00530
00531 AssertArg(AllocSetIsValid(set));
00532
00533 #ifdef MEMORY_CONTEXT_CHECKING
00534
00535 AllocSetCheck(context);
00536 #endif
00537
00538
00539 MemSetAligned(set->freelist, 0, sizeof(set->freelist));
00540 set->blocks = NULL;
00541 set->keeper = NULL;
00542
00543 while (block != NULL)
00544 {
00545 AllocBlock next = block->next;
00546
00547 #ifdef CLOBBER_FREED_MEMORY
00548
00549 memset(block, 0x7F, block->freeptr - ((char *) block));
00550 #endif
00551 free(block);
00552 block = next;
00553 }
00554 }
00555
00556
00557
00558
00559
00560
00561 static void *
00562 AllocSetAlloc(MemoryContext context, Size size)
00563 {
00564 AllocSet set = (AllocSet) context;
00565 AllocBlock block;
00566 AllocChunk chunk;
00567 int fidx;
00568 Size chunk_size;
00569 Size blksize;
00570
00571 AssertArg(AllocSetIsValid(set));
00572
00573
00574
00575
00576
00577 if (size > set->allocChunkLimit)
00578 {
00579 chunk_size = MAXALIGN(size);
00580 blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
00581 block = (AllocBlock) malloc(blksize);
00582 if (block == NULL)
00583 {
00584 MemoryContextStats(TopMemoryContext);
00585 ereport(ERROR,
00586 (errcode(ERRCODE_OUT_OF_MEMORY),
00587 errmsg("out of memory"),
00588 errdetail("Failed on request of size %lu.",
00589 (unsigned long) size)));
00590 }
00591 block->aset = set;
00592 block->freeptr = block->endptr = ((char *) block) + blksize;
00593
00594 chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
00595 chunk->aset = set;
00596 chunk->size = chunk_size;
00597 #ifdef MEMORY_CONTEXT_CHECKING
00598 chunk->requested_size = size;
00599
00600 if (size < chunk_size)
00601 ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
00602 #endif
00603 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00604
00605 randomize_mem((char *) AllocChunkGetPointer(chunk), size);
00606 #endif
00607
00608
00609
00610
00611
00612 if (set->blocks != NULL)
00613 {
00614 block->next = set->blocks->next;
00615 set->blocks->next = block;
00616 }
00617 else
00618 {
00619 block->next = NULL;
00620 set->blocks = block;
00621 }
00622
00623 AllocAllocInfo(set, chunk);
00624 return AllocChunkGetPointer(chunk);
00625 }
00626
00627
00628
00629
00630
00631
00632
00633 fidx = AllocSetFreeIndex(size);
00634 chunk = set->freelist[fidx];
00635 if (chunk != NULL)
00636 {
00637 Assert(chunk->size >= size);
00638
00639 set->freelist[fidx] = (AllocChunk) chunk->aset;
00640
00641 chunk->aset = (void *) set;
00642
00643 #ifdef MEMORY_CONTEXT_CHECKING
00644 chunk->requested_size = size;
00645
00646 if (size < chunk->size)
00647 ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
00648 #endif
00649 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00650
00651 randomize_mem((char *) AllocChunkGetPointer(chunk), size);
00652 #endif
00653
00654 AllocAllocInfo(set, chunk);
00655 return AllocChunkGetPointer(chunk);
00656 }
00657
00658
00659
00660
00661 chunk_size = (1 << ALLOC_MINBITS) << fidx;
00662 Assert(chunk_size >= size);
00663
00664
00665
00666
00667
00668 if ((block = set->blocks) != NULL)
00669 {
00670 Size availspace = block->endptr - block->freeptr;
00671
00672 if (availspace < (chunk_size + ALLOC_CHUNKHDRSZ))
00673 {
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 while (availspace >= ((1 << ALLOC_MINBITS) + ALLOC_CHUNKHDRSZ))
00687 {
00688 Size availchunk = availspace - ALLOC_CHUNKHDRSZ;
00689 int a_fidx = AllocSetFreeIndex(availchunk);
00690
00691
00692
00693
00694
00695
00696 if (availchunk != ((Size) 1 << (a_fidx + ALLOC_MINBITS)))
00697 {
00698 a_fidx--;
00699 Assert(a_fidx >= 0);
00700 availchunk = ((Size) 1 << (a_fidx + ALLOC_MINBITS));
00701 }
00702
00703 chunk = (AllocChunk) (block->freeptr);
00704
00705 block->freeptr += (availchunk + ALLOC_CHUNKHDRSZ);
00706 availspace -= (availchunk + ALLOC_CHUNKHDRSZ);
00707
00708 chunk->size = availchunk;
00709 #ifdef MEMORY_CONTEXT_CHECKING
00710 chunk->requested_size = 0;
00711 #endif
00712 chunk->aset = (void *) set->freelist[a_fidx];
00713 set->freelist[a_fidx] = chunk;
00714 }
00715
00716
00717 block = NULL;
00718 }
00719 }
00720
00721
00722
00723
00724 if (block == NULL)
00725 {
00726 Size required_size;
00727
00728
00729
00730
00731
00732 blksize = set->nextBlockSize;
00733 set->nextBlockSize <<= 1;
00734 if (set->nextBlockSize > set->maxBlockSize)
00735 set->nextBlockSize = set->maxBlockSize;
00736
00737
00738
00739
00740
00741 required_size = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
00742 while (blksize < required_size)
00743 blksize <<= 1;
00744
00745
00746 block = (AllocBlock) malloc(blksize);
00747
00748
00749
00750
00751
00752 while (block == NULL && blksize > 1024 * 1024)
00753 {
00754 blksize >>= 1;
00755 if (blksize < required_size)
00756 break;
00757 block = (AllocBlock) malloc(blksize);
00758 }
00759
00760 if (block == NULL)
00761 {
00762 MemoryContextStats(TopMemoryContext);
00763 ereport(ERROR,
00764 (errcode(ERRCODE_OUT_OF_MEMORY),
00765 errmsg("out of memory"),
00766 errdetail("Failed on request of size %lu.",
00767 (unsigned long) size)));
00768 }
00769
00770 block->aset = set;
00771 block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
00772 block->endptr = ((char *) block) + blksize;
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 if (set->keeper == NULL && blksize == set->initBlockSize)
00784 set->keeper = block;
00785
00786 block->next = set->blocks;
00787 set->blocks = block;
00788 }
00789
00790
00791
00792
00793 chunk = (AllocChunk) (block->freeptr);
00794
00795 block->freeptr += (chunk_size + ALLOC_CHUNKHDRSZ);
00796 Assert(block->freeptr <= block->endptr);
00797
00798 chunk->aset = (void *) set;
00799 chunk->size = chunk_size;
00800 #ifdef MEMORY_CONTEXT_CHECKING
00801 chunk->requested_size = size;
00802
00803 if (size < chunk->size)
00804 ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
00805 #endif
00806 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00807
00808 randomize_mem((char *) AllocChunkGetPointer(chunk), size);
00809 #endif
00810
00811 AllocAllocInfo(set, chunk);
00812 return AllocChunkGetPointer(chunk);
00813 }
00814
00815
00816
00817
00818
00819 static void
00820 AllocSetFree(MemoryContext context, void *pointer)
00821 {
00822 AllocSet set = (AllocSet) context;
00823 AllocChunk chunk = AllocPointerGetChunk(pointer);
00824
00825 AllocFreeInfo(set, chunk);
00826
00827 #ifdef MEMORY_CONTEXT_CHECKING
00828
00829 if (chunk->requested_size < chunk->size)
00830 if (((char *) pointer)[chunk->requested_size] != 0x7E)
00831 elog(WARNING, "detected write past chunk end in %s %p",
00832 set->header.name, chunk);
00833 #endif
00834
00835 if (chunk->size > set->allocChunkLimit)
00836 {
00837
00838
00839
00840
00841 AllocBlock block = set->blocks;
00842 AllocBlock prevblock = NULL;
00843
00844 while (block != NULL)
00845 {
00846 if (chunk == (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ))
00847 break;
00848 prevblock = block;
00849 block = block->next;
00850 }
00851 if (block == NULL)
00852 elog(ERROR, "could not find block containing chunk %p", chunk);
00853
00854 Assert(block->freeptr == ((char *) block) +
00855 (chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ));
00856
00857
00858 if (prevblock == NULL)
00859 set->blocks = block->next;
00860 else
00861 prevblock->next = block->next;
00862 #ifdef CLOBBER_FREED_MEMORY
00863
00864 memset(block, 0x7F, block->freeptr - ((char *) block));
00865 #endif
00866 free(block);
00867 }
00868 else
00869 {
00870
00871 int fidx = AllocSetFreeIndex(chunk->size);
00872
00873 chunk->aset = (void *) set->freelist[fidx];
00874
00875 #ifdef CLOBBER_FREED_MEMORY
00876
00877 memset(pointer, 0x7F, chunk->size);
00878 #endif
00879
00880 #ifdef MEMORY_CONTEXT_CHECKING
00881
00882 chunk->requested_size = 0;
00883 #endif
00884 set->freelist[fidx] = chunk;
00885 }
00886 }
00887
00888
00889
00890
00891
00892
00893
00894 static void *
00895 AllocSetRealloc(MemoryContext context, void *pointer, Size size)
00896 {
00897 AllocSet set = (AllocSet) context;
00898 AllocChunk chunk = AllocPointerGetChunk(pointer);
00899 Size oldsize = chunk->size;
00900
00901 #ifdef MEMORY_CONTEXT_CHECKING
00902
00903 if (chunk->requested_size < oldsize)
00904 if (((char *) pointer)[chunk->requested_size] != 0x7E)
00905 elog(WARNING, "detected write past chunk end in %s %p",
00906 set->header.name, chunk);
00907 #endif
00908
00909
00910
00911
00912
00913
00914 if (oldsize >= size)
00915 {
00916 #ifdef MEMORY_CONTEXT_CHECKING
00917 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00918
00919 if (size > chunk->requested_size)
00920 randomize_mem((char *) AllocChunkGetPointer(chunk) + chunk->requested_size,
00921 size - chunk->requested_size);
00922 #endif
00923
00924 chunk->requested_size = size;
00925
00926 if (size < oldsize)
00927 ((char *) pointer)[size] = 0x7E;
00928 #endif
00929 return pointer;
00930 }
00931
00932 if (oldsize > set->allocChunkLimit)
00933 {
00934
00935
00936
00937
00938
00939 AllocBlock block = set->blocks;
00940 AllocBlock prevblock = NULL;
00941 Size chksize;
00942 Size blksize;
00943
00944 while (block != NULL)
00945 {
00946 if (chunk == (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ))
00947 break;
00948 prevblock = block;
00949 block = block->next;
00950 }
00951 if (block == NULL)
00952 elog(ERROR, "could not find block containing chunk %p", chunk);
00953
00954 Assert(block->freeptr == ((char *) block) +
00955 (chunk->size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ));
00956
00957
00958 chksize = MAXALIGN(size);
00959 blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
00960 block = (AllocBlock) realloc(block, blksize);
00961 if (block == NULL)
00962 {
00963 MemoryContextStats(TopMemoryContext);
00964 ereport(ERROR,
00965 (errcode(ERRCODE_OUT_OF_MEMORY),
00966 errmsg("out of memory"),
00967 errdetail("Failed on request of size %lu.",
00968 (unsigned long) size)));
00969 }
00970 block->freeptr = block->endptr = ((char *) block) + blksize;
00971
00972
00973 chunk = (AllocChunk) (((char *) block) + ALLOC_BLOCKHDRSZ);
00974 if (prevblock == NULL)
00975 set->blocks = block;
00976 else
00977 prevblock->next = block;
00978 chunk->size = chksize;
00979
00980 #ifdef MEMORY_CONTEXT_CHECKING
00981 #ifdef RANDOMIZE_ALLOCATED_MEMORY
00982
00983 randomize_mem((char *) AllocChunkGetPointer(chunk) + chunk->requested_size,
00984 size - chunk->requested_size);
00985 #endif
00986
00987 chunk->requested_size = size;
00988
00989 if (size < chunk->size)
00990 ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
00991 #endif
00992
00993 return AllocChunkGetPointer(chunk);
00994 }
00995 else
00996 {
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 AllocPointer newPointer;
01009
01010
01011 newPointer = AllocSetAlloc((MemoryContext) set, size);
01012
01013
01014 memcpy(newPointer, pointer, oldsize);
01015
01016
01017 AllocSetFree((MemoryContext) set, pointer);
01018
01019 return newPointer;
01020 }
01021 }
01022
01023
01024
01025
01026
01027
01028 static Size
01029 AllocSetGetChunkSpace(MemoryContext context, void *pointer)
01030 {
01031 AllocChunk chunk = AllocPointerGetChunk(pointer);
01032
01033 return chunk->size + ALLOC_CHUNKHDRSZ;
01034 }
01035
01036
01037
01038
01039
01040 static bool
01041 AllocSetIsEmpty(MemoryContext context)
01042 {
01043
01044
01045
01046
01047
01048
01049 if (context->isReset)
01050 return true;
01051 return false;
01052 }
01053
01054
01055
01056
01057
01058 static void
01059 AllocSetStats(MemoryContext context, int level)
01060 {
01061 AllocSet set = (AllocSet) context;
01062 long nblocks = 0;
01063 long nchunks = 0;
01064 long totalspace = 0;
01065 long freespace = 0;
01066 AllocBlock block;
01067 AllocChunk chunk;
01068 int fidx;
01069 int i;
01070
01071 for (block = set->blocks; block != NULL; block = block->next)
01072 {
01073 nblocks++;
01074 totalspace += block->endptr - ((char *) block);
01075 freespace += block->endptr - block->freeptr;
01076 }
01077 for (fidx = 0; fidx < ALLOCSET_NUM_FREELISTS; fidx++)
01078 {
01079 for (chunk = set->freelist[fidx]; chunk != NULL;
01080 chunk = (AllocChunk) chunk->aset)
01081 {
01082 nchunks++;
01083 freespace += chunk->size + ALLOC_CHUNKHDRSZ;
01084 }
01085 }
01086
01087 for (i = 0; i < level; i++)
01088 fprintf(stderr, " ");
01089
01090 fprintf(stderr,
01091 "%s: %lu total in %ld blocks; %lu free (%ld chunks); %lu used\n",
01092 set->header.name, totalspace, nblocks, freespace, nchunks,
01093 totalspace - freespace);
01094 }
01095
01096
01097 #ifdef MEMORY_CONTEXT_CHECKING
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107 static void
01108 AllocSetCheck(MemoryContext context)
01109 {
01110 AllocSet set = (AllocSet) context;
01111 char *name = set->header.name;
01112 AllocBlock block;
01113
01114 for (block = set->blocks; block != NULL; block = block->next)
01115 {
01116 char *bpoz = ((char *) block) + ALLOC_BLOCKHDRSZ;
01117 long blk_used = block->freeptr - bpoz;
01118 long blk_data = 0;
01119 long nchunks = 0;
01120
01121
01122
01123
01124 if (!blk_used)
01125 {
01126 if (set->keeper != block)
01127 elog(WARNING, "problem in alloc set %s: empty block %p",
01128 name, block);
01129 }
01130
01131
01132
01133
01134 while (bpoz < block->freeptr)
01135 {
01136 AllocChunk chunk = (AllocChunk) bpoz;
01137 Size chsize,
01138 dsize;
01139 char *chdata_end;
01140
01141 chsize = chunk->size;
01142 dsize = chunk->requested_size;
01143 chdata_end = ((char *) chunk) + (ALLOC_CHUNKHDRSZ + dsize);
01144
01145
01146
01147
01148 if (dsize > chsize)
01149 elog(WARNING, "problem in alloc set %s: req size > alloc size for chunk %p in block %p",
01150 name, chunk, block);
01151 if (chsize < (1 << ALLOC_MINBITS))
01152 elog(WARNING, "problem in alloc set %s: bad size %lu for chunk %p in block %p",
01153 name, (unsigned long) chsize, chunk, block);
01154
01155
01156 if (chsize > set->allocChunkLimit &&
01157 chsize + ALLOC_CHUNKHDRSZ != blk_used)
01158 elog(WARNING, "problem in alloc set %s: bad single-chunk %p in block %p",
01159 name, chunk, block);
01160
01161
01162
01163
01164
01165
01166 if (dsize > 0 && chunk->aset != (void *) set)
01167 elog(WARNING, "problem in alloc set %s: bogus aset link in block %p, chunk %p",
01168 name, block, chunk);
01169
01170
01171
01172
01173 if (dsize > 0 && dsize < chsize && *chdata_end != 0x7E)
01174 elog(WARNING, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
01175 name, block, chunk);
01176
01177 blk_data += chsize;
01178 nchunks++;
01179
01180 bpoz += ALLOC_CHUNKHDRSZ + chsize;
01181 }
01182
01183 if ((blk_data + (nchunks * ALLOC_CHUNKHDRSZ)) != blk_used)
01184 elog(WARNING, "problem in alloc set %s: found inconsistent memory block %p",
01185 name, block);
01186 }
01187 }
01188
01189 #endif