Skip to content

Commit

Permalink
Replace a few sqlite3_malloc()+memset() sequences with calls to sqlit…
Browse files Browse the repository at this point in the history
…e3MallocZero().
  • Loading branch information
danielk-1977 committed Jul 30, 2012
1 parent c742e7e commit fd7b919
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,11 @@ static void stat3Init(
nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
mxSample = sqlite3_value_int(argv[1]);
n = sizeof(*p) + sizeof(p->a[0])*mxSample;
p = sqlite3_malloc( n );
p = sqlite3MallocZero( n );
if( p==0 ){
sqlite3_result_error_nomem(context);
return;
}
memset(p, 0, n);
p->a = (struct Stat3Sample*)&p[1];
p->nRow = nRow;
p->mxSample = mxSample;
Expand Down
3 changes: 1 addition & 2 deletions src/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,14 @@ sqlite3_backup *sqlite3_backup_init(
** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
** call to sqlite3_backup_init() and is destroyed by a call to
** sqlite3_backup_finish(). */
p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
if( !p ){
sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
}
}

/* If the allocation succeeded, populate the new object. */
if( p ){
memset(p, 0, sizeof(sqlite3_backup));
p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
p->pDestDb = pDestDb;
Expand Down
3 changes: 1 addition & 2 deletions src/bitvec.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,9 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){
/* Allocate the Bitvec to be tested and a linear array of
** bits to act as the reference */
pBitvec = sqlite3BitvecCreate( sz );
pV = sqlite3_malloc( (sz+7)/8 + 1 );
pV = sqlite3MallocZero( (sz+7)/8 + 1 );
pTmpSpace = sqlite3_malloc(BITVEC_SZ);
if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
memset(pV, 0, (sz+7)/8 + 1);

/* NULL pBitvec tests */
sqlite3BitvecSet(0, 1);
Expand Down
3 changes: 1 addition & 2 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ static int rehash(Hash *pH, unsigned int new_size){
** allocation as a benign.
*/
sqlite3BeginBenignMalloc();
new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) );
sqlite3EndBenignMalloc();

if( new_ht==0 ) return 0;
sqlite3_free(pH->ht);
pH->ht = new_ht;
pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
memset(new_ht, 0, new_size*sizeof(struct _ht));
for(elem=pH->first, pH->first=0; elem; elem = next_elem){
unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
next_elem = elem->next;
Expand Down
6 changes: 2 additions & 4 deletions src/pcache1.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,10 @@ static int pcache1ResizeHash(PCache1 *p){

pcache1LeaveMutex(p->pGroup);
if( p->nHash ){ sqlite3BeginBenignMalloc(); }
apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
if( p->nHash ){ sqlite3EndBenignMalloc(); }
pcache1EnterMutex(p->pGroup);
if( apNew ){
memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
for(i=0; i<p->nHash; i++){
PgHdr1 *pPage;
PgHdr1 *pNext = p->apHash[i];
Expand Down Expand Up @@ -584,9 +583,8 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
assert( szExtra < 300 );

sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
pCache = (PCache1 *)sqlite3_malloc(sz);
pCache = (PCache1 *)sqlite3MallocZero(sz);
if( pCache ){
memset(pCache, 0, sz);
if( separateCache ){
pGroup = (PGroup*)&pCache[1];
pGroup->mxPinned = 10;
Expand Down
3 changes: 1 addition & 2 deletions src/vdbetrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ void sqlite3ExplainBegin(Vdbe *pVdbe){
if( pVdbe ){
Explain *p;
sqlite3BeginBenignMalloc();
p = sqlite3_malloc( sizeof(Explain) );
p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
if( p ){
memset(p, 0, sizeof(*p));
p->pVdbe = pVdbe;
sqlite3_free(pVdbe->pExplain);
pVdbe->pExplain = p;
Expand Down

0 comments on commit fd7b919

Please sign in to comment.