jmemansi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if defined(__MACOSX__)
  2. /*
  3. * jmemansi.c
  4. *
  5. * Copyright (C) 1992-1996, Thomas G. Lane.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file provides a simple generic implementation of the system-
  10. * dependent portion of the JPEG memory manager. This implementation
  11. * assumes that you have the ANSI-standard library routine tmpfile().
  12. * Also, the problem of determining the amount of memory available
  13. * is shoved onto the user.
  14. */
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. #include "jmemsys.h" /* import the system-dependent declarations */
  19. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  20. extern void * malloc JPP((size_t size));
  21. extern void free JPP((void *ptr));
  22. #endif
  23. #ifndef SEEK_SET /* pre-ANSI systems may not define this; */
  24. #define SEEK_SET 0 /* if not, assume 0 is correct */
  25. #endif
  26. /*
  27. * Memory allocation and freeing are controlled by the regular library
  28. * routines malloc() and free().
  29. */
  30. GLOBAL(void *)
  31. jpeg_get_small (j_common_ptr cinfo, jpeg_size_t sizeofobject)
  32. {
  33. return (void *) malloc(sizeofobject);
  34. }
  35. GLOBAL(void)
  36. jpeg_free_small (j_common_ptr cinfo, void * object, jpeg_size_t sizeofobject)
  37. {
  38. free(object);
  39. }
  40. /*
  41. * "Large" objects are treated the same as "small" ones.
  42. * NB: although we include FAR keywords in the routine declarations,
  43. * this file won't actually work in 80x86 small/medium model; at least,
  44. * you probably won't be able to process useful-size images in only 64KB.
  45. */
  46. GLOBAL(void FAR *)
  47. jpeg_get_large (j_common_ptr cinfo, jpeg_size_t sizeofobject)
  48. {
  49. return (void FAR *) malloc(sizeofobject);
  50. }
  51. GLOBAL(void)
  52. jpeg_free_large (j_common_ptr cinfo, void FAR * object, jpeg_size_t sizeofobject)
  53. {
  54. free(object);
  55. }
  56. /*
  57. * This routine computes the total memory space available for allocation.
  58. * It's impossible to do this in a portable way; our current solution is
  59. * to make the user tell us (with a default value set at compile time).
  60. * If you can actually get the available space, it's a good idea to subtract
  61. * a slop factor of 5% or so.
  62. */
  63. #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
  64. #define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
  65. #endif
  66. GLOBAL(long)
  67. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  68. long max_bytes_needed, long already_allocated)
  69. {
  70. return cinfo->mem->max_memory_to_use - already_allocated;
  71. }
  72. /*
  73. * Backing store (temporary file) management.
  74. * Backing store objects are only used when the value returned by
  75. * jpeg_mem_available is less than the total space needed. You can dispense
  76. * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  77. */
  78. METHODDEF(void)
  79. read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  80. void FAR * buffer_address,
  81. long file_offset, long byte_count)
  82. {
  83. if (fseek(info->temp_file, file_offset, SEEK_SET))
  84. ERREXIT(cinfo, JERR_TFILE_SEEK);
  85. if (JFREAD(info->temp_file, buffer_address, byte_count)
  86. != (size_t) byte_count)
  87. ERREXIT(cinfo, JERR_TFILE_READ);
  88. }
  89. METHODDEF(void)
  90. write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  91. void FAR * buffer_address,
  92. long file_offset, long byte_count)
  93. {
  94. if (fseek(info->temp_file, file_offset, SEEK_SET))
  95. ERREXIT(cinfo, JERR_TFILE_SEEK);
  96. if (JFWRITE(info->temp_file, buffer_address, byte_count)
  97. != (size_t) byte_count)
  98. ERREXIT(cinfo, JERR_TFILE_WRITE);
  99. }
  100. METHODDEF(void)
  101. close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
  102. {
  103. fclose(info->temp_file);
  104. /* Since this implementation uses tmpfile() to create the file,
  105. * no explicit file deletion is needed.
  106. */
  107. }
  108. /*
  109. * Initial opening of a backing-store object.
  110. *
  111. * This version uses tmpfile(), which constructs a suitable file name
  112. * behind the scenes. We don't have to use info->temp_name[] at all;
  113. * indeed, we can't even find out the actual name of the temp file.
  114. */
  115. GLOBAL(void)
  116. jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  117. long total_bytes_needed)
  118. {
  119. if ((info->temp_file = tmpfile()) == NULL)
  120. ERREXITS(cinfo, JERR_TFILE_CREATE, "");
  121. info->read_backing_store = read_backing_store;
  122. info->write_backing_store = write_backing_store;
  123. info->close_backing_store = close_backing_store;
  124. }
  125. /*
  126. * These routines take care of any system-dependent initialization and
  127. * cleanup required.
  128. */
  129. GLOBAL(long)
  130. jpeg_mem_init (j_common_ptr cinfo)
  131. {
  132. return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  133. }
  134. GLOBAL(void)
  135. jpeg_mem_term (j_common_ptr cinfo)
  136. {
  137. /* no work */
  138. }
  139. #endif