Skip to content

Commit

Permalink
evec: (26%)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyht committed Apr 10, 2019
1 parent 396f1f1 commit 91ce1bc
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/applications/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ set(KIT_DEPENDS)
# - 添加测试 -
# ETestAdd([NAME] [ON|OFF] [DEST Path] [CREATE])
#
ETestAdd(perf_test ON CREATE)
ETestAdd(perf_test OFF CREATE)
32 changes: 16 additions & 16 deletions src/libs/etools/etype.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,28 @@ typedef union eval_s{
constr C_s, C_sa[1], * C_sp; // const ver
}eval_t, eval, * evalp;

#define eval_mk(_v, v) (eval_t){._v = v}
#define EVAL_MK(_v, v) (eval_t){._v = v}

#define EVAL_ZORE eval_mk(p, 0)
#define EVAL_ZORE EVAL_MK(p, 0)

#define EVAL_I8( v) eval_mk(i8 = v);
#define EVAL_I16(v) eval_mk(i16 = v);
#define EVAL_I32(v) eval_mk(i32 = v);
#define EVAL_I64(v) eval_mk(i64 = v);
#define EVAL_I8( v) EVAL_MK(i8 , v)
#define EVAL_I16(v) EVAL_MK(i16, v)
#define EVAL_I32(v) EVAL_MK(i32, v)
#define EVAL_I64(v) EVAL_MK(i64, v)

#define EVAL_U8( v) eval_mk(u8 = v);
#define EVAL_U16(v) eval_mk(i16 = v);
#define EVAL_U32(v) eval_mk(i32 = v);
#define EVAL_U64(v) eval_mk(i64 = v);
#define EVAL_U8( v) EVAL_MK(u8 , v)
#define EVAL_U16(v) EVAL_MK(i16, v)
#define EVAL_U32(v) EVAL_MK(i32, v)
#define EVAL_U64(v) EVAL_MK(i64, v)

#define EVAL_F32(v) eval_mk(f32 = v);
#define EVAL_F64(v) eval_mk(f64 = v);
#define EVAL_F32(v) EVAL_MK(f32, v)
#define EVAL_F64(v) EVAL_MK(f64, v)

#define EVAL_S( v) eval_mk(s = v);
#define EVAL_P( v) eval_mk(p = v);
#define EVAL_S( v) EVAL_MK(s, v)
#define EVAL_P( v) EVAL_MK(p, v)

#define EVAL_CS( v) eval_mk(C_s = v);
#define EVAL_CP( v) eval_mk(C_p = v);
#define EVAL_CS( v) EVAL_MK(C_s, v)
#define EVAL_CP( v) EVAL_MK(C_p, v)

#pragma pack()

Expand Down
2 changes: 1 addition & 1 deletion src/libs/etools/evar.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ evar evar_gen (etypev t, int cnt, int size)
return (evar){ t | __ETYPEV_PTR_MASK,
size,
cnt,
eval_mk(p, ecalloc(cnt, size))
EVAL_P(ecalloc(cnt, size))
};
}

Expand Down
70 changes: 37 additions & 33 deletions src/libs/etools/evar.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,39 @@ typedef enum etypev_s{
__E_EVAR = 0x1f , // internal using

//! val
E_NAV = 0, // not a var
E_NAV = 0, // not a var

#undef _XX
#define _XX(type, v) \
E_ ## type = v, \
E_ ## type ## _a = v | __ETYPEV_ARR_MASK, \
E_ ## type ## _p = v | __ETYPEV_PTR_MASK

_XX(CHAR, 1), // treat CHAR as I8, they are typically the same
_XX(CHAR, 1), // E_CHAR treat CHAR as I8, they are typically the same

_XX(I8 , 1),
_XX(I16 , 2),
_XX(I32 , 3),
_XX(I64 , 4),
_XX(I8 , 1), // E_I8
_XX(I16 , 2), // E_I16
_XX(I32 , 3), // E_I32
_XX(I64 , 4), // E_I64

_XX(U8 , 5),
_XX(U16 , 6),
_XX(U32 , 7),
_XX(U64 , 8),
_XX(U8 , 5), // E_U8
_XX(U16 , 6), // E_U16
_XX(U32 , 7), // E_U32
_XX(U64 , 8), // E_U64

_XX(F32 , 9),
_XX(F64 , 10),
_XX(F32 , 9), // E_F32
_XX(F64 , 10), // E_F64

_XX(PTR , 11),
_XX(STR , 12), // handle as ptr internal
_XX(RAW , 13), // handle as ptr internal
_XX(PTR , 11), // E_PTR
_XX(STR , 12), // E_STR handle as ptr internal
_XX(RAW , 13), // E_RAW handle as ptr internal

_XX(USER, 14),
_XX(USER, 14), // E_USER

}etypev;

