OSDN Git Service

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