1 /*
2 * Copyright (C) 2007-2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jmemsys.h" /* import the system-dependent declarations */
21
22 #include <cutils/ashmem.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
27 extern void * malloc JPP((size_t size));
28 extern void free JPP((void *ptr));
29 #endif
30
31 /*
32 * Memory allocation and freeing are controlled by the regular library
33 * routines malloc() and free().
34 */
35
36 GLOBAL(void *)
jpeg_get_small(j_common_ptr cinfo,size_t sizeofobject)37 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
38 {
39 return (void *) malloc(sizeofobject);
40 }
41
42 GLOBAL(void)
jpeg_free_small(j_common_ptr cinfo,void * object,size_t sizeofobject)43 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
44 {
45 free(object);
46 }
47
48
49 /*
50 * "Large" objects are treated the same as "small" ones.
51 * NB: although we include FAR keywords in the routine declarations,
52 * this file won't actually work in 80x86 small/medium model; at least,
53 * you probably won't be able to process useful-size images in only 64KB.
54 */
55
56 GLOBAL(void FAR *)
jpeg_get_large(j_common_ptr cinfo,size_t sizeofobject)57 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
58 {
59 return (void FAR *) malloc(sizeofobject);
60 }
61
62 GLOBAL(void)
jpeg_free_large(j_common_ptr cinfo,void FAR * object,size_t sizeofobject)63 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
64 {
65 free(object);
66 }
67
68 /*
69 * This routine computes the total memory space available for allocation.
70 * It's impossible to do this in a portable way; our current solution is
71 * to make the user tell us (with a default value set at compile time).
72 * If you can actually get the available space, it's a good idea to subtract
73 * a slop factor of 5% or so.
74 */
75
76 #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
77 #define DEFAULT_MAX_MEM 10000000L /* default: ten megabyte */
78 #endif
79
80 GLOBAL(long)
jpeg_mem_available(j_common_ptr cinfo,long min_bytes_needed,long max_bytes_needed,long already_allocated)81 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
82 long max_bytes_needed, long already_allocated)
83 {
84 return cinfo->mem->max_memory_to_use - already_allocated;
85 }
86
87
88 /*
89 * Backing store (temporary file) management.
90 * Backing store objects are only used when the value returned by
91 * jpeg_mem_available is less than the total space needed. You can dispense
92 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
93 */
94
95 METHODDEF(void)
read_backing_store(j_common_ptr cinfo,backing_store_ptr info,void FAR * buffer_address,long file_offset,long byte_count)96 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
97 void FAR * buffer_address,
98 long file_offset, long byte_count)
99 {
100 memmove(buffer_address, info->addr + file_offset, byte_count);
101 }
102
103
104 METHODDEF(void)
write_backing_store(j_common_ptr cinfo,backing_store_ptr info,void FAR * buffer_address,long file_offset,long byte_count)105 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
106 void FAR * buffer_address,
107 long file_offset, long byte_count)
108 {
109 memmove(info->addr + file_offset, buffer_address, byte_count);
110 }
111
112
113 METHODDEF(void)
close_backing_store(j_common_ptr cinfo,backing_store_ptr info)114 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
115 {
116 munmap(info->addr, info->size);
117 close(info->temp_file);
118 }
119
120 LOCAL(int)
get_ashmem(backing_store_ptr info,long total_bytes_needed)121 get_ashmem(backing_store_ptr info, long total_bytes_needed)
122 {
123 char path[1024];
124 snprintf(path, 1023, "%d.tmp.ashmem", getpid());
125 int fd = ashmem_create_region(path, total_bytes_needed);
126 if (fd == -1) {
127 return -1;
128 }
129 int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
130 if (err) {
131 return -1;
132 }
133 info->addr = mmap(NULL, total_bytes_needed, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
134 info->size = total_bytes_needed;
135 info->temp_file = fd;
136 return fd;
137 }
138
139 /*
140 * Initial opening of a backing-store object.
141 * This version uses ashmem to get a shared memory of total-bytes_needed.
142 */
143
144 GLOBAL(void)
jpeg_open_backing_store(j_common_ptr cinfo,backing_store_ptr info,long total_bytes_needed)145 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
146 long total_bytes_needed)
147 {
148 if (get_ashmem(info, total_bytes_needed) == -1)
149 ERREXITS(cinfo, JERR_TFILE_CREATE, "");
150 info->read_backing_store = read_backing_store;
151 info->write_backing_store = write_backing_store;
152 info->close_backing_store = close_backing_store;
153 }
154
155 /*
156 * These routines take care of any system-dependent initialization and
157 * cleanup required.
158 */
159
160 GLOBAL(long)
jpeg_mem_init(j_common_ptr cinfo)161 jpeg_mem_init (j_common_ptr cinfo)
162 {
163 return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
164 }
165
166 GLOBAL(void)
jpeg_mem_term(j_common_ptr cinfo)167 jpeg_mem_term (j_common_ptr cinfo)
168 {
169 /* no work */
170 }
171