allocations.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under both the BSD-style license (found in the
  7. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8. * in the COPYING file in the root directory of this source tree).
  9. * You may select, at your option, one of the above-listed licenses.
  10. */
  11. /* This file provides custom allocation primitives
  12. */
  13. #define ZSTD_DEPS_NEED_MALLOC
  14. #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
  15. #include "compiler.h" /* MEM_STATIC */
  16. #define ZSTD_STATIC_LINKING_ONLY
  17. #include <linux/zstd.h> /* ZSTD_customMem */
  18. #ifndef ZSTD_ALLOCATIONS_H
  19. #define ZSTD_ALLOCATIONS_H
  20. /* custom memory allocation functions */
  21. MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
  22. {
  23. if (customMem.customAlloc)
  24. return customMem.customAlloc(customMem.opaque, size);
  25. return ZSTD_malloc(size);
  26. }
  27. MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
  28. {
  29. if (customMem.customAlloc) {
  30. /* calloc implemented as malloc+memset;
  31. * not as efficient as calloc, but next best guess for custom malloc */
  32. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  33. ZSTD_memset(ptr, 0, size);
  34. return ptr;
  35. }
  36. return ZSTD_calloc(1, size);
  37. }
  38. MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
  39. {
  40. if (ptr!=NULL) {
  41. if (customMem.customFree)
  42. customMem.customFree(customMem.opaque, ptr);
  43. else
  44. ZSTD_free(ptr);
  45. }
  46. }
  47. #endif /* ZSTD_ALLOCATIONS_H */