MloUtils.h

00001 /*****************************************************************
00002 |
00003 |    Melo - Runtime Utilities
00004 |
00005 |    Copyright 2004-2006 Axiomatic Systems LLC
00006 |
00007 |    This file is part of Melo (Melo AAC Decoder).
00008 |
00009 |    Unless you have obtained Melo under a difference license,
00010 |    this version of Melo is Melo|GPL.
00011 |    Melo|GPL is free software; you can redistribute it and/or modify
00012 |    it under the terms of the GNU General Public License as published by
00013 |    the Free Software Foundation; either version 2, or (at your option)
00014 |    any later version.
00015 |
00016 |    Melo|GPL is distributed in the hope that it will be useful,
00017 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 |    GNU General Public License for more details.
00020 |
00021 |    You should have received a copy of the GNU General Public License
00022 |    along with Melo|GPL; see the file COPYING.  If not, write to the
00023 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00024 |    02111-1307, USA.
00025 |
00026  ****************************************************************/
00027 
00028 #ifndef _MLO_UTILS_H_
00029 #define _MLO_UTILS_H_
00030 
00031 /*----------------------------------------------------------------------
00032 |    includes
00033 +---------------------------------------------------------------------*/
00034 #include "MloConfig.h"
00035 #include "MloTypes.h"
00036 
00037 #if defined(MLO_CONFIG_HAVE_STDLIB_H)
00038 #include <stdlib.h>
00039 #endif /* MLO_CONFIG_HAVE_STDLIB_H */
00040 
00041 #if defined(MLO_CONFIG_HAVE_STRING_H)
00042 #include <string.h>
00043 #endif /* MLO_CONFIG_HAVE_STRING_H */
00044 
00045 #if defined (MLO_CONFIG_HAVE_LIMITS_H)
00046 #include <limits.h>
00047 #endif   /* MLO_CONFIG_HAVE_LIMITS_H */
00048 
00049 #if defined(DMALLOC)
00050 #include <dmalloc.h>
00051 #endif
00052 
00053 /*----------------------------------------------------------------------
00054 |    macros
00055 +---------------------------------------------------------------------*/
00056 #define  MLO_ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
00057 
00058 #if ! defined (MLO_CONFIG_HAVE_LIMITS_H) && ! defined (CHAR_BIT)
00059 #define  CHAR_BIT          (8)
00060 #endif   /* MLO_CONFIG_HAVE_LIMITS_H */
00061 #define  MLO_BIT_DEPTH(x)  (sizeof (x) * CHAR_BIT)
00062 #define  MLO_MAX_VAL_U(x)  ((1L << MLO_BIT_DEPTH (x)) - 1)
00063 #define  MLO_MAX_VAL_S(x)  ((1L << (MLO_BIT_DEPTH (x) - 1)) - 1)
00064 #define  MLO_MIN_VAL_S(x)  (-1L << (MLO_BIT_DEPTH (x) - 1))
00065 
00066 #define  MLO_ABS(x)        (((x) < 0) ? (-(x)) : (x))
00067 #define  MLO_SIGN(x)       (((x) < 0) ? -1 : 1)
00068 #define  MLO_MAX(x,a)      (((x) < (a)) ? (a) : (x))
00069 #define  MLO_MIN(x,a)      (((a) < (x)) ? (a) : (x))
00070 #define  MLO_BOUND(x,a,b)  (MLO_MAX (MLO_MIN ((x), (b)), (a)))
00071 
00072 /*----------------------------------------------------------------------
00073 |    byte I/O
00074 +---------------------------------------------------------------------*/
00075 #ifdef __cplusplus
00076 extern "C" {
00077 #endif /* __cplusplus */
00078 
00079 extern void MLO_BytesFromInt32Be(unsigned char* buffer, unsigned long value);
00080 extern void MLO_BytesFromInt16Be(unsigned char* buffer, unsigned short value);
00081 extern unsigned long MLO_BytesToInt32Be(const unsigned char* buffer);
00082 extern unsigned short MLO_BytesToInt16Be(const unsigned char* buffer);
00083 
00084 extern void MLO_BytesFromInt32Le(unsigned char* buffer, unsigned long value);
00085 extern void MLO_BytesFromInt16Le(unsigned char* buffer, unsigned short value);
00086 extern unsigned long MLO_BytesToInt32Le(const unsigned char* buffer);
00087 extern unsigned short MLO_BytesToInt16Le(const unsigned char* buffer);
00088 
00089 /*----------------------------------------------------------------------
00090 |    C Runtime
00091 +---------------------------------------------------------------------*/
00092 #if defined(MLO_CONFIG_HAVE_MALLOC)
00093 #define MLO_AllocateMemory malloc
00094 #else
00095 extern void* MLO_AllocateMemory(unsigned int);
00096 #endif
00097 
00098 #if defined(MLO_CONFIG_HAVE_CALLOC)
00099 #define MLO_AllocateZeroMemory(x) calloc(1,(x))
00100 #else
00101 extern void* MLO_AllocateZeroMemory(unsigned int);
00102 #endif
00103 
00104 #if defined(MLO_CONFIG_HAVE_FREE)
00105 #define MLO_FreeMemory free
00106 #else
00107 extern void MLO_FreeMemory(void* pointer);
00108 #endif
00109 
00110 #if defined(MLO_CONFIG_HAVE_MEMCPY)
00111 #define MLO_CopyMemory memcpy
00112 #else
00113 extern void MLO_CopyMemory(void* dest, const void* src, MLO_Size size);
00114 #endif
00115 
00116 #if defined(MLO_CONFIG_HAVE_MEMMOVE)
00117 #define MLO_MoveMemory memmove
00118 #else
00119 extern void MLO_MoveMemory(void* dest, const void* src, MLO_Size size);
00120 #endif
00121 
00122 #if defined(MLO_CONFIG_HAVE_MEMSET)
00123 #define MLO_SetMemory memset
00124 #else
00125 extern void MLO_SetMemory(void* dest, int c, MLO_Size size);
00126 #endif
00127 
00128 #ifdef __cplusplus
00129 }
00130 #endif /* __cplusplus */
00131 
00132 #endif /* _MLO_UTILS_H_ */