alloc_ucl.c 3.5 KB

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