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;
lladd_hash_header lhh;
memset(&lhh,0,sizeof(lhh));
try_ret(NULLRID) {
hashHeader = Talloc(xid, sizeof(lladd_hash_header));
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) {
pthread_mutex_lock(&linear_hash_mutex);
int argSize = sizeof(linearHash_insert_arg)+keySize;
linearHash_insert_arg * arg = malloc(argSize);
linearHash_insert_arg * arg = calloc(1,argSize);
arg->hashHeader = hashHeader;
arg->keySize = 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()) {
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->keySize = keySize;
arg->valueSize = valueSize;
@ -396,7 +398,7 @@ compensated_function static void ThashSplitBucket(int xid, recordid hashHeader,
return;
}
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) {
it->hashHeader = hashHeader;
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; }
it->bucket.slot++;
if(it->bucket.slot < it->numBuckets) {
TlinkedListClose(xid, it->it);
it->it = TlinkedListIterator(xid, it->bucket, it->keySize, it->valueSize);
} else {
free(it);
TlinkedListClose(xid, it->it);
it->it = 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) {
recordid bucketList;
Tread(xid, it->bucket, &bucketList);
TpagedListClose(xid,it->pit);
it->pit = TpagedListIterator(xid, bucketList);
} else {
free(it);
TpagedListClose(xid,it->pit);
it->pit = 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) {
if(it->it) {
free(it->it);
TlinkedListClose(xid, it->it);
}
if(it->pit) {
free(it->pit);
TpagedListClose(xid, it->pit);
}
free(it);
}
@ -493,9 +499,8 @@ lladdIterator_t * ThashGenericIterator(int xid, recordid hash) {
void linearHashNTAIterator_close(int xid, void * impl) {
lladd_linearHashNTA_generic_it * it = impl;
if(it->lastKey || it->lastValue) {
ThashDone(xid, it->hit); // otherwise, ThashNext returned zero, and freed it for us...
}
ThashDone(xid, it->hit);
if(it->lastKey) {
free(it->lastKey);
}

View file

@ -394,7 +394,9 @@ compensated_function lladd_linkedList_iterator * TlinkedListIterator(int xid, re
it->listRoot = list;
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) {
if(it->next.size == -1) { free(it); return 0; }

View file

@ -15,6 +15,7 @@ compensated_function recordid TpagedListAlloc(int xid) {
try_ret(NULLRID) {
ret = Talloc(xid, sizeof(pagedListHeader));
pagedListHeader header;
memset(&header,0,sizeof(header));
header.thisPage = 0;
header.nextPage = NULLRID;
Tset(xid, ret, &header);
@ -30,12 +31,7 @@ compensated_function int TpagedListInsert(int xid, recordid list, const byte * k
recordid headerRid = list;
pagedListHeader firstHeader = header;
/* byte * garbage;
ret = (TpagedListFind(xid, list, key, keySize, &garbage) != -1);
if(ret) {
free(garbage);
TpagedListRemove(xid, list, key, keySize);
} */
ret = 0;
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) {
if(compensation_error()) { break; }
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
recordid newHeadRid = Talloc(xid, sizeof(pagedListHeader));
pagedListHeader newHead;
memset(&newHead,0,sizeof(newHead));
newHead.thisPage = 0;
newHead.nextPage = firstHeader.nextPage;
@ -227,7 +215,9 @@ compensated_function lladd_pagedList_iterator * TpagedListIterator(int xid, reco
return it;
}
void TpagedListClose(int xid, lladd_pagedList_iterator * it) {
free(it);
}
compensated_function int TpagedListNext(int xid, lladd_pagedList_iterator * it,
byte ** key, int * keySize,
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. */
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.
If this function returns 1, the caller must free() the malloced memory
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. */
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.
If this function returns 1, the caller must free() the malloced memory
returned via the key and value arguments.*/