Rev 26 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 26 | pmbaty | 1 | /* alloc.c -- memory allocation | 
| 2 | |||
| 3 |    This file is part of the UCL data compression library. | ||
| 4 | |||
| 5 |    Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer | ||
| 6 |    All Rights Reserved. | ||
| 7 | |||
| 8 |    The UCL library is free software; you can redistribute it and/or | ||
| 9 |    modify it under the terms of the GNU General Public License as | ||
| 10 |    published by the Free Software Foundation; either version 2 of | ||
| 11 |    the License, or (at your option) any later version. | ||
| 12 | |||
| 13 |    The UCL library is distributed in the hope that it will be useful, | ||
| 14 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||
| 16 |    GNU General Public License for more details. | ||
| 17 | |||
| 18 |    You should have received a copy of the GNU General Public License | ||
| 19 |    along with the UCL library; see the file COPYING. | ||
| 20 |    If not, write to the Free Software Foundation, Inc., | ||
| 21 |    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 22 | |||
| 23 |    Markus F.X.J. Oberhumer | ||
| 24 |    <markus@oberhumer.com> | ||
| 25 |    http://www.oberhumer.com/opensource/ucl/ | ||
| 26 |  */ | ||
| 27 | |||
| 28 | |||
| 29 | #include "ucl_conf.h" | ||
| 30 | |||
| 31 | |||
| 32 | /*********************************************************************** | ||
| 33 | // implementation | ||
| 34 | ************************************************************************/ | ||
| 35 | |||
| 36 | #if defined(__UCL_MMODEL_HUGE) | ||
| 37 | |||
| 38 | #define acc_hsize_t             ucl_uint | ||
| 39 | #define acc_hvoid_p             ucl_voidp | ||
| 40 | #define ACCLIB_PUBLIC(r,f)      static r __UCL_CDECL f | ||
| 41 | #define acc_halloc              ucl_malloc_internal | ||
| 42 | #define acc_hfree               ucl_free_internal | ||
| 43 | #include "acc/acclib/halloc.ch" | ||
| 44 | #undef ACCLIB_PUBLIC | ||
| 45 | |||
| 46 | #else | ||
| 47 | |||
| 48 | UCL_PRIVATE(ucl_voidp) | ||
| 49 | ucl_malloc_internal(ucl_uint size) | ||
| 50 | { | ||
| 51 | ucl_voidp p = NULL; | ||
| 38 | pmbaty | 52 | #if 0 // Pierre-Marie Baty -- avoid tautological compare | 
| 26 | pmbaty | 53 | if (size < ~(size_t)0) | 
| 38 | pmbaty | 54 | #endif // 0 | 
| 26 | pmbaty | 55 | p = (ucl_voidp) malloc((size_t) size); | 
| 56 | return p; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | UCL_PRIVATE(void) | ||
| 61 | ucl_free_internal(ucl_voidp p) | ||
| 62 | { | ||
| 63 | if (p) | ||
| 64 | free(p); | ||
| 65 | } | ||
| 66 | |||
| 67 | #endif | ||
| 68 | |||
| 69 | |||
| 70 | /*********************************************************************** | ||
| 71 | // public interface using the global hooks | ||
| 72 | ************************************************************************/ | ||
| 73 | |||
| 74 | /* global allocator hooks */ | ||
| 75 | static ucl_malloc_hook_t ucl_malloc_hook = ucl_malloc_internal; | ||
| 76 | static ucl_free_hook_t ucl_free_hook = ucl_free_internal; | ||
| 77 | |||
| 78 | UCL_PUBLIC(void) | ||
| 79 | ucl_set_malloc_hooks(ucl_malloc_hook_t a, ucl_free_hook_t f) | ||
| 80 | { | ||
| 81 | ucl_malloc_hook = ucl_malloc_internal; | ||
| 82 | ucl_free_hook = ucl_free_internal; | ||
| 83 | if (a) | ||
| 84 | ucl_malloc_hook = a; | ||
| 85 | if (f) | ||
| 86 | ucl_free_hook = f; | ||
| 87 | } | ||
| 88 | |||
| 89 | UCL_PUBLIC(void) | ||
| 90 | ucl_get_malloc_hooks(ucl_malloc_hook_t* a, ucl_free_hook_t* f) | ||
| 91 | { | ||
| 92 | if (a) | ||
| 93 | *a = ucl_malloc_hook; | ||
| 94 | if (f) | ||
| 95 | *f = ucl_free_hook; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | UCL_PUBLIC(ucl_voidp) | ||
| 100 | ucl_malloc(ucl_uint size) | ||
| 101 | { | ||
| 102 | if (size <= 0) | ||
| 103 | return NULL; | ||
| 104 | return ucl_malloc_hook(size); | ||
| 105 | } | ||
| 106 | |||
| 107 | UCL_PUBLIC(ucl_voidp) | ||
| 108 | ucl_alloc(ucl_uint nelems, ucl_uint size) | ||
| 109 | { | ||
| 110 | ucl_uint s = nelems * size; | ||
| 111 | if (nelems <= 0 || s / nelems != size) | ||
| 112 | return NULL; | ||
| 113 | return ucl_malloc(s); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | UCL_PUBLIC(void) | ||
| 118 | ucl_free(ucl_voidp p) | ||
| 119 | { | ||
| 120 | if (p) | ||
| 121 | ucl_free_hook(p); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | /* | ||
| 126 | vi:ts=4:et | ||
| 127 | */ |