#define __EVAR_ITEM_LEN_MAP {0, 1, 2, 4, 8, 1, 2, 4, 8, 4, 8, 8, 8, 8}

//! __ETYPEV_ARR_MASK:
//!
//! |type|size|cnt|v |
Expand All @@ -81,31 +83,33 @@ typedef struct evar_s{
uint type : 8; // type
uint size : 24; // element size
uint cnt ; // element count
eval_t v ; // val(data)
eval v ; // val(data)
}evar_t, evar, * evarp;

#define evar_mk(_t, _p, _v) (evar_t){.type = _t, .v._p = _v}
#define EVAR_MK(_t, _p, _v) (evar){.type = _t, .v._p = _v}

#define EVAR_ZORE EVAR_MK(E_NAV, p, 0)

#define EVAR_ZORE evar_mk(E_NAV, p, 0)
#define EVAR_I8( v) EVAR_MK(E_I8 , i8 , (v))
#define EVAR_I16(v) EVAR_MK(E_I16, i16, (v))
#define EVAR_I32(v) EVAR_MK(E_I32, i32, (v))
#define EVAR_I64(v) EVAR_MK(E_I64, i64, (v))

#define EVAR_I8 (v) evar_mk(E_I8 , i8 , (v))
#define EVAR_I16(v) evar_mk(E_I16, i16, (v))
#define EVAR_I32(v) evar_mk(E_I32, i32, (v))
#define EVAR_I64(v) evar_mk(E_I64, i64, (v))
#define EVAR_U8( v) EVAR_MK(E_U8 , u8 , (v))
#define EVAR_U16(v) EVAR_MK(E_U16, u16, (v))
#define EVAR_U32(v) EVAR_MK(E_U32, u32, (v))
#define EVAR_U64(v) EVAR_MK(E_U64, u64, (v))

#define EVAR_U8 (v) evar_mk(E_U8 , u8 , (v))
#define EVAR_U16(v) evar_mk(E_U16, u16, (v))
#define EVAR_U32(v) evar_mk(E_U32, u32, (v))
#define EVAR_U64(v) evar_mk(E_U64, u64, (v))
#define EVAR_F32(v) EVAR_MK(E_F32, f32, (v))
#define EVAR_F64(v) EVAR_MK(E_F64, f64, (v))

#define EVAR_F32(v) evar_mk(E_F32, f32, (v))
#define EVAR_F64(v) evar_mk(E_F64, f64, (v))
#define EVAR_S( v) EVAR_MK(E_STR, s, (v))
#define EVAR_P( v) EVAR_MK(E_PTR, p, (v))

#define EVAR_S( v) evar_mk(E_STR, s, (v))
#define EVAR_P( v) evar_mk(E_PTR, p, (v))
#define EVAR_CS( v) EVAR_MK(E_STR, C_s, (v))
#define EVAR_CP( v) EVAR_MK(E_PTR, C_p, (v))

#define EVAR_CS( v) evar_mk(E_STR, C_s, (v))
#define EVAR_CP( v) evar_mk(E_PTR, C_p, (v))
#define EVAR_RAW(p, l) (evar){E_RAW | __ETYPEV_PTR_MASK, (l), 1, EVAL_CP(p)}

//! for stack using
evar evar_gen (etypev t, int cnt, int size); // create automaticlly, call evar_free() after using it
Expand Down
87 changes: 50 additions & 37 deletions src/libs/etools/evec.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,16 @@ static evec __evec_new(etypev type, int size);

evec evec_new(etypev type)
{
static const u8 _size_map[] = {0, 1, 2, 4, 8, 1, 2, 4, 8, 8, 8, 1};
static const u8 _size_map[] = __EVAR_ITEM_LEN_MAP;

type &= __ETYPEV_VAR_MASK;

return __evec_new(type, _size_map[type]);
return type >= E_USER ? 0 : __evec_new(type, _size_map[type]);
}

evec evec_newEx(int size)
{
return __evec_new(E_RAW, size);
return __evec_new(E_USER, size);
}

