f970f8e2fd81d08901048b2070054dd71ed7f1f1 angie Tue Apr 9 11:47:57 2013 -0700 Use localmem for annoRow storage, instead of many independent malloc & free calls. refs #6152 diff --git src/lib/annoRow.c src/lib/annoRow.c index f2f38ae..920e5bc 100644 --- src/lib/annoRow.c +++ src/lib/annoRow.c @@ -1,122 +1,70 @@ /* annoRow -- basic data interchange unit of annoGratorQuery framework. */ #include "annoRow.h" -#include "annoStreamer.h" struct annoRow *annoRowFromStringArray(char *chrom, uint start, uint end, boolean rightJoinFail, - char **wordsIn, int numCols) + char **wordsIn, int numCols, struct lm *lm) /* Allocate & return an annoRow with words cloned from wordsIn. */ { struct annoRow *aRow; -AllocVar(aRow); -aRow->chrom = cloneString(chrom); +lmAllocVar(lm, aRow); +aRow->chrom = lmCloneString(lm, chrom); aRow->start = start; aRow->end = end; aRow->rightJoinFail = rightJoinFail; char **words; -AllocArray(words, numCols); +lmAllocArray(lm, words, numCols); int i; for (i = 0; i < numCols; i++) - words[i] = cloneString(wordsIn[i]); + words[i] = lmCloneString(lm, wordsIn[i]); aRow->data = words; return aRow; } struct annoRow *annoRowWigNew(char *chrom, uint start, uint end, boolean rightJoinFail, - float *values) + float *values, struct lm *lm) /* Allocate & return an annoRowWig, with clone of values; length of values is (end-start). */ { struct annoRow *row; -AllocVar(row); -row->chrom = cloneString(chrom); +lmAllocVar(lm, row); +row->chrom = lmCloneString(lm, chrom); row->start = start; row->end = end; -row->data = cloneMem(values, (end - start) * sizeof(values[0])); +row->data = lmCloneMem(lm, values, (end - start) * sizeof(values[0])); row->rightJoinFail = rightJoinFail; return row; } -struct annoRow *annoRowClone(struct annoRow *rowIn, struct annoStreamer *source) -/* Allocate & return a single annoRow cloned from rowIn. */ +struct annoRow *annoRowClone(struct annoRow *rowIn, enum annoRowType rowType, int numCols, + struct lm *lm) +/* Allocate & return a single annoRow cloned from rowIn. If rowIn is NULL, return NULL. + * If type is arWig, numCols is ignored. */ { if (rowIn == NULL) return NULL; -if (source->rowType == arWords || source->rowType == arVcf) +if (rowType == arWords || rowType == arVcf) return annoRowFromStringArray(rowIn->chrom, rowIn->start, rowIn->end, rowIn->rightJoinFail, - rowIn->data, source->numCols); -else if (source->rowType == arWig) + rowIn->data, numCols, lm); +else if (rowType == arWig) return annoRowWigNew(rowIn->chrom, rowIn->start, rowIn->end, rowIn->rightJoinFail, - (float *)rowIn->data); + (float *)rowIn->data, lm); else - errAbort("annoRowClone: unrecognized type %d", source->rowType); + errAbort("annoRowClone: unrecognized type %d", rowType); return NULL; } -static void annoRowWordsFree(struct annoRow **pRow, int numCols) -/* Free a single annoRow with type arWords. */ -{ -if (pRow == NULL) - return; -struct annoRow *row = *pRow; -freeMem(row->chrom); -char **words = row->data; -int i; -for (i = 0; i < numCols; i++) - freeMem(words[i]); -freeMem(row->data); -freez(pRow); -} - -static void annoRowWigFree(struct annoRow **pRow) -/* Free a single annoRow with type arWig. */ -{ -if (pRow == NULL) - return; -struct annoRow *row = *pRow; -freeMem(row->chrom); -freeMem(row->data); -freez(pRow); -} - -void annoRowFree(struct annoRow **pRow, struct annoStreamer *source) -/* Free a single annoRow. */ -{ -if (pRow == NULL) - return; -if (source->rowType == arWords || source->rowType == arVcf) - annoRowWordsFree(pRow, source->numCols); -else if (source->rowType == arWig) - annoRowWigFree(pRow); -else - errAbort("annoRowFree: unrecognized type %d", source->rowType); -} - -void annoRowFreeList(struct annoRow **pList, struct annoStreamer *source) -/* Free a list of annoRows. */ -{ -if (pList == NULL) - return; -struct annoRow *row, *nextRow; -for (row = *pList; row != NULL; row = nextRow) - { - nextRow = row->next; - annoRowFree(&row, source); - } -*pList = NULL; -} - int annoRowCmp(const void *va, const void *vb) /* Compare two annoRows' {chrom, start, end}. */ { struct annoRow *rowA = *((struct annoRow **)va); struct annoRow *rowB = *((struct annoRow **)vb); int dif = strcmp(rowA->chrom, rowB->chrom); if (dif == 0) { dif = rowA->start - rowB->start; if (dif == 0) dif = rowA->end - rowB->end; } return dif; }