zcomp_lz4.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2014 Sergey Senozhatsky.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/lz4.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/mm.h>
  14. #include "zcomp_lz4.h"
  15. static void *zcomp_lz4_create(gfp_t flags)
  16. {
  17. void *ret;
  18. ret = kmalloc(LZ4_MEM_COMPRESS, flags);
  19. if (!ret)
  20. ret = __vmalloc(LZ4_MEM_COMPRESS,
  21. flags | __GFP_HIGHMEM,
  22. PAGE_KERNEL);
  23. return ret;
  24. }
  25. static void zcomp_lz4_destroy(void *private)
  26. {
  27. kvfree(private);
  28. }
  29. static int zcomp_lz4_compress(const unsigned char *src, unsigned char *dst,
  30. size_t *dst_len, void *private)
  31. {
  32. /* return : Success if return 0 */
  33. return lz4_compress(src, PAGE_SIZE, dst, dst_len, private);
  34. }
  35. static int zcomp_lz4_decompress(const unsigned char *src, size_t src_len,
  36. unsigned char *dst)
  37. {
  38. size_t dst_len = PAGE_SIZE;
  39. /* return : Success if return 0 */
  40. return lz4_decompress_unknownoutputsize(src, src_len, dst, &dst_len);
  41. }
  42. struct zcomp_backend zcomp_lz4 = {
  43. .compress = zcomp_lz4_compress,
  44. .decompress = zcomp_lz4_decompress,
  45. .create = zcomp_lz4_create,
  46. .destroy = zcomp_lz4_destroy,
  47. .name = "lz4",
  48. };