static evec __evec_new(etypev type, int size)
Expand All @@ -843,8 +843,9 @@ static evec __evec_new(etypev type, int size)
return out;
}

uint evec_len(evec v) { return v ? _v_cnt(v) : 0; }
uint evec_cap(evec v) { return v ? _v_cap(v) : 0; }
uint evec_len (evec v) { return v ? _v_cnt (v) : 0; }
uint evec_cap (evec v) { return v ? _v_cap (v) : 0; }
uint evec_size(evec v) { return v ? _v_size(v) : 0; }

/** -----------------------------------------------------
*
Expand Down Expand Up @@ -925,57 +926,69 @@ static void __split_destroy_ex(_split s, eobj_rls_ex_cb rls, eval prvt)
* ------------------------------------------------------
*/
static void __evec_get_pos(evec v, uint idx, _pos p );
static bool __evec_addV (evec v, uint idx, eval val, etypev type);

bool evec_pushV(evec v, eval val) { return __evec_addV(v, 0 , val , v ? _v_type(v): E_NAV); }
bool evec_pushI(evec v, i64 val) { return __evec_addV(v, 0 , eval_mk(i , val), E_I64); }
bool evec_pushF(evec v, f64 val) { return __evec_addV(v, 0 , eval_mk(f , val), E_F64); }
bool evec_pushS(evec v, constr str) { return __evec_addV(v, 0 , eval_mk(C_s, str), E_STR); }
bool evec_pushP(evec v, conptr ptr) { return __evec_addV(v, 0 , eval_mk(C_p, ptr), E_PTR); }
bool evec_pushR(evec v, uint len) { return __evec_addV(v, 0 , eval_mk(u32, len), E_RAW); }

bool evec_appdV(evec v, eval val) { return __evec_addV(v, UINT_MAX, val , v ? _v_type(v): E_NAV); }
bool evec_appdI(evec v, i64 val) { return __evec_addV(v, UINT_MAX, eval_mk(i , val), E_I64); }
bool evec_appdF(evec v, f64 val) { return __evec_addV(v, UINT_MAX, eval_mk(f , val), E_F64); }
bool evec_appdS(evec v, constr str) { return __evec_addV(v, UINT_MAX, eval_mk(C_s, str), E_STR); }
bool evec_appdP(evec v, conptr ptr) { return __evec_addV(v, UINT_MAX, eval_mk(C_p, ptr), E_PTR); }
bool evec_appdR(evec v, uint len) { return __evec_addV(v, UINT_MAX, eval_mk(u32, len), E_RAW); }

bool evec_addV(evec v, uint i, eval val) { return __evec_addV(v, i, val , v ? _v_type(v): E_NAV); }
bool evec_addI(evec v, uint i, i64 val) { return __evec_addV(v, i, eval_mk(i , val), E_I64); }
bool evec_addF(evec v, uint i, f64 val) { return __evec_addV(v, i, eval_mk(f , val), E_F64); }
bool evec_addS(evec v, uint i, constr str) { return __evec_addV(v, i, eval_mk(C_s, str), E_STR); }
bool evec_addP(evec v, uint i, conptr ptr) { return __evec_addV(v, i, eval_mk(C_p, ptr), E_PTR); }
bool evec_addR(evec v, uint i, uint len) { return __evec_addV(v, i, eval_mk(u32, len), E_RAW); }

static bool __evec_addV(evec v, uint idx, eval val, etypev type)
static bool __evec_addV (evec v, uint idx, evar var);

bool evec_pushV(evec v, evar var) { return __evec_addV(v, 0 , var ); }
bool evec_pushI(evec v, i64 val) { return __evec_addV(v, 0 , EVAR_I64(val)); }
bool evec_pushF(evec v, f64 val) { return __evec_addV(v, 0 , EVAR_F64(val)); }
bool evec_pushS(evec v, constr str) { return __evec_addV(v, 0 , EVAR_CS (str)); }
bool evec_pushP(evec v, conptr ptr) { return __evec_addV(v, 0 , EVAR_CP (ptr)); }
bool evec_pushR(evec v, uint len) { return __evec_addV(v, 0 , EVAR_RAW(0, len)); }

