---
title: 使用示例
description: "本章节提供KUAF所有函数接口的使用示例。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_kuaf_16_0016.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_kuaf_16_0016.html
indexId: 01f26a1fa7a9e2fd5f81026f5c8f5b6d5ca50086e10c820826e26d55702fd51677
---
# 使用示例

本章节提供KUAF所有函数接口的使用示例。

1. 设置环境变量。

```
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. 创建kuaf.c文件。
3. 按“i”进入编辑模式，在文件中添加以下内容。

```
#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_（） 适配，创建调度结构体ctx，zlib函数本身入参数据封装成zlib_ctx结构体
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适配
if (kz_get_devices()) {  // kae硬件可用性检测
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适配
if (kz_get_devices()) {  // kae硬件可用检测
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. 按“Esc”键，输入:wq!，按“Enter”保存并退出编辑。
5. 编译kuaf.c文件，指定输出的可执行文件名称为kuaf。

```
gcc kuaf.c -L/usr/local/kuaf/lib -lkuaf -lkaezip -lz -lz_sw -o kuaf
```


6. 执行可执行文件kuaf。

```
./kuaf
```


  输出如下运行结果信息。

```
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
```
