Rate This Document
Findability
Accuracy
Completeness
Readability

Usage Example

This section provides an example of using all KUAF function interfaces.

  1. Set environment variables.
    export C_INCLUDE_PATH=/usr/local/kuaf/include:$C_INCLUDE_PATH
    export LD_LIBRARY_PATH=/usr/local/kuaf/lib/:/usr/local/lib:$LD_LIBRARY_PATH
  2. Create a kuaf.c file.
  3. Press i to enter the insert mode and add the following content to the file:
    #include "zlib.h"
    #include "kaezip.h"
    #include "kuaf_sc.h"
    #include "kuaf_alg.h"
    int main()
    {
        z_stream strm;
        strm.zalloc = (alloc_func)0;
        strm.zfree = (free_func)0;
        strm.opaque = (voidpf)0;
        // deflateInit2_() adaptation. The ctx scheduling structure is created, and the input data of the zlib function is encapsulated into the zlib_ctx structure.
        printf("kuaf_ctx_scheduler_create ctx here.\n");
        scheduler_ctx *ctx = kuaf_ctx_scheduler_create(ALG_COMP, ALG_ZLIB_DEFLATE);
        if (!ctx) {
            return Z_ERRNO;
        }
        printf("kuaf_zlib_ctx zlib_ctx malloc here.\n");
        kuaf_zlib_ctx *zlib_ctx =(kuaf_zlib_ctx *)malloc(sizeof(kuaf_zlib_ctx));
        if (!zlib_ctx) {
            kuaf_ctx_scheduler_free(ctx);
            return Z_ERRNO;
        }
        zlib_ctx->strm = &strm;
        zlib_ctx->flush = 0;
        ctx->data = (void *)zlib_ctx;
        printf("kuaf_ctx_scheduler here.\n");
        int ret = kuaf_ctx_scheduler(ctx);
        if (ret != KUAF_SUCCESS) {
            free(ctx->data);
            ctx->data = NULL;
            kuaf_ctx_scheduler_free(ctx);
            return Z_ERRNO;
        }
        /* Call KAE Hardware */
        if (ctx->dev_id != 0) {
            printf("kz_deflateInit2_ call here.\n");
        } else {
            printf("lz_deflateInit2_ call here.\n");
        }
        // deflate adaptation
        if (kz_get_devices()) {  // Check whether KAE hardware is available.
            printf("kz_deflate call here.\n");
            kuaf_zlib_ctx *zlib_ctx = (kuaf_zlib_ctx *)ctx->data;
            // zlib_ctx->flush = flush;
            printf("kuaf_ctx_process_sync here \n");
            int ret = kuaf_ctx_process_sync(ctx);
            // ret < 0 means process failed
            if (ret < 0) {
                free(ctx->data);
                ctx->data = NULL;
                kuaf_ctx_scheduler_free(ctx);
                ret = Z_ERRNO;
                return -1;
            }
        } else {
            printf("lz_deflate call here.");
        }
        // deflateEnd adaptation
        if (kz_get_devices()) {  // Check whether KAE hardware is available.
            printf("deflateEnd kz_get_devices call here.\n");
            int ret = Z_OK;
            if (ctx->dev_id != 0) {
                kuaf_ctx_end_process(ctx);
            }
            if (ctx->data != NULL) {
                free(ctx->data);
                ctx->data = NULL;
            }
            kuaf_ctx_scheduler_free(ctx);
            printf("deflateEnd ctx free call here.\n");
            if (ret != Z_OK) {
                return ret;
            } else {
                printf("kz_deflateEnd call here.\n");
            }
        } else {
            printf("lz_deflateEnd call here.\n");
        }
        return 0;
    }
  4. Press Esc, type :wq!, and press Enter to save the file and exit.
  5. Compile the kuaf.c file and specify the name of the output executable file as kuaf.
    gcc kuaf.c -L/usr/local/kuaf/lib -lkuaf -lkaezip -lz -lz_sw -o kuaf
  6. Run the kuaf executable file.
    ./kuaf
    The execution result is as follows:
    kuaf_ctx_scheduler_create ctx here.
    kuaf_zlib_ctx zlib_ctx malloc here.
    kuaf_ctx_shcheduler here.
    kz_deflateInit2_ call here.
    kz_deflate call here.
    kuaf_ctx_process_sync here