JNIEXPORT void JNICALL Java_com_aio4c_buffer_Buffer_limit__I(JNIEnv* jvm, jobject buffer, jint limit) { Buffer* _buffer = _GetBuffer(jvm, buffer); if (BufferLimit(_buffer, limit) == NULL) { (*jvm)->ThrowNew(jvm, (*jvm)->FindClass(jvm, "Ljava/lang/IllegalArgumentException;"), "invalid limit"); return; } }
void BufferReassignSeg(Buffer buffer, Seg seg) { AVERT(Buffer, buffer); AVERT(Seg, seg); AVER(!BufferIsReset(buffer)); AVER(BufferBase(buffer) >= SegBase(seg)); AVER(BufferLimit(buffer) <= SegLimit(seg)); AVER(BufferPool(buffer) == SegPool(seg)); Method(Buffer, buffer, reassignSeg)(buffer, seg); }
/* AMSTStressBufferedSeg -- Stress test for a buffered seg * * Test splitting or merging a buffered seg. * * .bmerge: A merge is performed when the segment had previously * been split and the segment above meets the constraints (i.e. empty, * not already attached to a buffer and similar colour) * * .bsplit: Whether or not a merge happpened, a split is performed if * the limit of the buffered region is also the limit of an arena * grain, and yet does not correspond to the segment limit, provided * that the part of the segment above the buffer is all free. */ static void AMSTStressBufferedSeg(Seg seg, Buffer buffer) { AMSTSeg amstseg; AMST amst; Arena arena; Addr limit; Buffer segBuf; AVERT(Seg, seg); AVERT(Buffer, buffer); AVER(SegBuffer(&segBuf, seg) && segBuf == buffer); amstseg = Seg2AMSTSeg(seg); AVERT(AMSTSeg, amstseg); limit = BufferLimit(buffer); arena = PoolArena(SegPool(seg)); amst = PoolAMST(SegPool(seg)); AVERT(AMST, amst); if (amstseg->next != NULL) { Seg segHi = AMSTSeg2Seg(amstseg->next); if (AMSSegIsFree(segHi) && SegGrey(segHi) == SegGrey(seg)) { /* .bmerge */ Seg mergedSeg; Res res; res = SegMerge(&mergedSeg, seg, segHi); if (ResOK == res) { amst->bmerges++; printf("J"); } else { /* deliberate fails only */ AVER(amst->failSegs); } } } if (SegLimit(seg) != limit && AddrIsArenaGrain(limit, arena) && AMSSegRegionIsFree(seg, limit, SegLimit(seg))) { /* .bsplit */ Seg segLo, segHi; Res res; res = SegSplit(&segLo, &segHi, seg, limit); if (ResOK == res) { amst->bsplits++; printf("C"); } else { /* deliberate fails only */ AVER(amst->failSegs); } } }
void BufferDetach(Buffer buffer, Pool pool) { AVERT(Buffer, buffer); AVER(BufferIsReady(buffer)); if (!BufferIsReset(buffer)) { Addr init, limit; Size spare; buffer->mode |= BufferModeTRANSITION; /* Ask the owning pool to do whatever it needs to before the */ /* buffer is detached (e.g. copy buffer state into pool state). */ Method(Pool, pool, bufferEmpty)(pool, buffer); /* run any class-specific detachment method */ Method(Buffer, buffer, detach)(buffer); init = BufferGetInit(buffer); limit = BufferLimit(buffer); spare = AddrOffset(init, limit); buffer->emptySize += spare; if (buffer->isMutator) { ArenaGlobals(buffer->arena)->emptyMutatorSize += spare; ArenaGlobals(buffer->arena)->allocMutatorSize += AddrOffset(buffer->base, init); } else { ArenaGlobals(buffer->arena)->emptyInternalSize += spare; } /* Reset the buffer. */ buffer->base = (Addr)0; buffer->initAtFlip = (Addr)0; buffer->ap_s.init = (mps_addr_t)0; buffer->ap_s.alloc = (mps_addr_t)0; buffer->ap_s.limit = (mps_addr_t)0; buffer->poolLimit = (Addr)0; buffer->mode &= ~(BufferModeATTACHED|BufferModeFLIPPED|BufferModeTRANSITION); EVENT2(BufferEmpty, buffer, spare); } }
Connection* NewConnection(BufferPool* pool, Address* address, bool freeAddress) { Connection* connection = NULL; if ((connection = aio4c_malloc(sizeof(Connection))) == NULL) { return NULL; } connection->readBuffer = AllocateBuffer(pool); connection->writeBuffer = AllocateBuffer(pool); BufferLimit(connection->writeBuffer, 0); connection->dataBuffer = NULL; connection->socket = -1; connection->state = AIO4C_CONNECTION_STATE_NONE; connection->stateLock = NewLock(); connection->systemHandlers = NewEventQueue(); connection->userHandlers = NewEventQueue(); connection->address = address; connection->closedForError = false; connection->string = AddressGetString(address); connection->stringAllocated = false; memset(connection->closedBy, 0, AIO4C_CONNECTION_OWNER_MAX * sizeof(bool)); connection->closedBy[AIO4C_CONNECTION_OWNER_ACCEPTOR] = true; connection->closedByLock = NewLock(); memset(connection->managedBy, 0, AIO4C_CONNECTION_OWNER_MAX * sizeof(bool)); connection->managedBy[AIO4C_CONNECTION_OWNER_ACCEPTOR] = true; connection->managedBy[AIO4C_CONNECTION_OWNER_CLIENT] = true; connection->managedByLock = NewLock(); connection->readKey = NULL; connection->writeKey = NULL; connection->pool = NULL; connection->freeAddress = freeAddress; connection->dataFactory = NULL; connection->dataFactoryArg = NULL; connection->canRead = false; connection->canWrite = false; connection->isFactory = false; return connection; }
int main(int argc, char* argv[]) { Buffer* a = NULL; aio4c_byte_t* data = NULL; aio4c_byte_t b = 0; char* s = NULL; int i = 0; int d = 12345; size_t len = 0; Aio4cInit(argc, argv, NULL, NULL); s = aio4c_malloc(strlen(TEST_STRING) + 1); assert(s != NULL); memcpy(s, TEST_STRING, strlen(TEST_STRING) + 1); a = NewBuffer(BUFFER_SIZE); assert(a != NULL); assert(BufferGetCapacity(a) == BUFFER_SIZE); assert(BufferGetLimit(a) == BUFFER_SIZE); assert(BufferGetPosition(a) == 0); assert(BufferHasRemaining(a) == true); assert(BufferRemaining(a) == BUFFER_SIZE); assert((data = BufferGetBytes(a)) != NULL); for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } assert(BufferPutInt(a, &d) == true); assert(BufferGetPosition(a) == sizeof(int)); assert(BufferFlip(a) == a); assert(BufferGetPosition(a) == 0); assert(BufferGetLimit(a) == sizeof(int)); d = 0; assert(BufferGetInt(a, &d) == true); assert(d == 12345); d = 45678; assert(BufferGetInt(a, &d) == false); assert(d == 45678); assert(BufferPutInt(a, &d) == false); assert(BufferReset(a) == a); assert(BufferPutString(a, s) == true); assert(BufferGetPosition(a) == (int)(strlen(s) + 1)); assert(BufferFlip(a) == a); assert(BufferGetPosition(a) == 0); assert(BufferGetLimit(a) == (int)(strlen(s) + 1)); for(i = 0; i < (int)(strlen(s) + 1); i++) { assert(BufferGetByte(a, &b) == true); assert(b == s[i]); assert(BufferGetPosition(a) == (i + 1)); } assert(BufferGetByte(a, &b) == false); assert(BufferHasRemaining(a) == false); assert(BufferReset(a) == a); assert(BufferGetCapacity(a) == BUFFER_SIZE); assert(BufferGetLimit(a) == BUFFER_SIZE); assert(BufferGetPosition(a) == 0); assert(BufferHasRemaining(a) == true); assert((data = BufferGetBytes(a)) != NULL); assert(BufferLimit(a, strlen(s) + 1) == a); assert(BufferGetLimit(a) == (int)(strlen(s) + 1)); len = strlen(s); s[len] = '0'; for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } assert(BufferPutString(a, s) == false); for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } s[len] = '\0'; assert(BufferPutString(a, s) == true); assert(BufferFlip(a) == a); for(i = 0; i < (int)(strlen(s) + 1); i++) { assert(BufferGetByte(a, &b) == true); assert(b == s[i]); assert(BufferGetPosition(a) == (i + 1)); } assert(BufferHasRemaining(a) == false); Aio4cEnd(); return 0; }