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