Make the mips compiler not complain when bitwise operations do not have parenthesis.
[kugel-rb.git] / tools / ucl / src / alloc.c
blobd32ea5113a2f3e13f684204d34a942185f655f57
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
6 All Rights Reserved.
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/
29 #include "ucl_conf.h"
31 #if defined(HAVE_MALLOC_H)
32 # include <malloc.h>
33 #endif
34 #if defined(__palmos__)
35 # include <System/MemoryMgr.h>
36 #endif
39 #undef ucl_alloc_hook
40 #undef ucl_free_hook
41 #undef ucl_alloc
42 #undef ucl_malloc
43 #undef ucl_free
46 /***********************************************************************
47 // implementation
48 ************************************************************************/
50 UCL_PRIVATE(ucl_voidp)
51 ucl_alloc_internal(ucl_uint nelems, ucl_uint size)
53 ucl_voidp p = NULL;
54 unsigned long s = (unsigned long) nelems * size;
56 if (nelems <= 0 || size <= 0 || s < nelems || s < size)
57 return NULL;
59 #if defined(__palmos__)
60 p = (ucl_voidp) MemPtrNew(s);
61 #elif (UCL_UINT_MAX <= SIZE_T_MAX)
62 if (s < 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);
70 #else
71 if (s < SIZE_T_MAX)
72 p = (ucl_voidp) malloc((size_t)s);
73 #endif
75 return p;
79 UCL_PRIVATE(void)
80 ucl_free_internal(ucl_voidp p)
82 if (!p)
83 return;
85 #if defined(__palmos__)
86 MemPtrFree(p);
87 #elif (UCL_UINT_MAX <= SIZE_T_MAX)
88 free(p);
89 #elif defined(HAVE_HALLOC) && defined(__DMC__)
90 _hfree(p);
91 #elif defined(HAVE_HALLOC)
92 hfree(p);
93 #else
94 free(p);
95 #endif
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)
111 if (!ucl_alloc_hook)
112 return NULL;
114 return ucl_alloc_hook(nelems,size);
118 UCL_PUBLIC(ucl_voidp)
119 ucl_malloc(ucl_uint size)
121 if (!ucl_alloc_hook)
122 return NULL;
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);
133 #else
134 return ucl_alloc_hook(size,1);
135 #endif
139 UCL_PUBLIC(void)
140 ucl_free(ucl_voidp p)
142 if (!ucl_free_hook)
143 return;
145 ucl_free_hook(p);
150 vi:ts=4:et