1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/* alloc.c -- memory allocation
This file is part of the UCL data compression library.
Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
All Rights Reserved.
The UCL library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
The UCL library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the UCL library; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
http://www.oberhumer.com/opensource/ucl/
*/
#include "ucl_conf.h"
#if defined(HAVE_MALLOC_H)
# include <malloc.h>
#endif
#if defined(__palmos__)
# include <System/MemoryMgr.h>
#endif
#undef ucl_alloc_hook
#undef ucl_free_hook
#undef ucl_alloc
#undef ucl_malloc
#undef ucl_free
/***********************************************************************
// implementation
************************************************************************/
UCL_PRIVATE(ucl_voidp)
ucl_alloc_internal(ucl_uint nelems, ucl_uint size)
{
ucl_voidp p = NULL;
unsigned long s = (unsigned long) nelems * size;
if (nelems <= 0 || size <= 0 || s < nelems || s < size)
return NULL;
#if defined(__palmos__)
p = (ucl_voidp) MemPtrNew(s);
#elif (UCL_UINT_MAX <= SIZE_T_MAX)
if (s < SIZE_T_MAX)
p = (ucl_voidp) malloc((size_t)s);
#elif defined(HAVE_HALLOC) && defined(__DMC__)
if (size < SIZE_T_MAX)
p = (ucl_voidp) _halloc(nelems,(size_t)size);
#elif defined(HAVE_HALLOC)
if (size < SIZE_T_MAX)
p = (ucl_voidp) halloc(nelems,(size_t)size);
#else
if (s < SIZE_T_MAX)
p = (ucl_voidp) malloc((size_t)s);
#endif
return p;
}
UCL_PRIVATE(void)
ucl_free_internal(ucl_voidp p)
{
if (!p)
return;
#if defined(__palmos__)
MemPtrFree(p);
#elif (UCL_UINT_MAX <= SIZE_T_MAX)
free(p);
#elif defined(HAVE_HALLOC) && defined(__DMC__)
_hfree(p);
#elif defined(HAVE_HALLOC)
hfree(p);
#else
free(p);
#endif
}
/***********************************************************************
// public interface using the global hooks
************************************************************************/
/* global allocator hooks */
ucl_alloc_hook_t ucl_alloc_hook = ucl_alloc_internal;
ucl_free_hook_t ucl_free_hook = ucl_free_internal;
UCL_PUBLIC(ucl_voidp)
ucl_alloc(ucl_uint nelems, ucl_uint size)
{
if (!ucl_alloc_hook)
return NULL;
return ucl_alloc_hook(nelems,size);
}
UCL_PUBLIC(ucl_voidp)
ucl_malloc(ucl_uint size)
{
if (!ucl_alloc_hook)
return NULL;
#if defined(__palmos__)
return ucl_alloc_hook(size,1);
#elif (UCL_UINT_MAX <= SIZE_T_MAX)
return ucl_alloc_hook(size,1);
#elif defined(HAVE_HALLOC)
/* use segment granularity by default */
if (size + 15 > size) /* avoid overflow */
return ucl_alloc_hook((size+15)/16,16);
return ucl_alloc_hook(size,1);
#else
return ucl_alloc_hook(size,1);
#endif
}
UCL_PUBLIC(void)
ucl_free(ucl_voidp p)
{
if (!ucl_free_hook)
return;
ucl_free_hook(p);
}
/*
vi:ts=4:et
*/
|