// Arbitrary-size push function unsigned int pushToBuffer(buffer_t *b, void *d, unsigned int l) { unsigned int elementIndex, byteIndex; // Loop through all elements for (elementIndex = 0; elementIndex < l; elementIndex++) { // Loop through all bytes of each element for (byteIndex = 0; byteIndex < b->width; byteIndex++) { // Only push to buffer if it is not full or overwriting is allowed if ( (!isBufferFull(b)) || (b->behavior.bits.overwrite) ) { pushByte(b, *( (unsigned char*)(d + elementIndex * (b->width) + byteIndex) )); } // If buffer is full, return a count of those elments not pushed else { // Pop all bytes of incomplete elements // -This should never run, but added just in case unsigned int failedbytes; for (failedbytes = byteIndex; failedbytes > 0; failedbytes--) { // If it is a queue pop comes from tail, so decrement head decrement(b, &(b->head)); } // Return a count of failed push operations // -Include partial pushes in count return l - elementIndex; } } } return 0; }
// Byte-size push function void pushByte(buffer_t *b, unsigned char d){ // If we are overwriting a full buffer, increment tail pointer so that the // head doesn't move past the tail if ( (isBufferFull(b)) && (b->behavior.bits.overwrite) ) { increment(b, &(b->tail)); } // Regardless of FIFO or FILO, always push to head *((unsigned char*)b->head) = d; increment(b, &(b->head)); }
void ChunkIndex::insertEntries(ID id, unsigned offset) { if(isBufferFull() == true) { idTable->resize(HASH_CAPACITY); idTableEntries = (ID*)idTable->get_address(); offsetTable->resize(HASH_CAPACITY); offsetTableEntries = (ID*)offsetTable->get_address(); } idTableEntries[tableSize] = id; offsetTableEntries[tableSize] = offset; tableSize++; }
void bufferWriteChar(Buffer* buffer, char c) { int isFull = isBufferFull(buffer); if (!isFull) { char* sPointer = (char*) buffer->s; // Shift to the right cell index sPointer += buffer->writeIndex; *sPointer = c; buffer->writeIndex++; buffer->writeIndex %= buffer->length; } else { // We must log the problem writeError(IO_BUFFER_FULL); // Print Buffer printDebugBuffer(getErrorOutputStreamLogger(), buffer); } }
// Inserts a worker thread's text message into the next // available slot in the circular buffer. void insertToBuffer(int in_workerthread_id, request* clientRequest, int messageID, pixel** in_pixArray, int in_rows, int in_cols, int in_frame, int in_repeat) { while (1) { // Check for empty slots; if no empty slots, wait. if (isBufferFull() == TRUE) { pthread_cond_wait(&bufferWait, &bufferMutex); } else { // Grab buffer mutex to ensure exclusive access to buffer. pthread_mutex_lock(&bufferMutex); } // Go through all buffer slots and find an empty one; int i; for (i = 0; i < MAXSLOTS; i++) { if (buffer[i].isFilled == FALSE) { buffer[i].isFilled = TRUE; buffer[i].workerthread_id = in_workerthread_id; buffer[i].clientRequest = clientRequest; buffer[i].priority = clientRequest->priority; buffer[i].messageID = messageID; buffer[i].pixArray = in_pixArray; buffer[i].rows = in_rows; buffer[i].cols = in_cols; buffer[i].frame = in_frame; buffer[i].repeat = in_repeat; openSlots--; // Check to see if m slots are full, then signal dispatcher if (openSlots == (MAXSLOTS - DISPATCHER_BATCH_AMOUNT)) { pthread_cond_signal(&dispatcherWait); // Have the worker thread wait until the dispatcher wakes it pthread_cond_wait(&bufferWait, &bufferMutex); } // Release buffer mutex. pthread_mutex_unlock(&bufferMutex); return; } } // Release buffer mutex. pthread_mutex_unlock(&bufferMutex); } }