Home | History | Annotate | Download | only in jpeg
      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 *)
     37 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
     38 {
     39   return (void *) malloc(sizeofobject);
     40 }
     41 
     42 GLOBAL(void)
     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 *)
     57 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
     58 {
     59   return (void FAR *) malloc(sizeofobject);
     60 }
     61 
     62 GLOBAL(void)
     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)
     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)
     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)
    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)
    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)
    121 get_ashmem(backing_store_ptr info, long total_bytes_needed)
    122 {
    123   char path[1024];
    124   // FIXME: Does this name need to be unique? What happens if two jpegs
    125   // are being decoded simultaneously in the same process?
    126   snprintf(path, 1023, "%d.tmp.ashmem", getpid());
    127   int fd = ashmem_create_region(path, total_bytes_needed);
    128   if (fd == -1) {
    129       return -1;
    130   }
    131   int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
    132   if (err) {
    133     close(fd);
    134     return -1;
    135   }
    136   void* addr = mmap(NULL, total_bytes_needed, PROT_READ | PROT_WRITE,
    137                     MAP_PRIVATE, fd, 0);
    138   if (-1 == (long) addr) {
    139     close(fd);
    140     return -1;
    141   }
    142   info->addr = addr;
    143   info->size = total_bytes_needed;
    144   info->temp_file = fd;
    145   return fd;
    146 }
    147 
    148 /*
    149  * Initial opening of a backing-store object.
    150  * This version uses ashmem to get a shared memory of total-bytes_needed.
    151  */
    152 
    153 GLOBAL(void)
    154 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
    155 			 long total_bytes_needed)
    156 {
    157   if (get_ashmem(info, total_bytes_needed) == -1)
    158     ERREXITS(cinfo, JERR_TFILE_CREATE, "");
    159   info->read_backing_store = read_backing_store;
    160   info->write_backing_store = write_backing_store;
    161   info->close_backing_store = close_backing_store;
    162 }
    163 
    164 /*
    165  * These routines take care of any system-dependent initialization and
    166  * cleanup required.
    167  */
    168 
    169 GLOBAL(long)
    170 jpeg_mem_init (j_common_ptr cinfo)
    171 {
    172   return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
    173 }
    174 
    175 GLOBAL(void)
    176 jpeg_mem_term (j_common_ptr cinfo)
    177 {
    178   /* no work */
    179 }
    180