listPo appendToList(heapPo H, listPo list, termPo el) { basePo base = C_BASE(list->base); int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); gcAddRoot(H, (ptrPo) (&el)); if (base->max == list->start + list->length && base->max < base->length) { lockHeap(H); if (base->max == list->start + list->length && base->max < base->length) { // check after locking heap base->els[base->max++] = el; listPo slice = (listPo) newSlice(H, base, list->start, list->length + 1); gcReleaseRoot(H, root); releaseHeapLock(H); return slice; } releaseHeapLock(H); } basePo nb = copyBase(H, base, list->start, list->length, (list->length / 8) + 2); nb->els[nb->max++] = el; gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, nb->max - nb->min); gcReleaseRoot(H, root); return slice; }
listPo prependToList(heapPo H, listPo list, termPo el) { basePo base = C_BASE(list->base); int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); gcAddRoot(H, (ptrPo) (&el)); // logMsg(logFile, "list before prepend: %T", list); if (base->min == list->start && base->min > 0) { lockHeap(H); if (base->max == list->start && base->min > 0) { // check after locking heap base->els[--base->min] = el; listPo slice = (listPo) newSlice(H, base, list->start - 1, list->length + 1); gcReleaseRoot(H, root); releaseHeapLock(H); //logMsg(logFile, "slice after prepend: %T", slice); return slice; } releaseHeapLock(H); } basePo nb = copyBase(H, base, list->start, list->length, (list->length / 8) + 2); nb->els[--nb->min] = el; gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, list->length + 1); // logMsg(logFile, "slice post prepend: %T", slice); gcReleaseRoot(H, root); return slice; }
listPo removeListEl(heapPo H, listPo list, integer px) { basePo base = C_BASE(list->base); int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); integer delta = base->length / 8; integer newLen = list->length + delta; basePo nb = (basePo) allocateObject(H, baseClass, BaseCellCount(newLen)); integer ocount = list->length; assert(ocount >= 0); integer extra = newLen - ocount; nb->min = extra / 2; nb->max = nb->min + ocount-1; nb->length = newLen; for (integer ix = 0; ix < px; ix++) { nb->els[nb->min + ix] = base->els[list->start + ix]; } for (integer ix = px + 1; ix < ocount; ix++) { nb->els[nb->min + ix] = base->els[list->start + ix]; } gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, list->length - 1); assert(saneList(H,slice)); gcReleaseRoot(H, root); releaseHeapLock(H); return slice; }
listPo replaceListEl(heapPo H, listPo list, integer px, termPo vl) { if (px >= listSize(list)) return appendToList(H, list, vl); else if (px < 0) return prependToList(H, list, vl); else { basePo base = C_BASE(list->base); int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); gcAddRoot(H, (ptrPo) (&vl)); integer delta = base->length / 8; basePo nb = copyBase(H, base, list->start, list->length, delta); nb->els[nb->min + px] = vl; gcAddRoot(H, (ptrPo) &nb); listPo slice = (listPo) newSlice(H, nb, nb->min, list->length); assert(saneList(H, slice)); gcReleaseRoot(H, root); releaseHeapLock(H); return slice; } }
termPo sliceList(heapPo H, listPo list, integer from, integer count) { assert(from >= 0 && from + count <= list->length); int root = gcAddRoot(H, (ptrPo) &list); listPo slice = (listPo) newSlice(H, C_BASE(list->base), list->start + from, count); gcReleaseRoot(H, root); return (termPo) slice; }
ByteSliceOutputStream::ByteSliceOutputStream(Pool* pPool) : m_pHeadSlice(NULL) , m_pTailSlice(NULL) , m_nTotalSize(0) , m_pWriteUpto(NULL) , m_pPool(pPool) { newSlice(); }
void ByteSliceOutputStream::write(const void* buffer, size_t bufSize) { const char* pBufferPtr = (const char*)buffer; while (bufSize > 0) { if (!m_pWriteUpto || m_pWriteUpto >= m_pTailSlice->data + m_pTailSlice->size) { newSlice(); } size_t nWriteSize = (bufSize > (size_t)(m_pTailSlice->data + m_pTailSlice->size - m_pWriteUpto)) ? (m_pTailSlice->data + m_pTailSlice->size - m_pWriteUpto) : bufSize; memcpy(m_pWriteUpto, pBufferPtr, nWriteSize); m_pWriteUpto += nWriteSize; pBufferPtr += nWriteSize; bufSize -= nWriteSize; } }
listPo insertListEl(heapPo H, listPo list, integer px, termPo vl) { basePo base = C_BASE(list->base); if (px <= 0) return prependToList(H, list, vl); else if (px >= listSize(list)) return appendToList(H, list, vl); else { int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); gcAddRoot(H, (ptrPo) (&vl)); integer delta = base->length / 8; integer newLen = base->length + delta + 1; basePo nb = (basePo) allocateObject(H, baseClass, BaseCellCount(newLen)); integer ocount = list->length; assert(ocount >= 0); integer extra = newLen - ocount; nb->min = extra / 2 - 1; nb->max = nb->min + ocount + 1; nb->length = newLen; for (integer ix = 0; ix < px; ix++) { nb->els[nb->min + ix] = base->els[base->min + ix]; } nb->els[nb->min + px] = vl; for (integer ix = px; ix < ocount; ix++) { nb->els[nb->min + ix + 1] = base->els[base->min + ix]; } gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, ocount + 1); gcReleaseRoot(H, root); releaseHeapLock(H); assert(saneList(H, slice)); return slice; } }
void ByteSliceOutputStream::write(InputStream& inStream, offset_t size) { offset_t sizeToCopy = size; if (size == (offset_t)(-1)) { sizeToCopy = inStream.getSize() - inStream.tell(); } while (sizeToCopy > 0) { if (!m_pWriteUpto || m_pWriteUpto >= m_pTailSlice->data + m_pTailSlice->size) { newSlice(); } size_t nWriteSize = ((size_t)sizeToCopy > (size_t)(m_pTailSlice->data + m_pTailSlice->size - m_pWriteUpto)) ? (m_pTailSlice->data + m_pTailSlice->size - m_pWriteUpto) : (size_t)sizeToCopy; inStream.read((void*)m_pWriteUpto, nWriteSize); m_pWriteUpto += nWriteSize; sizeToCopy -= (offset_t)nWriteSize; } }
//============================================================================= // // class TolderLay // void trend::TolderLay::newSlice(TrxCellRef* const ctrans, bool fill, bool reusable, unsigned) { assert( 0 == total_slctdx()); newSlice(ctrans, fill, reusable); }