jmemansi.c 4.5 KB

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