C_CAPSULE_START OPP_INLINE int opp_factory_create_full(struct opp_factory*obuff , SYNC_UWORD16_T inc , SYNC_UWORD16_T obj_size , int token_offset , unsigned char property , opp_callback_t callback ) { SYNC_ASSERT(obj_size < (1024<<1)); SYNC_ASSERT(inc); #ifdef SYNC_LOW_MEMORY SYNC_ASSERT(inc < 1024); #else SYNC_ASSERT(inc < (1024<<3)); #endif if(obuff->sign == OPPF_INITIALIZED_INTERNAL) { SYNC_LOG(SYNC_ERROR, "obj is already initiated\n"); SYNC_ASSERT(obuff->sign != OPPF_INITIALIZED_INTERNAL); return -1; } /* check possible leak capacity */ if(inc >= 0x7FFF) { SYNC_LOG(SYNC_ERROR, "too big allocation\n"); SYNC_ASSERT(inc < 0x7FFF); return -1; } /* pool size */ const SYNC_UWORD16_T pool_size = inc; /* obj size */ SYNC_UWORD16_T fixed_obj_size = obj_size + sizeof(struct opp_object); fixed_obj_size = OPP_NORMALIZE_SIZE(fixed_obj_size); /* bitstring size */ unsigned long long bitstring_size = (inc+7) >> 3; bitstring_size = OPP_NORMALIZE_SIZE(bitstring_size); bitstring_size = bitstring_size*BITFIELD_SIZE; /* check possible leak */ if(bitstring_size >= 0x7FFF) { SYNC_LOG(SYNC_ERROR, "too big allocation\n"); SYNC_ASSERT(bitstring_size < 0x7FFF); return -1; } const unsigned long long memory_chunk_size = sizeof(struct opp_pool) + fixed_obj_size*pool_size + bitstring_size; /* check possible leak */ if(0x7FFFFFFF < memory_chunk_size) { SYNC_LOG(SYNC_ERROR, "too big allocation\n"); SYNC_ASSERT(0x7FFFFFFF > memory_chunk_size); return -1; } #ifndef OBJ_MAX_BUFFER_SIZE #define OBJ_MAX_BUFFER_SIZE (4096<<10) #endif SYNC_ASSERT(obuff->memory_chunk_size < OBJ_MAX_BUFFER_SIZE); struct opp_factory template = {
void freceive_more_async(boost::asio::io_service & ios, aziomq::socket & socket, const const_buf_vec & expected_bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); //create vector of raw bufs to fill from length of expected_bufs buf_vec_t buf_vec(std::distance(std::begin(expected_bufs), std::end(expected_bufs))); zero(buf_vec); //create azio buffer vector mutable_buf_vec bufs; init(bufs, buf_vec); auto it = std::begin(bufs); auto e = std::end(bufs); std::exception_ptr err; socket.async_receive_more(boost::asio::buffer(*it++), [&ios, &socket, &bufs, &it, e, &err, expected_bufs](const boost::system::error_code & ec, aziomq::socket::more_result mr) { if (ec) { err = std::make_exception_ptr(boost::system::system_error(ec)); } else { for (; mr.second && it != e; ++it) { mr = socket.receive_more(boost::asio::buffer(*it)); err = check_res(bufs, expected_bufs); } } ios.stop(); }); ios.run(); if (err != std::exception_ptr()) std::rethrow_exception(err); }
void freceive_async(boost::asio::io_service & ios, aziomq::socket & socket, const const_buf_vec & expected_bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); //create vector of raw bufs to fill from length of expected_bufs buf_vec_t buf_vec(std::distance(std::begin(expected_bufs), std::end(expected_bufs))); zero(buf_vec); //create azio buffer vector mutable_buf_vec bufs; init(bufs, buf_vec); std::exception_ptr err; socket.async_receive(bufs, [&ios, &bufs, &err, expected_bufs](const boost::system::error_code & ec, size_t bytes_transferred) { if (ec) { err = std::make_exception_ptr(boost::system::system_error(ec)); } else { err = check_res(bufs, expected_bufs); } ios.stop(); }); ios.run(); if (err != std::exception_ptr()) std::rethrow_exception(err); }
void fsend_async(boost::asio::io_service & ios, aziomq::socket & socket, const const_buf_vec & bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); std::exception_ptr err; socket.async_send(bufs, [&ios,&err](const boost::system::error_code & ec, size_t bytes_transferred) { if (ec) err = std::make_exception_ptr(boost::system::system_error(ec)); ios.stop(); }); ios.run(); if (err != std::exception_ptr()) std::rethrow_exception(err); }
OPP_INLINE static SYNC_UWORD8_T opp_alloc4_count_slots(SYNC_UWORD16_T size, const SYNC_UWORD16_T obj_size, const SYNC_UWORD16_T pool_size) { SYNC_UWORD8_T slot_count = 1; if(!size) /* the default size is one slot */ return 1; size += sizeof(struct opp_object); /* we need to add object header */ OPP_NORMALIZE_SIZE(size); /* we need to convert the size into a chunk size */ /* TODO optimize the following line */ slot_count = size / obj_size + ((size % obj_size)?1:0); /* find the number of slot_count needed */ /* number of slot_count must be smaller than bit per string */ if(slot_count > BIT_PER_STRING) { SYNC_LOG(SYNC_ERROR, "Alloc failed:solt %d cannot fit in %d-bit header for request size %d\n", slot_count, BIT_PER_STRING, size); return 0; } /* number of slot_count must be smaller than object pool size */ if(slot_count > pool_size) { SYNC_LOG(SYNC_ERROR, "Alloc failed:Cannot fit solts %d in pool of %d\n", slot_count, pool_size); return 0; } /* apply some invariant testing */ SYNC_ASSERT(slot_count != 0); SYNC_ASSERT(slot_count <= BIT_PER_STRING && slot_count <= pool_size); return slot_count; }
void freceive_sync(boost::asio::io_service &, aziomq::socket & socket, const const_buf_vec & expected_bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); // create vector of raw bufs to fill from length of expected_bufs buf_vec_t buf_vec(std::distance(std::begin(expected_bufs), std::end(expected_bufs))); zero(buf_vec); // create azio buffer vector mutable_buf_vec bufs; init(bufs, buf_vec); socket.receive(bufs, flags); auto e = check_res(bufs, expected_bufs); if (e != std::exception_ptr()) std::rethrow_exception(e); }
C_CAPSULE_START OPP_INLINE struct opp_pool*opp_factory_create_pool_donot_use(struct opp_factory*obuff, struct opp_pool*addpoint, void*nofreememory) { // allocate a block of memory struct opp_pool*pool = (struct opp_pool*)nofreememory; opp_factory_profiler_checkleak(); if(!pool && !(pool = (struct opp_pool*)profiler_replace_malloc(obuff->memory_chunk_size))) { SYNC_LOG(SYNC_ERROR, "Out of memory\n"); return NULL; } if(!obuff->pools) { obuff->pools = pool; pool->idx = 0; pool->next = NULL; } else { SYNC_ASSERT(addpoint); // insert the pool in appropriate place pool->next = addpoint->next; addpoint->next = pool; pool->idx = addpoint->idx+1; } if(pool->idx > 64) { opp_watchdog_report(WATCHDOG_ALERT, "pool->idx > 64"); } obuff->pool_count++; opp_factory_profiler_checkleak(); SYNC_UWORD8_T*ret = (SYNC_UWORD8_T*)(pool+1); /* clear bitstring */ memset(ret, 0, obuff->bitstring_size); // setup pool pool->bitstring = (BITSTRING_TYPE*)ret; #ifdef OPP_HAS_RECYCLING pool->recycled = NULL; #endif pool->head = ret + (obuff->bitstring_size); pool->end = ret + obuff->memory_chunk_size - sizeof(struct opp_pool); pool->flags = nofreememory?0:OPP_POOL_FREEABLE; CHECK_POOL(pool); return pool; }
void freceive_more_sync(boost::asio::io_service &, aziomq::socket & socket, const const_buf_vec & expected_bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); // create vector of raw bufs to fill from length of expected_bufs buf_vec_t buf_vec(std::distance(std::begin(expected_bufs), std::end(expected_bufs))); zero(buf_vec); // create azio buffer vector mutable_buf_vec bufs; init(bufs, buf_vec); aziomq::socket::more_result mr = std::make_pair(0, true); for (auto it = std::begin(bufs), e = std::end(bufs); mr.second && it != e; ++it) { mr = socket.receive_more(boost::asio::buffer(*it), flags); } auto e = check_res(bufs, expected_bufs); if (e != std::exception_ptr()) std::rethrow_exception(e); }
void fsend_sync(boost::asio::io_service &, aziomq::socket & socket, const const_buf_vec & bufs, int flags) { SYNC_LOG(__PRETTY_FUNCTION__); socket.send(bufs, flags); }