summaryrefslogtreecommitdiff
path: root/drivers/block/zram/backend_lz4hc.c
blob: 536f7a0073c47852a0c68bc687e9a068aeb6d2a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <linux/kernel.h>
#include <linux/lz4.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

#include "backend_lz4hc.h"

struct lz4hc_ctx {
	void *mem;
	s32 level;
};

static void lz4hc_destroy(void *ctx)
{
	struct lz4hc_ctx *zctx = ctx;

	vfree(zctx->mem);
	kfree(zctx);
}

static void *lz4hc_create(void)
{
	struct lz4hc_ctx *ctx;

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return NULL;

	/* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */
	ctx->level = LZ4HC_DEFAULT_CLEVEL;
	ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS);
	if (!ctx->mem)
		goto error;

	return ctx;
error:
	lz4hc_destroy(ctx);
	return NULL;
}

static int lz4hc_compress(void *ctx, const unsigned char *src, size_t src_len,
			  unsigned char *dst, size_t *dst_len)
{
	struct lz4hc_ctx *zctx = ctx;
	int ret;

	ret = LZ4_compress_HC(src, dst, src_len, *dst_len,
			      zctx->level, zctx->mem);
	if (!ret)
		return -EINVAL;
	*dst_len = ret;
	return 0;
}

static int lz4hc_decompress(void *ctx, const unsigned char *src,
			    size_t src_len, unsigned char *dst, size_t dst_len)
{
	int ret;

	ret = LZ4_decompress_safe(src, dst, src_len, dst_len);
	if (ret < 0)
		return -EINVAL;
	return 0;
}

const struct zcomp_ops backend_lz4hc = {
	.compress	= lz4hc_compress,
	.decompress	= lz4hc_decompress,
	.create_ctx	= lz4hc_create,
	.destroy_ctx	= lz4hc_destroy,
	.name		= "lz4hc",
};