bool evec_appdV(evec v, evar var) { return __evec_addV(v, UINT_MAX, var ); }
bool evec_appdI(evec v, i64 val) { return __evec_addV(v, UINT_MAX, EVAR_I64(val)); }
bool evec_appdF(evec v, f64 val) { return __evec_addV(v, UINT_MAX, EVAR_F64(val)); }
bool evec_appdS(evec v, constr str) { return __evec_addV(v, UINT_MAX, EVAR_CS (str)); }
bool evec_appdP(evec v, conptr ptr) { return __evec_addV(v, UINT_MAX, EVAR_CP (ptr)); }
bool evec_appdR(evec v, uint len) { return __evec_addV(v, UINT_MAX, EVAR_RAW(0, len)); }

bool evec_addV(evec v, uint i, evar var) { return __evec_addV(v, i, var ); }
bool evec_addI(evec v, uint i, i64 val) { return __evec_addV(v, i, EVAR_I64(val)); }
bool evec_addF(evec v, uint i, f64 val) { return __evec_addV(v, i, EVAR_F64(val)); }
bool evec_addS(evec v, uint i, constr str) { return __evec_addV(v, i, EVAR_CS (str)); }
bool evec_addP(evec v, uint i, conptr ptr) { return __evec_addV(v, i, EVAR_CP (ptr)); }
bool evec_addR(evec v, uint i, uint len) { return __evec_addV(v, i, EVAR_RAW(0, len)); }

static bool __evec_addV(evec v, uint idx, evar var)
{
_pos_t info;

is1_ret(!v || type != _v_type(v), false);
var.type &= __ETYPEV_VAR_MASK;

if(type > E_PTR)
is1_ret(!v || var.type != _v_type(v), false);

if(var.type > E_PTR)
{
switch (type)
switch (var.type)
{
case E_STR : val.s = strdup (val.s ); break;
case E_RAW : val.p = emalloc(val.u32); break;
case E_STR : var.v.s = strdup (var.v.s ); break;

case E_RAW : {
char* buf = emalloc(var.size);
if(var.v.p)
{
memcpy(buf, var.v.p, var.size);
}
var.v.p = buf;
}
break;

default : break;
}
}

if(idx == 0)
{
return __split_push(_v_head(v), val.r, _v_size(v));
return __split_push(_v_head(v), var.v.r, _v_size(v));
}
else if(idx >= _v_cnt(v))
{
return __split_appd(_v_tail(v), val.r, _v_size(v));
return __split_appd(_v_tail(v), var.v.r, _v_size(v));
}

__evec_get_pos(v, idx, &info);

return __split_insert(info.s, val.r, _v_size(v), info.pos, 1);
return __split_insert(info.s, var.v.r, _v_size(v), info.pos, 1);
}

static void __evec_get_pos(evec v, uint idx, _pos p)
Expand Down
11 changes: 6 additions & 5 deletions src/libs/etools/evec.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ evec evec_newEx(int size);

bool evec_reserve(evec v);

uint evec_len(evec v);
uint evec_cap(evec v);
uint evec_len (evec v);
uint evec_cap (evec v);
uint evec_size(evec v);

int evec_clear (evec v);
int evec_clearEx(evec v, eobj_rls_ex_cb rls, eval prvt);
Expand All @@ -36,21 +37,21 @@ int evec_freeEx(evec v, eobj_rls_ex_cb rls, eval prvt);
*
* ------------------------------------------------------
*/
bool evec_pushV(evec v, eval val);
bool evec_pushV(evec v, evar var);
bool evec_pushI(evec v, i64 val);
bool evec_pushF(evec v, f64 val);
bool evec_pushS(evec v, constr str);
bool evec_pushP(evec v, conptr ptr);
bool evec_pushR(evec v, uint len);

bool evec_appdV(evec v, eval val);
bool evec_appdV(evec v, evar var);
bool evec_appdI(evec v, i64 val);
bool evec_appdF(evec v, f64 val);
bool evec_appdS(evec v, constr str);
bool evec_appdP(evec v, conptr ptr);
bool evec_appdR(evec v, uint len);

bool evec_addV(evec v, uint i, eval val);
bool evec_addV(evec v, uint i, evar var);
bool evec_addI(evec v, uint i, i64 str);
bool evec_addF(evec v, uint i, f64 str);
bool evec_addS(evec v, uint i, constr str); // hold as ptr actually
Expand Down
4 changes: 3 additions & 1 deletion src/libs/etools/testing/evec/test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ int evec_add_test()

char data[] = "0123456789";



for(int i = 0; i < 10; i++)
{
evec_addV(v, 10, *(eval*)&data[i]);
evec_addV(v, 10, EVAR_RAW(&data[i], 1));
}


Expand Down

0 comments on commit 91ce1bc

Please sign in to comment.