backend_lz4hc.c (6a81bdfeb35094c3097650306a5fda9a990d8a97) | backend_lz4hc.c (b8f03cb703a160e14f87a467a4443adc5d940087) |
---|---|
1#include <linux/kernel.h> 2#include <linux/lz4.h> 3#include <linux/slab.h> 4#include <linux/vmalloc.h> 5 6#include "backend_lz4hc.h" 7 | 1#include <linux/kernel.h> 2#include <linux/lz4.h> 3#include <linux/slab.h> 4#include <linux/vmalloc.h> 5 6#include "backend_lz4hc.h" 7 |
8struct lz4hc_ctx { 9 void *mem; 10 s32 level; 11}; | 8static void lz4hc_release_params(struct zcomp_params *params) 9{ 10} |
12 | 11 |
13static void lz4hc_destroy(struct zcomp_ctx *ctx) | 12static int lz4hc_setup_params(struct zcomp_params *params) |
14{ | 13{ |
15 struct lz4hc_ctx *zctx = ctx->context; | 14 if (params->level == ZCOMP_PARAM_NO_LEVEL) 15 params->level = LZ4HC_DEFAULT_CLEVEL; |
16 | 16 |
17 if (!zctx) 18 return; | 17 return 0; 18} |
19 | 19 |
20 vfree(zctx->mem); 21 kfree(zctx); | 20static void lz4hc_destroy(struct zcomp_ctx *ctx) 21{ 22 vfree(ctx->context); |
22} 23 24static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx) 25{ | 23} 24 25static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx) 26{ |
26 struct lz4hc_ctx *zctx; 27 28 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL); 29 if (!zctx) | 27 ctx->context = vmalloc(LZ4HC_MEM_COMPRESS); 28 if (!ctx->context) |
30 return -ENOMEM; 31 | 29 return -ENOMEM; 30 |
32 ctx->context = zctx; 33 if (params->level != ZCOMP_PARAM_NO_LEVEL) 34 zctx->level = params->level; 35 else 36 zctx->level = LZ4HC_DEFAULT_CLEVEL; 37 38 zctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); 39 if (!zctx->mem) 40 goto error; 41 | |
42 return 0; | 31 return 0; |
43error: 44 lz4hc_destroy(ctx); 45 return -EINVAL; | |
46} 47 | 32} 33 |
48static int lz4hc_compress(struct zcomp_ctx *ctx, struct zcomp_req *req) | 34static int lz4hc_compress(struct zcomp_params *params, struct zcomp_ctx *ctx, 35 struct zcomp_req *req) |
49{ | 36{ |
50 struct lz4hc_ctx *zctx = ctx->context; | |
51 int ret; 52 53 ret = LZ4_compress_HC(req->src, req->dst, req->src_len, req->dst_len, | 37 int ret; 38 39 ret = LZ4_compress_HC(req->src, req->dst, req->src_len, req->dst_len, |
54 zctx->level, zctx->mem); | 40 params->level, ctx->context); |
55 if (!ret) 56 return -EINVAL; 57 req->dst_len = ret; 58 return 0; 59} 60 | 41 if (!ret) 42 return -EINVAL; 43 req->dst_len = ret; 44 return 0; 45} 46 |
61static int lz4hc_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req) | 47static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, 48 struct zcomp_req *req) |
62{ 63 int ret; 64 65 ret = LZ4_decompress_safe(req->src, req->dst, req->src_len, 66 req->dst_len); 67 if (ret < 0) 68 return -EINVAL; 69 return 0; 70} 71 72const struct zcomp_ops backend_lz4hc = { 73 .compress = lz4hc_compress, 74 .decompress = lz4hc_decompress, 75 .create_ctx = lz4hc_create, 76 .destroy_ctx = lz4hc_destroy, | 49{ 50 int ret; 51 52 ret = LZ4_decompress_safe(req->src, req->dst, req->src_len, 53 req->dst_len); 54 if (ret < 0) 55 return -EINVAL; 56 return 0; 57} 58 59const struct zcomp_ops backend_lz4hc = { 60 .compress = lz4hc_compress, 61 .decompress = lz4hc_decompress, 62 .create_ctx = lz4hc_create, 63 .destroy_ctx = lz4hc_destroy, |
64 .setup_params = lz4hc_setup_params, 65 .release_params = lz4hc_release_params, |
|
77 .name = "lz4hc", 78}; | 66 .name = "lz4hc", 67}; |