jmemcust.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2001-2017 Artifex Software, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This software is provided AS-IS with no warranty, either express or
  6. * implied.
  7. *
  8. * This software is distributed under license and may not be copied,
  9. * modified or distributed except as expressly authorized under the terms
  10. * of the license contained in the file LICENSE in this distribution.
  11. *
  12. * Refer to licensing information at http://www.artifex.com or contact
  13. * Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
  14. * CA 94903, U.S.A., +1(415)492-9861, for further information.
  15. */
  16. #if !defined(SHARE_JPEG) || SHARE_JPEG==0
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jmemsys.h"
  20. #include "jerror.h"
  21. #include "jmemcust.h"
  22. GLOBAL(void *)
  23. jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
  24. {
  25. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  26. return (void *) (cmem->j_mem_get_small)(cinfo, sizeofobject);
  27. }
  28. GLOBAL(void)
  29. jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
  30. {
  31. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  32. (cmem->j_mem_free_small)(cinfo, object, sizeofobject);
  33. }
  34. /*
  35. * "Large" objects are treated the same as "small" ones.
  36. * NB: although we include FAR keywords in the routine declarations,
  37. * this file won't actually work in 80x86 small/medium model; at least,
  38. * you probably won't be able to process useful-size images in only 64KB.
  39. */
  40. GLOBAL(void FAR *)
  41. jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
  42. {
  43. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  44. return (void *) (cmem->j_mem_get_large)(cinfo, sizeofobject);
  45. }
  46. GLOBAL(void)
  47. jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  48. {
  49. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  50. (cmem->j_mem_free_large)(cinfo, object, sizeofobject);
  51. }
  52. /*
  53. * This routine computes the total memory space available for allocation.
  54. * Here we always say, "we got all you want bud!"
  55. */
  56. GLOBAL(long)
  57. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  58. long max_bytes_needed, long already_allocated)
  59. {
  60. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  61. long ret = max_bytes_needed;
  62. if (cmem->j_mem_avail)
  63. ret = (cmem->j_mem_avail)(cinfo);
  64. return ret;
  65. }
  66. /*
  67. * Backing store (temporary file) management.
  68. * Since jpeg_mem_available always promised the moon,
  69. * this should never be called and we can just error out.
  70. */
  71. GLOBAL(void)
  72. jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  73. long total_bytes_needed)
  74. {
  75. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  76. if (cmem->j_mem_open_backing_store) {
  77. (cmem->j_mem_open_backing_store)(cinfo, info, total_bytes_needed);
  78. }
  79. else
  80. ERREXIT(cinfo, JERR_NO_BACKING_STORE);
  81. }
  82. /*
  83. * These routines take care of any system-dependent initialization and
  84. * cleanup required. Here, there isn't any.
  85. */
  86. GLOBAL(long)
  87. jpeg_mem_init (j_common_ptr cinfo)
  88. {
  89. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  90. long ret = 0;
  91. if (cmem->j_mem_init)
  92. ret = (cmem->j_mem_init)(cinfo);
  93. return ret;
  94. }
  95. GLOBAL(void)
  96. jpeg_mem_term (j_common_ptr cinfo)
  97. {
  98. jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
  99. if (cmem->j_mem_term)
  100. (cmem->j_mem_term)(cinfo);
  101. }
  102. GLOBAL(jpeg_cust_mem_data *)
  103. jpeg_cust_mem_init(jpeg_cust_mem_data *custm, void *priv,
  104. j_custmem_init_ptr init,
  105. j_custmem_term_ptr term,
  106. j_custmem_avail_ptr avail,
  107. j_custmem_get_small_ptr get_small,
  108. j_custmem_free_small_ptr free_small,
  109. j_cust_mem_get_large_ptr get_large,
  110. j_custmem_free_large_ptr free_large,
  111. j_custmem_open_backing_store_ptr open_backing_store)
  112. {
  113. jpeg_cust_mem_data *lcustm = NULL;
  114. /* We need at least the following for a viable memory manager */
  115. if (get_small && free_small && get_large && free_large)
  116. {
  117. lcustm = custm;
  118. lcustm->priv = priv;
  119. lcustm->j_mem_init = init;
  120. lcustm->j_mem_term = term;
  121. lcustm->j_mem_avail = avail;
  122. lcustm->j_mem_get_small = get_small;
  123. lcustm->j_mem_free_small = free_small;
  124. lcustm->j_mem_get_large = get_large;
  125. lcustm->j_mem_free_large = free_large;
  126. lcustm->j_mem_open_backing_store = open_backing_store;
  127. }
  128. return lcustm;
  129. }
  130. #endif /* !defined(SHARE_JPEG) || SHARE_JPEG==0 */