1/*
2 * Copyright (C) 2001-2017 Artifex Software, Inc.
3 * All Rights Reserved.
4 *
5 * This software is provided AS-IS with no warranty, either express or
6 * implied.
7 *
8 * This software is distributed under license and may not be copied,
9 * modified or distributed except as expressly authorized under the terms
10 * of the license contained in the file LICENSE in this distribution.
11 *
12 * Refer to licensing information at http://www.artifex.com or contact
13 * Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
14 * CA 94903, U.S.A., +1(415)492-9861, for further information.
15 */
16
17#if !defined(SHARE_JPEG) || SHARE_JPEG==0
18
19#include "jinclude.h"
20#include "jpeglib.h"
21#include "jmemsys.h"
22#include "jerror.h"
23#include "jmemcust.h"
24
25GLOBAL(void *)
26jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
27{
28 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
29
30 return (void *) (cmem->j_mem_get_small)(cinfo, sizeofobject);
31}
32
33GLOBAL(void)
34jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
35{
36 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
37
38 (cmem->j_mem_free_small)(cinfo, object, sizeofobject);
39}
40
41/*
42 * "Large" objects are treated the same as "small" ones.
43 * NB: although we include FAR keywords in the routine declarations,
44 * this file won't actually work in 80x86 small/medium model; at least,
45 * you probably won't be able to process useful-size images in only 64KB.
46 */
47
48GLOBAL(void FAR *)
49jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
50{
51 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
52
53 return (void *) (cmem->j_mem_get_large)(cinfo, sizeofobject);
54}
55
56GLOBAL(void)
57jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
58{
59 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
60
61 (cmem->j_mem_free_large)(cinfo, object, sizeofobject);
62}
63
64/*
65 * This routine computes the total memory space available for allocation.
66 * Here we always say, "we got all you want bud!"
67 */
68
69GLOBAL(long)
70jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
71 long max_bytes_needed, long already_allocated)
72{
73 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
74 long ret = max_bytes_needed;
75
76 if (cmem->j_mem_avail)
77 ret = (cmem->j_mem_avail)(cinfo);
78
79 return ret;
80}
81
82/*
83 * Backing store (temporary file) management.
84 * Since jpeg_mem_available always promised the moon,
85 * this should never be called and we can just error out.
86 */
87
88GLOBAL(void)
89jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
90 long total_bytes_needed)
91{
92 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
93
94 if (cmem->j_mem_open_backing_store) {
95 (cmem->j_mem_open_backing_store)(cinfo, info, total_bytes_needed);
96 }
97 else
98 ERREXIT(cinfo, JERR_NO_BACKING_STORE);
99}
100
101/*
102 * These routines take care of any system-dependent initialization and
103 * cleanup required. Here, there isn't any.
104 */
105
106GLOBAL(long)
107jpeg_mem_init (j_common_ptr cinfo)
108{
109 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
110 long ret = 0;
111
112 if (cmem->j_mem_init)
113 ret = (cmem->j_mem_init)(cinfo);
114
115 return ret;
116}
117
118GLOBAL(void)
119jpeg_mem_term (j_common_ptr cinfo)
120{
121 jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
122
123 if (cmem->j_mem_term)
124 (cmem->j_mem_term)(cinfo);
125}
126
127GLOBAL(jpeg_cust_mem_data *)
128jpeg_cust_mem_init(jpeg_cust_mem_data *custm, void *priv,
129 j_custmem_init_ptr init,
130 j_custmem_term_ptr term,
131 j_custmem_avail_ptr avail,
132 j_custmem_get_small_ptr get_small,
133 j_custmem_free_small_ptr free_small,
134 j_cust_mem_get_large_ptr get_large,
135 j_custmem_free_large_ptr free_large,
136 j_custmem_open_backing_store_ptr open_backing_store)
137{
138 jpeg_cust_mem_data *lcustm = NULL;
139
140 /* We need at least the following for a viable memory manager */
141 if (get_small && free_small && get_large && free_large)
142 {
143 lcustm = custm;
144
145 lcustm->priv = priv;
146 lcustm->j_mem_init = init;
147 lcustm->j_mem_term = term;
148 lcustm->j_mem_avail = avail;
149 lcustm->j_mem_get_small = get_small;
150 lcustm->j_mem_free_small = free_small;
151 lcustm->j_mem_get_large = get_large;
152 lcustm->j_mem_free_large = free_large;
153 lcustm->j_mem_open_backing_store = open_backing_store;
154 }
155 return lcustm;
156}
157
158#endif /* !defined(SHARE_JPEG) || SHARE_JPEG==0 */
159