bigdict.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include "datagen.h"
  16. #include "mem.h"
  17. #define ZSTD_STATIC_LINKING_ONLY
  18. #include "zstd.h"
  19. static int
  20. compress(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
  21. void* dst, size_t dstCapacity,
  22. void const* src, size_t srcSize,
  23. void* roundtrip, ZSTD_EndDirective end)
  24. {
  25. ZSTD_inBuffer in = {src, srcSize, 0};
  26. ZSTD_outBuffer out = {dst, dstCapacity, 0};
  27. int ended = 0;
  28. while (!ended && (in.pos < in.size || out.pos > 0)) {
  29. size_t rc;
  30. out.pos = 0;
  31. rc = ZSTD_compressStream2(cctx, &out, &in, end);
  32. if (ZSTD_isError(rc))
  33. return 1;
  34. if (end == ZSTD_e_end && rc == 0)
  35. ended = 1;
  36. {
  37. ZSTD_inBuffer rtIn = {dst, out.pos, 0};
  38. ZSTD_outBuffer rtOut = {roundtrip, srcSize, 0};
  39. rc = 1;
  40. while (rtIn.pos < rtIn.size || rtOut.pos > 0) {
  41. rtOut.pos = 0;
  42. rc = ZSTD_decompressStream(dctx, &rtOut, &rtIn);
  43. if (ZSTD_isError(rc)) {
  44. fprintf(stderr, "Decompression error: %s\n", ZSTD_getErrorName(rc));
  45. return 1;
  46. }
  47. if (rc == 0)
  48. break;
  49. }
  50. if (ended && rc != 0) {
  51. fprintf(stderr, "Frame not finished!\n");
  52. return 1;
  53. }
  54. }
  55. }
  56. return 0;
  57. }
  58. int main(int argc, const char** argv)
  59. {
  60. ZSTD_CCtx* cctx = ZSTD_createCCtx();
  61. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  62. const size_t dataSize = (size_t)1 << 30;
  63. const size_t outSize = ZSTD_compressBound(dataSize);
  64. const size_t bufferSize = (size_t)1 << 31;
  65. char* buffer = (char*)malloc(bufferSize);
  66. void* out = malloc(outSize);
  67. void* roundtrip = malloc(dataSize);
  68. (void)argc;
  69. (void)argv;
  70. if (!buffer || !out || !roundtrip || !cctx || !dctx) {
  71. fprintf(stderr, "Allocation failure\n");
  72. return 1;
  73. }
  74. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31)))
  75. return 1;
  76. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1)))
  77. return 1;
  78. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9)))
  79. return 1;
  80. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1)))
  81. return 1;
  82. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt)))
  83. return 1;
  84. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7)))
  85. return 1;
  86. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7)))
  87. return 1;
  88. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1)))
  89. return 1;
  90. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10)))
  91. return 1;
  92. if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10)))
  93. return 1;
  94. if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31)))
  95. return 1;
  96. RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe);
  97. /* Compress 30 GB */
  98. {
  99. int i;
  100. for (i = 0; i < 10; ++i) {
  101. fprintf(stderr, "Compressing 1 GB\n");
  102. if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
  103. return 1;
  104. }
  105. }
  106. fprintf(stderr, "Compressing 1 GB\n");
  107. if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
  108. return 1;
  109. fprintf(stderr, "Success!\n");
  110. free(roundtrip);
  111. free(out);
  112. free(buffer);
  113. ZSTD_freeDCtx(dctx);
  114. ZSTD_freeCCtx(cctx);
  115. return 0;
  116. }