void *spx_object_new_alone(const size_t s,err_t *err){ if(0 == s){ *err = EINVAL; return NULL; } return spx_alloc(1,s,err); }
err_t spx_lazy_mmap_nb(SpxLogDelegate *log,char *ptr,int sock,size_t size,off_t begin){/*{{{*/ err_t err = 0; size_t len = 0; byte_t *ctx = spx_alloc(SpxKB,sizeof(byte_t),&err); if(NULL == ctx){ SpxLogFmt2(log,SpxLogError,err, "alloc buffer for lazy recv is fail.,msg size:%lld.", SpxKB); return err; } size_t totalsize = SpxMax(size,SpxKB); size_t recvbytes = 0; while(recvbytes < totalsize){ size_t recvs = SpxMin(SpxKB,(totalsize - recvbytes)); err = spx_read_nb(sock,ctx,recvs,&len); if(0 != err ){ SpxLogFmt2(log,SpxLogError,err, "lazy recv is fail.recvsize:%lld,realsize:%lld,writed size:%lld.", recvs,len,recvbytes); break; } len = 0; memcpy(ptr + begin + recvbytes,ctx,recvs); recvbytes += recvs; memset(ctx,0,recvs); } SpxFree(ctx); return err; }/*}}}*/
DIdxSet::DIdxSet(const DIdxSet& old) : IdxSet() { len = old.size(); len = (len < 1) ? 1 : len; spx_alloc(idx, len); IdxSet::operator= ( old ); }
#include <stdlib.h> #include <string.h> #include <errno.h> #include "spx_alloc.h" #include "spx_errno.h" #include "spx_message.h" #include "spx_types.h" #include "spx_string.h" #include "spx_defs.h" union d2i{ double v; uint64_t i; }; union f2i{ float v; uint32_t i; }; struct spx_msg *spx_msg_new(const size_t len,err_t *err){/*{{{*/ if (0 == len){ *err = EINVAL; return NULL; } struct spx_msg *ctx = spx_alloc_alone(sizeof(*ctx),err); if(NULL == ctx){ return NULL; } ctx->buf = spx_alloc_alone(len ,err); if(NULL == ctx->buf){ goto r1; } ctx->last = ctx->buf; ctx->s = len; ctx->busylen = 0; return ctx; r1: SpxFree(ctx); return NULL; }/*}}}*/ err_t spx_msg_free(struct spx_msg **ctx){/*{{{*/ if(NULL != (*ctx)->buf){ SpxFree((*ctx)->buf); } (*ctx)->last = NULL; (*ctx)->s = 0; SpxFree(*ctx); return 0; }/*}}}*/ err_t spx_msg_seek(struct spx_msg *ctx,off_t offset,int whence){ if(NULL == ctx || NULL == ctx->buf){ return EINVAL; } switch(whence){ case SpxMsgSeekSet :{ ctx->last =(uchar_t *) SpxMemIncr(ctx->buf,offset); break; } case SpxMsgSeekCurrent:{ ctx->last =(uchar_t *) SpxMemIncr(ctx->last,offset); break; } case SpxMsgSeekEnd:{ ctx->last =(uchar_t *) SpxMemIncr(ctx->buf,spx_msg_size(ctx) + offset); break; } } return 0; } void spx_msg_peek(struct spx_msg *ctx,off_t off){ ctx->last = (uchar_t*) (ctx->buf + off); } void spx_msg_front(struct spx_msg *ctx){ ctx->last = ctx->buf; } err_t spx_msg_align(struct spx_msg *ctx,off_t offset){ return spx_msg_seek(ctx,offset,SpxMsgSeekCurrent); } void spx_msg_clear(struct spx_msg *ctx){ SpxZeroLen(ctx->buf,ctx->s); ctx->last = ctx->buf; ctx->busylen = 0; ctx->err = 0; } err_t spx_msg_pack_int( struct spx_msg *ctx,const int v){/*{{{*/ return spx_msg_pack_i32(ctx,(i32_t) v); }/*}}}*/ err_t spx_msg_pack_i8(struct spx_msg *ctx,const i8_t v){/*{{{*/ if(NULL == ctx) return EINVAL; *(ctx->last) = (char) v; (ctx->last)++; ctx->busylen++; return 0; }/*}}}*/ err_t spx_msg_pack_i32( struct spx_msg *ctx,const i32_t v){/*{{{*/ if(NULL == ctx) return EINVAL; spx_msg_i2b(ctx->last,v); (ctx->last) += sizeof(i32_t); ctx->busylen += sizeof(i32_t); return 0; }/*}}}*/ err_t spx_msg_pack_i64( struct spx_msg *ctx,const i64_t v) {/*{{{*/ if (NULL == ctx) return EINVAL; spx_msg_l2b(ctx->last,v); (ctx->last) += sizeof(i64_t); ctx->busylen += sizeof(i64_t); return 0; }/*}}}*/ err_t spx_msg_pack_u8( struct spx_msg *ctx,const u8_t v){/*{{{*/ if (NULL == ctx) return EINVAL; *(ctx->last) = (uchar_t) v; (ctx->last)++; ctx->busylen ++; return 0; }/*}}}*/ err_t spx_msg_pack_u32( struct spx_msg *ctx,const u32_t v){/*{{{*/ if(NULL == ctx) return EINVAL; spx_msg_i2b(ctx->last,(i32_t) v); (ctx->last) += sizeof(u32_t); ctx->busylen += sizeof(u32_t); return 0; }/*}}}*/ err_t spx_msg_pack_u64( struct spx_msg *ctx,const u64_t v){/*{{{*/ if(NULL == ctx) return EINVAL; spx_msg_l2b(ctx->last,(i64_t) v); (ctx->last) += sizeof(u64_t); ctx->busylen += sizeof(u64_t); return 0; }/*}}}*/ err_t spx_msg_pack_double( struct spx_msg *ctx,const double v){/*{{{*/ if (NULL == ctx) return EINVAL; union d2i n; SpxZero(n); n.v = v; spx_msg_l2b(ctx->last,n.i); (ctx->last) += sizeof(n.i); (ctx->busylen) += sizeof(n.i); return 0; }/*}}}*/ err_t spx_msg_pack_float( struct spx_msg *ctx,const float v){/*{{{*/ if (NULL == ctx) return EINVAL; union f2i n; SpxZero(n); n.v = v; spx_msg_i2b(ctx->last,n.i); (ctx->last) += sizeof(n.i); (ctx->busylen) += sizeof(n.i); return 0; }/*}}}*/ err_t spx_msg_pack_true( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; *(ctx->last) = (uchar_t) true; (ctx->last)++; ctx->busylen ++; return 0; }/*}}}*/ err_t spx_msg_pack_false( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; *(ctx->last) = (uchar_t) false; (ctx->last)++; ctx->busylen ++; return 0; }/*}}}*/ err_t spx_msg_pack_string( struct spx_msg *ctx,string_t s){/*{{{*/ if (NULL == ctx) return EINVAL; ctx->last = SpxMemcpy(ctx->last,s,spx_string_len(s)); ctx->busylen += spx_string_len(s); return 0; }/*}}}*/ err_t spx_msg_pack_fixed_string( struct spx_msg *ctx,string_t s,size_t len){/*{{{*/ if (NULL == ctx) return EINVAL; ctx->last = SpxMemcpy(ctx->last,s,spx_string_len(s)); ctx->last += len - spx_string_len(s); ctx->busylen += len; return 0; }/*}}}*/ err_t spx_msg_pack_ubytes( struct spx_msg *ctx,const ubyte_t *b,const size_t len){/*{{{*/ if (NULL == ctx) return EINVAL; ctx->last = SpxMemcpy(ctx->last,b,len); ctx->busylen += len; return 0; }/*}}}*/ err_t spx_msg_pack_bytes( struct spx_msg *ctx,const byte_t *b,const size_t len){/*{{{*/ if (NULL == ctx) return EINVAL; ctx->last = SpxMemcpy(ctx->last,b,len); ctx->busylen += len; return 0; }/*}}}*/ err_t spx_msg_pack_fixed_chars( struct spx_msg *ctx,const char *b,const size_t len){/*{{{*/ if (NULL == ctx) return EINVAL; size_t size = strlen(b); ctx->last = SpxMemcpy(ctx->last,b,size); ctx->last += len - size; ctx->busylen += len; return 0; }/*}}}*/ int spx_msg_unpack_int( struct spx_msg *ctx){/*{{{*/ return spx_msg_unpack_i32(ctx); }/*}}}*/ i8_t spx_msg_unpack_i8( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; i8_t n = 0; n = (i8_t) *(ctx->last); ctx->last++; return n; }/*}}}*/ i32_t spx_msg_unpack_i32( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; i32_t v = spx_msg_b2i(ctx->last); ctx->last += sizeof(i32_t); return v; }/*}}}*/ i64_t spx_msg_unpack_i64( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; i64_t v = spx_msg_b2l(ctx->last); ctx->last += sizeof(i64_t); return v; }/*}}}*/ u8_t spx_msg_unpack_u8( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; i8_t n = 0; n = (i8_t) *(ctx->last); ctx->last++; return n; }/*}}}*/ u32_t spx_msg_unpack_u32( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; u32_t v = spx_msg_b2i(ctx->last); ctx->last += sizeof(u32_t); return v; }/*}}}*/ u64_t spx_msg_unpack_u64( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; u64_t v = spx_msg_b2l(ctx->last); ctx->last += sizeof(u64_t); return v; }/*}}}*/ double spx_msg_unpack_double( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; union d2i n; SpxZero(n); n.i = (u64_t) spx_msg_b2l(ctx->last); ctx->last += sizeof(n.i); return n.v; }/*}}}*/ float spx_msg_unpack_float( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; union f2i n; SpxZero(n); n.i = (u32_t) spx_msg_b2i(ctx->last); ctx->last += sizeof(n.i); return n.v; }/*}}}*/ bool_t spx_msg_unpack_bool( struct spx_msg *ctx){/*{{{*/ if (NULL == ctx) return EINVAL; u8_t n = spx_msg_unpack_u8(ctx); return (bool_t) n; }/*}}}*/ string_t spx_msg_unpack_string( struct spx_msg *ctx,\ const size_t len,err_t *err){/*{{{*/ string_t p = NULL; p = spx_string_newlen(ctx->last,len,err); ctx->last += len; return p; }/*}}}*/ ubyte_t *spx_msg_unpack_ubytes( struct spx_msg *ctx,const size_t len,err_t *err){/*{{{*/ ubyte_t *buff = spx_alloc(len,sizeof(byte_t),err); if(NULL == buff){ return NULL; } size_t alive = ctx->s - (ctx->buf - ctx->last); memcpy(buff,ctx->last,(int) SpxMin(alive,len)); ctx->last += len; return buff; }/*}}}*/
SPxOut::SPxOut(const SPxOut& rhs) { m_verbosity = rhs.m_verbosity; m_streams = 0; spx_alloc(m_streams, INFO3+1); m_streams = new (m_streams) std::ostream*[INFO3+1]; m_streams[ ERROR ] = m_streams[ WARNING ] = rhs.m_streams[ERROR]; for ( int i = DEBUG; i <= INFO3; ++i ) m_streams[ i ] = rhs.m_streams[ i ]; }
/// constructor SPxOut::SPxOut() : m_verbosity( ERROR ) , m_streams(0) { spx_alloc(m_streams, INFO3+1); m_streams = new (m_streams) std::ostream*[INFO3+1]; m_streams[ ERROR ] = m_streams[ WARNING ] = &std::cerr; for ( int i = DEBUG; i <= INFO3; ++i ) m_streams[ i ] = &std::cout; }
DIdxSet::DIdxSet(int n) : IdxSet() { len = (n < 1) ? 1 : n; spx_alloc(idx, len); }
void DSVector::allocMem(int len) { spx_alloc(theelem, len); setMem(len, theelem); }