Fixed memory leaks on iterator open/close Also zero out some memory defensively.

This commit is contained in:
Sears Russell 2008-03-01 19:32:34 +00:00
parent 4c83e1c373
commit f05b0233a3
5 changed files with 28 additions and 29 deletions

View file

@ -50,6 +50,8 @@ compensated_function recordid ThashCreate(int xid, int keySize, int valueSize) {
recordid hashHeader; recordid hashHeader;
lladd_hash_header lhh; lladd_hash_header lhh;
memset(&lhh,0,sizeof(lhh));
try_ret(NULLRID) { try_ret(NULLRID) {
hashHeader = Talloc(xid, sizeof(lladd_hash_header)); hashHeader = Talloc(xid, sizeof(lladd_hash_header));
if(keySize == VARIABLE_LENGTH || valueSize == VARIABLE_LENGTH) { if(keySize == VARIABLE_LENGTH || valueSize == VARIABLE_LENGTH) {
@ -168,7 +170,7 @@ Operation getLinearHashRemove() {
compensated_function int ThashInsert(int xid, recordid hashHeader, const byte* key, int keySize, const byte* value, int valueSize) { compensated_function int ThashInsert(int xid, recordid hashHeader, const byte* key, int keySize, const byte* value, int valueSize) {
pthread_mutex_lock(&linear_hash_mutex); pthread_mutex_lock(&linear_hash_mutex);
int argSize = sizeof(linearHash_insert_arg)+keySize; int argSize = sizeof(linearHash_insert_arg)+keySize;
linearHash_insert_arg * arg = malloc(argSize); linearHash_insert_arg * arg = calloc(1,argSize);
arg->hashHeader = hashHeader; arg->hashHeader = hashHeader;
arg->keySize = keySize; arg->keySize = keySize;
memcpy(arg+1, key, keySize); memcpy(arg+1, key, keySize);
@ -259,7 +261,7 @@ compensated_function int ThashRemove(int xid, recordid hashHeader, const byte *
begin_action_ret(pthread_mutex_unlock, &linear_hash_mutex, compensation_error()) { begin_action_ret(pthread_mutex_unlock, &linear_hash_mutex, compensation_error()) {
int argSize = sizeof(linearHash_remove_arg) + keySize + valueSize; int argSize = sizeof(linearHash_remove_arg) + keySize + valueSize;
linearHash_remove_arg * arg = malloc(argSize); linearHash_remove_arg * arg = calloc(1,argSize);
arg->hashHeader = hashHeader; arg->hashHeader = hashHeader;
arg->keySize = keySize; arg->keySize = keySize;
arg->valueSize = valueSize; arg->valueSize = valueSize;
@ -396,7 +398,7 @@ compensated_function static void ThashSplitBucket(int xid, recordid hashHeader,
return; return;
} }
lladd_hash_iterator * ThashIterator(int xid, recordid hashHeader, int keySize, int valueSize) { lladd_hash_iterator * ThashIterator(int xid, recordid hashHeader, int keySize, int valueSize) {
lladd_hash_iterator * it = malloc(sizeof(lladd_hash_iterator)); lladd_hash_iterator * it = calloc(1,sizeof(lladd_hash_iterator));
begin_action_ret(free, it, NULL) { begin_action_ret(free, it, NULL) {
it->hashHeader = hashHeader; it->hashHeader = hashHeader;
lladd_hash_header lhh; lladd_hash_header lhh;
@ -434,9 +436,11 @@ int ThashNext(int xid, lladd_hash_iterator * it, byte ** key, int * keySize, byt
if(compensation_error()) { return 0; } if(compensation_error()) { return 0; }
it->bucket.slot++; it->bucket.slot++;
if(it->bucket.slot < it->numBuckets) { if(it->bucket.slot < it->numBuckets) {
TlinkedListClose(xid, it->it);
it->it = TlinkedListIterator(xid, it->bucket, it->keySize, it->valueSize); it->it = TlinkedListIterator(xid, it->bucket, it->keySize, it->valueSize);
} else { } else {
free(it); TlinkedListClose(xid, it->it);
it->it = 0;
return 0; return 0;
} }
} }
@ -448,9 +452,11 @@ int ThashNext(int xid, lladd_hash_iterator * it, byte ** key, int * keySize, byt
if(it->bucket.slot < it->numBuckets) { if(it->bucket.slot < it->numBuckets) {
recordid bucketList; recordid bucketList;
Tread(xid, it->bucket, &bucketList); Tread(xid, it->bucket, &bucketList);
TpagedListClose(xid,it->pit);
it->pit = TpagedListIterator(xid, bucketList); it->pit = TpagedListIterator(xid, bucketList);
} else { } else {
free(it); TpagedListClose(xid,it->pit);
it->pit = 0;
return 0; return 0;
} }
} }
@ -461,10 +467,10 @@ int ThashNext(int xid, lladd_hash_iterator * it, byte ** key, int * keySize, byt
void ThashDone(int xid, lladd_hash_iterator * it) { void ThashDone(int xid, lladd_hash_iterator * it) {
if(it->it) { if(it->it) {
free(it->it); TlinkedListClose(xid, it->it);
} }
if(it->pit) { if(it->pit) {
free(it->pit); TpagedListClose(xid, it->pit);
} }
free(it); free(it);
} }
@ -493,9 +499,8 @@ lladdIterator_t * ThashGenericIterator(int xid, recordid hash) {
void linearHashNTAIterator_close(int xid, void * impl) { void linearHashNTAIterator_close(int xid, void * impl) {
lladd_linearHashNTA_generic_it * it = impl; lladd_linearHashNTA_generic_it * it = impl;
if(it->lastKey || it->lastValue) { ThashDone(xid, it->hit);
ThashDone(xid, it->hit); // otherwise, ThashNext returned zero, and freed it for us...
}
if(it->lastKey) { if(it->lastKey) {
free(it->lastKey); free(it->lastKey);
} }

View file

@ -394,7 +394,9 @@ compensated_function lladd_linkedList_iterator * TlinkedListIterator(int xid, re
it->listRoot = list; it->listRoot = list;
return it; return it;
} }
void TlinkedListClose(int xid, lladd_linkedList_iterator * it) {
free(it);
}
compensated_function int TlinkedListNext(int xid, lladd_linkedList_iterator * it, byte ** key, int * keySize, byte **value, int * valueSize) { compensated_function int TlinkedListNext(int xid, lladd_linkedList_iterator * it, byte ** key, int * keySize, byte **value, int * valueSize) {
if(it->next.size == -1) { free(it); return 0; } if(it->next.size == -1) { free(it); return 0; }

View file

@ -15,6 +15,7 @@ compensated_function recordid TpagedListAlloc(int xid) {
try_ret(NULLRID) { try_ret(NULLRID) {
ret = Talloc(xid, sizeof(pagedListHeader)); ret = Talloc(xid, sizeof(pagedListHeader));
pagedListHeader header; pagedListHeader header;
memset(&header,0,sizeof(header));
header.thisPage = 0; header.thisPage = 0;
header.nextPage = NULLRID; header.nextPage = NULLRID;
Tset(xid, ret, &header); Tset(xid, ret, &header);
@ -30,12 +31,7 @@ compensated_function int TpagedListInsert(int xid, recordid list, const byte * k
recordid headerRid = list; recordid headerRid = list;
pagedListHeader firstHeader = header; pagedListHeader firstHeader = header;
/* byte * garbage;
ret = (TpagedListFind(xid, list, key, keySize, &garbage) != -1);
if(ret) {
free(garbage);
TpagedListRemove(xid, list, key, keySize);
} */
ret = 0; ret = 0;
int entrySize = sizeof(pagedListEntry) + keySize + valueSize; int entrySize = sizeof(pagedListEntry) + keySize + valueSize;
@ -47,19 +43,11 @@ compensated_function int TpagedListInsert(int xid, recordid list, const byte * k
while(rid.size == -1) { while(rid.size == -1) {
if(compensation_error()) { break; } if(compensation_error()) { break; }
if(header.nextPage.size == -1) { if(header.nextPage.size == -1) {
/* header.nextPage = Talloc(xid, sizeof(pagedListHeader));
DEBUG("allocing on new page %d\n", header.nextPage.page);
Tset(xid, headerRid, &header);
pagedListHeader newHead;
newHead.thisPage = 0;
newHead.nextPage.page =0;
newHead.nextPage.slot =0;
newHead.nextPage.size =-1;
Tset(xid, header.nextPage, &newHead); */
// We're at the end of the list // We're at the end of the list
recordid newHeadRid = Talloc(xid, sizeof(pagedListHeader)); recordid newHeadRid = Talloc(xid, sizeof(pagedListHeader));
pagedListHeader newHead; pagedListHeader newHead;
memset(&newHead,0,sizeof(newHead));
newHead.thisPage = 0; newHead.thisPage = 0;
newHead.nextPage = firstHeader.nextPage; newHead.nextPage = firstHeader.nextPage;
@ -227,7 +215,9 @@ compensated_function lladd_pagedList_iterator * TpagedListIterator(int xid, reco
return it; return it;
} }
void TpagedListClose(int xid, lladd_pagedList_iterator * it) {
free(it);
}
compensated_function int TpagedListNext(int xid, lladd_pagedList_iterator * it, compensated_function int TpagedListNext(int xid, lladd_pagedList_iterator * it,
byte ** key, int * keySize, byte ** key, int * keySize,
byte ** value, int * valueSize) { byte ** value, int * valueSize) {

View file

@ -40,6 +40,7 @@ compensated_function int TlinkedListMove(int xid, recordid start_list, recordid
@return a new iterator initialized to the head of the list. */ @return a new iterator initialized to the head of the list. */
compensated_function lladd_linkedList_iterator * TlinkedListIterator(int xid, recordid list, int keySize, int valueSize); compensated_function lladd_linkedList_iterator * TlinkedListIterator(int xid, recordid list, int keySize, int valueSize);
void TlinkedListClose(int xid, lladd_linkedList_iterator * it);
/** @return 1 if there was another entry to be iterated over. 0 otherwise. /** @return 1 if there was another entry to be iterated over. 0 otherwise.
If this function returns 1, the caller must free() the malloced memory If this function returns 1, the caller must free() the malloced memory
returned via the key and value arguments.*/ returned via the key and value arguments.*/

View file

@ -100,6 +100,7 @@ compensated_function int TpagedListMove(int xid, recordid start_list, recordid e
@return a new iterator initialized to the head of the list. */ @return a new iterator initialized to the head of the list. */
lladd_pagedList_iterator * TpagedListIterator(int xid, recordid list); lladd_pagedList_iterator * TpagedListIterator(int xid, recordid list);
void TpagedListClose(int xid, lladd_pagedList_iterator *it);
/** @return 1 if there was another entry to be iterated over. 0 otherwise. /** @return 1 if there was another entry to be iterated over. 0 otherwise.
If this function returns 1, the caller must free() the malloced memory If this function returns 1, the caller must free() the malloced memory
returned via the key and value arguments.*/ returned via the key and value arguments.*/