--- libelwix/src/vars.c 2013/03/07 23:04:48 1.3 +++ libelwix/src/vars.c 2013/11/14 13:58:12 1.5.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: vars.c,v 1.3 2013/03/07 23:04:48 misho Exp $ +* $Id: vars.c,v 1.5.4.1 2013/11/14 13:58:12 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -46,7 +46,7 @@ SUCH DAMAGE. #include "global.h" -static int +static inline int vars2buffer(u_char * __restrict buf, int buflen, int be, array_t * __restrict vars) { int Limit = 0; @@ -123,6 +123,7 @@ vars2buffer(u_char * __restrict buf, int buflen, int b break; case buffer: case string: + case ptr: if (AIT_LEN(val) > buflen - Limit) { elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); @@ -145,7 +146,7 @@ vars2buffer(u_char * __restrict buf, int buflen, int b return Limit; } -static array_t * +static inline array_t * buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy) { array_t *vars; @@ -218,7 +219,8 @@ buffer2vars(u_char * __restrict buf, int buflen, int v val->val.net = le64toh(v[i].val.net); break; case data: - /* WARNING:: remap data type to buffer */ + case ptr: + /* WARNING:: remap data and ptr type to buffer! */ val->val_type = buffer; case buffer: case string: @@ -258,7 +260,7 @@ buffer2vars(u_char * __restrict buf, int buflen, int v * @vars = Variable array * return: -1 error, 0 nothing done or >0 size of marshaled data */ -inline int +int ait_vars2buffer(u_char * __restrict buf, int buflen, array_t * __restrict vars) { return vars2buffer(buf, buflen, 42, vars); @@ -274,7 +276,7 @@ ait_vars2buffer(u_char * __restrict buf, int buflen, a *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy() * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy() */ -inline array_t * +array_t * ait_buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy) { return buffer2vars(buf, buflen, vnum, zcpy); @@ -290,7 +292,7 @@ ait_buffer2vars(u_char * __restrict buf, int buflen, i * @vars = Variable array * return: -1 error, 0 nothing done or >0 size of marshaled data */ -inline int +int ait_vars2map(u_char *buf, int buflen, array_t *vars) { return vars2buffer(buf, buflen, 0, vars); @@ -306,7 +308,7 @@ ait_vars2map(u_char *buf, int buflen, array_t *vars) *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy() * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy() */ -inline array_t * +array_t * ait_map2vars(u_char *buf, int buflen, int vnum, int zcpy) { return buffer2vars(buf, buflen, vnum, zcpy); @@ -321,7 +323,7 @@ ait_map2vars(u_char *buf, int buflen, int vnum, int zc * @varnum = Number of variables * return: =NULL error or !=NULL allocated array */ -inline array_t * +array_t * ait_allocVars(int varnum) { array_t *arr; @@ -349,7 +351,7 @@ ait_allocVars(int varnum) * @n = index of variable into array * return: NULL error or !=NULL ait_val_t element */ -inline ait_val_t * +ait_val_t * ait_getVars(array_t ** __restrict vars, int n) { register int i; @@ -382,7 +384,7 @@ ait_getVars(array_t ** __restrict vars, int n) * @vars = Variable array * return: -1 error or size of array */ -inline int +int ait_clrVars(array_t * __restrict vars) { register int i; @@ -404,24 +406,66 @@ ait_clrVars(array_t * __restrict vars) * @vars = Variable array * return: none */ -inline void +void ait_freeVars(array_t ** __restrict vars) { + register int i; + ait_val_t *v; + if (!vars || !*vars) return; - ait_clrVars(*vars); - array_Free(*vars); + for (i = 0; i < array_Size(*vars); i++) + if ((v = array(*vars, i, ait_val_t*))) { + AIT_FREE_VAL(v); + /* free memory if isn't zero copy */ + if (!AIT_IN(v) && (*vars)->arr_data[i]) { + e_free((*vars)->arr_data[i]); + (*vars)->arr_data[i] = NULL; + } + } + (*vars)->arr_last = -1; + array_Destroy(vars); } +/* + * ait_resideVars() - Calculate footprint of resided variables into array + * + * @vars = Variable array + * return: bytes for whole array + */ +size_t +ait_resideVars(array_t * __restrict vars) +{ + size_t ret = 0; + register int i; + if (vars) { + ret = array_Size(vars) * sizeof(ait_val_t); + for (i = 0; i < array_Size(vars); i++) + switch (AIT_TYPE(array(vars, i, ait_val_t*))) { + case buffer: + case string: + case data: + case ptr: + ret += AIT_LEN(array(vars, i, ait_val_t*)); + break; + default: + break; + } + } + + return ret; +} + + /* * ait_allocVar() - Allocate memory for variable * * return: NULL error or new variable, after use free variable with ait_freeVar() */ -inline ait_val_t * +ait_val_t * ait_allocVar(void) { ait_val_t *v = NULL; @@ -442,7 +486,7 @@ ait_allocVar(void) * @val = Variable * return: none */ -inline void +void ait_freeVar(ait_val_t ** __restrict val) { if (val && *val) { @@ -570,7 +614,7 @@ _cmp_arr_val_desc(const void *a, const void *b) * @cmp = Custom compare function for sorting. If =NULL compare by value * return: none */ -inline void +void ait_sortVarsByVal(array_t * __restrict vars, int order, int (*cmp)(const void*, const void*)) { if (!vars) @@ -591,7 +635,7 @@ ait_sortVarsByVal(array_t * __restrict vars, int order * @order = Sort order. If =0 ascend or !=0 descend * return: none */ -inline void +void ait_sortVarsByKey(array_t * __restrict vars, int order) { if (!vars) @@ -702,7 +746,7 @@ ait_hashVar(ait_val_t * __restrict v, const char * __r * @vars = Variables * return -1 error or 0 ok */ -inline int +int ait_hashKeyVars(array_t * __restrict vars) { register int i; @@ -723,7 +767,7 @@ ait_hashKeyVars(array_t * __restrict vars) * @key = Search string * return: NULL error or not found, !=NULL valid element */ -inline ait_val_t * +ait_val_t * ait_findKeyHash(array_t * __restrict vars, const char * __restrict key) { u_short k = 0; @@ -777,7 +821,7 @@ ait_sprintfVar(ait_val_t * __restrict v, const char *f * @... = data * return: -1 error or 0 ok */ -inline int +int ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, u_int l, ...) { va_list lst; @@ -812,7 +856,7 @@ ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, * @v = variable * return: return raw data */ -inline uint64_t +uint64_t ait_getlikeVar(ait_val_t * __restrict v) { if (!v) @@ -828,7 +872,7 @@ ait_getlikeVar(ait_val_t * __restrict v) * @b = 2nd variable * return: 0 is equal or !=0 is different */ -inline int +int ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b) { intptr_t ret;