diff --git a/demo/lenet.c b/demo/lenet.c index a52ef6c..39e4cb6 100644 --- a/demo/lenet.c +++ b/demo/lenet.c @@ -74,15 +74,15 @@ int main(int argc, const char *argv[]) /* Parameters */ cc_tensor_t *conv1_w, *conv1_b, *conv2_w, *conv2_b, *fc1_w, *fc1_b, *fc2_w, *fc2_b; - cc_int32 shape_conv1_w[] = {32, 1, 5, 5, 0}; - cc_int32 shape_conv1_b[] = {32, 0}; - cc_int32 shape_conv2_w[] = {64, 32, 5, 5, 0}; - cc_int32 shape_conv2_b[] = {64, 0}; - cc_int32 shape_flat[] = {-1, 1, 1, 0}; - cc_int32 shape_fc1_w[] = {128, 3136, 1, 1, 0}; - cc_int32 shape_fc1_b[] = {128, 0}; - cc_int32 shape_fc2_w[] = {10, 128, 1, 1, 0}; - cc_int32 shape_fc2_b[] = {10, 0}; + cc_ssize shape_conv1_w[] = {32, 1, 5, 5, 0}; + cc_ssize shape_conv1_b[] = {32, 0}; + cc_ssize shape_conv2_w[] = {64, 32, 5, 5, 0}; + cc_ssize shape_conv2_b[] = {64, 0}; + cc_ssize shape_flat[] = {-1, 1, 1, 0}; + cc_ssize shape_fc1_w[] = {128, 3136, 1, 1, 0}; + cc_ssize shape_fc1_b[] = {128, 0}; + cc_ssize shape_fc2_w[] = {10, 128, 1, 1, 0}; + cc_ssize shape_fc2_b[] = {10, 0}; arg_parser(argc, (char**)argv); diff --git a/demo/simple.c b/demo/simple.c index a395c29..9510766 100644 --- a/demo/simple.c +++ b/demo/simple.c @@ -3,7 +3,7 @@ int main(int argc, const char *argv[]) { cc_tensor_t *tensor; - cc_int32 shape[] = {3, 3, 3, 0}; + cc_ssize shape[] = {3, 3, 3, 0}; tensor = cc_create(shape, CC_FLOAT32, "tensor0"); cc_property(tensor); cc_free(tensor); diff --git a/demo/vgg16.c b/demo/vgg16.c index ed9a933..d872b75 100644 --- a/demo/vgg16.c +++ b/demo/vgg16.c @@ -86,7 +86,7 @@ void arg_parser(int argc, char* const argv[]) void vgg16(cc_tensor_t *in, cc_tensor_t **out) { - static int __shape0[] = {-1, 1, 1, 0}; + static cc_ssize __shape0[] = {-1, 1, 1, 0}; static const char *p_namels[] = { "000.w", "000.b", "001.w", "001.b", "002.w", "002.b", "003.w", "003.b", "004.w", "004.b", "005.w", "005.b", "006.w", "006.b", diff --git a/src/cc_basic.c b/src/cc_basic.c index dc6bf07..afdb25f 100644 --- a/src/cc_basic.c +++ b/src/cc_basic.c @@ -11,28 +11,28 @@ #include "util_log.h" #include "cc_basic.h" -static cc_int32 _calc_elems(const cc_int32 *shape) +static cc_ssize _calc_elems(const cc_ssize *shape) { - cc_int32 elems; + cc_ssize elems; elems = *shape; while (*++shape) elems *= *shape; return elems; } -cc_int32 cc_elements(const cc_tensor_t *tensor) +cc_ssize cc_elements(const cc_tensor_t *tensor) { - cc_int32 elems; + cc_ssize elems; if (!tensor) return 0; elems = _calc_elems(tensor->shape); return elems; } -void cc_shape_fix(cc_int32 *shape, cc_int32 elems) +void cc_shape_fix(cc_ssize *shape, cc_ssize elems) { - cc_int32 v, i = 0, f = 0, s = 1; - const cc_int32 *sptr = shape; + cc_ssize v, i = 0, f = 0, s = 1; + const cc_ssize *sptr = shape; while ((v = *sptr)) { if (v == -1) { #ifdef ENABLE_CC_ASSERT @@ -49,10 +49,10 @@ void cc_shape_fix(cc_int32 *shape, cc_int32 elems) } } -cc_int32 cc_dimension(const cc_tensor_t *tensor) +cc_ssize cc_dimension(const cc_tensor_t *tensor) { - cc_int32 dim = 0; - const cc_int32 *sptr; + cc_ssize dim = 0; + const cc_ssize *sptr; if (!tensor) return 0; sptr = tensor->shape; @@ -61,10 +61,10 @@ cc_int32 cc_dimension(const cc_tensor_t *tensor) return dim; } -cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_int32 *shape) +cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_ssize *shape) { - cc_int32 elems; - const cc_int32 *sptr; + cc_ssize elems; + const cc_ssize *sptr; cc_shape_fix(shape, _calc_elems(tensor->shape)); #ifdef ENABLE_CC_ASSERT cc_assert_zero(_calc_elems(tensor->shape) - _calc_elems(shape)); @@ -77,9 +77,9 @@ cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_int32 *shape) list_erase(tensor->container, CC_TENSOR_SHAPE)); cc_assert_ptr( list_set_data(tensor->container, CC_TENSOR_SHAPE, - shape, (sptr - shape + 1) * sizeof(cc_int32))); + shape, (sptr - shape + 1) * sizeof(cc_ssize))); cc_assert_ptr( - tensor->shape = (cc_int32*) + tensor->shape = (cc_ssize*) list_index(tensor->container, CC_TENSOR_SHAPE)); return tensor; } @@ -87,9 +87,9 @@ cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_int32 *shape) int cc_compare_by_shape(const cc_tensor_t *a, const cc_tensor_t *b) { int ret = 0; - const cc_int32 *ptra = a->shape; - const cc_int32 *ptrb = b->shape; - while(!(ret = *(cc_int32*)ptra - *(cc_int32*)ptrb) && *ptra) { + const cc_ssize *ptra = a->shape; + const cc_ssize *ptrb = b->shape; + while(!(ret = *(cc_ssize*)ptra - *(cc_ssize*)ptrb) && *ptra) { ptra++; ptrb++; } @@ -101,22 +101,22 @@ int cc_compare_by_shape(const cc_tensor_t *a, const cc_tensor_t *b) } cc_tensor_t *cc_stack(cc_tensor_t **tsr, - cc_int32 ntsr, cc_int32 axis, const char *name) + cc_ssize ntsr, cc_ssize axis, const char *name) { cc_tensor_t *yield; - cc_int32 *shape; - cc_int32 i, dim, size, umem, ymem; - cc_int32 ncp = 0, nstp = 0; - cc_int32 off = 0, unit = 1; + cc_ssize *shape; + cc_ssize i, dim, size, umem, ymem; + cc_ssize ncp = 0, nstp = 0; + cc_ssize off = 0, unit = 1; dim = cc_dimension(tsr[0]); cc_assert_ptr(shape = - (cc_int32*)calloc(dim + 2, sizeof(cc_int32))); + (cc_ssize*)calloc(dim + 2, sizeof(cc_ssize))); if (dim == axis) { /* axis <= dim */ off = 1; shape[0] = 1; } memcpy(shape + off, - tsr[0]->shape, dim * sizeof(cc_int32)); + tsr[0]->shape, dim * sizeof(cc_ssize)); shape[dim + off - 1 - axis] *= ntsr; cc_assert_ptr(yield = cc_create(shape, *tsr[0]->dtype, name)); @@ -141,16 +141,16 @@ cc_tensor_t *cc_stack(cc_tensor_t **tsr, } cc_tensor_t *cc_concat(cc_tensor_t **tsr, - cc_int32 ntsr, cc_int32 axis, const char *name) + cc_ssize ntsr, cc_ssize axis, const char *name) { cc_tensor_t *yield; - cc_int32 *shape; - cc_int32 i, j, dim, raxis, umem, cmem; - cc_int32 ncp = 0, nseg = 1, unit = 1; + cc_ssize *shape; + cc_ssize i, j, dim, raxis, umem, cmem; + cc_ssize ncp = 0, nseg = 1, unit = 1; dim = cc_dimension(tsr[0]); cc_assert_ptr(shape = - (cc_int32*)calloc(dim + 1, sizeof(cc_int32))); - memcpy(shape, tsr[0]->shape, dim * sizeof(cc_int32)); + (cc_ssize*)calloc(dim + 1, sizeof(cc_ssize))); + memcpy(shape, tsr[0]->shape, dim * sizeof(cc_ssize)); raxis = dim - 1 - axis; shape[raxis] = 0; for (i = 0; i < ntsr; ++i) { @@ -247,19 +247,19 @@ static void _cc_print_indent(int n) void cc_print(const cc_tensor_t *tensor) { - cc_int32 *sops, *sbak; + cc_ssize *sops, *sbak; int fidt, cidt; - cc_int32 i, j, dim, lelem, lsize, esize, ssize, npt = 0; + cc_ssize i, j, dim, lelem, lsize, esize, ssize, npt = 0; FILE *ostream = (FILE*)utlog_get_ostream(); dim = cc_dimension(tensor); esize = cc_dtype_size(*tensor->dtype); lelem = tensor->shape[dim - 1]; lsize = lelem * esize; - ssize = (dim + 1) * sizeof(cc_int32); + ssize = (dim + 1) * sizeof(cc_ssize); cc_assert_ptr(sops = - (cc_int32*)calloc(dim + 1, sizeof(cc_int32))); + (cc_ssize*)calloc(dim + 1, sizeof(cc_ssize))); cc_assert_ptr(sbak = - (cc_int32*)calloc(dim + 1, sizeof(cc_int32))); + (cc_ssize*)calloc(dim + 1, sizeof(cc_ssize))); memcpy(sops, tensor->shape, ssize); memcpy(sbak, tensor->shape, ssize); sops[dim - 1] = 0; @@ -280,8 +280,8 @@ cc_tensor_t *cc_clip_by_value(cc_tensor_t *tensor, const void *min, const void *max, const char *name) { cc_tensor_t *yield; - cc_int32 elems = *tensor->shape; - const cc_int32 *sptr = tensor->shape; + cc_ssize elems = *tensor->shape; + const cc_ssize *sptr = tensor->shape; while (*++sptr) elems *= *sptr; if (!name || !strcmp(name, tensor->name)) @@ -297,8 +297,8 @@ cc_tensor_t *cc_cast(cc_tensor_t *tensor, cc_dtype dtype, const char *name) { cc_tensor_t *cast; - const cc_int32 *sptr = tensor->shape; - cc_int32 memsize, elems = *tensor->shape; + const cc_ssize *sptr = tensor->shape; + cc_ssize memsize, elems = *tensor->shape; while (*++sptr) elems *= *sptr; memsize = cc_dtype_size(dtype) * elems; @@ -373,8 +373,8 @@ cc_tensor_t *cc_scalar(cc_tensor_t *tensor, char op, const void *data, const char *name) { cc_tensor_t *yield; - cc_int32 elems = *tensor->shape; - const cc_int32 *sptr = tensor->shape; + cc_ssize elems = *tensor->shape; + const cc_ssize *sptr = tensor->shape; while (*++sptr) elems *= *sptr; if (!name || !strcmp(name, tensor->name)) @@ -417,8 +417,8 @@ cc_tensor_t *cc_elemwise(cc_tensor_t *a, cc_tensor_t *b, char op, const char *name) { cc_tensor_t *yield; - cc_int32 elems = *a->shape; - const cc_int32 *sptr = a->shape; + cc_ssize elems = *a->shape; + const cc_ssize *sptr = a->shape; while (*++sptr) elems *= *sptr; #ifdef ENABLE_CC_ASSERT @@ -460,9 +460,9 @@ cc_tensor_t *cc_elemwise(cc_tensor_t *a, } cc_tensor_t *cc_from_array(void *arr, - const cc_int32 *shape, cc_dtype dtype, const char *name) + const cc_ssize *shape, cc_dtype dtype, const char *name) { - cc_int32 memsize; + cc_ssize memsize; cc_tensor_t *tensor; cc_assert_ptr(tensor = cc_create(shape, dtype, name)); memsize = list_getlen(tensor->container, CC_TENSOR_DATA); diff --git a/src/cc_basic.h b/src/cc_basic.h index a7fe646..7659ce9 100644 --- a/src/cc_basic.h +++ b/src/cc_basic.h @@ -7,11 +7,11 @@ #include "cc_tensor.h" -cc_int32 cc_elements (const cc_tensor_t *tensor); +cc_ssize cc_elements (const cc_tensor_t *tensor); -cc_int32 cc_dimension(const cc_tensor_t *tensor); +cc_ssize cc_dimension(const cc_tensor_t *tensor); -void cc_shape_fix(cc_int32 *shape, cc_int32 elems); +void cc_shape_fix(cc_ssize *shape, cc_ssize elems); /* * Before reshape, `cc_reshape` will check and fix shape @@ -19,15 +19,15 @@ void cc_shape_fix(cc_int32 *shape, cc_int32 elems); * the argument `shape = [-1, 3]`. After reshaping, the tensor's shape * should be `[9, 3]`, and `shape` will be modified to `[9, 3]`. */ -cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_int32 *shape); +cc_tensor_t *cc_reshape(cc_tensor_t *tensor, cc_ssize *shape); int cc_compare_by_shape(const cc_tensor_t *a, const cc_tensor_t *b); cc_tensor_t *cc_stack(cc_tensor_t **tsr, - cc_int32 ntsr, cc_int32 axis, const char *name); + cc_ssize ntsr, cc_ssize axis, const char *name); cc_tensor_t *cc_concat(cc_tensor_t **tsr, - cc_int32 ntsr, cc_int32 axis, const char *name); + cc_ssize ntsr, cc_ssize axis, const char *name); void cc_print(const cc_tensor_t *tensor); @@ -51,7 +51,7 @@ cc_tensor_t *cc_clip_by_value(cc_tensor_t *tensor, const void *min, const void *max, const char *name); cc_tensor_t *cc_from_array(void *arr, - const cc_int32 *shape, cc_dtype dtype, const char *name); + const cc_ssize *shape, cc_dtype dtype, const char *name); #ifdef __cplusplus } diff --git a/src/cc_conv2d.c b/src/cc_conv2d.c index ce027c6..4e0c173 100644 --- a/src/cc_conv2d.c +++ b/src/cc_conv2d.c @@ -19,22 +19,22 @@ #include "global_fn_cfg.h" extern fn_conv2d _conv2d; -cc_int32 cc_conv2d_shape_calc( - cc_int32 i, cc_int32 k, cc_int32 s, cc_int32 p) +cc_ssize cc_conv2d_shape_calc( + cc_ssize i, cc_ssize k, cc_ssize s, cc_ssize p) { - return (cc_int32)((i - k + 2 * p) / s) + 1; + return (cc_ssize)((i - k + 2 * p) / s) + 1; } cc_tensor_t *cc_conv2d(const cc_tensor_t *inp, const cc_tensor_t *kernel, const cc_tensor_t *bias, - cc_int32 s, cc_int32 p, cc_int32 off, const char *name) + cc_ssize s, cc_ssize p, cc_ssize off, const char *name) { cc_uint8 *omp_out_buf = NULL; cc_tensor_t *oup = NULL; const cc_tensor_t *inp_pad; - cc_int32 o_ch_size, p_ch_mem_size, o_ch_mem_size, + cc_ssize o_ch_size, p_ch_mem_size, o_ch_mem_size, k_ch_mem_size, k_mem_size, num_omp_threads, i, j; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; char pad_name[CC_CONV2D_PAD_NAME_LEN]; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); @@ -139,11 +139,11 @@ cc_tensor_t *cc_conv2d(const cc_tensor_t *inp, cc_tensor_t *cc_dw_conv2d(cc_tensor_t *inp, const cc_tensor_t *kernel, const cc_tensor_t *bias, - cc_int32 s, cc_int32 p, cc_int32 off, const char *name) + cc_ssize s, cc_ssize p, cc_ssize off, const char *name) { cc_tensor_t *inp_pad, *oup = NULL; - cc_int32 o_ch_size, p_ch_mem_size, o_ch_mem_size, k_ch_mem_size, i; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize o_ch_size, p_ch_mem_size, o_ch_mem_size, k_ch_mem_size, i; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; char pad_name[CC_CONV2D_PAD_NAME_LEN]; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); @@ -220,9 +220,9 @@ cc_tensor_t *cc_pw_conv2d(cc_tensor_t *inp, const cc_tensor_t *kernel, { cc_uint8 *omp_out_buf = NULL; cc_tensor_t *oup = NULL; - cc_int32 o_ch_size, o_ch_mem_size, + cc_ssize o_ch_size, o_ch_mem_size, k_ch_mem_size, k_mem_size, num_omp_threads, i, j; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); cc_assert_zero(cc_dimension(kernel) - CC_CONV2D_KERNEL_DIM); diff --git a/src/cc_conv2d.h b/src/cc_conv2d.h index b3b6fe2..2e32e70 100644 --- a/src/cc_conv2d.h +++ b/src/cc_conv2d.h @@ -18,17 +18,17 @@ enum cc_conv2d_kernel { #include "cc_tensor.h" -cc_int32 cc_conv2d_shape_calc( - cc_int32 i, cc_int32 k, cc_int32 s, cc_int32 p); +cc_ssize cc_conv2d_shape_calc( + cc_ssize i, cc_ssize k, cc_ssize s, cc_ssize p); cc_tensor_t *cc_conv2d(const cc_tensor_t *inp, const cc_tensor_t *kernel, const cc_tensor_t *bias, - cc_int32 s, cc_int32 p, cc_int32 off, const char *name); + cc_ssize s, cc_ssize p, cc_ssize off, const char *name); /* Depth-wise convolution 2d */ cc_tensor_t *cc_dw_conv2d(cc_tensor_t *inp, const cc_tensor_t *kernel, const cc_tensor_t *bias, - cc_int32 s, cc_int32 p, cc_int32 off, const char *name); + cc_ssize s, cc_ssize p, cc_ssize off, const char *name); /* Point-wise convolution 2d */ cc_tensor_t *cc_pw_conv2d(cc_tensor_t *inp, const cc_tensor_t *kernel, diff --git a/src/cc_dtype.c b/src/cc_dtype.c index d84f4d4..1475999 100644 --- a/src/cc_dtype.c +++ b/src/cc_dtype.c @@ -14,7 +14,7 @@ void __________cc_datatype_check__________() COMPILER_ASSERT(CC_8B_LEN - sizeof(cc_float64)); } -int cc_dtype_size(cc_dtype dt) +cc_ssize cc_dtype_size(cc_dtype dt) { switch (dt) { case CC_INT8: diff --git a/src/cc_dtype.h b/src/cc_dtype.h index d932630..38925be 100644 --- a/src/cc_dtype.h +++ b/src/cc_dtype.h @@ -35,6 +35,9 @@ typedef _DT_INT64 cc_int64; typedef _DT_FLOAT32 cc_float32; typedef _DT_FLOAT64 cc_float64; +typedef cc_int64 cc_ssize; +typedef cc_uint64 cc_usize; + /* * 7 6 5 4 3 ~ 0 * | | | | |_____| @@ -80,7 +83,7 @@ typedef _DT_FLOAT64 cc_float64; #define cc_dtype_check(dt1, dt2) (!(dt1 - dt2)) -int cc_dtype_size(cc_dtype dt); +cc_ssize cc_dtype_size(cc_dtype dt); const char *cc_dtype_to_string(cc_dtype dt); diff --git a/src/cc_fmap2d.c b/src/cc_fmap2d.c index 992df32..d8bd11a 100644 --- a/src/cc_fmap2d.c +++ b/src/cc_fmap2d.c @@ -17,7 +17,7 @@ cc_tensor_t *cc_fmap2d_bias(cc_tensor_t *inp, const cc_tensor_t *bias, const char *name) { cc_tensor_t *fmap; - cc_int32 i, ch_size, ch_mem_size, dt_size; + cc_ssize i, ch_size, ch_mem_size, dt_size; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); cc_assert_zero(*inp->dtype - *bias->dtype); @@ -48,8 +48,8 @@ cc_tensor_t *cc_fmap2d_flat(cc_tensor_t *inp, const char *name) { cc_tensor_t *flat = NULL; cc_uint8 *sptr, *dptr; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; - cc_int32 i, j ,ch_size, dt_size; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize i, j ,ch_size, dt_size; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); #endif diff --git a/src/cc_fullycon.c b/src/cc_fullycon.c index f40be69..0dce0f5 100644 --- a/src/cc_fullycon.c +++ b/src/cc_fullycon.c @@ -13,9 +13,9 @@ extern fn_array_dot_prod _array_dot_prod; cc_tensor_t *cc_fully_connected(const cc_tensor_t *inp, const cc_tensor_t *w, const cc_tensor_t *b, const char *name) { - cc_int32 i, mmsize, dtsize; + cc_ssize i, mmsize, dtsize; cc_tensor_t *oup = NULL; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; #ifdef ENABLE_CC_ASSERT cc_assert((cc_dimension(w) == CC_CONV2D_KERNEL_DIM) || (cc_dimension(w) == CC_FULLYCON_KERNEL_DIM)); diff --git a/src/cc_image.c b/src/cc_image.c index ab72cc6..39d2edd 100644 --- a/src/cc_image.c +++ b/src/cc_image.c @@ -10,8 +10,8 @@ cc_tensor_t *cc_image2tensor(const UTIM_IMG *img, const char *name) { cc_uint8 *ptr; - cc_int32 i, j, ch_size; - cc_int32 shape[4] = {0}; /* [C, H, W, \0] */ + cc_ssize i, j, ch_size; + cc_ssize shape[4] = {0}; /* [C, H, W, \0] */ cc_tensor_t *tensor; shape[CC_IT_SHAPE_C] = img->channels; shape[CC_IT_SHAPE_H] = img->ysize; @@ -33,8 +33,8 @@ UTIM_IMG *cc_tensor2image(const cc_tensor_t *tensor) { UTIM_IMG *img; cc_uint8 *pxls, *ptr; - cc_int32 i, j, ch_size, npxls; - const cc_int32 *sptr = tensor->shape; + cc_ssize i, j, ch_size, npxls; + const cc_ssize *sptr = tensor->shape; if (cc_dimension(tensor) != 3) /* [C, H, W] */ return NULL; img = utim_create(sptr[CC_IT_SHAPE_W], diff --git a/src/cc_normfn.c b/src/cc_normfn.c index 326b2f2..a1274a5 100644 --- a/src/cc_normfn.c +++ b/src/cc_normfn.c @@ -13,10 +13,10 @@ static cc_float32 cc_norm_eps_dfl_fp32 = CC_NORM_EPSILON_DFL_FP32; cc_tensor_t *cc_load_bin_norm_para(const char *w_path, const char *b_path, const char *m_path, const char *v_path, const char *e_path, - cc_int32 nchl, cc_dtype dtype, const char *name) + cc_ssize nchl, cc_dtype dtype, const char *name) { - cc_int32 shape[] = {0, 1, 1, 0}; - cc_int32 i; + cc_ssize shape[] = {0, 1, 1, 0}; + cc_ssize i; cc_tensor_t *para; cc_tensor_t *tsr[CC_NORM_PARAMETERS]; shape[0] = nchl; @@ -51,7 +51,7 @@ cc_tensor_t *cc_batch_norm2d(cc_tensor_t *inp, const cc_tensor_t *para, const char *name) { cc_tensor_t *oup; - cc_int32 i, dt_size, ch_size, ch_mem_size; + cc_ssize i, dt_size, ch_size, ch_mem_size; #ifdef ENABLE_CC_ASSERT cc_assert_zero(cc_dimension(inp) - CC_CNN2D_DIM); cc_assert_zero(*inp->dtype - *para->dtype); diff --git a/src/cc_normfn.h b/src/cc_normfn.h index 874268f..cdb975f 100644 --- a/src/cc_normfn.h +++ b/src/cc_normfn.h @@ -36,7 +36,7 @@ cc_tensor_t *cc_load_bin_norm_para( const char *m_path, /* Mean */ const char *v_path, /* Var */ const char *e_path, /* Epsilon */ - cc_int32 nchl, /* Channel */ + cc_ssize nchl, /* Channel */ cc_dtype dtype, const char *name); diff --git a/src/cc_pad2d.c b/src/cc_pad2d.c index b8df403..ac99e33 100644 --- a/src/cc_pad2d.c +++ b/src/cc_pad2d.c @@ -13,14 +13,14 @@ dtsize); cc_tensor_t *cc_pad2d(const cc_tensor_t *inp, - cc_int32 p, cc_int32 offset, const char *name) + cc_ssize p, cc_ssize offset, const char *name) { cc_tensor_t *pad = NULL; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; - cc_int32 soffset = offset ? 1 : 0; - cc_int32 poffset = offset > 0 ? 1 : 0; - cc_int32 i, j, c, dtsize = cc_dtype_size(*inp->dtype); - cc_int32 i_ch_size, i_ch_mem_size, i_row_mem_size, + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize soffset = offset ? 1 : 0; + cc_ssize poffset = offset > 0 ? 1 : 0; + cc_ssize i, j, c, dtsize = cc_dtype_size(*inp->dtype); + cc_ssize i_ch_size, i_ch_mem_size, i_row_mem_size, p_ch_size, p_ch_mem_size, p_row_mem_size; i_ch_size = inp->shape[CC_CNN2D_SHAPE_W] * inp->shape[CC_CNN2D_SHAPE_H]; diff --git a/src/cc_pad2d.h b/src/cc_pad2d.h index 9669272..8a64f3a 100644 --- a/src/cc_pad2d.h +++ b/src/cc_pad2d.h @@ -8,7 +8,7 @@ #include "cc_tensor.h" cc_tensor_t *cc_pad2d(const cc_tensor_t *inp, - cc_int32 p, cc_int32 offset, const char *name); + cc_ssize p, cc_ssize offset, const char *name); #ifdef __cplusplus } diff --git a/src/cc_pool2d.c b/src/cc_pool2d.c index 809cab3..d099387 100644 --- a/src/cc_pool2d.c +++ b/src/cc_pool2d.c @@ -9,10 +9,10 @@ extern fn_max_pool2d _max_pool2d; extern fn_avg_pool2d _avg_pool2d; cc_tensor_t *cc_max_pool2d( - const cc_tensor_t *inp, cc_int32 s, const char *name) + const cc_tensor_t *inp, cc_ssize s, const char *name) { - cc_int32 i, i_ch_size, i_ch_mem_size, o_ch_size, o_ch_mem_size; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize i, i_ch_size, i_ch_mem_size, o_ch_size, o_ch_mem_size; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; cc_tensor_t *pool = NULL; #ifdef AUTO_TSRMGR pool = cc_tsrmgr_get(name); @@ -43,10 +43,10 @@ cc_tensor_t *cc_max_pool2d( } cc_tensor_t *cc_avg_pool2d( - const cc_tensor_t *inp, cc_int32 s, const char *name) + const cc_tensor_t *inp, cc_ssize s, const char *name) { - cc_int32 i, i_ch_size, i_ch_mem_size, o_ch_size, o_ch_mem_size; - cc_int32 shape[CC_CNN2D_SHAPE] = {0}; + cc_ssize i, i_ch_size, i_ch_mem_size, o_ch_size, o_ch_mem_size; + cc_ssize shape[CC_CNN2D_SHAPE] = {0}; cc_tensor_t *pool = NULL; #ifdef AUTO_TSRMGR pool = cc_tsrmgr_get(name); diff --git a/src/cc_pool2d.h b/src/cc_pool2d.h index 856ea60..6543c92 100644 --- a/src/cc_pool2d.h +++ b/src/cc_pool2d.h @@ -8,10 +8,10 @@ #include "cc_tensor.h" cc_tensor_t *cc_max_pool2d( - const cc_tensor_t *inp, cc_int32 s, const char *name); + const cc_tensor_t *inp, cc_ssize s, const char *name); cc_tensor_t *cc_avg_pool2d( - const cc_tensor_t *inp, cc_int32 s, const char *name); + const cc_tensor_t *inp, cc_ssize s, const char *name); #ifdef __cplusplus } diff --git a/src/cc_tensor.c b/src/cc_tensor.c index 248bcc3..2299006 100644 --- a/src/cc_tensor.c +++ b/src/cc_tensor.c @@ -6,11 +6,11 @@ #include "cc_tensor.h" #include "cc_tsrmgr.h" -cc_tensor_t *cc_create(const cc_int32 *shape, +cc_tensor_t *cc_create(const cc_ssize *shape, cc_dtype dtype, const char *name) { - const cc_int32 *sptr = shape; - cc_int32 memsize, elems = *shape; + const cc_ssize *sptr = shape; + cc_ssize memsize, elems = *shape; cc_tensor_t *tensor; while (*++sptr) elems *= *sptr; @@ -28,9 +28,9 @@ cc_tensor_t *cc_create(const cc_int32 *shape, memset(tensor->data, 0, memsize); cc_assert_ptr( list_set_data(tensor->container, CC_TENSOR_SHAPE, - shape, (sptr - shape + 1) * sizeof(cc_int32))); + shape, (sptr - shape + 1) * sizeof(cc_ssize))); cc_assert_ptr( - tensor->shape = (cc_int32*) + tensor->shape = (cc_ssize*) list_index(tensor->container, CC_TENSOR_SHAPE)); cc_assert_ptr( list_set_data(tensor->container, @@ -59,7 +59,7 @@ cc_tensor_t *cc_copy(const cc_tensor_t *tensor, const char *name) copied->data = (unsigned char*) list_index(copied->container, CC_TENSOR_DATA)); cc_assert_ptr( - copied->shape = (cc_int32*) + copied->shape = (cc_ssize*) list_index(copied->container, CC_TENSOR_SHAPE)); cc_assert_ptr( copied->dtype = (cc_dtype*) @@ -87,7 +87,7 @@ cc_tensor_t *cc_load(const char *filename) tensor->data = (unsigned char*) list_index(tensor->container, CC_TENSOR_DATA)); cc_assert_ptr( - tensor->shape = (cc_int32*) + tensor->shape = (cc_ssize*) list_index(tensor->container, CC_TENSOR_SHAPE)); cc_assert_ptr( tensor->dtype = (cc_dtype*) @@ -101,10 +101,10 @@ cc_tensor_t *cc_load(const char *filename) } cc_tensor_t *cc_load_bin(const char *filename, - const cc_int32 *shape, cc_dtype dtype, const char *name) + const cc_ssize *shape, cc_dtype dtype, const char *name) { FILE *fp; - cc_int32 memsize; + cc_ssize memsize; cc_tensor_t *tensor; cc_assert_ptr(tensor = cc_create(shape, dtype, name)); cc_assert_ptr(fp = fopen(filename, "rb")); @@ -133,14 +133,14 @@ void cc_property(const cc_tensor_t *tensor) { char buf[BUFLEN]; char *bptr = buf; - const cc_int32 *sptr; + const cc_ssize *sptr; if (!tensor) { utlog_format(UTLOG_WARN, "invalid tensor\n"); return; } sptr = tensor->shape; while (*sptr) { - bptr += sprintf(bptr, "%d, ", *sptr++); + bptr += sprintf(bptr, "%lld, ", *sptr++); if ((bptr - buf) > BUFLIM) { sprintf(bptr, "..., "); break; diff --git a/src/cc_tensor.h b/src/cc_tensor.h index 30c68e2..2829c18 100644 --- a/src/cc_tensor.h +++ b/src/cc_tensor.h @@ -26,18 +26,18 @@ typedef struct { const char *name; unsigned char *data; const cc_dtype *dtype; - const cc_int32 *shape; + const cc_ssize *shape; } cc_tensor_t; cc_tensor_t *cc_create( - const cc_int32 *shape, cc_dtype dtype, const char *name); + const cc_ssize *shape, cc_dtype dtype, const char *name); cc_tensor_t *cc_copy(const cc_tensor_t *tensor, const char *name); cc_tensor_t *cc_load(const char *filename); cc_tensor_t *cc_load_bin(const char *filename, - const cc_int32 *shape, cc_dtype dtype, const char *name); + const cc_ssize *shape, cc_dtype dtype, const char *name); void cc_save(const cc_tensor_t *tensor, const char *filename); diff --git a/src/cc_tsrmgr.c b/src/cc_tsrmgr.c index 9bbb393..cef02bb 100644 --- a/src/cc_tsrmgr.c +++ b/src/cc_tsrmgr.c @@ -47,7 +47,7 @@ void cc_tsrmgr_init(void) utlog_format(UTLOG_WARN, "cc_tsrmgr: already initialized\n"); return; } - global_tsrmgr_table = new_rbt(getkey, compare); + global_tsrmgr_table = rbt_new(getkey, compare); return; } @@ -65,7 +65,7 @@ void cc_tsrmgr_clear(void) if (!global_tsrmgr_table) return; _tsrmgr_clear(global_tsrmgr_table->root); - free_rbt(global_tsrmgr_table); + rbt_del(global_tsrmgr_table); global_tsrmgr_table = NULL; global_counter = 0; return; @@ -142,7 +142,7 @@ void cc_tsrmgr_del(const char *name) return; } ptr = (struct pair*) - rbt_delete(global_tsrmgr_table, (void*)name); + rbt_erase(global_tsrmgr_table, (void*)name); if (ptr) { free_pair(ptr); global_counter--; @@ -159,7 +159,7 @@ cc_tensor_t *cc_tsrmgr_get(const char *name) return NULL; } ptr = (struct pair*) - rbt_search(global_tsrmgr_table, (void*)name); + rbt_find(global_tsrmgr_table, (void*)name); if (ptr) return (cc_tensor_t*)ptr->dat; return NULL; @@ -171,14 +171,14 @@ static void _print_property(cc_tensor_t *tensor) { char buf[BUFLEN]; char *bptr = buf; - const cc_int32 *sptr; + const cc_ssize *sptr; if (!tensor) { utlog_format(UTLOG_WARN, "invalid tensor\n"); return; } sptr = tensor->shape; while (*sptr) { - bptr += sprintf(bptr, "%d, ", *sptr++); + bptr += sprintf(bptr, "%lld, ", *sptr++); if ((bptr - buf) > BUFLIM) { sprintf(bptr, "..., "); break; @@ -221,15 +221,15 @@ struct tsrmgr_pack_state { struct list *pack; cc_tensor_t *csr; cc_uint8 *dptr; - cc_int32 len, off; - cc_uint32 idc; + cc_ssize len, off; + cc_usize idc; }; static struct tsrmgr_pack_state _ps; static void _tsrmgr_pack(struct rbt_node *n) { - cc_uint32 i; + cc_usize i; if (n != rbt_nil()) { _tsrmgr_pack(n->left); _ps.csr = (cc_tensor_t*) @@ -271,8 +271,8 @@ void cc_tsrmgr_unpack(struct list *tls) cc_tensor_t *t; struct list *container; cc_uint8 *dptr, *rptr; - cc_int32 j, off, len; - cc_uint32 i; + cc_ssize j, off, len; + cc_usize i; if (!cc_tsrmgr_status()) cc_tsrmgr_init(); for (i = 0; i < tls->counter; ++i) { @@ -301,7 +301,7 @@ void cc_tsrmgr_unpack(struct list *tls) t->dtype = (cc_dtype*) list_index(container, CC_TENSOR_DTYPE)); cc_assert_ptr( - t->shape = (cc_int32*) + t->shape = (cc_ssize*) list_index(container, CC_TENSOR_SHAPE)); cc_tsrmgr_reg(t); } diff --git a/src/util_list.c b/src/util_list.c index 3fff4c0..71c16f0 100644 --- a/src/util_list.c +++ b/src/util_list.c @@ -1,1189 +1,1181 @@ -#include -#include -#include -#include - -#include "util_list.h" - -#ifdef ENABLE_SSHM - #include "sshm.h" -#endif - -#ifndef byte - #define LS_TYPE_BYTE - #define byte unsigned char -#endif -#ifndef uint - #define LS_TYPE_UINT - #define uint unsigned int -#endif - -#define LS_ALLOC(type) ((type*)calloc(1, sizeof(type))) - -#define ASSERT_ON_BUILD(condition)\ - ((void)sizeof(char[1 - 2*!!(condition)])) - -void __________compile_time_test___________() -{ -#ifdef ENABLE_FOPS - ASSERT_ON_BUILD(24 - LIST_INFO_LEN); -#endif -} - -static lsw_t _gstate; - -static uint _djb_hash(const void *s, uint len, uint seed) -{ - uint hash = seed; - uint i; - for (i = 0; i < len; i++) { - hash = (hash << 5) + hash + *((byte*)s + i); - } - return hash; -} - -static int _strcmp(const void *a, const void *b) -{ - return strcmp((const char*)a, (const char*)b); -} - -static uint _string_hash(const void *s, uint seed) -{ - return _djb_hash(s, strlen((const char*)s), seed); -} - -static uint _seed; -static __hashFx _hfx = _string_hash; -static __compFx _cmp = _strcmp; - -static LS_INLINE -int _is_little_endian(void) -{ - uint i = 1; - unsigned char *c = (unsigned char*)&i; - if (*c) - return 1; - return 0; -} - -static LS_INLINE -void _memswap(void *a, void *b, int len) -{ - unsigned char *p1 = (unsigned char*)a; - unsigned char *p2 = (unsigned char*)b; - while (len--) { - *p1 ^= *p2; - *p2 ^= *p1; - *p1++ ^= *p2++; - } -} - -static LS_INLINE -void _memrev(void *i, int len) -{ - int j = len >> 1; - unsigned char *c = (unsigned char*)i; - for (len--; j > 0; j--) { - *c ^= *(c + len); - *(c + len) ^= *c; - *c ^= *(c + len); - c++, len -= 2; - } -} - -static LS_INLINE -void _switch_byte_order(struct list *ls) { - _memrev(&ls->length , sizeof(uint)); - _memrev(&ls->key , sizeof(uint)); - _memrev(&ls->counter, sizeof(uint)); - _memrev(&ls->blen , sizeof(uint)); - _memrev(&ls->scale , sizeof(uint)); -} - -static LS_INLINE -struct list *list_new_static(uint scale, uint blen) -{ - struct list *ls; - if (!scale || !blen) - return NULL; - ls = LS_ALLOC(struct list); - if (!ls) - return NULL; - SET_FLAG(ls->flag, LIST_STATIC_MODE); - ls->length = scale * blen; - ls->mem = (byte*)calloc(ls->length, sizeof(byte)); - if (!ls->mem) { - free(ls); - return NULL; - } - ls->blen = blen; - ls->scale = scale; - ls->counter = scale; - return ls; -} - -static LS_INLINE -struct list *list_new_dynamic(uint scale) -{ - struct list *ls; - if (!scale) - return NULL; - ls = LS_ALLOC(struct list); - if (!ls) - return NULL; - SET_FLAG(ls->flag, LIST_DYNAMIC_MODE); - ls->index = (byte**)calloc(scale, sizeof(byte*)); - if (!ls->index) { - free(ls); - return NULL; - } - ls->scale = scale; - return ls; -} - -struct list *list_new(uint scale, uint blen) -{ - if (!blen) - return list_new_dynamic(scale); - return list_new_static(scale, blen); -} - -static LS_INLINE -struct list *list_clone_static(struct list *ls) -{ - struct list *clone; - if (!ls) - return NULL; - clone = (struct list*)malloc(sizeof(struct list)); - if (!clone) - return NULL; - memcpy(clone, ls, sizeof(struct list)); - if (ls->name) { - clone->name = (char*)malloc(LIST_NAME_LEN); - if (!clone->name) { - free(clone); - return NULL; - } - strcpy(clone->name, ls->name); - } else { - clone->name = NULL; - } - clone->mem = (byte*)malloc(ls->length); - if (!clone->mem) { - if (clone->name) - free(clone->name); - free(clone); - return NULL; - } - memcpy(clone->mem, ls->mem, ls->length); - clone->index = NULL; - return clone; -} - -#define PROCESS_CLONE_DYN_INDEX(i) \ -if (ls->index[i]) { \ - if (!list_set_data(clone, i, \ - ls->index[i] + sizeof(rlen_t), \ - *(rlen_t*)ls->index[i])) { \ - return clone; \ - } \ -} - -static LS_INLINE -struct list *list_clone_dynamic(struct list *ls) -{ - uint i; - struct list *clone; - if (!ls) - return NULL; - clone = (struct list*)malloc(sizeof(struct list)); - if (!clone) - return NULL; - memcpy(clone, ls, sizeof(struct list)); - if (ls->name) { - clone->name = (char*)malloc(LIST_NAME_LEN); - if (!clone->name) { - free(clone); - return NULL; - } - strcpy(clone->name, ls->name); - } else { - clone->name = NULL; - } - clone->index = (byte**)malloc(ls->scale * sizeof(byte*)); - if (!clone->index) { - if (clone->name) - free(clone->name); - free(clone); - return NULL; - } - memset(clone->index, 0, ls->scale * sizeof(byte*)); - clone->length = 0; - clone->counter = 0; - for (i = 0; i < ls->scale; ++i) { - PROCESS_CLONE_DYN_INDEX(i); - } - return clone; -} - -struct list *list_clone(struct list *ls) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_clone_dynamic(ls); - else /* LIST_STATIC_MODE */ - return list_clone_static(ls); -} - -static LS_INLINE -void list_del_static(struct list *ls) -{ - if (!ls) - return; - if (ls->name) - free(ls->name); - if (ls->mem) - free(ls->mem); - free(ls); -} - -static LS_INLINE -void list_del_dynamic(struct list *ls) -{ - if (!ls) - return; - if (ls->name) - free(ls->name); - while (ls->scale--) { - if (ls->index[ls->scale]) - free(ls->index[ls->scale]); - } - if (ls->index) - free(ls->index); - free(ls); -} - -void list_del(struct list *ls) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - list_del_dynamic(ls); - else /* LIST_STATIC_MODE */ - list_del_static(ls); -} - -lsw_t list_rename(struct list *ls, const char *name) -{ - if (TEST_FLAG(ls->flag, LIST_NOT_SHARED)) { - if (!name) { - if (ls->name) - free(ls->name); - ls->name = NULL; - return LSS_SUCCESS; - } - if (strlen(name) >= LIST_NAME_LEN) - return LSS_ARG_ILL; - if (ls->name) - free(ls->name); - ls->name = (char*)malloc(LIST_NAME_LEN); - if (!ls->name) - return LSS_MALLOC_ERR; - strcpy(ls->name, name); - } else { /* LIST_SHARED_ */ - strcpy(ls->name, name); - } - return LSS_SUCCESS; -} - -static LS_INLINE -lsw_t list_resize_static(struct list *ls, uint scale) -{ - byte *new_mem; - uint new_len = scale * ls->blen; - if (TEST_FLAG(ls->flag, LIST_UNRESIZABLE)) - return LSS_BAD_OBJ; - new_mem = (byte*)realloc(ls->mem, new_len); - if (!new_mem) - return LSS_MALLOC_ERR; - if (scale > ls->scale) { - memset(ls->mem + ls->scale * ls->blen, 0, - (scale - ls->scale) * ls->blen); - } - ls->mem = new_mem; - ls->length = new_len; - ls->scale = scale; - ls->counter = scale; - return LSS_SUCCESS; -} - -static LS_INLINE -lsw_t list_resize_dynamic(struct list *ls, uint scale) -{ - byte **new_index; - uint i = 0; - if (TEST_FLAG(ls->flag, LIST_UNRESIZABLE)) - return LSS_BAD_OBJ; - if (scale < ls->scale) { - i = scale; - for (; i < ls->scale; ++i) - list_erase(ls, i); - } - new_index = (byte**)realloc(ls->index, scale * sizeof(byte*)); - if (!new_index) - return LSS_MALLOC_ERR; - ls->index = new_index; - if (scale > ls->scale) { - memset((byte*)new_index + ls->scale * sizeof(byte*), 0, - (scale - ls->scale) * sizeof(byte*)); - } - ls->scale = scale; - return LSS_SUCCESS; -} - -lsw_t list_resize(struct list *ls, uint scale) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_resize_dynamic(ls, scale); - else /* LIST_STATIC_MODE */ - return list_resize_static(ls, scale); -} - -static LS_INLINE -void *list_set_data_sta(struct list *ls, - uint id, const void *data, uint len) -{ - void *ptr; - if (id >= ls->scale) { - _gstate = LSS_BAD_ID; - return NULL; - } - len = ls->blen < len ? ls->blen : len; - ptr = ls->mem + id * ls->blen; - memcpy(ptr, data, len); - return ptr; -} - -static LS_INLINE -void *list_set_data_dyn(struct list *ls, - uint id, const void *data, uint len) -{ - uint mlen = 0; - void *ptr; - if (id >= ls->scale) { - _gstate = LSS_BAD_ID; - return NULL; - } - if (ls->index[id]) { - _gstate = LSS_DYN_ID_EXIST; - return NULL; - } - mlen = len + sizeof(rlen_t); - ls->index[id] = (byte*)malloc(mlen); - if (!ls->index[id]) { - _gstate = LSS_MALLOC_ERR; - return NULL; - } - ptr = ls->index[id] + sizeof(rlen_t); - memcpy(ptr, data, len); - (*(rlen_t*)ls->index[id]) = len; - ls->counter++; - ls->length += mlen; - return ptr; -} - -void *list_set_data(struct list *ls, uint id, - const void *record, uint len) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_set_data_dyn(ls, id, record, len); - else /* LIST_STATIC_MODE */ - return list_set_data_sta(ls, id, record, len); -} - -void *list_alloc(struct list *ls, uint id, uint nbyte) -{ - if (id >= ls->scale) - return NULL; - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { - if (ls->index[id]) - list_erase(ls, id); - ls->index[id] = (byte*)malloc(nbyte + sizeof(rlen_t)); - if (!ls->index[id]) - return NULL; - *(rlen_t*)ls->index[id] = nbyte; - ls->length += (nbyte + sizeof(rlen_t)); - ls->counter++; - return ls->index[id] + sizeof(rlen_t); - } else { /* LIST_STATIC_MODE */ - if (nbyte > ls->blen) - return NULL; - else - return ls->mem + id * ls->blen; - } -} - -static LS_INLINE -void *list_index_sta(struct list *ls, uint id) -{ - if (id >= ls->scale) - return NULL; - else - return ls->mem + id * ls->blen; -} - -static LS_INLINE -void *list_index_dyn(struct list *ls, uint id) -{ - if (id >= ls->scale) - return NULL; - else { - if (ls->index[id]) - return (ls->index[id] + sizeof(rlen_t)); - else - return NULL; - } -} - -void *list_index(struct list *ls, uint id) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_index_dyn(ls, id); - else /* LIST_STATIC_MODE */ - return list_index_sta(ls, id); -} - -static LS_INLINE -lsw_t list_erase_sta(struct list *ls, uint id) -{ - if (id >= ls->scale) - return LSS_BAD_ID; - memset(ls->mem + id * ls->blen, 0, ls->blen); - return LSS_SUCCESS; -} - -uint list_getlen(struct list *ls, uint id) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { - if (ls->index[id]) - return *(rlen_t*)ls->index[id]; - else - return 0; - } - else /* LIST_STATIC_MODE */ - return ls->blen; -} - -static LS_INLINE -lsw_t list_erase_dyn(struct list *ls, uint id) -{ - if (id >= ls->scale) - return LSS_BAD_ID; - if (ls->index[id]) { - ls->length -= sizeof(rlen_t); - ls->length -= *(rlen_t*)ls->index[id]; - ls->counter--; - free(ls->index[id]); - ls->index[id] = NULL; - } - return LSS_SUCCESS; -} - -lsw_t list_erase(struct list *ls, uint id) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_erase_dyn(ls, id); - else /* LIST_STATIC_MODE */ - return list_erase_sta(ls, id); -} - -static LS_INLINE -lsw_t list_swap_sta(struct list *ls, uint id1, uint id2) -{ - if (id1 >= ls->scale || id2 >= ls->scale) - return LSS_BAD_ID; - _memswap(ls->mem + id1 * ls->blen, - ls->mem + id2 * ls->blen, ls->blen); - return LSS_SUCCESS; -} - -static LS_INLINE -lsw_t list_swap_dyn(struct list *ls, uint id1, uint id2) -{ - byte *p; - if (id1 >= ls->scale || id2 >= ls->scale) - return LSS_BAD_ID; - p = ls->index[id1]; - ls->index[id1] = ls->index[id2]; - ls->index[id2] = p; - return LSS_SUCCESS; -} - -lsw_t list_swap(struct list *ls, - uint id1, uint id2) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_swap_dyn(ls, id1, id2); - else /* LIST_STATIC_MODE */ - return list_swap_sta(ls, id1, id2); -} - -#ifdef ENABLE_FOPS - -static void *std_open(const char *pathname, const char *mode) -{ - return fopen(pathname, mode); -} - -static int std_close(void *fp) -{ - return fclose((FILE*)fp); -} - -static long std_read(void *buf, long size, void *fp) -{ - return fread(buf, size, 1, (FILE*)fp); -} - -static long std_write(void *buf, long size, void *fp) -{ - return fwrite(buf, size, 1, (FILE*)fp); -} - -static struct lsio_struct lsio = { - /* .open = */ std_open, - /* .close = */ std_close, - /* .read = */ std_read, - /* .write = */ std_write, - /* .seek = */ NULL, - /* .tell = */ NULL -}; - -void list_set_io_stdio(void) -{ - lsio.open = std_open; - lsio.close = std_close; - lsio.read = std_read; - lsio.write = std_write; -} - -static const unsigned char sebheader[] = {'s', 'e', 'b'}; -#ifdef ENABLE_SEB -#include "seb.h" - -#define SEB_ENC_CTRL_DFL 1 - -static int _seb_encode_ctrl = SEB_ENC_CTRL_DFL; - -void list_set_seb_ctrl(int v) -{ - _seb_encode_ctrl = v; -} - -static void *_seb_open(const char *pathname, const char *mode) -{ - seb_global_parameter(SEB_GLOBAL_ENCODE_CTRL, _seb_encode_ctrl); - return sebfopen(pathname, mode); -} - -static int _seb_close(void *fp) -{ - sebfclose((sebFILE*)fp); - return 0; -} - -static long _seb_read(void *buf, long size, void *fp) -{ - return sebfread(buf, size, 1, (sebFILE*)fp); -} - -static long _seb_write(void *buf, long size, void *fp) -{ - return sebfwrite(buf, size, 1, (sebFILE*)fp); -} - -void list_set_io_seb(void) -{ - lsio.open = _seb_open; - lsio.close = _seb_close; - lsio.read = _seb_read; - lsio.write = _seb_write; -} -#endif /* ENABLE_SEB */ - -static const unsigned char gzheader[] = {0x1F, 0x8B, 0x08}; -#ifdef ENABLE_ZLIB -#include - -#define ZLIB_COMPRESS_LV_DFL 7 - -static int _zlib_compress_level = ZLIB_COMPRESS_LV_DFL; - -void list_set_zlib_ctrl(int v) -{ - _zlib_compress_level = v; -} - -static void *_zlib_open(const char *pathname, const char *mode) -{ - char open_r[] = "rb"; - char open_w[] = "wb"; - char open_wl[8]; - if (*mode == 'r') { - return gzopen(pathname, open_r); - } else if (*mode == 'w') { - sprintf(open_wl, "%s%d", open_w, _zlib_compress_level); - return gzopen(pathname, open_wl); - } else { - /* Unsupported mode */ - return NULL; - } -} - -static int _zlib_close(void *fp) -{ - return gzclose((gzFile)fp); -} - -static long _zlib_read(void *buf, long size, void *fp) -{ - return gzread((gzFile)fp, buf, size); -} - -static long _zlib_write(void *buf, long size, void *fp) -{ - return gzwrite((gzFile)fp, buf, size); -} - -void list_set_io_zlib(void) -{ - lsio.open = _zlib_open; - lsio.close = _zlib_close; - lsio.read = _zlib_read; - lsio.write = _zlib_write; -} -#endif /* ENABLE_ZLIB */ - -/* - * Exported file structure of Static LIST - * name_len | list->name | list | list->mem - */ -static lsw_t list_export_static( - struct list *ls, const char *path) -{ - void *fp; - uint name_len = 0; - fp = lsio.open(path, "wb"); - if (!fp) - return LSS_FILE_ERR; - if (ls->name) - name_len = strlen(ls->name); - if (!_is_little_endian()) { - _memrev(&name_len, sizeof(uint)); - _switch_byte_order(ls); - } - lsio.write(&name_len, sizeof(uint), fp); - lsio.write(ls->name, name_len, fp); - lsio.write((byte*)ls + - LIST_INFO_OFFSET, LIST_INFO_LEN, fp); - lsio.write(ls->mem, ls->length, fp); - if (!_is_little_endian()) { - _switch_byte_order(ls); - } - lsio.close(fp); - return LSS_SUCCESS; -} - -#define PROCESS_EXPORT_DYN \ -if (ls->index[sc]) { \ - if (!_is_little_endian()) \ - _memrev(ls->index[sc], sizeof(rlen_t)); \ - lsio.write(ls->index[sc], sizeof(rlen_t) + \ - *(rlen_t*)ls->index[sc], fp); \ - if (!_is_little_endian()) { \ - _memrev(ls->index[sc], sizeof(rlen_t)); \ - _memrev(&sc, sizeof(uint)); \ - } \ - lsio.write(&sc, sizeof(uint), fp); \ - if (!_is_little_endian()) \ - _memrev(&sc, sizeof(uint)); \ -} - -/* - * Exported file structure of Dynamic LIST - * name_len | list->name | list | *index[0:N] - */ -static lsw_t list_export_dynamic( - struct list *ls, const char *path) -{ - void *fp; - uint name_len = 0; - uint sc = ls->scale; - fp = lsio.open(path, "wb"); - if (!fp) - return LSS_FILE_ERR; - if (ls->name) - name_len = strlen(ls->name); - if (!_is_little_endian()) { - _memrev(&name_len, sizeof(uint)); - _switch_byte_order(ls); - } - lsio.write(&name_len, sizeof(uint), fp); - lsio.write(ls->name, name_len, fp); - lsio.write((byte*)ls + - LIST_INFO_OFFSET, LIST_INFO_LEN, fp); - while (sc--) { - PROCESS_EXPORT_DYN; - } - if (!_is_little_endian()) { - _switch_byte_order(ls); - } - lsio.close(fp); - return LSS_SUCCESS; -} - -lsw_t list_export(struct list *ls, const char *path) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return list_export_dynamic(ls, path); - else /* LIST_STATIC_MODE */ - return list_export_static(ls, path); -} - -#define PROCESS_IMPORT_DYN_RECORD \ - ls->index = (byte**) \ - malloc(ls->scale * sizeof(byte*)); \ - if (!ls->index) { \ - free(ls); \ - free(name); \ - lsio.close(fp); \ - return NULL; \ - } \ - memset(ls->index, 0, ls->scale * sizeof(byte*)); \ - while (lsio.read(&len, sizeof(rlen_t), fp)) { \ - if (!_is_little_endian()) \ - _memrev(&len, sizeof(rlen_t)); \ - tmp = (byte*)malloc(len + sizeof(rlen_t)); \ - if (!tmp) { \ - lsio.close(fp); \ - return ls; \ - } \ - *(rlen_t*)tmp = len; \ - lsio.read(tmp + \ - sizeof(rlen_t), len, fp); \ - lsio.read(&id, sizeof(uint), fp); \ - if (!_is_little_endian()) \ - _memrev(&id, sizeof(uint)); \ - ls->index[id] = tmp; \ - } - -#define PROCESS_IMPORT_STATIC_RECORD \ - ls->mem = (byte*)malloc(ls->length); \ - if (!ls->mem) { \ - free(ls); \ - free(name); \ - lsio.close(fp); \ - return NULL; \ - } \ - lsio.read(ls->mem, ls->length, fp); - -#define PROCESS_IMPORT_OPEN_FILE \ - fp = lsio.open(path, "rb"); \ - if (!fp) \ - return NULL; \ - ls = LS_ALLOC(struct list); \ - if (!ls) { \ - lsio.close(fp); \ - return NULL; \ - } \ - lsio.read(&name_len, sizeof(uint), fp); \ - if (!_is_little_endian()) \ - _memrev(&name_len, sizeof(uint)); \ - if (name_len) { \ - name_len++; \ - name = (char*)malloc(name_len); \ - if (!name) { \ - free(ls); \ - lsio.close(fp); \ - return NULL; \ - } \ - memset(name, 0, name_len); \ - lsio.read(name, name_len - 1, fp); \ - } \ - lsio.read((byte*)ls + LIST_INFO_OFFSET, \ - LIST_INFO_LEN, fp); \ - if (!_is_little_endian()) \ - _switch_byte_order(ls); \ - ls->name = name; - -static int _allow_io_auto_switch = 1; - -void list_enable_io_auto_switch(void) -{ - _allow_io_auto_switch = 1; -} - -void list_disable_io_auto_switch(void) -{ - _allow_io_auto_switch = 0; -} - -#define F_MAGIC_LEN 4 -static void _list_import_io_auto_switch(const char *pathname) -{ - unsigned char buf[F_MAGIC_LEN] = {0}; - FILE *fp = fopen(pathname, "rb"); - assert(fp); - fread(buf, F_MAGIC_LEN, 1, fp); - if (!memcmp(sebheader, buf, sizeof(sebheader))) { -#ifdef ENABLE_SEB - list_set_io_seb(); -#else - assert(0); -#endif /* ENABLE_SEB */ - } else if (!memcmp(gzheader, buf, sizeof(gzheader))) { -#ifdef ENABLE_ZLIB - list_set_io_zlib(); -#else - assert(0); -#endif /* ENABLE_ZLIB */ - } else { - list_set_io_stdio(); - } - fclose(fp); -} - -struct list *list_import(const char *path) -{ - struct list *ls; - struct lsio_struct lsio_bak; - void *fp; - char *name = NULL; - byte *tmp; - uint name_len, id; - rlen_t len; - if (_allow_io_auto_switch) { - lsio_bak = lsio; - _list_import_io_auto_switch(path); - } - PROCESS_IMPORT_OPEN_FILE; - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { - PROCESS_IMPORT_DYN_RECORD; - } else { /* LIST_STATIC_MODE */ - PROCESS_IMPORT_STATIC_RECORD; - } - lsio.close(fp); - if (_allow_io_auto_switch) - lsio = lsio_bak; - return ls; -} - -struct lsio_struct *list_get_io_ctrl_struct(void) -{ - return &lsio; -} -#else /* DISABLE FILE OPS */ -struct lsio_struct *list_get_io_ctrl_struct(void) -{ - return NULL; -} -#endif /* ENABLE_FOPS */ - -#ifdef ENABLE_SSHM -struct list *list_new_shared(uint scale, uint blen, uint key) -{ - byte *shm; - uint shmlen = 0; - struct list *ls = LS_ALLOC(struct list); - if (!ls) - return NULL; - memset(ls, 0, sizeof(struct list)); - shmlen += LIST_NAME_LEN; - shmlen += sizeof(struct list); - SET_FLAG(ls->flag, LIST_STATIC_MODE); - SET_FLAG(ls->flag, LIST_UNRESIZABLE); - SET_FLAG(ls->flag, LIST_SHARED_MEM); - SET_FLAG(ls->flag, LIST_SHARED_OWNER); - ls->length = scale * blen; - shmlen += ls->length; - shm = (byte*)create_shm(shmlen, key); - if (!shm) { - free(ls); - return NULL; - } - memset(shm, 0, shmlen); - memcpy(shm, ls, sizeof(struct list)); - free(ls); - ls = (struct list*)shm; - ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; - ls->key = key; - ls->blen = blen; - ls->scale = scale; - ls->counter = scale; - ls->name = (char*)shm + sizeof(struct list); - return ls; -} - -struct list *list_link_shared(uint len, uint key) -{ - byte *shm; - struct list *ls = - (struct list*) - malloc(sizeof(struct list)); - if (!ls) - return NULL; - shm = (byte*)create_shm(len + - sizeof(struct list) + LIST_NAME_LEN, key); - if (!shm) { - free(ls); - return NULL; - } - memcpy(ls, shm, sizeof(struct list)); - ls->name = (char*)shm + sizeof(struct list); - ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; - SET_FLAG(ls->flag, LIST_UNRESIZABLE); - SET_FLAG(ls->flag, LIST_SHARED_MEM); - SET_FLAG(ls->flag, LIST_SHARED_USER); - return ls; -} - -lsw_t list_del_shared(struct list *ls) -{ - uint shmlen; - if (TEST_FLAG(ls->flag, LIST_SHARED_USER)) - return LSS_ARG_ILL; - shmlen = (sizeof(struct list) + - LIST_NAME_LEN + ls->length); - if (free_shm(shmlen, ls->key)) - return LSS_SHM_ERR; - return LSS_SUCCESS; -} - -#endif /* ENABLE_SSHM */ - -#if defined(ENABLE_SSHM) && defined(ENABLE_FOPS) -#define PROCESS_IMPORT_SHARED \ - byte *shm; \ - char *name; \ - uint name_len = 0; \ - uint shmlen = 0; \ - fp = lsio.open(path, "rb"); \ - if (!fp) \ - return NULL; \ - ls = (struct list*) \ - malloc(sizeof(struct list)); \ - if (!ls) \ - return NULL; \ - lsio.read(&name_len, sizeof(uint), fp); \ - if (!_is_little_endian()) \ - _memrev(&name_len, sizeof(uint)); \ - name_len++; \ - name = (char*)malloc(name_len); \ - if (!name) { \ - free(ls); \ - return NULL; \ - } \ - memset(name, 0, name_len); \ - lsio.read(name, name_len - 1, fp); \ - lsio.read(ls, (sizeof(struct list)), fp); \ - if (!_is_little_endian()) \ - _switch_byte_order(ls); \ - shmlen = sizeof(struct list) + LIST_NAME_LEN; \ - shmlen += ls->length; \ - shm = (byte*)create_shm(shmlen, key); \ - if (!shm) { \ - free(ls); \ - free(name); \ - lsio.close(fp); \ - return NULL; \ - } \ - lsio.read(shm + sizeof(struct list) + \ - LIST_NAME_LEN, ls->length, fp); \ - memcpy(shm, ls, sizeof(struct list)); \ - strcpy((char*)shm + sizeof(struct list), name); \ - free(name); \ - ls->name = (char*)shm + sizeof(struct list); \ - ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; - -struct list * -list_import_shared(const char *path, uint key) -{ - struct list *ls; - void *fp; - PROCESS_IMPORT_SHARED; - lsio.close(fp); - return ls; -} -#endif /* ENABLE_SSHM && ENABLE_FOPS */ - -ccnt_t list_get_record_counter(struct list *ls, uint id) -{ - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { - if (ls->index[id]) - return 1; - else - return 0; - } else { /* LIST_STATIC_MODE */ - if (TEST_FLAG(ls->flag, LIST_HASH_TABLE)) - return (*(ccnt_t*)(ls->mem + id * ls->blen)) >> 1; - } - return 1; -} - -int list_hash_table_test_id(struct list *ls, uint id) -{ - return (*(ccnt_t*)(ls->mem + id * ls->blen)) & 1; -} - -struct list *list_new_hash_table(uint scale, uint blen) -{ - struct list *ls; - if (!blen) /* support LIST_STATIC_MODE only */ - return NULL; - ls = list_new_static(scale, blen + sizeof(ccnt_t)); - if (!ls) - return NULL; - SET_FLAG(ls->flag, LIST_UNRESIZABLE); - SET_FLAG(ls->flag, LIST_HASH_TABLE); - return ls; -} - -lsw_t list_hash_id_calc(struct list *ls, - const void *data, uint *hi, uint *id) -{ - uint nhash, cid; - nhash = _hfx(data, _seed) % ls->scale; - *hi = nhash; - cid = nhash; - while (list_hash_table_test_id(ls, cid)) { - cid++; - if (cid == ls->scale) - cid = 0; - if (cid == nhash) - return LSS_ERR_LISTFULL; - } - *id = cid; - return LSS_SUCCESS; -} - -lsw_t list_hash_table_insert(struct list *ls, - const void *record, uint len) -{ - uint hi, id, dlen; - ccnt_t *hrec, *rrec; - lsw_t stat; - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return LSS_BAD_OBJ; - if ((stat = list_hash_id_calc(ls, record, &hi, &id))) - return stat; - hrec = (ccnt_t*)(ls->mem + hi * ls->blen); - rrec = (ccnt_t*)(ls->mem + id * ls->blen); - dlen = ls->blen - sizeof(ccnt_t); - if (CCNT_VAL(*hrec) == CCNT_MAX) - return LSS_CCNT_MAX; - memcpy(rrec + 1, record, len > dlen ? dlen : len); - (*hrec) += CCNT_INC; - (*rrec) |= 1; /* SET FLAG */ - return LSS_SUCCESS; -} - -lsw_t list_hash_table_find(struct list *ls, - const void *key, uint *id) -{ - uint nhash, cid; - byte *rec; - ccnt_t nrec; - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return LSS_BAD_OBJ; - nhash = _hfx(key, _seed) % ls->scale; - cid = nhash; - rec = ls->mem + cid * ls->blen; - nrec = (*(ccnt_t*)(ls->mem + cid * ls->blen)) >> 1; - if (!nrec) - return LSS_OBJ_NOFOUND; - while (nrec--) { - if (_hfx(rec + sizeof(ccnt_t), - _seed) % ls->scale == nhash) { - if (!_cmp(rec + sizeof(ccnt_t), key)) { - *id = cid; - return LSS_SUCCESS; - } - } - cid++; - if (cid == nhash) - return LSS_OBJ_NOFOUND; - if (cid == ls->scale) { - cid = 0; - rec = ls->mem; - } else { - rec += ls->blen; - } - } - return LSS_SUCCESS; -} - -lsw_t list_hash_table_del(struct list *ls, const void *key) -{ - uint nhash, id; - byte *rec, *csr; - ccnt_t nrec; - if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) - return LSS_BAD_OBJ; - nhash = _hfx(key, _seed) % ls->scale; - rec = ls->mem + nhash * ls->blen; - csr = rec; - nrec = (*(ccnt_t*)rec) >> 1; - if (!nrec) - return LSS_SUCCESS; - id = nhash; - while (1) { - if (_hfx(csr + sizeof(ccnt_t), - _seed) % ls->scale == nhash) { - if (!_cmp(csr + sizeof(ccnt_t), key)) { - nrec--; - if (!nrec) - break; - } - } - id++; - if (id == nhash) { - if (nrec) - return LSS_OBJ_NOFOUND; - return LSS_BAD_OBJ; - } - if (id == ls->scale) { - id = 0; - csr = ls->mem; - } else { - csr += ls->blen; - } - } - (*(ccnt_t*)csr) ^= 1; /* CLR FLAG */ - (*(ccnt_t*)rec) -= CCNT_INC; - return LSS_SUCCESS; -} - -void list_print_properties(struct list *ls, void *stream) -{ - FILE *fp = stream ? (FILE*)stream : stdout; - if (!ls) { - fprintf(fp, "[ERROR] : Bad object, stopped!\n"); - return; - } - if (ls->name) - fprintf(fp, "[%s]\n", ls->name); - else - fprintf(fp, "Not named list.\n"); - if (TEST_FLAG(ls->flag, LIST_STATIC_MODE)) - fprintf(fp, "[MODE] = STATIC\n"); - else - fprintf(fp, "[MODE] = DYNAMIC\n"); - fprintf(fp, "[length] = %d\n", ls->length); - fprintf(fp, "[scale] = %d\n", ls->scale); - fprintf(fp, "[record] = %d\n", ls->counter); - fprintf(fp, "[block length] = %d\n", ls->blen); -} - -#ifdef LS_TYPE_BYTE - #undef byte -#endif -#ifdef LS_TYPE_UINT - #undef uint -#endif +#define _UTIL_LIST_C_ +#include +#include +#include +#include + +#include "util_list.h" + +#ifdef ENABLE_SSHM + #include "sshm.h" +#endif + +#define LS_ALLOC(type) ((type*)calloc(1, sizeof(type))) + +#define ASSERT_ON_BUILD(condition)\ + ((void)sizeof(char[1 - 2*!!(condition)])) + +void __________compile_time_test___________() +{ +#ifdef ENABLE_FOPS + ASSERT_ON_BUILD( + (24 - LIST_INFO_LEN) & (48 - LIST_INFO_LEN)); +#endif +} + +static lsw_t _gstate; + +static uint _djb_hash(const void *s, uint len, uint seed) +{ + uint hash = seed; + uint i; + for (i = 0; i < len; i++) { + hash = (hash << 5) + hash + *((byte*)s + i); + } + return hash; +} + +static int _strcmp(const void *a, const void *b) +{ + return strcmp((const char*)a, (const char*)b); +} + +static uint _string_hash(const void *s, uint seed) +{ + return _djb_hash(s, strlen((const char*)s), seed); +} + +static uint _seed; +static __hashFx _hfx = _string_hash; +static __compFx _cmp = _strcmp; + +static LS_INLINE +int _is_little_endian(void) +{ + uint i = 1; + unsigned char *c = (unsigned char*)&i; + if (*c) + return 1; + return 0; +} + +static LS_INLINE +void _memswap(void *a, void *b, int len) +{ + unsigned char *p1 = (unsigned char*)a; + unsigned char *p2 = (unsigned char*)b; + while (len--) { + *p1 ^= *p2; + *p2 ^= *p1; + *p1++ ^= *p2++; + } +} + +static LS_INLINE +void _memrev(void *i, int len) +{ + int j = len >> 1; + unsigned char *c = (unsigned char*)i; + for (len--; j > 0; j--) { + *c ^= *(c + len); + *(c + len) ^= *c; + *c ^= *(c + len); + c++, len -= 2; + } +} + +static LS_INLINE +void _switch_byte_order(struct list *ls) { + _memrev(&ls->length , sizeof(uint)); + _memrev(&ls->key , sizeof(uint)); + _memrev(&ls->counter, sizeof(uint)); + _memrev(&ls->blen , sizeof(uint)); + _memrev(&ls->scale , sizeof(uint)); +} + +static LS_INLINE +struct list *list_new_static(uint scale, uint blen) +{ + struct list *ls; + if (!scale || !blen) + return NULL; + ls = LS_ALLOC(struct list); + if (!ls) + return NULL; + SET_FLAG(ls->flag, LIST_STATIC_MODE); + ls->length = scale * blen; + ls->mem = (byte*)calloc(ls->length, sizeof(byte)); + if (!ls->mem) { + free(ls); + return NULL; + } + ls->blen = blen; + ls->scale = scale; + ls->counter = scale; + return ls; +} + +static LS_INLINE +struct list *list_new_dynamic(uint scale) +{ + struct list *ls; + if (!scale) + return NULL; + ls = LS_ALLOC(struct list); + if (!ls) + return NULL; + SET_FLAG(ls->flag, LIST_DYNAMIC_MODE); + ls->index = (byte**)calloc(scale, sizeof(byte*)); + if (!ls->index) { + free(ls); + return NULL; + } + ls->scale = scale; + return ls; +} + +struct list *list_new(uint scale, uint blen) +{ + if (!blen) + return list_new_dynamic(scale); + return list_new_static(scale, blen); +} + +static LS_INLINE +struct list *list_clone_static(struct list *ls) +{ + struct list *clone; + if (!ls) + return NULL; + clone = (struct list*)malloc(sizeof(struct list)); + if (!clone) + return NULL; + memcpy(clone, ls, sizeof(struct list)); + if (ls->name) { + clone->name = (char*)malloc(LIST_NAME_LEN); + if (!clone->name) { + free(clone); + return NULL; + } + strcpy(clone->name, ls->name); + } else { + clone->name = NULL; + } + clone->mem = (byte*)malloc(ls->length); + if (!clone->mem) { + if (clone->name) + free(clone->name); + free(clone); + return NULL; + } + memcpy(clone->mem, ls->mem, ls->length); + clone->index = NULL; + return clone; +} + +#define PROCESS_CLONE_DYN_INDEX(i) \ +if (ls->index[i]) { \ + if (!list_set_data(clone, i, \ + ls->index[i] + sizeof(rlen_t), \ + *(rlen_t*)ls->index[i])) { \ + return clone; \ + } \ +} + +static LS_INLINE +struct list *list_clone_dynamic(struct list *ls) +{ + uint i; + struct list *clone; + if (!ls) + return NULL; + clone = (struct list*)malloc(sizeof(struct list)); + if (!clone) + return NULL; + memcpy(clone, ls, sizeof(struct list)); + if (ls->name) { + clone->name = (char*)malloc(LIST_NAME_LEN); + if (!clone->name) { + free(clone); + return NULL; + } + strcpy(clone->name, ls->name); + } else { + clone->name = NULL; + } + clone->index = (byte**)malloc(ls->scale * sizeof(byte*)); + if (!clone->index) { + if (clone->name) + free(clone->name); + free(clone); + return NULL; + } + memset(clone->index, 0, ls->scale * sizeof(byte*)); + clone->length = 0; + clone->counter = 0; + for (i = 0; i < ls->scale; ++i) { + PROCESS_CLONE_DYN_INDEX(i); + } + return clone; +} + +struct list *list_clone(struct list *ls) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_clone_dynamic(ls); + else /* LIST_STATIC_MODE */ + return list_clone_static(ls); +} + +static LS_INLINE +void list_del_static(struct list *ls) +{ + if (!ls) + return; + if (ls->name) + free(ls->name); + if (ls->mem) + free(ls->mem); + free(ls); +} + +static LS_INLINE +void list_del_dynamic(struct list *ls) +{ + if (!ls) + return; + if (ls->name) + free(ls->name); + while (ls->scale--) { + if (ls->index[ls->scale]) + free(ls->index[ls->scale]); + } + if (ls->index) + free(ls->index); + free(ls); +} + +void list_del(struct list *ls) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + list_del_dynamic(ls); + else /* LIST_STATIC_MODE */ + list_del_static(ls); +} + +lsw_t list_rename(struct list *ls, const char *name) +{ + if (TEST_FLAG(ls->flag, LIST_NOT_SHARED)) { + if (!name) { + if (ls->name) + free(ls->name); + ls->name = NULL; + return LSS_SUCCESS; + } + if (strlen(name) >= LIST_NAME_LEN) + return LSS_ARG_ILL; + if (ls->name) + free(ls->name); + ls->name = (char*)malloc(LIST_NAME_LEN); + if (!ls->name) + return LSS_MALLOC_ERR; + strcpy(ls->name, name); + } else { /* LIST_SHARED_ */ + strcpy(ls->name, name); + } + return LSS_SUCCESS; +} + +static LS_INLINE +lsw_t list_resize_static(struct list *ls, uint scale) +{ + byte *new_mem; + uint new_len = scale * ls->blen; + if (TEST_FLAG(ls->flag, LIST_UNRESIZABLE)) + return LSS_BAD_OBJ; + new_mem = (byte*)realloc(ls->mem, new_len); + if (!new_mem) + return LSS_MALLOC_ERR; + if (scale > ls->scale) { + memset(ls->mem + ls->scale * ls->blen, 0, + (scale - ls->scale) * ls->blen); + } + ls->mem = new_mem; + ls->length = new_len; + ls->scale = scale; + ls->counter = scale; + return LSS_SUCCESS; +} + +static LS_INLINE +lsw_t list_resize_dynamic(struct list *ls, uint scale) +{ + byte **new_index; + uint i = 0; + if (TEST_FLAG(ls->flag, LIST_UNRESIZABLE)) + return LSS_BAD_OBJ; + if (scale < ls->scale) { + i = scale; + for (; i < ls->scale; ++i) + list_erase(ls, i); + } + new_index = (byte**)realloc(ls->index, scale * sizeof(byte*)); + if (!new_index) + return LSS_MALLOC_ERR; + ls->index = new_index; + if (scale > ls->scale) { + memset((byte*)new_index + ls->scale * sizeof(byte*), 0, + (scale - ls->scale) * sizeof(byte*)); + } + ls->scale = scale; + return LSS_SUCCESS; +} + +lsw_t list_resize(struct list *ls, uint scale) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_resize_dynamic(ls, scale); + else /* LIST_STATIC_MODE */ + return list_resize_static(ls, scale); +} + +static LS_INLINE +void *list_set_data_sta(struct list *ls, + uint id, const void *data, uint len) +{ + void *ptr; + if (id >= ls->scale) { + _gstate = LSS_BAD_ID; + return NULL; + } + len = ls->blen < len ? ls->blen : len; + ptr = ls->mem + id * ls->blen; + memcpy(ptr, data, len); + return ptr; +} + +static LS_INLINE +void *list_set_data_dyn(struct list *ls, + uint id, const void *data, uint len) +{ + uint mlen = 0; + void *ptr; + if (id >= ls->scale) { + _gstate = LSS_BAD_ID; + return NULL; + } + if (ls->index[id]) { + _gstate = LSS_DYN_ID_EXIST; + return NULL; + } + mlen = len + sizeof(rlen_t); + ls->index[id] = (byte*)malloc(mlen); + if (!ls->index[id]) { + _gstate = LSS_MALLOC_ERR; + return NULL; + } + ptr = ls->index[id] + sizeof(rlen_t); + memcpy(ptr, data, len); + (*(rlen_t*)ls->index[id]) = len; + ls->counter++; + ls->length += mlen; + return ptr; +} + +void *list_set_data(struct list *ls, uint id, + const void *record, uint len) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_set_data_dyn(ls, id, record, len); + else /* LIST_STATIC_MODE */ + return list_set_data_sta(ls, id, record, len); +} + +void *list_alloc(struct list *ls, uint id, uint nbyte) +{ + if (id >= ls->scale) + return NULL; + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { + if (ls->index[id]) + list_erase(ls, id); + ls->index[id] = (byte*)malloc(nbyte + sizeof(rlen_t)); + if (!ls->index[id]) + return NULL; + *(rlen_t*)ls->index[id] = nbyte; + ls->length += (nbyte + sizeof(rlen_t)); + ls->counter++; + return ls->index[id] + sizeof(rlen_t); + } else { /* LIST_STATIC_MODE */ + if (nbyte > ls->blen) + return NULL; + else + return ls->mem + id * ls->blen; + } +} + +static LS_INLINE +void *list_index_sta(struct list *ls, uint id) +{ + if (id >= ls->scale) + return NULL; + else + return ls->mem + id * ls->blen; +} + +static LS_INLINE +void *list_index_dyn(struct list *ls, uint id) +{ + if (id >= ls->scale) + return NULL; + else { + if (ls->index[id]) + return (ls->index[id] + sizeof(rlen_t)); + else + return NULL; + } +} + +void *list_index(struct list *ls, uint id) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_index_dyn(ls, id); + else /* LIST_STATIC_MODE */ + return list_index_sta(ls, id); +} + +static LS_INLINE +lsw_t list_erase_sta(struct list *ls, uint id) +{ + if (id >= ls->scale) + return LSS_BAD_ID; + memset(ls->mem + id * ls->blen, 0, ls->blen); + return LSS_SUCCESS; +} + +uint list_getlen(struct list *ls, uint id) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { + if (ls->index[id]) + return *(rlen_t*)ls->index[id]; + else + return 0; + } + else /* LIST_STATIC_MODE */ + return ls->blen; +} + +static LS_INLINE +lsw_t list_erase_dyn(struct list *ls, uint id) +{ + if (id >= ls->scale) + return LSS_BAD_ID; + if (ls->index[id]) { + ls->length -= sizeof(rlen_t); + ls->length -= *(rlen_t*)ls->index[id]; + ls->counter--; + free(ls->index[id]); + ls->index[id] = NULL; + } + return LSS_SUCCESS; +} + +lsw_t list_erase(struct list *ls, uint id) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_erase_dyn(ls, id); + else /* LIST_STATIC_MODE */ + return list_erase_sta(ls, id); +} + +static LS_INLINE +lsw_t list_swap_sta(struct list *ls, uint id1, uint id2) +{ + if (id1 >= ls->scale || id2 >= ls->scale) + return LSS_BAD_ID; + _memswap(ls->mem + id1 * ls->blen, + ls->mem + id2 * ls->blen, ls->blen); + return LSS_SUCCESS; +} + +static LS_INLINE +lsw_t list_swap_dyn(struct list *ls, uint id1, uint id2) +{ + byte *p; + if (id1 >= ls->scale || id2 >= ls->scale) + return LSS_BAD_ID; + p = ls->index[id1]; + ls->index[id1] = ls->index[id2]; + ls->index[id2] = p; + return LSS_SUCCESS; +} + +lsw_t list_swap(struct list *ls, + uint id1, uint id2) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_swap_dyn(ls, id1, id2); + else /* LIST_STATIC_MODE */ + return list_swap_sta(ls, id1, id2); +} + +#ifdef ENABLE_FOPS + +static void *std_open(const char *pathname, const char *mode) +{ + return fopen(pathname, mode); +} + +static int std_close(void *fp) +{ + return fclose((FILE*)fp); +} + +static long std_read(void *buf, long size, void *fp) +{ + return fread(buf, size, 1, (FILE*)fp); +} + +static long std_write(void *buf, long size, void *fp) +{ + return fwrite(buf, size, 1, (FILE*)fp); +} + +static struct lsio_struct lsio = { + /* .open = */ std_open, + /* .close = */ std_close, + /* .read = */ std_read, + /* .write = */ std_write, + /* .seek = */ NULL, + /* .tell = */ NULL +}; + +void list_set_io_stdio(void) +{ + lsio.open = std_open; + lsio.close = std_close; + lsio.read = std_read; + lsio.write = std_write; +} + +static const unsigned char sebheader[] = {'s', 'e', 'b'}; +#ifdef ENABLE_SEB +#include "seb.h" + +#define SEB_ENC_CTRL_DFL 1 + +static int _seb_encode_ctrl = SEB_ENC_CTRL_DFL; + +void list_set_seb_ctrl(int v) +{ + _seb_encode_ctrl = v; +} + +static void *_seb_open(const char *pathname, const char *mode) +{ + seb_global_parameter(SEB_GLOBAL_ENCODE_CTRL, _seb_encode_ctrl); + return sebfopen(pathname, mode); +} + +static int _seb_close(void *fp) +{ + sebfclose((sebFILE*)fp); + return 0; +} + +static long _seb_read(void *buf, long size, void *fp) +{ + return sebfread(buf, size, 1, (sebFILE*)fp); +} + +static long _seb_write(void *buf, long size, void *fp) +{ + return sebfwrite(buf, size, 1, (sebFILE*)fp); +} + +void list_set_io_seb(void) +{ + lsio.open = _seb_open; + lsio.close = _seb_close; + lsio.read = _seb_read; + lsio.write = _seb_write; +} +#endif /* ENABLE_SEB */ + +static const unsigned char gzheader[] = {0x1F, 0x8B, 0x08}; +#ifdef ENABLE_ZLIB +#include + +#define ZLIB_COMPRESS_LV_DFL 7 + +static int _zlib_compress_level = ZLIB_COMPRESS_LV_DFL; + +void list_set_zlib_ctrl(int v) +{ + _zlib_compress_level = v; +} + +static void *_zlib_open(const char *pathname, const char *mode) +{ + char open_r[] = "rb"; + char open_w[] = "wb"; + char open_wl[8]; + if (*mode == 'r') { + return gzopen(pathname, open_r); + } else if (*mode == 'w') { + sprintf(open_wl, "%s%d", open_w, _zlib_compress_level); + return gzopen(pathname, open_wl); + } else { + /* Unsupported mode */ + return NULL; + } +} + +static int _zlib_close(void *fp) +{ + return gzclose((gzFile)fp); +} + +static long _zlib_read(void *buf, long size, void *fp) +{ + return gzread((gzFile)fp, buf, size); +} + +static long _zlib_write(void *buf, long size, void *fp) +{ + return gzwrite((gzFile)fp, buf, size); +} + +void list_set_io_zlib(void) +{ + lsio.open = _zlib_open; + lsio.close = _zlib_close; + lsio.read = _zlib_read; + lsio.write = _zlib_write; +} +#endif /* ENABLE_ZLIB */ + +/* + * Exported file structure of Static LIST + * name_len | list->name | list | list->mem + */ +static lsw_t list_export_static( + struct list *ls, const char *path) +{ + void *fp; + uint name_len = 0; + fp = lsio.open(path, "wb"); + if (!fp) + return LSS_FILE_ERR; + if (ls->name) + name_len = strlen(ls->name); + if (!_is_little_endian()) { + _memrev(&name_len, sizeof(uint)); + _switch_byte_order(ls); + } + lsio.write(&name_len, sizeof(uint), fp); + lsio.write(ls->name, name_len, fp); + lsio.write((byte*)ls + + LIST_INFO_OFFSET, LIST_INFO_LEN, fp); + lsio.write(ls->mem, ls->length, fp); + if (!_is_little_endian()) { + _switch_byte_order(ls); + } + lsio.close(fp); + return LSS_SUCCESS; +} + +#define PROCESS_EXPORT_DYN \ +if (ls->index[sc]) { \ + if (!_is_little_endian()) \ + _memrev(ls->index[sc], sizeof(rlen_t)); \ + lsio.write(ls->index[sc], sizeof(rlen_t) + \ + *(rlen_t*)ls->index[sc], fp); \ + if (!_is_little_endian()) { \ + _memrev(ls->index[sc], sizeof(rlen_t)); \ + _memrev(&sc, sizeof(uint)); \ + } \ + lsio.write(&sc, sizeof(uint), fp); \ + if (!_is_little_endian()) \ + _memrev(&sc, sizeof(uint)); \ +} + +/* + * Exported file structure of Dynamic LIST + * name_len | list->name | list | *index[0:N] + */ +static lsw_t list_export_dynamic( + struct list *ls, const char *path) +{ + void *fp; + uint name_len = 0; + uint sc = ls->scale; + fp = lsio.open(path, "wb"); + if (!fp) + return LSS_FILE_ERR; + if (ls->name) + name_len = strlen(ls->name); + if (!_is_little_endian()) { + _memrev(&name_len, sizeof(uint)); + _switch_byte_order(ls); + } + lsio.write(&name_len, sizeof(uint), fp); + lsio.write(ls->name, name_len, fp); + lsio.write((byte*)ls + + LIST_INFO_OFFSET, LIST_INFO_LEN, fp); + while (sc--) { + PROCESS_EXPORT_DYN; + } + if (!_is_little_endian()) { + _switch_byte_order(ls); + } + lsio.close(fp); + return LSS_SUCCESS; +} + +lsw_t list_export(struct list *ls, const char *path) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return list_export_dynamic(ls, path); + else /* LIST_STATIC_MODE */ + return list_export_static(ls, path); +} + +#define PROCESS_IMPORT_DYN_RECORD \ + ls->index = (byte**) \ + malloc(ls->scale * sizeof(byte*)); \ + if (!ls->index) { \ + free(ls); \ + free(name); \ + lsio.close(fp); \ + return NULL; \ + } \ + memset(ls->index, 0, ls->scale * sizeof(byte*)); \ + while (lsio.read(&len, sizeof(rlen_t), fp)) { \ + if (!_is_little_endian()) \ + _memrev(&len, sizeof(rlen_t)); \ + tmp = (byte*)malloc(len + sizeof(rlen_t)); \ + if (!tmp) { \ + lsio.close(fp); \ + return ls; \ + } \ + *(rlen_t*)tmp = len; \ + lsio.read(tmp + \ + sizeof(rlen_t), len, fp); \ + lsio.read(&id, sizeof(uint), fp); \ + if (!_is_little_endian()) \ + _memrev(&id, sizeof(uint)); \ + ls->index[id] = tmp; \ + } + +#define PROCESS_IMPORT_STATIC_RECORD \ + ls->mem = (byte*)malloc(ls->length); \ + if (!ls->mem) { \ + free(ls); \ + free(name); \ + lsio.close(fp); \ + return NULL; \ + } \ + lsio.read(ls->mem, ls->length, fp); + +#define PROCESS_IMPORT_OPEN_FILE \ + fp = lsio.open(path, "rb"); \ + if (!fp) \ + return NULL; \ + ls = LS_ALLOC(struct list); \ + if (!ls) { \ + lsio.close(fp); \ + return NULL; \ + } \ + lsio.read(&name_len, sizeof(uint), fp); \ + if (!_is_little_endian()) \ + _memrev(&name_len, sizeof(uint)); \ + if (name_len) { \ + name_len++; \ + name = (char*)malloc(name_len); \ + if (!name) { \ + free(ls); \ + lsio.close(fp); \ + return NULL; \ + } \ + memset(name, 0, name_len); \ + lsio.read(name, name_len - 1, fp); \ + } \ + lsio.read((byte*)ls + LIST_INFO_OFFSET, \ + LIST_INFO_LEN, fp); \ + if (!_is_little_endian()) \ + _switch_byte_order(ls); \ + ls->name = name; + +static int _allow_io_auto_switch = 1; + +void list_enable_io_auto_switch(void) +{ + _allow_io_auto_switch = 1; +} + +void list_disable_io_auto_switch(void) +{ + _allow_io_auto_switch = 0; +} + +#define F_MAGIC_LEN 4 +static void _list_import_io_auto_switch(const char *pathname) +{ + unsigned char buf[F_MAGIC_LEN] = {0}; + FILE *fp = fopen(pathname, "rb"); + assert(fp); + fread(buf, F_MAGIC_LEN, 1, fp); + if (!memcmp(sebheader, buf, sizeof(sebheader))) { +#ifdef ENABLE_SEB + list_set_io_seb(); +#else + assert(0); +#endif /* ENABLE_SEB */ + } else if (!memcmp(gzheader, buf, sizeof(gzheader))) { +#ifdef ENABLE_ZLIB + list_set_io_zlib(); +#else + assert(0); +#endif /* ENABLE_ZLIB */ + } else { + list_set_io_stdio(); + } + fclose(fp); +} + +struct list *list_import(const char *path) +{ + struct list *ls; + struct lsio_struct lsio_bak; + void *fp; + char *name = NULL; + byte *tmp; + uint name_len, id; + rlen_t len; + if (_allow_io_auto_switch) { + lsio_bak = lsio; + _list_import_io_auto_switch(path); + } + PROCESS_IMPORT_OPEN_FILE; + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { + PROCESS_IMPORT_DYN_RECORD; + } else { /* LIST_STATIC_MODE */ + PROCESS_IMPORT_STATIC_RECORD; + } + lsio.close(fp); + if (_allow_io_auto_switch) + lsio = lsio_bak; + return ls; +} + +struct lsio_struct *list_get_io_ctrl_struct(void) +{ + return &lsio; +} +#else /* DISABLE FILE OPS */ +struct lsio_struct *list_get_io_ctrl_struct(void) +{ + return NULL; +} +#endif /* ENABLE_FOPS */ + +#ifdef ENABLE_SSHM +struct list *list_new_shared(uint scale, uint blen, uint key) +{ + byte *shm; + uint shmlen = 0; + struct list *ls = LS_ALLOC(struct list); + if (!ls) + return NULL; + memset(ls, 0, sizeof(struct list)); + shmlen += LIST_NAME_LEN; + shmlen += sizeof(struct list); + SET_FLAG(ls->flag, LIST_STATIC_MODE); + SET_FLAG(ls->flag, LIST_UNRESIZABLE); + SET_FLAG(ls->flag, LIST_SHARED_MEM); + SET_FLAG(ls->flag, LIST_SHARED_OWNER); + ls->length = scale * blen; + shmlen += ls->length; + shm = (byte*)create_shm(shmlen, key); + if (!shm) { + free(ls); + return NULL; + } + memset(shm, 0, shmlen); + memcpy(shm, ls, sizeof(struct list)); + free(ls); + ls = (struct list*)shm; + ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; + ls->key = key; + ls->blen = blen; + ls->scale = scale; + ls->counter = scale; + ls->name = (char*)shm + sizeof(struct list); + return ls; +} + +struct list *list_link_shared(uint len, uint key) +{ + byte *shm; + struct list *ls = (struct list*)malloc(sizeof(struct list)); + if (!ls) + return NULL; + shm = (byte*)create_shm(len + + sizeof(struct list) + LIST_NAME_LEN, key); + if (!shm) { + free(ls); + return NULL; + } + memcpy(ls, shm, sizeof(struct list)); + ls->name = (char*)shm + sizeof(struct list); + ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; + SET_FLAG(ls->flag, LIST_UNRESIZABLE); + SET_FLAG(ls->flag, LIST_SHARED_MEM); + SET_FLAG(ls->flag, LIST_SHARED_USER); + return ls; +} + +lsw_t list_del_shared(struct list *ls) +{ + uint shmlen; + if (TEST_FLAG(ls->flag, LIST_SHARED_USER)) + return LSS_ARG_ILL; + shmlen = (sizeof(struct list) + + LIST_NAME_LEN + ls->length); + if (free_shm(shmlen, ls->key)) + return LSS_SHM_ERR; + return LSS_SUCCESS; +} + +#endif /* ENABLE_SSHM */ + +#if defined(ENABLE_SSHM) && defined(ENABLE_FOPS) +#define PROCESS_IMPORT_SHARED \ + byte *shm; \ + char *name; \ + uint name_len = 0; \ + uint shmlen = 0; \ + fp = lsio.open(path, "rb"); \ + if (!fp) \ + return NULL; \ + ls = (struct list*) \ + malloc(sizeof(struct list)); \ + if (!ls) \ + return NULL; \ + lsio.read(&name_len, sizeof(uint), fp); \ + if (!_is_little_endian()) \ + _memrev(&name_len, sizeof(uint)); \ + name_len++; \ + name = (char*)malloc(name_len); \ + if (!name) { \ + free(ls); \ + return NULL; \ + } \ + memset(name, 0, name_len); \ + lsio.read(name, name_len - 1, fp); \ + lsio.read(ls, (sizeof(struct list)), fp); \ + if (!_is_little_endian()) \ + _switch_byte_order(ls); \ + shmlen = sizeof(struct list) + LIST_NAME_LEN; \ + shmlen += ls->length; \ + shm = (byte*)create_shm(shmlen, key); \ + if (!shm) { \ + free(ls); \ + free(name); \ + lsio.close(fp); \ + return NULL; \ + } \ + lsio.read(shm + sizeof(struct list) + \ + LIST_NAME_LEN, ls->length, fp); \ + memcpy(shm, ls, sizeof(struct list)); \ + strcpy((char*)shm + sizeof(struct list), name); \ + free(name); \ + ls->name = (char*)shm + sizeof(struct list); \ + ls->mem = shm + sizeof(struct list) + LIST_NAME_LEN; + +struct list * +list_import_shared(const char *path, uint key) +{ + struct list *ls; + void *fp; + PROCESS_IMPORT_SHARED; + lsio.close(fp); + return ls; +} +#endif /* ENABLE_SSHM && ENABLE_FOPS */ + +ccnt_t list_get_record_counter(struct list *ls, uint id) +{ + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) { + if (ls->index[id]) + return 1; + else + return 0; + } else { /* LIST_STATIC_MODE */ + if (TEST_FLAG(ls->flag, LIST_HASH_TABLE)) + return (*(ccnt_t*)(ls->mem + id * ls->blen)) >> 1; + } + return 1; +} + +int list_hash_table_test_id(struct list *ls, uint id) +{ + return (*(ccnt_t*)(ls->mem + id * ls->blen)) & 1; +} + +struct list *list_new_hash_table(uint scale, uint blen) +{ + struct list *ls; + if (!blen) /* support LIST_STATIC_MODE only */ + return NULL; + ls = list_new_static(scale, blen + sizeof(ccnt_t)); + if (!ls) + return NULL; + SET_FLAG(ls->flag, LIST_UNRESIZABLE); + SET_FLAG(ls->flag, LIST_HASH_TABLE); + return ls; +} + +lsw_t list_hash_id_calc(struct list *ls, + const void *data, uint *hi, uint *id) +{ + uint nhash, cid; + nhash = _hfx(data, _seed) % ls->scale; + *hi = nhash; + cid = nhash; + while (list_hash_table_test_id(ls, cid)) { + cid++; + if (cid == ls->scale) + cid = 0; + if (cid == nhash) + return LSS_ERR_LISTFULL; + } + *id = cid; + return LSS_SUCCESS; +} + +lsw_t list_hash_table_insert(struct list *ls, + const void *record, uint len) +{ + uint hi, id, dlen; + ccnt_t *hrec, *rrec; + lsw_t stat; + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return LSS_BAD_OBJ; + if ((stat = list_hash_id_calc(ls, record, &hi, &id))) + return stat; + hrec = (ccnt_t*)(ls->mem + hi * ls->blen); + rrec = (ccnt_t*)(ls->mem + id * ls->blen); + dlen = ls->blen - sizeof(ccnt_t); + if (CCNT_VAL(*hrec) == CCNT_MAX) + return LSS_CCNT_MAX; + memcpy(rrec + 1, record, len > dlen ? dlen : len); + (*hrec) += CCNT_INC; + (*rrec) |= 1; /* SET FLAG */ + return LSS_SUCCESS; +} + +lsw_t list_hash_table_find(struct list *ls, + const void *key, uint *id) +{ + uint nhash, cid; + byte *rec; + ccnt_t nrec; + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return LSS_BAD_OBJ; + nhash = _hfx(key, _seed) % ls->scale; + cid = nhash; + rec = ls->mem + cid * ls->blen; + nrec = (*(ccnt_t*)(ls->mem + cid * ls->blen)) >> 1; + if (!nrec) + return LSS_OBJ_NOFOUND; + while (nrec--) { + if (_hfx(rec + sizeof(ccnt_t), + _seed) % ls->scale == nhash) { + if (!_cmp(rec + sizeof(ccnt_t), key)) { + *id = cid; + return LSS_SUCCESS; + } + } + cid++; + if (cid == nhash) + return LSS_OBJ_NOFOUND; + if (cid == ls->scale) { + cid = 0; + rec = ls->mem; + } else { + rec += ls->blen; + } + } + return LSS_SUCCESS; +} + +lsw_t list_hash_table_del(struct list *ls, const void *key) +{ + uint nhash, id; + byte *rec, *csr; + ccnt_t nrec; + if (TEST_FLAG(ls->flag, LIST_DYNAMIC_MODE)) + return LSS_BAD_OBJ; + nhash = _hfx(key, _seed) % ls->scale; + rec = ls->mem + nhash * ls->blen; + csr = rec; + nrec = (*(ccnt_t*)rec) >> 1; + if (!nrec) + return LSS_SUCCESS; + id = nhash; + while (1) { + if (_hfx(csr + sizeof(ccnt_t), + _seed) % ls->scale == nhash) { + if (!_cmp(csr + sizeof(ccnt_t), key)) { + nrec--; + if (!nrec) + break; + } + } + id++; + if (id == nhash) { + if (nrec) + return LSS_OBJ_NOFOUND; + return LSS_BAD_OBJ; + } + if (id == ls->scale) { + id = 0; + csr = ls->mem; + } else { + csr += ls->blen; + } + } + (*(ccnt_t*)csr) ^= 1; /* CLR FLAG */ + (*(ccnt_t*)rec) -= CCNT_INC; + return LSS_SUCCESS; +} + +void list_print_properties(struct list *ls, void *stream) +{ + FILE *fp = stream ? (FILE*)stream : stdout; + if (!ls) { + fprintf(fp, "[ERROR] : Bad object, stopped!\n"); + return; + } + if (ls->name) + fprintf(fp, "[%s]\n", ls->name); + else + fprintf(fp, "Not named list.\n"); + if (TEST_FLAG(ls->flag, LIST_STATIC_MODE)) + fprintf(fp, "[MODE] = STATIC\n"); + else + fprintf(fp, "[MODE] = DYNAMIC\n"); + fprintf(fp, "[length] = %lld\n", ls->length); + fprintf(fp, "[scale] = %lld\n", ls->scale); + fprintf(fp, "[record] = %lld\n", ls->counter); + fprintf(fp, "[block length] = %lld\n", ls->blen); +} + +#ifdef LS_TYPE_BYTE + #undef byte +#endif +#ifdef LS_TYPE_UINT + #undef uint +#endif +#undef _UTIL_LIST_C_ diff --git a/src/util_list.h b/src/util_list.h index 09eb264..4308b1b 100644 --- a/src/util_list.h +++ b/src/util_list.h @@ -1,227 +1,236 @@ -#ifndef _LIST_H_ -#define _LIST_H_ - -#ifdef __cplusplus - extern "C" { -#endif - -#ifdef CONFIG_STD_C89 - #define LS_INLINE -#else - #define LS_INLINE inline -#endif - -#ifndef byte - #define LS_TYPE_BYTE - #define byte unsigned char -#endif -#ifndef uint - #define LS_TYPE_UINT - #define uint unsigned int -#endif - -typedef unsigned char lsw_t; - -enum LSS_STATUS_ { - LSS_SUCCESS = 0, - LSS_BAD_ID, - LSS_MALLOC_ERR, - LSS_DYN_ID_EXIST, - LSS_BAD_OBJ, - LSS_ARG_ILL, - LSS_LNAME_ERR, - LSS_FILE_ERR, - LSS_SHM_ERR, - LSS_ERR_LISTFULL, - LSS_OBJ_NOFOUND, - LSS_CCNT_MAX -}; - -#define SET_BIT(val, bitn) (val |=(1<<(bitn))) -#define CLR_BIT(val, bitn) (val&=~(1<<(bitn))) -#define GET_BIT(val, bitn) (val & (1<<(bitn))) - -#define SET_FLAG(flag, bft) \ - if(bft & 0x0F)SET_BIT(flag, bft >> 4); else CLR_BIT(flag, bft >> 4); -#define TEST_FLAG(flag, bft) \ - !(((bft & 0x0F) << (bft >> 4)) ^ (flag & (1 << (bft >> 4)))) - -#define CONFLICT_COUNTER byte -#define REC_LENGTH_TYPE uint - -typedef CONFLICT_COUNTER ccnt_t; -typedef REC_LENGTH_TYPE rlen_t; - -#ifndef LIST_NAME_LEN - #define LIST_NAME_LEN 128 -#endif - -#ifndef LIST_PATH_LEN - #define LIST_PATH_LEN 256 -#endif - -struct list { - char *name; /* name of LIST */ - byte *mem; /* memory pool */ - byte **index; /* index table */ - uint length; /* length of memory pool */ - uint key; /* key of shared mem */ - uint counter; /* record counter */ - uint blen; /* length of a record */ - uint scale; /* scale */ - byte flag; /* list's mode flag */ - byte placeholder[3]; -}; - -#define LIST_INFO_LEN (sizeof(struct list) - sizeof(void*) * 3) -#define LIST_INFO_OFFSET (sizeof(struct list) - LIST_INFO_LEN) - -#define LIST_FLAG_DEFAULT 0x00 -#define LIST_STATIC_MODE 0x00 -#define LIST_DYNAMIC_MODE 0x01 -#define LIST_HASH_NOT_SET 0x40 -#define LIST_HASH_TABLE 0x41 -#define LIST_RESIZABLE 0x50 -#define LIST_UNRESIZABLE 0x51 -#define LIST_SHARED_USER 0x60 -#define LIST_SHARED_OWNER 0x61 -#define LIST_NOT_SHARED 0x70 -#define LIST_SHARED_MEM 0x71 - -struct list *list_new(uint scale, uint blen); - -struct list *list_clone(struct list *ls); - -void list_del(struct list *ls); - -lsw_t list_resize(struct list *ls, uint scale); - -lsw_t list_rename(struct list *ls, const char *name); - -void *list_set_data(struct list *ls, uint id, - const void *data, uint len); - -void *list_alloc(struct list *ls, uint id, uint nbyte); - -void *list_index(struct list *ls, uint id); - -uint list_getlen(struct list *ls, uint id); - -#define list_calc_dynlen(r) \ - (*((rlen_t*)((unsigned char*)r - sizeof(rlen_t)))) - -lsw_t list_erase(struct list *ls, uint id); - -lsw_t list_swap(struct list *ls, uint id1, uint id2); - -typedef void *(lsio_open) (const char *pathname, const char *mode); -typedef int (lsio_close)(void *fp); -typedef long (lsio_read) (void *buf, long size, void *fp); -typedef long (lsio_write)(void *buf, long size, void *fp); -typedef long (lsio_seek) (void *fp, long off, int whence); -typedef long (lsio_tell) (void *fp); - -struct lsio_struct { - lsio_open *open; - lsio_close *close; - lsio_read *read; - lsio_write *write; - lsio_seek *seek; - lsio_tell *tell; -}; - -struct lsio_struct *list_get_io_ctrl_struct(void); - -void list_set_io_stdio(void); - -/* Set `lsio` to seb ways */ -extern void list_set_io_seb(void); -/* Set seb compression level */ -extern int list_get_seb_ctrl(void); -extern void list_set_seb_ctrl(int v); - -/* Set `lsio` to zlib ways */ -extern void list_set_io_zlib(void); -/* Set Zlib compression level */ -extern int list_get_zlib_ctrl(void); -extern void list_set_zlib_ctrl(int v); - -lsw_t list_export(struct list *ls, const char *path); - -void list_enable_io_auto_switch(void); -void list_disable_io_auto_switch(void); - -struct list *list_import(const char *path); - -/* struct list | name | mem */ - -struct list *list_new_shared(uint scale, uint blen, uint key); - -struct list *list_link_shared(uint len, uint key); - -#define list_shared_shm_len(list) \ - (sizeof(LIST) + LIST_NAME_LEN + list->length) - -lsw_t list_del_shared(struct list *ls); - -#define list_export_shared list_export_static -struct list *list_import_shared(const char *path, uint key); - -#define list_set_unresizable(list) \ - SET_FLAG(list->flag, LIST_UNRESIZABLE) -#define list_set_resizable(list) \ - SET_FLAG(list->flag, LIST_RESIZABLE) - -/* - * Simple hash table, LIST_STATIC_MODE only - * For each record: | CONFLICT_COUNTER | Data | - * - * CONFLICT_COUNTER: Only used in hast table under static mode, - * the number of conflicted elements is limited to 127 - * 0 1 2 3 4 5 6 7 - * x x x x x x x f - * _____________ \__.Flag - * \___.CONFLICT_COUNTER - */ - -#define CCNT_INC 0x02 /* (1 << 1)*/ -#define CCNT_MAX 0x7F -#define CCNT_VAL(cnt) (cnt >> 1) - -typedef uint (*__hashFx)(const void*, uint); -typedef int (*__compFx)(const void*, const void*); - -void list_set_hash_fn(__hashFx hfx); -void list_set_hash_seed(uint seed); - -int list_hash_table_test_id(struct list *ls, uint id); - -ccnt_t list_get_record_counter(struct list *ls, uint id); - -struct list *list_new_hash_table(uint scale, uint blen); - -lsw_t list_hash_id_calc(struct list *ls, - const void *data, uint *hi, uint *id); - -lsw_t list_hash_table_insert(struct list *ls, - const void *record, uint len); - -lsw_t list_hash_table_find(struct list *ls, - const void *key, uint *id); - -lsw_t list_hash_table_del(struct list *ls, const void *key); - -void list_print_properties(struct list *ls, void *stream); - -#ifdef LS_TYPE_BYTE - #undef byte -#endif -#ifdef LS_TYPE_UINT - #undef uint -#endif - -#ifdef __cplusplus - } -#endif - -#endif /* _LIST_H_ */ +#ifndef _UTIL_LIST_H_ +#define _UTIL_LIST_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +#ifdef CONFIG_STD_C89 + #define LS_INLINE +#else + #define LS_INLINE inline +#endif + +#ifndef byte + #define LS_TYPE_BYTE + #define byte unsigned char +#endif +#ifndef uint +#ifdef _MSC_VER + #define LS_TYPE_UINT + #define uint unsigned __int64 +#else /* 64bit integer for gcc, clang... */ + #define LS_TYPE_UINT + #define uint unsigned long long +#endif +#endif + +typedef unsigned char lsw_t; + +enum { + LSS_SUCCESS = 0, + LSS_BAD_ID, + LSS_MALLOC_ERR, + LSS_DYN_ID_EXIST, + LSS_BAD_OBJ, + LSS_ARG_ILL, + LSS_LNAME_ERR, + LSS_FILE_ERR, + LSS_SHM_ERR, + LSS_ERR_LISTFULL, + LSS_OBJ_NOFOUND, + LSS_CCNT_MAX +}; + +#define SET_BIT(val, bitn) (val |=(1<<(bitn))) +#define CLR_BIT(val, bitn) (val&=~(1<<(bitn))) +#define GET_BIT(val, bitn) (val & (1<<(bitn))) + +#define SET_FLAG(flag, bft) \ + if(bft & 0x0F)SET_BIT(flag, bft >> 4); else CLR_BIT(flag, bft >> 4); +#define TEST_FLAG(flag, bft) \ + !(((bft & 0x0F) << (bft >> 4)) ^ (flag & (1 << (bft >> 4)))) + +#define CONFLICT_COUNTER byte +#define REC_LENGTH_TYPE uint + +typedef CONFLICT_COUNTER ccnt_t; +typedef REC_LENGTH_TYPE rlen_t; + +#ifndef LIST_NAME_LEN + #define LIST_NAME_LEN 128 +#endif + +#ifndef LIST_PATH_LEN + #define LIST_PATH_LEN 256 +#endif + +struct list { + char *name; /* name of LIST */ + byte *mem; /* memory pool */ + byte **index; /* index table */ + uint length; /* length of memory pool */ + uint key; /* key of shared mem */ + uint counter; /* record counter */ + uint blen; /* length of a record */ + uint scale; /* scale */ +union { + byte flag; /* list's mode flag */ + uint _placeholder; +}; +}; + +#define LIST_INFO_LEN (sizeof(struct list) - sizeof(void*) * 3) +#define LIST_INFO_OFFSET (sizeof(struct list) - LIST_INFO_LEN) + +#define LIST_FLAG_DEFAULT 0x00 +#define LIST_STATIC_MODE 0x00 +#define LIST_DYNAMIC_MODE 0x01 +#define LIST_HASH_NOT_SET 0x40 +#define LIST_HASH_TABLE 0x41 +#define LIST_RESIZABLE 0x50 +#define LIST_UNRESIZABLE 0x51 +#define LIST_SHARED_USER 0x60 +#define LIST_SHARED_OWNER 0x61 +#define LIST_NOT_SHARED 0x70 +#define LIST_SHARED_MEM 0x71 + +struct list *list_new(uint scale, uint blen); + +struct list *list_clone(struct list *ls); + +void list_del(struct list *ls); + +lsw_t list_resize(struct list *ls, uint scale); + +lsw_t list_rename(struct list *ls, const char *name); + +void *list_set_data(struct list *ls, uint id, + const void *data, uint len); + +void *list_alloc(struct list *ls, uint id, uint nbyte); + +void *list_index(struct list *ls, uint id); + +uint list_getlen(struct list *ls, uint id); + +#define list_calc_dynlen(r) \ + (*((rlen_t*)((unsigned char*)r - sizeof(rlen_t)))) + +lsw_t list_erase(struct list *ls, uint id); + +lsw_t list_swap(struct list *ls, uint id1, uint id2); + +typedef void *(lsio_open) (const char *pathname, const char *mode); +typedef int (lsio_close)(void *fp); +typedef long (lsio_read) (void *buf, long size, void *fp); +typedef long (lsio_write)(void *buf, long size, void *fp); +typedef long (lsio_seek) (void *fp, long off, int whence); +typedef long (lsio_tell) (void *fp); + +struct lsio_struct { + lsio_open *open; + lsio_close *close; + lsio_read *read; + lsio_write *write; + lsio_seek *seek; + lsio_tell *tell; +}; + +struct lsio_struct *list_get_io_ctrl_struct(void); + +void list_set_io_stdio(void); + +/* Set `lsio` to seb ways */ +extern void list_set_io_seb(void); +/* Set seb compression level */ +extern int list_get_seb_ctrl(void); +extern void list_set_seb_ctrl(int v); + +/* Set `lsio` to zlib ways */ +extern void list_set_io_zlib(void); +/* Set Zlib compression level */ +extern int list_get_zlib_ctrl(void); +extern void list_set_zlib_ctrl(int v); + +lsw_t list_export(struct list *ls, const char *path); + +void list_enable_io_auto_switch(void); +void list_disable_io_auto_switch(void); + +struct list *list_import(const char *path); + +/* struct list | name | mem */ + +struct list *list_new_shared(uint scale, uint blen, uint key); + +struct list *list_link_shared(uint len, uint key); + +#define list_shared_shm_len(list) \ + (sizeof(LIST) + LIST_NAME_LEN + list->length) + +lsw_t list_del_shared(struct list *ls); + +#define list_export_shared list_export_static +struct list *list_import_shared(const char *path, uint key); + +#define list_set_unresizable(list) \ + SET_FLAG(list->flag, LIST_UNRESIZABLE) +#define list_set_resizable(list) \ + SET_FLAG(list->flag, LIST_RESIZABLE) + +/* + * Simple hash table, LIST_STATIC_MODE only + * For each record: | CONFLICT_COUNTER | Data | + * + * CONFLICT_COUNTER: Only used in hast table under static mode, + * the number of conflicted elements is limited to 127 + * 0 1 2 3 4 5 6 7 + * x x x x x x x f + * _____________ \__.Flag + * \___.CONFLICT_COUNTER + */ + +#define CCNT_INC 0x02 /* (1 << 1)*/ +#define CCNT_MAX 0x7F +#define CCNT_VAL(cnt) (cnt >> 1) + +typedef uint (*__hashFx)(const void*, uint); +typedef int (*__compFx)(const void*, const void*); + +void list_set_hash_fn(__hashFx hfx); +void list_set_hash_seed(uint seed); + +int list_hash_table_test_id(struct list *ls, uint id); + +ccnt_t list_get_record_counter(struct list *ls, uint id); + +struct list *list_new_hash_table(uint scale, uint blen); + +lsw_t list_hash_id_calc(struct list *ls, + const void *data, uint *hi, uint *id); + +lsw_t list_hash_table_insert(struct list *ls, + const void *record, uint len); + +lsw_t list_hash_table_find(struct list *ls, + const void *key, uint *id); + +lsw_t list_hash_table_del(struct list *ls, const void *key); + +void list_print_properties(struct list *ls, void *stream); + +#ifndef _UTIL_LIST_C_ + #ifdef LS_TYPE_BYTE + #undef byte + #endif + #ifdef LS_TYPE_UINT + #undef uint + #endif +#endif + +#ifdef __cplusplus + } +#endif + +#endif /* _UTIL_LIST_H_ */ diff --git a/src/util_rbt.c b/src/util_rbt.c index db67fa5..81fa9bb 100644 --- a/src/util_rbt.c +++ b/src/util_rbt.c @@ -24,7 +24,7 @@ struct rbt_node *rbt_nil(void) return &node_nil; } -struct rbtree *new_rbt(void*(*get_key)(struct rbt_node *), +struct rbtree *rbt_new(void*(*get_key)(struct rbt_node *), int (*compare)(const void*, const void*)) { struct rbtree *t = (struct rbtree*)malloc(sizeof(struct rbtree)); @@ -154,7 +154,7 @@ void *rbt_insert(struct rbtree *t, void *d) return NULL; } -static struct rbt_node *__search_rbt_node(struct rbtree *t, void *key) +static struct rbt_node *__find_rbt_node(struct rbtree *t, void *key) { struct rbt_node *z = t->root; while (z != &node_nil) { @@ -168,9 +168,9 @@ static struct rbt_node *__search_rbt_node(struct rbtree *t, void *key) return NULL; } -void *rbt_search(struct rbtree *t, void *key) +void *rbt_find(struct rbtree *t, void *key) { - struct rbt_node *z = __search_rbt_node(t, key); + struct rbt_node *z = __find_rbt_node(t, key); if (z) return z->data; return NULL; @@ -188,7 +188,7 @@ static void __free_rbt(struct rbt_node *root) __free_rbt(r); } -void free_rbt(struct rbtree *t) +void rbt_del(struct rbtree *t) { __free_rbt(t->root); free(t); @@ -271,12 +271,12 @@ static void __rb_tree_delete_fixup(struct rbtree *t, struct rbt_node *x) x->color = RBT_COLOR_BLACK; } -void *rbt_delete(struct rbtree *t, void *key) +void *rbt_erase(struct rbtree *t, void *key) { struct rbt_node *y, *z, *x, *hold_node_to_delete; unsigned char y_original_color; void *node_to_return; - hold_node_to_delete = y = z = __search_rbt_node(t, key); + hold_node_to_delete = y = z = __find_rbt_node(t, key); if (y == NULL) /* Node not exist */ return NULL; node_to_return = y->data; diff --git a/src/util_rbt.h b/src/util_rbt.h index b4b8321..1b706d5 100644 --- a/src/util_rbt.h +++ b/src/util_rbt.h @@ -1,5 +1,5 @@ -#ifndef _RBT_H_ -#define _RBT_H_ +#ifndef _UTIL_RBT_H_ +#define _UTIL_RBT_H_ #ifdef __cplusplus extern "C" { @@ -22,21 +22,20 @@ struct rbtree { int (*compare)(const void*, const void*); }; -struct rbtree *new_rbt(void*(*get_key)(struct rbt_node *), +struct rbtree *rbt_new(void*(*get_key)(struct rbt_node *), int (*compare)(const void*, const void*)); -void free_rbt(struct rbtree *t); +void rbt_del(struct rbtree *t); struct rbt_node *rbt_nil(void); void *rbt_insert(struct rbtree *t, void *d); +void *rbt_find(struct rbtree *t, void *key); +void *rbt_erase(struct rbtree *t, void *key); -void *rbt_delete(struct rbtree *t, void *key); - -void *rbt_search(struct rbtree *t, void *key); #ifdef __cplusplus } #endif -#endif /* _RBT_H_ */ +#endif /* _UTIL_RBT_H_ */ diff --git a/src/util_vec.h b/src/util_vec.h index 64181a8..020aaff 100644 --- a/src/util_vec.h +++ b/src/util_vec.h @@ -1,5 +1,5 @@ -#ifndef _VECTOR_H_ -#define _VECTOR_H_ +#ifndef _UTIL_VECTOR_H_ +#define _UTIL_VECTOR_H_ #ifdef CONFIG_STD_C89 #define size_t unsigned int @@ -45,4 +45,4 @@ void vector_insert(struct vector *vec, int index, void *elem); #undef uint8_t #endif -#endif /* _VECTOR_H_ */ +#endif /* _UTIL_VECTOR_H_ */ diff --git a/util/mkpkg.c b/util/mkpkg.c index c2a1a99..20d43c6 100644 --- a/util/mkpkg.c +++ b/util/mkpkg.c @@ -135,7 +135,7 @@ cc_dtype resolve_dtype(const char *s) return dtype; } -static void resolve_shape(const char *s, cc_int32 *shape) +static void resolve_shape(const char *s, cc_ssize *shape) { do { if (*s == ',') @@ -148,7 +148,7 @@ static void resolve_shape(const char *s, cc_int32 *shape) static void load_parameter(const char *path, cJSON *item) { cc_dtype dtype; - cc_int32 shape[32] = {0}; + cc_ssize shape[32] = {0}; char pathname[256]; sprintf(pathname, "%s/%s", path, item->string); dtype = resolve_dtype(item->child->valuestring); @@ -220,7 +220,7 @@ static void para_compose_norm(const char *name) int i, len; /* cc_tensor_t *w, *b, *m, *v; */ cc_tensor_t *tsr[CC_NORM_PARAMETERS]; - cc_int32 shape[] = {-1, 1, 1, 0}; + cc_ssize shape[] = {-1, 1, 1, 0}; len = strlen(name); memcpy(buf, name, len); buf[len] = '\0';