memory_manager.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (c) the JPEG XL Project Authors. All rights reserved.
  2. *
  3. * Use of this source code is governed by a BSD-style
  4. * license that can be found in the LICENSE file.
  5. */
  6. /** @addtogroup libjxl_common
  7. * @{
  8. * @file memory_manager.h
  9. * @brief Abstraction functions used by JPEG XL to allocate memory.
  10. */
  11. #ifndef JXL_MEMORY_MANAGER_H_
  12. #define JXL_MEMORY_MANAGER_H_
  13. #include <stddef.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * Allocating function for a memory region of a given size.
  19. *
  20. * Allocates a contiguous memory region of size @p size bytes. The returned
  21. * memory may not be aligned to a specific size or initialized at all.
  22. *
  23. * @param opaque custom memory manager handle provided by the caller.
  24. * @param size in bytes of the requested memory region.
  25. * @return @c NULL if the memory can not be allocated,
  26. * @return pointer to the memory otherwise.
  27. */
  28. typedef void* (*jpegxl_alloc_func)(void* opaque, size_t size);
  29. /**
  30. * Deallocating function pointer type.
  31. *
  32. * This function @b MUST do nothing if @p address is @c NULL.
  33. *
  34. * @param opaque custom memory manager handle provided by the caller.
  35. * @param address memory region pointer returned by ::jpegxl_alloc_func, or @c
  36. * NULL.
  37. */
  38. typedef void (*jpegxl_free_func)(void* opaque, void* address);
  39. /**
  40. * Memory Manager struct.
  41. * These functions, when provided by the caller, will be used to handle memory
  42. * allocations.
  43. */
  44. typedef struct JxlMemoryManagerStruct {
  45. /** The opaque pointer that will be passed as the first parameter to all the
  46. * functions in this struct. */
  47. void* opaque;
  48. /** Memory allocation function. This can be NULL if and only if also the
  49. * free() member in this class is NULL. All dynamic memory will be allocated
  50. * and freed with these functions if they are not NULL, otherwise with the
  51. * standard malloc/free. */
  52. jpegxl_alloc_func alloc;
  53. /** Free function matching the alloc() member. */
  54. jpegxl_free_func free;
  55. /* TODO(deymo): Add cache-aligned alloc/free functions here. */
  56. } JxlMemoryManager;
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. #endif /* JXL_MEMORY_MANAGER_H_ */
  61. /** @}*/