Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add infrastructure to support a hierarchy of memory allocations with automatic deallocation of substructure. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | malloc-enhancement |
Files: | files | file ages | folders |
SHA1: |
48ef221c28ceaeb11427d9fe3049aa16 |
User & Date: | drh 2010-07-24 18:25:21.000 |
Context
2010-07-24
| ||
19:08 | Additional malloc sanity changes. Use sqlite3MemLink() on Index.zColAff and Table.zColAff as a proof of concept. (check-in: e5ecb15984 user: drh tags: malloc-enhancement) | |
18:25 | Add infrastructure to support a hierarchy of memory allocations with automatic deallocation of substructure. (check-in: 48ef221c28 user: drh tags: malloc-enhancement) | |
16:34 | Make sure all memory from sqlite3DbMalloc() is freed by sqlite3DbFree() and all memory from sqlite3_malloc() is freed by sqlite3_free(). (check-in: ac1f37a647 user: drh tags: malloc-enhancement) | |
Changes
Changes to src/malloc.c.
︙ | ︙ | |||
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 | ************************************************************************* ** ** Memory allocation functions used throughout sqlite. */ #include "sqliteInt.h" #include <stdarg.h> /* ** This routine runs when the memory allocator sees that the ** total memory allocation is about to exceed the soft heap ** limit. */ static void softHeapLimitEnforcer( void *NotUsed, sqlite3_int64 NotUsed2, int allocSize ){ UNUSED_PARAMETER2(NotUsed, NotUsed2); sqlite3_release_memory(allocSize); } /* ** Set the soft heap-size limit for the library. Passing a zero or ** negative value indicates no limit. */ void sqlite3_soft_heap_limit(int n){ sqlite3_uint64 iLimit; int overage; if( n<0 ){ iLimit = 0; }else{ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | ************************************************************************* ** ** Memory allocation functions used throughout sqlite. */ #include "sqliteInt.h" #include <stdarg.h> /* ** There are two general-purpose memory allocators: ** ** Simple: ** ** sqlite3_malloc ** sqlite3_free ** sqlite3_realloc ** sqlite3Malloc ** sqlite3MallocSize ** sqlite3_mprintf ** ** Enhanced: ** ** sqlite3DbMallocRaw ** sqlite3DbMallocZero ** sqlite3DbFree ** sqlite3DbRealloc ** sqlite3MPrintf ** sqlite3DbMalloc ** ** All external allocations use the simple memory allocator. ** The enhanced allocator is used internally only, and is not ** available to extensions or applications. ** ** The enhanced allocator is a wrapper around the simple allocator that ** adds the following capabilities: ** ** (1) Access to lookaside memory associated with a database connection. ** ** (2) The ability to link allocations into a hierarchy with automatic ** deallocation of all elements of the subhierarchy whenever any ** element within the hierarchy is deallocated. ** ** The two allocators are incompatible in the sense that allocations that ** originate from the simple allocator must be deallocated using the simple ** deallocator and allocations that originate from the enhanced allocator must ** be deallocated using the enhanced deallocator. You cannot check-out ** memory from one allocator then return it to the other. */ /* ** The automatic hierarchical deallocation feature of the enhanced allocator ** is implemented by adding an instance of the following structure to the ** header of each enhanced allocation. ** ** In order to preserve alignment, this structure must be a multiple of ** 8 bytes in size. */ typedef struct EMemHdr EMemHdr; struct EMemHdr { EMemHdr *pEChild; /* List of children of this node */ EMemHdr *pESibling; /* Other nodes that are children of the same parent */ #ifdef SQLITE_MEMDEBUG u32 iEMemMagic; /* Magic number for sanity checking */ u32 isAChild; /* True if this allocate is a child of another */ #endif }; /* ** Macros for querying and setting debugging fields of the EMemHdr object. */ #ifdef SQLITE_MEMDEBUG # define isValidEMem(E) ((E)->iEMemMagic==0xc0a43fad) # define setValidEMem(E) (E)->iEMemMagic = 0xc0a43fad # define clearValidEMem(E) (E)->iEMemMagic = 0x12345678 # define isChildEMem(E) ((E)->isAChild!=0) # define setChildEMem(E) (E)->isAChild = 1 # define clearChildEMem(E) (E)->isAChild = 0 #else # define isValidEMem(E) # define setValidEMem(E) # define clearValidEMem(E) # define isChildEMem(E) # define setChildEMem(E) # define clearChildEMem(E) #endif /* ** This routine runs when the memory allocator sees that the ** total memory allocation is about to exceed the soft heap ** limit. */ static void softHeapLimitEnforcer( void *NotUsed, sqlite3_int64 NotUsed2, int allocSize ){ UNUSED_PARAMETER2(NotUsed, NotUsed2); sqlite3_release_memory(allocSize); } /* ** Set the soft heap-size limit for the library. Passing a zero or ** negative value indicates no limit. ** ** If the total amount of memory allocated (by all threads) exceeds ** the soft heap limit, then sqlite3_release_memory() is invoked to ** try to free up some memory before proceeding. */ void sqlite3_soft_heap_limit(int n){ sqlite3_uint64 iLimit; int overage; if( n<0 ){ iLimit = 0; }else{ |
︙ | ︙ | |||
414 415 416 417 418 419 420 421 422 423 424 425 426 | #else #define isLookaside(A,B) 0 #endif /* ** Return the size of a memory allocation previously obtained from ** sqlite3Malloc() or sqlite3_malloc(). */ int sqlite3MallocSize(void *p){ assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); return sqlite3GlobalConfig.m.xSize(p); } | > > > | > > > > > | | | 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 | #else #define isLookaside(A,B) 0 #endif /* ** Return the size of a memory allocation previously obtained from ** sqlite3Malloc() or sqlite3_malloc(). ** ** The size returned is the usable size and does not include any ** bookkeeping overhead or sentinals at the end of the allocation. */ int sqlite3MallocSize(void *p){ assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); return sqlite3GlobalConfig.m.xSize(p); } int sqlite3DbMallocSize(sqlite3 *db, void *pObj){ EMemHdr *p = (EMemHdr*)pObj; assert( db==0 || sqlite3_mutex_held(db->mutex) ); if( p ){ p--; assert( isValidEMem(p) ); } if( isLookaside(db, p) ){ return db->lookaside.sz - sizeof(EMemHdr); }else{ assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); assert( sqlite3MemdebugHasType(p, db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); return sqlite3GlobalConfig.m.xSize(p) - sizeof(EMemHdr); } } /* ** Free memory previously obtained from sqlite3Malloc(). */ void sqlite3_free(void *p){ |
︙ | ︙ | |||
451 452 453 454 455 456 457 | }else{ sqlite3GlobalConfig.m.xFree(p); } } /* ** Free memory that might be associated with a particular database | | | > > > > > > | | > | | | | | | | | > | | > | | > | > > > | 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 | }else{ sqlite3GlobalConfig.m.xFree(p); } } /* ** Free memory that might be associated with a particular database ** connection. All child allocations are also freed. */ void sqlite3DbFree(sqlite3 *db, void *pObj){ EMemHdr *p = (EMemHdr*)pObj; assert( db==0 || sqlite3_mutex_held(db->mutex) ); if( p ) p--; while( p ){ EMemHdr *pNext = p->pESibling; assert( isValidEMem(p) ); if( p->pEChild ) sqlite3DbFree(db, (void*)&p->pEChild[1]); if( isLookaside(db, p) ){ LookasideSlot *pBuf = (LookasideSlot*)p; clearValidEMem(p); pBuf->pNext = db->lookaside.pFree; db->lookaside.pFree = pBuf; db->lookaside.nOut--; }else{ assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); assert( sqlite3MemdebugHasType(p, db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); sqlite3MemdebugSetType(p, MEMTYPE_HEAP); clearValidEMem(p); sqlite3_free(p); } p = pNext; } } /* ** Change the size of an existing memory allocation. ** ** This is the same as sqlite3_realloc() except that it assumes that ** the memory subsystem has already been initialized. */ void *sqlite3Realloc(void *pOld, int nBytes){ int nOld, nNew; void *pNew; if( pOld==0 ){ return sqlite3Malloc(nBytes); } |
︙ | ︙ | |||
569 570 571 572 573 574 575 | ** int *b = (int*)sqlite3DbMallocRaw(db, 200); ** if( b ) a[10] = 9; ** ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed ** that all prior mallocs (ex: "a") worked too. */ void *sqlite3DbMallocRaw(sqlite3 *db, int n){ | | > | > | | > > > > > | > > > > > > > | > | > > > | | | > | | | | | > > | | > | | 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 | ** int *b = (int*)sqlite3DbMallocRaw(db, 200); ** if( b ) a[10] = 9; ** ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed ** that all prior mallocs (ex: "a") worked too. */ void *sqlite3DbMallocRaw(sqlite3 *db, int n){ EMemHdr *p; assert( db==0 || sqlite3_mutex_held(db->mutex) ); n += sizeof(EMemHdr); #ifndef SQLITE_OMIT_LOOKASIDE if( db ){ LookasideSlot *pBuf; if( db->mallocFailed ){ return 0; } if( db->lookaside.bEnabled && n<=db->lookaside.sz && (pBuf = db->lookaside.pFree)!=0 ){ db->lookaside.pFree = pBuf->pNext; db->lookaside.nOut++; if( db->lookaside.nOut>db->lookaside.mxOut ){ db->lookaside.mxOut = db->lookaside.nOut; } p = (EMemHdr*)pBuf; goto finish_emalloc_raw; } } #else if( db && db->mallocFailed ){ return 0; } #endif p = sqlite3Malloc(n); if( !p ){ if( db ) db->mallocFailed = 1; return 0; } sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE | ((db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP)); finish_emalloc_raw: memset(p, 0, sizeof(EMemHdr)); setValidEMem(p); return (void*)&p[1]; } /* ** Resize the block of memory pointed to by p to n bytes. If the ** resize fails, set the mallocFailed flag in the connection object. ** ** The pOld memory block must not be linked into an allocation hierarchy ** as a child. It is OK for the allocation to be the root of a hierarchy ** of allocations; the only restriction is that there must be no other ** allocations above the pOld allocation in the hierarchy. To resize ** an allocation that is a child within a hierarchy, first ** unlink the allocation, resize it, then relink it. */ void *sqlite3DbRealloc(sqlite3 *db, void *pOld, int n){ EMemHdr *p = (EMemHdr*)pOld; EMemHdr *pNew = 0; assert( db!=0 ); assert( sqlite3_mutex_held(db->mutex) ); if( db->mallocFailed==0 ){ if( p==0 ){ return sqlite3DbMallocRaw(db, n); } p--; assert( isValidEMem(p) ); /* pOld obtained from extended allocator */ assert( !isChildEMem(p) ); /* pOld must not be a child allocation */ if( isLookaside(db, p) ){ if( n+sizeof(EMemHdr)<=db->lookaside.sz ){ return pOld; } pNew = sqlite3DbMallocRaw(db, n); if( pNew ){ memcpy(pNew-1, p, db->lookaside.sz); setValidEMem(pNew-1); sqlite3DbFree(db, pOld); } }else{ assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) ); assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); sqlite3MemdebugSetType(p, MEMTYPE_HEAP); pNew = sqlite3_realloc(p, n+sizeof(EMemHdr)); if( !pNew ){ sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE|MEMTYPE_HEAP); db->mallocFailed = 1; }else{ sqlite3MemdebugSetType(pNew, MEMTYPE_RECURSIVE | (db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP)); setValidEMem(pNew); pNew++; } } } return (void*)pNew; } /* ** Attempt to reallocate p. If the reallocation fails, then free p ** and set the mallocFailed flag in the database connection. */ void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ |
︙ | ︙ | |||
685 686 687 688 689 690 691 692 693 694 695 696 697 698 | zNew = sqlite3DbMallocRaw(db, n+1); if( zNew ){ memcpy(zNew, z, n); zNew[n] = 0; } return zNew; } /* ** Create a string from the zFromat argument and the va_list that follows. ** Store the string in memory obtained from sqliteMalloc() and make *pz ** point to that string. */ void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | zNew = sqlite3DbMallocRaw(db, n+1); if( zNew ){ memcpy(zNew, z, n); zNew[n] = 0; } return zNew; } /* ** Link extended allocation nodes such that deallocating the parent ** causes the child to be automatically deallocated. */ void sqlite3MemLink(void *pParentObj, void *pChildObj){ EMemHdr *pParent = (EMemHdr*)pParentObj; EMemHdr *pChild = (EMemHdr*)pChildObj; if( pParent && pChild ){ pParent--; assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */ pChild--; assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */ assert( !isChildEMem(pChild) ); /* pChildObj not a child of another obj */ pChild->pESibling = pParent->pEChild; pParent->pEChild = pChild; setChildEMem(pChild); } } /* ** pChildObj is a child object of pParentObj due to a prior call ** to sqlite3MemLink(). This routine breaks that linkage, making ** pChildObj an independent node that is not a child of any other node. */ void sqlite3MemUnlink(void *pParentObj, void *pChildObj){ EMemHdr *pParent = (EMemHdr*)pParentObj; EMemHdr *pChild = (EMemHdr*)pChildObj; EMemHdr **pp; assert( pParentObj!=0 ); assert( pChildObj!=0 ); pParent--; assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */ pChild--; assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */ assert( isChildEMem(pChild) ); /* pChildObj a child of something */ for(pp=&pParent->pEChild; (*pp)!=pChild; pp = &(*pp)->pESibling){ assert( *pp ); /* pChildObj is a child of pParentObj */ assert( isValidEMem(*pp) ); /* All children of pParentObj are valid */ assert( isChildEMem(*pp) ); /* All children of pParentObj are children */ } *pp = pChild->pESibling; pChild->pESibling = 0; clearChildEMem(pChild); } /* ** Create a string from the zFromat argument and the va_list that follows. ** Store the string in memory obtained from sqliteMalloc() and make *pz ** point to that string. */ void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ |
︙ | ︙ |
Changes to src/sqliteInt.h.
︙ | ︙ | |||
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 | void *sqlite3DbMallocRaw(sqlite3*, int); char *sqlite3DbStrDup(sqlite3*,const char*); char *sqlite3DbStrNDup(sqlite3*,const char*, int); void *sqlite3Realloc(void*, int); void *sqlite3DbReallocOrFree(sqlite3 *, void *, int); void *sqlite3DbRealloc(sqlite3 *, void *, int); void sqlite3DbFree(sqlite3*, void*); int sqlite3MallocSize(void*); int sqlite3DbMallocSize(sqlite3*, void*); void *sqlite3ScratchMalloc(int); void sqlite3ScratchFree(void*); void *sqlite3PageMalloc(int); void sqlite3PageFree(void*); void sqlite3MemSetDefault(void); | > > | 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 | void *sqlite3DbMallocRaw(sqlite3*, int); char *sqlite3DbStrDup(sqlite3*,const char*); char *sqlite3DbStrNDup(sqlite3*,const char*, int); void *sqlite3Realloc(void*, int); void *sqlite3DbReallocOrFree(sqlite3 *, void *, int); void *sqlite3DbRealloc(sqlite3 *, void *, int); void sqlite3DbFree(sqlite3*, void*); void sqlite3MemLink(void *pParent, void *pChild); void sqlite3MemUnlink(void *pParent, void *pChild); int sqlite3MallocSize(void*); int sqlite3DbMallocSize(sqlite3*, void*); void *sqlite3ScratchMalloc(int); void sqlite3ScratchFree(void*); void *sqlite3PageMalloc(int); void sqlite3PageFree(void*); void sqlite3MemSetDefault(void); |
︙ | ︙ |