bool sp_write(struct ring_buf_data* p_pd) { log_trace2("sp_write"); if(S_OK != mb_validate(p_pd)) { // message is not valid log_trace("ignoring invalid message send request, size: %d", rb_size(p_pd)); return(false); } log_trace2("send: -->"); for(uint8_t i=0, imax=rb_size(p_pd); i<imax; ++i) { const uint8_t val = rb_at(p_pd, i); const ssize_t bytesWritten = write(s_fd, &val, 1); if(1 != bytesWritten) { // error log_err("serial write error"); return(false); } log_trace2("%c", val); } log_trace2("<--"); // TODO: bug in atmega32 code requires an extra byte to be sent for now write(s_fd, "\n", 1); return(true); }
int main() { RB_TREE root = NULL; RB_TYPE rbt; int v,i; rb_type_create( &rbt, sizeof(VNODE), 0, cmp, NULL, NULL ); srand(2); for( i=0; i<1000; i++ ) { v = rand(); //v = 1000000-i; rb_insert( &rbt, &root, &v ); } rb_assert(&rbt,root); printf( "Tree has %d elements\n", rb_size(&root) ); srand(2); for( i=0; i<1000; i++ ) { v = rand(); //v = 1000000-i; rb_remove( &rbt, &root, &v ); } rb_assert(&rbt,root); printf( "Tree has %d elements\n", rb_size(&root) ); rb_free(&rbt,&root); return 0; }
void test_strings(void) { int i; char str[512]; struct rb_node *node; struct rb_tree *tree = rb_create(string_cmp, string_free, string_free, string_print, string_print); assert( rb_isempty(tree) == 1 ); srand(4545); for (i = 0; i < 10000; i++) { snprintf(str, sizeof(str), "test%d", rand() % 1000000); rb_insert(tree, strdup(str), strdup("value")); } srand(4545); for (i = 0; i < 10000; i++) { snprintf(str, sizeof(str), "test%d", rand() % 1000000); node = rb_find(tree, str); assert(node); } { node = rb_find(tree, "test46554A"); assert(node == NULL); } assert( rb_isempty(tree) == 0 ); assert( rb_size(tree) == 10000 ); srand(4545); for (i = 0; i < 10000; i++) { snprintf(str, sizeof(str), "test%d", rand() % 1000000); node = rb_find(tree, str); assert(node); rb_delete(tree, node); } assert( rb_isempty(tree) == 1 ); assert( rb_size(tree) == 0 ); rb_destroy(tree); }
void test_integers(void) { int i; intptr_t val; struct rb_node *node; struct rb_tree *tree = rb_create(integer_cmp, integer_free, integer_free, integer_print, integer_print); assert( rb_isempty(tree) == 1 ); srand(4545); for (i = 0; i < 10000; i++) { val = rand() % 1000000; rb_insert(tree, (void*)val, (void*)val); } srand(4545); for (i = 0; i < 10000; i++) { val = rand() % 1000000; node = rb_find(tree, (void*)val); assert(node); } { node = rb_find(tree, (void*)234324); assert(node == NULL); } assert( rb_isempty(tree) == 0 ); assert( rb_size(tree) == 10000 ); srand(4545); for (i = 0; i < 10000; i++) { val = rand() % 1000000; node = rb_find(tree, (void*)val); assert(node); rb_delete(tree, node); } assert( rb_isempty(tree) == 1 ); assert( rb_size(tree) == 0 ); rb_destroy(tree); }
void vc_iterate(VCSession *vc) { if (!vc) { return; } pthread_mutex_lock(vc->queue_mutex); struct RTPMessage *p; if (!rb_read(vc->vbuf_raw, (void **)&p)) { LOGGER_TRACE(vc->log, "no Video frame data available"); pthread_mutex_unlock(vc->queue_mutex); return; } pthread_mutex_unlock(vc->queue_mutex); const struct RTPHeader *const header = &p->header; uint32_t full_data_len; if (header->flags & RTP_LARGE_FRAME) { full_data_len = header->data_length_full; LOGGER_DEBUG(vc->log, "vc_iterate:001:full_data_len=%d", (int)full_data_len); } else { full_data_len = p->len; LOGGER_DEBUG(vc->log, "vc_iterate:002"); } LOGGER_DEBUG(vc->log, "vc_iterate: rb_read p->len=%d p->header.xe=%d", (int)full_data_len, p->header.xe); LOGGER_DEBUG(vc->log, "vc_iterate: rb_read rb size=%d", (int)rb_size(vc->vbuf_raw)); const vpx_codec_err_t rc = vpx_codec_decode(vc->decoder, p->data, full_data_len, nullptr, MAX_DECODE_TIME_US); free(p); if (rc != VPX_CODEC_OK) { LOGGER_ERROR(vc->log, "Error decoding video: %d %s", (int)rc, vpx_codec_err_to_string(rc)); return; } /* Play decoded images */ vpx_codec_iter_t iter = nullptr; for (vpx_image_t *dest = vpx_codec_get_frame(vc->decoder, &iter); dest != nullptr; dest = vpx_codec_get_frame(vc->decoder, &iter)) { if (vc->vcb) { vc->vcb(vc->av, vc->friend_number, dest->d_w, dest->d_h, (const uint8_t *)dest->planes[0], (const uint8_t *)dest->planes[1], (const uint8_t *)dest->planes[2], dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb_user_data); } vpx_img_free(dest); // is this needed? none of the VPx examples show that } }
static CSA_return_code _GetNumberEntries(_DtCmsCalendar *cal, int index, cms_attribute *attr) { uint size; size = rb_size(cal->tree) + hc_size(cal->list); attr->name.num = CSA_CAL_ATTR_NUMBER_ENTRIES_I; if (attr->name.name = strdup(CSA_CAL_ATTR_NUMBER_ENTRIES)) return (_DtCm_set_uint32_attrval(size, &attr->value)); else return (CSA_E_INSUFFICIENT_MEMORY); }
ssize_t buffering_read(neo4j_iostream_t *stream, void *buf, size_t nbyte) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->rcvbuf == NULL) { return neo4j_ios_read(ios->delegate, buf, nbyte); } if (nbyte > SSIZE_MAX) { nbyte = SSIZE_MAX; } size_t extracted = rb_extract(ios->rcvbuf, buf, nbyte); assert(extracted <= nbyte); if (extracted == nbyte) { return extracted; } buf = (uint8_t *)buf + extracted; nbyte -= extracted; struct iovec iov[3]; int iovcnt = 1; iov[0].iov_base = buf; iov[0].iov_len = nbyte; iovcnt += rb_space_iovec(ios->rcvbuf, iov + 1, rb_size(ios->rcvbuf)); ssize_t n = neo4j_ios_readv(ios->delegate, iov, iovcnt); if (n < 0) { return (extracted > 0)? (ssize_t)extracted : -1; } if ((size_t)n <= nbyte) { return extracted + (size_t)n; } rb_advance(ios->rcvbuf, n - nbyte); return extracted + nbyte; }
int buffering_flush(neo4j_iostream_t *stream) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (!rb_is_empty(ios->sndbuf)) { struct iovec iov[2]; unsigned int iovcnt = rb_data_iovec(ios->sndbuf, iov, rb_size(ios->sndbuf)); size_t written; if (neo4j_ios_writev_all(ios->delegate, iov, iovcnt, &written)) { rb_discard(ios->sndbuf, written); return -1; } rb_clear(ios->sndbuf); } return neo4j_ios_flush(ios->delegate); }
int Size(void *data_structure) { return rb_size((rbtree_t *)data_structure); }
ssize_t buffering_writev(neo4j_iostream_t *stream, const struct iovec *iov, unsigned int iovcnt) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->sndbuf == NULL) { return neo4j_ios_writev(ios->delegate, iov, iovcnt); } if (iovcnt > IOV_MAX-2) { iovcnt = IOV_MAX-2; } size_t nbyte = iovlen(iov, iovcnt); if (nbyte <= rb_space(ios->sndbuf)) { return rb_appendv(ios->sndbuf, iov, iovcnt); } size_t buffered = rb_used(ios->sndbuf); ALLOC_IOVEC(diov, iovcnt+2); if (diov == NULL) { return -1; } unsigned int diovcnt = rb_data_iovec(ios->sndbuf, diov, rb_size(ios->sndbuf)); memcpy(diov+diovcnt, iov, iovcnt * sizeof(struct iovec)); ssize_t written = neo4j_ios_writev(ios->delegate, diov, diovcnt + iovcnt); if (written < 0) { goto cleanup; } if ((size_t)written < buffered) { rb_discard(ios->sndbuf, written); memcpy(diov, iov, iovcnt * sizeof(struct iovec)); diovcnt = iovcnt; written = 0; } else { rb_clear(ios->sndbuf); written -= buffered; assert((size_t)written <= nbyte); diovcnt = iov_skip(diov, iov, iovcnt, written); } if (diovcnt == 0) { written = nbyte; goto cleanup; } written += rb_appendv(ios->sndbuf, diov, diovcnt); int errsv; cleanup: errsv = errno; FREE_IOVEC(diov); errno = errsv; return written; }
ssize_t buffering_write(neo4j_iostream_t *stream, const void *buf, size_t nbyte) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->sndbuf == NULL) { return neo4j_ios_write(ios->delegate, buf, nbyte); } if (nbyte > SSIZE_MAX) { nbyte = SSIZE_MAX; } if (nbyte <= rb_space(ios->sndbuf)) { return rb_append(ios->sndbuf, buf, nbyte); } size_t buffered = rb_used(ios->sndbuf); struct iovec iov[3]; unsigned int iovcnt = rb_data_iovec(ios->sndbuf, iov, rb_size(ios->sndbuf)); iov[iovcnt].iov_base = (uint8_t *)(intptr_t)buf; iov[iovcnt].iov_len = nbyte; ++iovcnt; ssize_t written = neo4j_ios_writev(ios->delegate, iov, iovcnt); if (written < 0) { return -1; } const uint8_t *rbytes; size_t remaining; if ((size_t)written < buffered) { rb_discard(ios->sndbuf, written); rbytes = buf; remaining = nbyte; written = 0; } else { rb_clear(ios->sndbuf); written -= buffered; assert((size_t)written <= nbyte); rbytes = (const uint8_t *)buf + written; remaining = nbyte - written; } if (remaining == 0) { return nbyte; } size_t appended = rb_append(ios->sndbuf, rbytes, remaining); return (size_t)written + appended; }
ssize_t buffering_readv(neo4j_iostream_t *stream, const struct iovec *iov, unsigned int iovcnt) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->rcvbuf == NULL) { return neo4j_ios_readv(ios->delegate, iov, iovcnt); } if (iovcnt > IOV_MAX-2) { iovcnt = IOV_MAX-2; } size_t nbyte = iovlen(iov, iovcnt); size_t extracted = rb_extractv(ios->rcvbuf, iov, iovcnt); assert(extracted <= nbyte); if (extracted == nbyte) { return extracted; } nbyte -= extracted; ALLOC_IOVEC(diov, iovcnt+2); if (diov == NULL) { return (extracted > 0)? (ssize_t)extracted : -1; } unsigned int diovcnt = iov_skip(diov, iov, iovcnt, extracted); diovcnt += rb_space_iovec(ios->rcvbuf, diov + diovcnt, rb_size(ios->rcvbuf)); ssize_t result = neo4j_ios_readv(ios->delegate, diov, diovcnt); if (result < 0) { result = (extracted > 0)? (ssize_t)extracted : -1; goto cleanup; } if ((size_t)result <= nbyte) { result += extracted; goto cleanup; } rb_advance(ios->rcvbuf, result - nbyte); result = extracted + nbyte; int errsv; cleanup: errsv = errno; FREE_IOVEC(diov); errno = errsv; return result; }
inline static int rb_percent_full(RB) { if (!RB) return 100; return rb_size(RB)*100/RB->buffersize; }
inline static rb_remaining_space(RB) { return RB->buffersize-rb_size(RB); }
void test_integers_multi(int factor) { int i; intptr_t val; unsigned int count; struct rb_node *node; struct rb_tree *tree = rb_create(integer_cmp, integer_free, integer_free, integer_print, integer_print); assert( rb_isempty(tree) == 1 ); for (i = 0; i < 100 * factor; i++) { val = i % factor; rb_insert(tree, (void*)val, (void*)val); } assert( rb_isempty(tree) == 0 ); assert( rb_size(tree) == (unsigned int)100 * factor); for (i = 0; i < factor; i++) { val = i; node = rb_find(tree, (void*)val); assert(node); count = 0; while (node != rb_end(tree) && (intptr_t)node->key == i) { ++count; node = rb_successor(tree, node); } assert(count == 100); } { count = 0; for(node = rb_begin(tree); node != rb_end(tree); node = rb_successor(tree, node)) { assert((intptr_t)node->key == (count++ / 100)); } } for (i = 0; i < 100 * factor; i++) { val = i % factor; node = rb_find(tree, (void*)val); assert(node); rb_delete(tree, node); } assert( rb_isempty(tree) == 1 ); rb_destroy(tree); }