A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 151 lines 3.6 kB view raw
1/* alloc.c -- memory allocation 2 3 This file is part of the UCL data compression library. 4 5 Copyright (C) 1996-2002 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#if defined(HAVE_MALLOC_H) 32# include <malloc.h> 33#endif 34#if defined(__palmos__) 35# include <System/MemoryMgr.h> 36#endif 37 38 39#undef ucl_alloc_hook 40#undef ucl_free_hook 41#undef ucl_alloc 42#undef ucl_malloc 43#undef ucl_free 44 45 46/*********************************************************************** 47// implementation 48************************************************************************/ 49 50UCL_PRIVATE(ucl_voidp) 51ucl_alloc_internal(ucl_uint nelems, ucl_uint size) 52{ 53 ucl_voidp p = NULL; 54 unsigned long s = (unsigned long) nelems * size; 55 56 if (nelems <= 0 || size <= 0 || s < nelems || s < size) 57 return NULL; 58 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 74 75 return p; 76} 77 78 79UCL_PRIVATE(void) 80ucl_free_internal(ucl_voidp p) 81{ 82 if (!p) 83 return; 84 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 96} 97 98 99/*********************************************************************** 100// public interface using the global hooks 101************************************************************************/ 102 103/* global allocator hooks */ 104ucl_alloc_hook_t ucl_alloc_hook = ucl_alloc_internal; 105ucl_free_hook_t ucl_free_hook = ucl_free_internal; 106 107 108UCL_PUBLIC(ucl_voidp) 109ucl_alloc(ucl_uint nelems, ucl_uint size) 110{ 111 if (!ucl_alloc_hook) 112 return NULL; 113 114 return ucl_alloc_hook(nelems,size); 115} 116 117 118UCL_PUBLIC(ucl_voidp) 119ucl_malloc(ucl_uint size) 120{ 121 if (!ucl_alloc_hook) 122 return NULL; 123 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 136} 137 138 139UCL_PUBLIC(void) 140ucl_free(ucl_voidp p) 141{ 142 if (!ucl_free_hook) 143 return; 144 145 ucl_free_hook(p); 146} 147 148 149/* 150vi:ts=4:et 151*/