OLD | NEW |
(Empty) | |
| 1 /* exif-mem.c |
| 2 * |
| 3 * Copyright (c) 2003 Lutz Mueller <lutz@users.sourceforge.net> |
| 4 * |
| 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Lesser General Public |
| 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. |
| 9 * |
| 10 * This library is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 * Lesser General Public License for more details. |
| 14 * |
| 15 * You should have received a copy of the GNU Lesser General Public |
| 16 * License along with this library; if not, write to the |
| 17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 * Boston, MA 02110-1301 USA. |
| 19 */ |
| 20 |
| 21 #include <libexif/exif-mem.h> |
| 22 |
| 23 #include <stdlib.h> |
| 24 |
| 25 struct _ExifMem { |
| 26 unsigned int ref_count; |
| 27 ExifMemAllocFunc alloc_func; |
| 28 ExifMemReallocFunc realloc_func; |
| 29 ExifMemFreeFunc free_func; |
| 30 }; |
| 31 |
| 32 /*! Default memory allocation function. */ |
| 33 static void * |
| 34 exif_mem_alloc_func (ExifLong ds) |
| 35 { |
| 36 return calloc ((size_t) ds, 1); |
| 37 } |
| 38 |
| 39 /*! Default memory reallocation function. */ |
| 40 static void * |
| 41 exif_mem_realloc_func (void *d, ExifLong ds) |
| 42 { |
| 43 return realloc (d, (size_t) ds); |
| 44 } |
| 45 |
| 46 /*! Default memory free function. */ |
| 47 static void |
| 48 exif_mem_free_func (void *d) |
| 49 { |
| 50 free (d); |
| 51 } |
| 52 |
| 53 ExifMem * |
| 54 exif_mem_new (ExifMemAllocFunc alloc_func, ExifMemReallocFunc realloc_func, |
| 55 ExifMemFreeFunc free_func) |
| 56 { |
| 57 ExifMem *mem; |
| 58 |
| 59 if (!alloc_func && !realloc_func) |
| 60 return NULL; |
| 61 mem = alloc_func ? alloc_func (sizeof (ExifMem)) : |
| 62 realloc_func (NULL, sizeof (ExifMem)); |
| 63 if (!mem) return NULL; |
| 64 mem->ref_count = 1; |
| 65 |
| 66 mem->alloc_func = alloc_func; |
| 67 mem->realloc_func = realloc_func; |
| 68 mem->free_func = free_func; |
| 69 |
| 70 return mem; |
| 71 } |
| 72 |
| 73 void |
| 74 exif_mem_ref (ExifMem *mem) |
| 75 { |
| 76 if (!mem) return; |
| 77 mem->ref_count++; |
| 78 } |
| 79 |
| 80 void |
| 81 exif_mem_unref (ExifMem *mem) |
| 82 { |
| 83 if (!mem) return; |
| 84 if (!--mem->ref_count) |
| 85 exif_mem_free (mem, mem); |
| 86 } |
| 87 |
| 88 void |
| 89 exif_mem_free (ExifMem *mem, void *d) |
| 90 { |
| 91 if (!mem) return; |
| 92 if (mem->free_func) { |
| 93 mem->free_func (d); |
| 94 return; |
| 95 } |
| 96 } |
| 97 |
| 98 void * |
| 99 exif_mem_alloc (ExifMem *mem, ExifLong ds) |
| 100 { |
| 101 if (!mem) return NULL; |
| 102 if (mem->alloc_func || mem->realloc_func) |
| 103 return mem->alloc_func ? mem->alloc_func (ds) : |
| 104 mem->realloc_func (NULL, ds); |
| 105 return NULL; |
| 106 } |
| 107 |
| 108 void * |
| 109 exif_mem_realloc (ExifMem *mem, void *d, ExifLong ds) |
| 110 { |
| 111 return (mem && mem->realloc_func) ? mem->realloc_func (d, ds) : NULL; |
| 112 } |
| 113 |
| 114 ExifMem * |
| 115 exif_mem_new_default (void) |
| 116 { |
| 117 return exif_mem_new (exif_mem_alloc_func, exif_mem_realloc_func, |
| 118 exif_mem_free_func); |
| 119 } |
OLD | NEW |