1 /* alloc.c -- memory allocation
3 This file is part of the UCL data compression library.
5 Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
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.
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.
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.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/ucl/
31 #if defined(HAVE_MALLOC_H)
34 #if defined(__palmos__)
35 # include <System/MemoryMgr.h>
46 /***********************************************************************
48 ************************************************************************/
50 UCL_PRIVATE(ucl_voidp
)
51 ucl_alloc_internal(ucl_uint nelems
, ucl_uint size
)
54 unsigned long s
= (unsigned long) nelems
* size
;
56 if (nelems
<= 0 || size
<= 0 || s
< nelems
|| s
< size
)
59 #if defined(__palmos__)
60 p
= (ucl_voidp
) MemPtrNew(s
);
61 #elif (UCL_UINT_MAX <= SIZE_T_MAX)
63 p
= (ucl_voidp
) malloc((size_t)s
);
64 #elif defined(HAVE_HALLOC) && defined(__DMC__)
65 if (size
< SIZE_T_MAX
)
66 p
= (ucl_voidp
) _halloc(nelems
,(size_t)size
);
67 #elif defined(HAVE_HALLOC)
68 if (size
< SIZE_T_MAX
)
69 p
= (ucl_voidp
) halloc(nelems
,(size_t)size
);
72 p
= (ucl_voidp
) malloc((size_t)s
);
80 ucl_free_internal(ucl_voidp p
)
85 #if defined(__palmos__)
87 #elif (UCL_UINT_MAX <= SIZE_T_MAX)
89 #elif defined(HAVE_HALLOC) && defined(__DMC__)
91 #elif defined(HAVE_HALLOC)
99 /***********************************************************************
100 // public interface using the global hooks
101 ************************************************************************/
103 /* global allocator hooks */
104 ucl_alloc_hook_t ucl_alloc_hook
= ucl_alloc_internal
;
105 ucl_free_hook_t ucl_free_hook
= ucl_free_internal
;
108 UCL_PUBLIC(ucl_voidp
)
109 ucl_alloc(ucl_uint nelems
, ucl_uint size
)
114 return ucl_alloc_hook(nelems
,size
);
118 UCL_PUBLIC(ucl_voidp
)
119 ucl_malloc(ucl_uint size
)
124 #if defined(__palmos__)
125 return ucl_alloc_hook(size
,1);
126 #elif (UCL_UINT_MAX <= SIZE_T_MAX)
127 return ucl_alloc_hook(size
,1);
128 #elif defined(HAVE_HALLOC)
129 /* use segment granularity by default */
130 if (size
+ 15 > size
) /* avoid overflow */
131 return ucl_alloc_hook((size
+15)/16,16);
132 return ucl_alloc_hook(size
,1);
134 return ucl_alloc_hook(size
,1);
140 ucl_free(ucl_voidp p
)