jmemnobs.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Unit jmemnobs;
  2. { Delphi3 -- > jmemnobs from jmemwin }
  3. { This file provides an Win32-compatible implementation of the system-
  4. dependent portion of the JPEG memory manager. }
  5. { Check jmemnobs.c }
  6. { Copyright (C) 1996, Jacques Nomssi Nzali }
  7. interface
  8. {$I jconfig.inc}
  9. uses
  10. jmorecfg,
  11. jdeferr,
  12. jerror,
  13. jpeglib;
  14. { The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
  15. be requested in a single call to jpeg_get_large (and jpeg_get_small for that
  16. matter, but that case should never come into play). This macro is needed
  17. to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
  18. On those machines, we expect that jconfig.h will provide a proper value.
  19. On machines with 32-bit flat address spaces, any large constant may be used.
  20. NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
  21. size_t and will be a multiple of sizeof(align_type). }
  22. {$IFDEF WINDOWS}
  23. const
  24. MAX_ALLOC_CHUNK = long(32752);
  25. {$ELSE}
  26. const
  27. MAX_ALLOC_CHUNK = long(1000000000);
  28. {$ENDIF}
  29. {GLOBAL}
  30. procedure jpeg_open_backing_store (cinfo : j_common_ptr;
  31. info : backing_store_ptr;
  32. total_bytes_needed : long);
  33. { These routines take care of any system-dependent initialization and
  34. cleanup required. }
  35. {GLOBAL}
  36. function jpeg_mem_init (cinfo : j_common_ptr) : long;
  37. {GLOBAL}
  38. procedure jpeg_mem_term (cinfo : j_common_ptr);
  39. { These two functions are used to allocate and release small chunks of
  40. memory. (Typically the total amount requested through jpeg_get_small is
  41. no more than 20K or so; this will be requested in chunks of a few K each.)
  42. Behavior should be the same as for the standard library functions malloc
  43. and free; in particular, jpeg_get_small must return NIL on failure.
  44. On most systems, these ARE malloc and free. jpeg_free_small is passed the
  45. size of the object being freed, just in case it's needed.
  46. On an 80x86 machine using small-data memory model, these manage near heap. }
  47. { Near-memory allocation and freeing are controlled by the regular library
  48. routines malloc() and free(). }
  49. {GLOBAL}
  50. function jpeg_get_small (cinfo : j_common_ptr;
  51. sizeofobject : size_t) : pointer;
  52. {GLOBAL}
  53. {object is a reserved word in Borland Pascal }
  54. procedure jpeg_free_small (cinfo : j_common_ptr;
  55. an_object : pointer;
  56. sizeofobject : size_t);
  57. { These two functions are used to allocate and release large chunks of
  58. memory (up to the total free space designated by jpeg_mem_available).
  59. The interface is the same as above, except that on an 80x86 machine,
  60. far pointers are used. On most other machines these are identical to
  61. the jpeg_get/free_small routines; but we keep them separate anyway,
  62. in case a different allocation strategy is desirable for large chunks. }
  63. { "Large" objects are allocated in far memory, if possible }
  64. {GLOBAL}
  65. function jpeg_get_large (cinfo : j_common_ptr;
  66. sizeofobject : size_t) : voidp; {far}
  67. {GLOBAL}
  68. procedure jpeg_free_large (cinfo : j_common_ptr;
  69. {var?} an_object : voidp; {FAR}
  70. sizeofobject : size_t);
  71. { This routine computes the total memory space available for allocation.
  72. It's impossible to do this in a portable way; our current solution is
  73. to make the user tell us (with a default value set at compile time).
  74. If you can actually get the available space, it's a good idea to subtract
  75. a slop factor of 5% or so. }
  76. {GLOBAL}
  77. function jpeg_mem_available (cinfo : j_common_ptr;
  78. min_bytes_needed : long;
  79. max_bytes_needed : long;
  80. already_allocated : long) : long;
  81. implementation
  82. { This structure holds whatever state is needed to access a single
  83. backing-store object. The read/write/close method pointers are called
  84. by jmemmgr.c to manipulate the backing-store object; all other fields
  85. are private to the system-dependent backing store routines. }
  86. { These two functions are used to allocate and release small chunks of
  87. memory. (Typically the total amount requested through jpeg_get_small is
  88. no more than 20K or so; this will be requested in chunks of a few K each.)
  89. Behavior should be the same as for the standard library functions malloc
  90. and free; in particular, jpeg_get_small must return NIL on failure.
  91. On most systems, these ARE malloc and free. jpeg_free_small is passed the
  92. size of the object being freed, just in case it's needed.
  93. On an 80x86 machine using small-data memory model, these manage near heap. }
  94. { Near-memory allocation and freeing are controlled by the regular library
  95. routines malloc() and free(). }
  96. {GLOBAL}
  97. function jpeg_get_small (cinfo : j_common_ptr;
  98. sizeofobject : size_t) : pointer;
  99. var
  100. p : pointer;
  101. begin
  102. GetMem(p, sizeofobject);
  103. jpeg_get_small := p;
  104. end;
  105. {GLOBAL}
  106. {object is a reserved word in Object Pascal }
  107. procedure jpeg_free_small (cinfo : j_common_ptr;
  108. an_object : pointer;
  109. sizeofobject : size_t);
  110. begin
  111. FreeMem(an_object, sizeofobject);
  112. end;
  113. { These two functions are used to allocate and release large chunks of
  114. memory (up to the total free space designated by jpeg_mem_available).
  115. The interface is the same as above, except that on an 80x86 machine,
  116. far pointers are used. On most other machines these are identical to
  117. the jpeg_get/free_small routines; but we keep them separate anyway,
  118. in case a different allocation strategy is desirable for large chunks. }
  119. {GLOBAL}
  120. function jpeg_get_large (cinfo : j_common_ptr;
  121. sizeofobject : size_t) : voidp; {far}
  122. var
  123. p : pointer;
  124. begin
  125. GetMem(p, sizeofobject);
  126. jpeg_get_large := p;
  127. end;
  128. {GLOBAL}
  129. procedure jpeg_free_large (cinfo : j_common_ptr;
  130. {var?} an_object : voidp; {FAR}
  131. sizeofobject : size_t);
  132. begin
  133. Freemem(an_object, sizeofobject);
  134. end;
  135. { This routine computes the total space still available for allocation by
  136. jpeg_get_large. If more space than this is needed, backing store will be
  137. used. NOTE: any memory already allocated must not be counted.
  138. There is a minimum space requirement, corresponding to the minimum
  139. feasible buffer sizes; jmemmgr.c will request that much space even if
  140. jpeg_mem_available returns zero. The maximum space needed, enough to hold
  141. all working storage in memory, is also passed in case it is useful.
  142. Finally, the total space already allocated is passed. If no better
  143. method is available, cinfo^.mem^.max_memory_to_use - already_allocated
  144. is often a suitable calculation.
  145. It is OK for jpeg_mem_available to underestimate the space available
  146. (that'll just lead to more backing-store access than is really necessary).
  147. However, an overestimate will lead to failure. Hence it's wise to subtract
  148. a slop factor from the true available space. 5% should be enough.
  149. On machines with lots of virtual memory, any large constant may be returned.
  150. Conversely, zero may be returned to always use the minimum amount of memory.}
  151. { This routine computes the total memory space available for allocation.
  152. It's impossible to do this in a portable way; our current solution is
  153. to make the user tell us (with a default value set at compile time).
  154. If you can actually get the available space, it's a good idea to subtract
  155. a slop factor of 5% or so. }
  156. const
  157. DEFAULT_MAX_MEM = long(300000); { for total usage about 450K }
  158. {GLOBAL}
  159. function jpeg_mem_available (cinfo : j_common_ptr;
  160. min_bytes_needed : long;
  161. max_bytes_needed : long;
  162. already_allocated : long) : long;
  163. begin
  164. {jpeg_mem_available := cinfo^.mem^.max_memory_to_use - already_allocated;}
  165. jpeg_mem_available := max_bytes_needed;
  166. end;
  167. { Initial opening of a backing-store object. This must fill in the
  168. read/write/close pointers in the object. The read/write routines
  169. may take an error exit if the specified maximum file size is exceeded.
  170. (If jpeg_mem_available always returns a large value, this routine can
  171. just take an error exit.) }
  172. { Initial opening of a backing-store object. }
  173. {GLOBAL}
  174. procedure jpeg_open_backing_store (cinfo : j_common_ptr;
  175. info : backing_store_ptr;
  176. total_bytes_needed : long);
  177. begin
  178. ERREXIT(cinfo, JERR_NO_BACKING_STORE);
  179. end;
  180. { These routines take care of any system-dependent initialization and
  181. cleanup required. jpeg_mem_init will be called before anything is
  182. allocated (and, therefore, nothing in cinfo is of use except the error
  183. manager pointer). It should return a suitable default value for
  184. max_memory_to_use; this may subsequently be overridden by the surrounding
  185. application. (Note that max_memory_to_use is only important if
  186. jpeg_mem_available chooses to consult it ... no one else will.)
  187. jpeg_mem_term may assume that all requested memory has been freed and that
  188. all opened backing-store objects have been closed. }
  189. { These routines take care of any system-dependent initialization and
  190. cleanup required. }
  191. {GLOBAL}
  192. function jpeg_mem_init (cinfo : j_common_ptr) : long;
  193. begin
  194. jpeg_mem_init := DEFAULT_MAX_MEM; { default for max_memory_to_use }
  195. end;
  196. {GLOBAL}
  197. procedure jpeg_mem_term (cinfo : j_common_ptr);
  198. begin
  199. end;
  200. end.