int jbin_add(jbin_t *self, jelement_t *elm) { // jerr_t rc; jelement_t *self_elm; self_elm = (jelement_t *)self; if (J_UNLIKELY(elm == self_elm)) { jlog_error(NULL, "cannot add self\n"); return J_ERROR; } // element name must be unique // for (;;) { // ; // } if (elm->parent_element) { return J_ERROR; } elm->parent_element = self_elm; // add it jlist_add(&elm->elements, &self_elm->elements); ++self->nelements; return J_OK; }
static __inline__ int __hm_net_buf_write(JNetBuf *buff, char *buf, size_t count, void *user_data, int *pending) { size_t size; int ret; JBufBlock *b; ret = hm_net_buf_append(buff, buf, count, user_data, pending); if (ret < 0) return ret; size = count - ret; buf += ret; if (!size) return count; if (buff->no_blocks) return 0; b = &((JNetRealBuf*)buff)->buff[buff->next_use]; if (J_UNLIKELY(b->is_busy)) BUG(); b->is_busy = 1; b->start_pos = 0; b->end_pos = b->start_pos + size; memcpy(&b->raw_data[b->start_pos], buf, size); buff->buffer_bytes += size; hm_net_buf_write_ok(buff); return count; }
/* * Flush a buffer. This is the consumer of the buffer. * we try to write all data in this buffer to network, using * the given function. * * @Ret: < 0, error. * = 0, all data has been flushed out. * > 0, bytes left in buffer. */ int __hm_net_buf_flush(JNetBuf *buff, void *user_data) { JBufBlock *b; int left, ret; while ( TRUE ) { /* buffer empty */ if (buff->next_deal == buff->num_buffers) return 0; b = &((JNetRealBuf*)buff)->buff[buff->next_deal]; if (J_UNLIKELY(!b->is_busy)) BUG(); left = b->end_pos - b->start_pos; if (J_UNLIKELY(left <= 0)) BUG(); ret = (*buff->flush)(&b->raw_data[b->start_pos], left, user_data); if (ret >= 0) { buff->buffer_bytes -= ret; if (ret == left) hm_net_buf_flush_ok(buff); else { BUG_ON(ret > left); b->start_pos += ret; return buff->buffer_bytes; } } else break; } return ret; }
/* * try to place data at the end of last used block. * @ret: >= 0, bytes appended. * < 0, error code. */ static __inline__ int hm_net_buf_append(JNetBuf *buff, char *buf, size_t count, void *user_data, int *pending) { int last, left; JBufBlock *b; if (count > MAX_IO_BUFFER_SIZE) return -E_PACKET2LONG; if (J_UNLIKELY(!count)) return 0; *pending = 1; /* we assume buffer is not empty */ last = hm_net_buf_last_pos(buff); if (last < 0) /* no data in buffer, try to send */ { left = (*buff->flush)(buf, count, user_data); if (left == count) *pending = 0; return left; } b = &((JNetRealBuf*)buff)->buff[last]; BUG_ON(!b->is_busy); left = MAX_IO_BUFFER_SIZE - b->end_pos; if (left >= count) { memcpy(&b->raw_data[b->end_pos], buf, count); b->end_pos += count; buff->buffer_bytes += count; return count; } return 0; }
static __inline__ void __free(void *ptr, size_t size) { if (J_UNLIKELY(!ptr)) return; #ifndef JLIB_MEM_DEBUG free(ptr); #else struct __alloc_cookie *head; struct __tail_cookie *tail; head = (struct __alloc_cookie*)ptr; head -= 1; if (head->magic != MAGIC) { fprintf(stderr, "__free(), head->magic != MAGIC!!!\n"); BUG(); } if (head->length != size) { fprintf(stderr, "__free(), head->length(%d) != size(%d)!!!\n", head->length, size); BUG(); } tail = (struct __tail_cookie*)(ptr + ALIGN(head->length, sizeof(void*))); if (tail->magic != MAGIC) { fprintf(stderr, "__free(), tail->magic != MAGIC!!!\n"); BUG(); } free(head); #endif }