jmemnobs.pas 9.5 KB

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