MprApp *mprGetApp(MprCtx ptr) { MprBlk *bp; mprAssert(ptr); bp = GET_HDR(ptr); mprAssert(VALID_HDR(bp)); CHECK_HDR(bp); mprAssert(bp->app->magic == APP_MAGIC); return bp->app; }
void *mprGetAllocParent(MprCtx ptr) { MprBlk *bp; mprAssert(VALID_BLK(ptr)); if (ptr == 0) { return 0; } bp = GET_HDR(ptr); mprAssert(VALID_HDR(bp)); CHECK_HDR(bp); return GET_PTR(bp->parent); }
uint mprGetAllocBlockSize(MprCtx ptr) { MprBlk *bp; mprAssert(VALID_BLK(ptr)); if (ptr == 0) { return 0; } bp = GET_HDR(ptr); mprAssert(VALID_HDR(bp)); CHECK_HDR(bp); return bp->size; }
MprDestructor mprSetDestructor(MprCtx ptr, MprDestructor destructor) { MprDestructor old; MprBlk *bp; mprAssert(VALID_BLK(ptr)); if (ptr == 0) { return 0; } bp = GET_HDR(ptr); mprAssert(bp); mprAssert(VALID_HDR(bp)); mprAssert(ptr != mprGetAllocParent(ptr)); CHECK_HDR(bp); old = bp->destructor; bp->destructor = destructor; return old; }
int FileListModel::importFromCsv(QWidget *parent, const QString &inFile) { QFile file(inFile); if(!file.open(QIODevice::ReadOnly)) { return CsvError_FileOpen; } QTextCodec *codec = NULL; QByteArray bomCheck = file.peek(16); if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf")) { codec = QTextCodec::codecForName("UTF-8"); } else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe")) { codec = QTextCodec::codecForName("UTF-16LE"); } else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff")) { codec = QTextCodec::codecForName("UTF-16BE"); } else { const QString systemDefault = tr("(System Default)"); QStringList codecList; codecList.append(systemDefault); codecList.append(lamexp_available_codepages()); QInputDialog *input = new QInputDialog(parent); input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:"))); input->setOkButtonText(tr("OK")); input->setCancelButtonText(tr("Cancel")); input->setTextEchoMode(QLineEdit::Normal); input->setComboBoxItems(codecList); if(input->exec() < 1) { LAMEXP_DELETE(input); return CsvError_Aborted; } if(input->textValue().compare(systemDefault, Qt::CaseInsensitive)) { qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData()); codec = QTextCodec::codecForName(input->textValue().toLatin1().constData()); } else { qDebug("Going to use the system's default codec!"); codec = QTextCodec::codecForName("System"); } LAMEXP_DELETE(input); } bomCheck.clear(); //----------------------// QTextStream stream(&file); stream.setAutoDetectUnicode(false); stream.setCodec(codec); QString headerLine = stream.readLine().simplified(); while(headerLine.isEmpty()) { if(stream.atEnd()) { qWarning("The file appears to be empty!"); return CsvError_FileRead; } qWarning("Skipping a blank line at beginning of CSV file!"); headerLine = stream.readLine().simplified(); } QStringList header = headerLine.split(";", QString::KeepEmptyParts); const int nCols = header.count(); const int nFiles = m_fileList.count(); if(nCols < 1) { qWarning("Header appears to be empty!"); return CsvError_FileRead; } bool *ignore = new bool[nCols]; memset(ignore, 0, sizeof(bool) * nCols); for(int i = 0; i < nCols; i++) { if((header[i] = header[i].trimmed()).isEmpty()) { ignore[i] = true; } } //----------------------// for(int i = 0; i < nFiles; i++) { if(stream.atEnd()) { LAMEXP_DELETE_ARRAY(ignore); return CsvError_Incomplete; } QString line = stream.readLine().simplified(); if(line.isEmpty()) { qWarning("Skipping a blank line in CSV file!"); continue; } QStringList data = line.split(";", QString::KeepEmptyParts); if(data.count() < header.count()) { qWarning("Skipping an incomplete line in CSV file!"); continue; } const QString key = m_fileList[i]; for(int j = 0; j < nCols; j++) { if(ignore[j]) { continue; } else if(CHECK_HDR(header.at(j), "POSITION")) { bool ok = false; unsigned int temp = data.at(j).trimmed().toUInt(&ok); if(ok) m_fileStore[key].metaInfo().setPosition(temp); } else if(CHECK_HDR(header.at(j), "TITLE")) { QString temp = data.at(j).trimmed(); if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp); } else if(CHECK_HDR(header.at(j), "ARTIST")) { QString temp = data.at(j).trimmed(); if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp); } else if(CHECK_HDR(header.at(j), "ALBUM")) { QString temp = data.at(j).trimmed(); if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp); } else if(CHECK_HDR(header.at(j), "GENRE")) { QString temp = data.at(j).trimmed(); if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp); } else if(CHECK_HDR(header.at(j), "YEAR")) { bool ok = false; unsigned int temp = data.at(j).trimmed().toUInt(&ok); if(ok) m_fileStore[key].metaInfo().setYear(temp); } else if(CHECK_HDR(header.at(j), "COMMENT")) { QString temp = data.at(j).trimmed(); if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp); } else { qWarning("Unkonw field '%s' will be ignored!", QUTF8(header.at(j))); ignore[j] = true; if(!checkArray(ignore, false, nCols)) { qWarning("No known fields left, aborting!"); return CsvError_NoTags; } } } } //----------------------// LAMEXP_DELETE_ARRAY(ignore); return CsvError_OK; }
void *mprSlabAllocBlock(MPR_LOC_DEC(ctx, loc), uint size, uint inc) { #if NO_SLAB return mprAllocBlock(MPR_LOC_PASS(ctx, loc), size); #else MprBlk *parent, *bp; MprSlabBlock *sb; MprApp *app; MprSlab *slab; int slabIndex; if (ctx == 0) { mprAssert(ctx); return 0; } mprAssert(size > 0); mprAssert(VALID_BLK(ctx)); parent = GET_HDR(ctx); mprAssert(VALID_HDR(parent)); CHECK_HDR(parent); size = SLAB_ALIGN(size); app = parent->app; mprAssert(app); slabIndex = GET_SLAB(size); if (slabIndex < 0 || slabIndex >= MPR_MAX_SLAB) { return mprAllocBlock(MPR_LOC_PASS(ctx, loc), size); } /* * Dequeue a block from the slab. "sb" will point to the user data * portion of the block (i.e. after the MprBlk header). Slabs must be * allocated off the "slabs" context to ensure they don't get freed * until after all other blocks are freed. */ mprLock(app->allocLock); slab = &app->alloc.slabs[slabIndex]; if ((sb = slab->next) == 0) { if (growSlab(MPR_LOC_ARGS(parent->app->alloc.slabs), slab, size, inc) < 0) { mprUnlock(app->allocLock); return 0; } sb = slab->next; } mprAssert(sb); /* * Dequeue the block */ slab->next = sb->next; #if BLD_FEATURE_ALLOC_STATS { MprSlabStats *slabStats; /* * Update the slab stats */ slabStats = &slab->stats; slabStats->totalAllocCount++; slabStats->freeCount--; slabStats->allocCount++; if (slabStats->allocCount > slabStats->peakAllocCount) { slabStats->peakAllocCount = slabStats->allocCount; } } #endif /* BLD_FEATURE_ALLOC_STATS */ bp = GET_HDR(sb); #if BLD_DEBUG && !BREW if (bp == stopAlloc) { mprBreakpoint(MPR_LOC, "breakOnAddr"); } #endif bp->size = size; bp->flags = ALLOC_MAGIC | ALLOC_FLAGS_SLAB_BLOCK; bp->destructor = 0; bp->parent = parent; if (parent->children == 0) { parent->children = bp; bp->next = bp->prev = bp; } else { /* * Append to the end of the list. Preserve alloc order */ bp->next = parent->children; bp->prev = parent->children->prev; parent->children->prev->next = bp; parent->children->prev = bp; } bp->children = 0; bp->app = app; #if BLD_FEATURE_ALLOC_LEAK_TRACK bp->location = loc; #endif mprUnlock(app->allocLock); return GET_PTR(bp); #endif }
void *mprReallocBlock(MPR_LOC_DEC(ctx, loc), void *ptr, uint size) { MprBlk *bp, *newbp, *firstChild, *cp; MprApp *app; void *newPtr; mprAssert(VALID_BLK(ctx)); mprAssert(size > 0); if (ptr == 0) { return mprAllocBlock(MPR_LOC_PASS(ctx, loc), size); } mprAssert(VALID_BLK(ptr)); bp = GET_HDR(ptr); mprAssert(bp); mprAssert(VALID_HDR(bp)); CHECK_HDR(bp); if (size < bp->size) { return ptr; } newPtr = mprAllocBlock(MPR_LOC_PASS(ctx, loc), size); if (newPtr == 0) { bp->flags &= ~ALLOC_FLAGS_FREE; free(bp); return 0; } newbp = GET_HDR(newPtr); mprAssert(newbp->size >= size); memcpy((char*) newbp + HDR_SIZE, (char*) bp + HDR_SIZE, bp->size); mprAssert(newbp->size >= size); /* * Fix the next / prev pointers */ app = bp->app; mprLock(app->allocLock); newbp->next->prev = newbp; newbp->prev->next = newbp; /* * Need to fix the parent pointer of all children */ if ((firstChild = newbp->children) != 0) { cp = firstChild; do { cp->parent = newbp; cp = cp->next; } while (cp != firstChild); } /* * May need to set the children pointer of our parent */ if (newbp->parent->children == bp) { newbp->parent->children = newbp; } /* * Free the original block */ mprFree(ptr); mprUnlock(app->allocLock); return GET_PTR(newbp); }
int mprFree(void *ptr) { MprAllocStats *stats; MprBlk *bp, *parent, *cp, *firstChild, *prev; MprApp *app; if (ptr == 0) { return 0; } mprAssert(VALID_BLK(ptr)); VALIDATE_BLOCK(ptr); bp = GET_HDR(ptr); #if BLD_DEBUG && !BREW if (bp == stopAlloc) { mprBreakpoint(MPR_LOC, "breakOnAddr"); } #endif mprAssert(bp); mprAssert(VALID_HDR(bp)); CHECK_HDR(bp); /* * Test if already freed */ mprAssert(! (bp->flags & ALLOC_FLAGS_FREE)); if (bp->flags & ALLOC_FLAGS_FREE) { return 0; } /* * Return if recursive freeing or this is a permanent block */ app = bp->app; mprLock(app->allocLock); if (bp->flags & (ALLOC_FLAGS_FREEING | ALLOC_FLAGS_KEEP)) { mprUnlock(app->allocLock); return 0; } bp->flags |= ALLOC_FLAGS_FREEING; /* * Call any destructors */ if (bp->destructor) { mprUnlock(app->allocLock); if ((bp->destructor)(ptr) < 0) { return -1; } mprLock(app->allocLock); bp->destructor = 0; } /* * Free the children. Free in reverse order so firstChild is preserved * during the list scan as an end of list marker. */ if ((firstChild = bp->children) != 0) { cp = firstChild->prev; while (cp != firstChild) { mprAssert(VALID_HDR(cp)); VALIDATE_BLOCK(GET_PTR(cp)); prev = cp->prev; /* * FUTURE - OPT. Make this inline */ mprFree(GET_PTR(cp)); cp = prev; } mprFree(GET_PTR(firstChild)); /* * Just for clarity */ bp->children = 0; } parent = bp->parent; mprAssert(VALID_HDR(parent)); /* * Unlink from the parent */ if (parent->children == bp) { if (bp->next == bp) { parent->children = 0; } else { parent->children = bp->next; } } /* * Remove from the sibling chain */ bp->prev->next = bp->next; bp->next->prev = bp->prev; bp->flags |= ALLOC_FLAGS_FREE; /* * Release the memory. If from a slab, return to the slab. Otherwise, * return to the O/S. */ if (bp->flags & ALLOC_FLAGS_SLAB_BLOCK) { slabFree(bp); } else { mprAssert(bp); /* * Update the stats */ stats = &bp->app->alloc.stats; stats->bytesAllocated -= (bp->size + HDR_SIZE); mprAssert(stats->bytesAllocated >= 0); stats->allocCount--; mprAssert(stats->allocCount >= 0); #if BLD_DEBUG && !BREW if (bp == stopAlloc) { mprBreakpoint(MPR_LOC, "breakOnAddr"); } #endif /* * Return to the O/S */ if (! (bp->flags & ALLOC_FLAGS_DONT_OS_FREE)) { free(bp); } } /* OPT */ if (app != ptr) { mprUnlock(app->allocLock); } return 0; }
void *mprAllocBlock(MPR_LOC_DEC(ctx, loc), uint size) { MprAllocStats *stats; MprBlk *bp, *parent; MprApp *app; int diff; mprAssert(size > 0); if (ctx == 0) { #if BREW mprAssert(ctx); return 0; #else ctx = rootCtx; #endif } if (size == 0) { size = 1; } mprAssert(VALID_BLK(ctx)); parent = GET_HDR(ctx); mprAssert(VALID_HDR(parent)); CHECK_HDR(parent); size = ALLOC_ALIGN(size); app = parent->app; stats = &app->alloc.stats; mprLock(app->allocLock); stats->bytesAllocated += size + HDR_SIZE; if (stats->bytesAllocated > stats->peakAllocated) { stats->peakAllocated = stats->bytesAllocated; } /* * Prevent allocation if over the maximum */ if (stats->maxMemory && stats->bytesAllocated > stats->maxMemory) { stats->bytesAllocated -= (size + HDR_SIZE); mprUnlock(app->allocLock); if (mprAllocException(MPR_LOC_PASS(ctx, loc), size, 0) < 0) { return 0; } mprLock(app->allocLock); } if ((bp = malloc(size + HDR_SIZE)) == 0) { mprAssert(bp); stats->errors++; mprUnlock(app->allocLock); mprAllocException(MPR_LOC_PASS(ctx, loc), size, 0); return 0; } #if BLD_DEBUG memset(bp, 0xf7, size + HDR_SIZE); #endif #if BLD_DEBUG && !BREW if (bp == stopAlloc) { mprBreakpoint(MPR_LOC, "breakOnAddr"); } #endif /* * Warn if allocation puts us over the red line */ if (stats->redLine && stats->bytesAllocated > stats->redLine) { mprUnlock(app->allocLock); if (mprAllocException(MPR_LOC_PASS(ctx, loc), size, 1) < 0) { return 0; } mprLock(app->allocLock); } bp->size = size; bp->flags = ALLOC_MAGIC; bp->destructor = 0; bp->parent = parent; if (parent->children == 0) { parent->children = bp; bp->next = bp->prev = bp; } else { /* * Append to the end of the list. Preserve alloc order */ bp->next = parent->children; bp->prev = parent->children->prev; parent->children->prev->next = bp; parent->children->prev = bp; } bp->children = 0; #if BLD_FEATURE_ALLOC_LEAK_TRACK bp->location = loc; #endif bp->app = parent->app; VALIDATE_BLOCK(GET_PTR(bp)); stats->allocCount++; /* * Monitor stack usage */ diff = (int) bp->app->stackStart - (int) &stats; if (diff < 0) { app->maxStack -= diff; app->stackStart = (void*) &stats; diff = 0; } if ((uint) diff > app->maxStack) { app->maxStack = diff; } mprUnlock(app->allocLock); return GET_PTR(bp); }
int mprStealAllocBlock(MPR_LOC_DEC(ctx, loc), const void *ptr) { MprBlk *bp, *parent; if (ptr == 0) { return 0; } mprAssert(VALID_BLK(ctx)); mprAssert(VALID_BLK(ptr)); bp = GET_HDR(ptr); #if BLD_DEBUG && !BREW if (bp == stopAlloc) { mprBreakpoint(MPR_LOC, "breakOnAddr"); } #endif mprAssert(bp); mprAssert(VALID_HDR(bp)); mprAssert(ptr != mprGetAllocParent(ptr)); CHECK_HDR(bp); mprAssert(bp->prev); mprAssert(bp->prev->next); mprAssert(bp->next); mprAssert(bp->next->prev); parent = bp->parent; mprAssert(VALID_HDR(parent)); mprLock(bp->app->allocLock); if (parent->children == bp) { if (bp->next == bp) { parent->children = 0; } else { parent->children = bp->next; } } bp->prev->next = bp->next; bp->next->prev = bp->prev; parent = GET_HDR(ctx); mprAssert(VALID_HDR(parent)); bp->parent = parent; if (parent->children == 0) { parent->children = bp; bp->next = bp->prev = bp; } else { bp->next = parent->children; bp->prev = parent->children->prev; parent->children->prev->next = bp; parent->children->prev = bp; } #if BLD_FEATURE_ALLOC_LEAK_TRACK bp->location = loc; #endif VALIDATE_BLOCK(GET_PTR(bp)); mprUnlock(bp->app->allocLock); return 0; }