Go to most recent revision | Details | 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; |
||
| 52 | if (size < ~(size_t)0) |
||
| 53 | p = (ucl_voidp) malloc((size_t) size); |
||
| 54 | return p; |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | UCL_PRIVATE(void) |
||
| 59 | ucl_free_internal(ucl_voidp p) |
||
| 60 | { |
||
| 61 | if (p) |
||
| 62 | free(p); |
||
| 63 | } |
||
| 64 | |||
| 65 | #endif |
||
| 66 | |||
| 67 | |||
| 68 | /*********************************************************************** |
||
| 69 | // public interface using the global hooks |
||
| 70 | ************************************************************************/ |
||
| 71 | |||
| 72 | /* global allocator hooks */ |
||
| 73 | static ucl_malloc_hook_t ucl_malloc_hook = ucl_malloc_internal; |
||
| 74 | static ucl_free_hook_t ucl_free_hook = ucl_free_internal; |
||
| 75 | |||
| 76 | UCL_PUBLIC(void) |
||
| 77 | ucl_set_malloc_hooks(ucl_malloc_hook_t a, ucl_free_hook_t f) |
||
| 78 | { |
||
| 79 | ucl_malloc_hook = ucl_malloc_internal; |
||
| 80 | ucl_free_hook = ucl_free_internal; |
||
| 81 | if (a) |
||
| 82 | ucl_malloc_hook = a; |
||
| 83 | if (f) |
||
| 84 | ucl_free_hook = f; |
||
| 85 | } |
||
| 86 | |||
| 87 | UCL_PUBLIC(void) |
||
| 88 | ucl_get_malloc_hooks(ucl_malloc_hook_t* a, ucl_free_hook_t* f) |
||
| 89 | { |
||
| 90 | if (a) |
||
| 91 | *a = ucl_malloc_hook; |
||
| 92 | if (f) |
||
| 93 | *f = ucl_free_hook; |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | UCL_PUBLIC(ucl_voidp) |
||
| 98 | ucl_malloc(ucl_uint size) |
||
| 99 | { |
||
| 100 | if (size <= 0) |
||
| 101 | return NULL; |
||
| 102 | return ucl_malloc_hook(size); |
||
| 103 | } |
||
| 104 | |||
| 105 | UCL_PUBLIC(ucl_voidp) |
||
| 106 | ucl_alloc(ucl_uint nelems, ucl_uint size) |
||
| 107 | { |
||
| 108 | ucl_uint s = nelems * size; |
||
| 109 | if (nelems <= 0 || s / nelems != size) |
||
| 110 | return NULL; |
||
| 111 | return ucl_malloc(s); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | UCL_PUBLIC(void) |
||
| 116 | ucl_free(ucl_voidp p) |
||
| 117 | { |
||
| 118 | if (p) |
||
| 119 | ucl_free_hook(p); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /* |
||
| 124 | vi:ts=4:et |
||
| 125 | */ |