A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 145 lines 3.5 kB view raw
1/* MikMod sound library 2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for 3 complete list. 4 5 This library is free software; you can redistribute it and/or modify 6 it under the terms of the GNU Library 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 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU Library General Public License for more details. 14 15 You should have received a copy of the GNU Library General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 18 02111-1307, USA. 19*/ 20 21/*============================================================================== 22 23 Dynamic memory routines 24 25==============================================================================*/ 26 27#ifdef HAVE_CONFIG_H 28#include "config.h" 29#endif 30 31#ifdef HAVE_POSIX_MEMALIGN 32#define _XOPEN_SOURCE 600 /* for posix_memalign */ 33#endif 34 35#include "string.h" 36#include "mikmod_internals.h" 37 38#if defined(HAVE_SSE2) || defined(HAVE_ALTIVEC) 39#undef WIN32_ALIGNED_MALLOC 40#if defined(_WIN32) && !defined(_WIN32_WCE) 41# if defined(_WIN64) /* OK with MSVC and MinGW */ 42# define WIN32_ALIGNED_MALLOC 43# elif defined(_MSC_VER) && (_MSC_VER >= 1300) 44# define WIN32_ALIGNED_MALLOC 45# elif defined(__MINGW32__) 46 /* no guarantees that msvcrt.dll will have it */ 47# endif 48#endif 49 50#define PTRSIZE (sizeof(void*)) 51 52/* return a 16 byte aligned address */ 53void* MikMod_amalloc(size_t size) 54{ 55 void *d; 56#if defined(HAVE_POSIX_MEMALIGN) 57 if (!posix_memalign(&d, 16, size)) { 58 memset(d, 0, size); 59 return d; 60 } 61#elif defined(WIN32_ALIGNED_MALLOC) 62 d = _aligned_malloc(size, 16); 63 if (d) { 64 ZeroMemory(d, size); 65 return d; 66 } 67#else 68 size_t s = (size)? ((size + (PTRSIZE-1)) & ~(PTRSIZE-1)) : PTRSIZE; 69 s += PTRSIZE + 16; 70 d = calloc(1, s); 71 if (d) { 72 char *pptr = (char *)d + PTRSIZE; 73 size_t err = ((size_t)pptr) & 15; 74 char *fptr = pptr + (16 - err); 75 *(size_t*)(fptr - PTRSIZE) = (size_t)d; 76 return fptr; 77 } 78#endif 79 80 _mm_errno = MMERR_OUT_OF_MEMORY; 81 if(_mm_errorhandler) _mm_errorhandler(); 82 return NULL; 83} 84 85void MikMod_afree(void *data) 86{ 87 if (!data) return; 88#if defined(HAVE_POSIX_MEMALIGN) 89 free(data); 90#elif defined(WIN32_ALIGNED_MALLOC) 91 _aligned_free(data); 92#else 93 free((void *) *(size_t*)((unsigned char *)data - PTRSIZE)); 94#endif 95} 96#endif /* (HAVE_SSE2) || (HAVE_ALTIVEC) */ 97 98void* MikMod_realloc(void *data, size_t size) 99{ 100 if (data) return realloc(data, size); 101 return calloc(1, size); 102} 103 104/* Same as malloc, but sets error variable _mm_error when fails */ 105void* MikMod_malloc(size_t size) 106{ 107 void *d = malloc(size); 108 if (d) return d; 109 110 _mm_errno = MMERR_OUT_OF_MEMORY; 111 if(_mm_errorhandler) _mm_errorhandler(); 112 return NULL; 113} 114 115/* Same as calloc, but sets error variable _mm_error when fails */ 116void* MikMod_calloc(size_t nitems, size_t size) 117{ 118 void *d = calloc(nitems, size); 119 if (d) return d; 120 121 _mm_errno = MMERR_OUT_OF_MEMORY; 122 if(_mm_errorhandler) _mm_errorhandler(); 123 return NULL; 124} 125 126void MikMod_free(void *data) 127{ 128 if (data) free(data); 129} 130 131/* like strdup(), but the result must be freed using MikMod_free() */ 132CHAR *MikMod_strdup(const CHAR *s) 133{ 134 size_t l; 135 CHAR *d; 136 137 if (!s) return NULL; 138 139 l = strlen(s) + 1; 140 d = (CHAR *) MikMod_malloc(l); 141 if (d) memcpy(d, s, l); 142 return d; 143} 144 145/* ex:set ts=4: */