* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
+/**
+ * @file mem.c
+ * default memory allocator for libavcodec.
+ */
+
#include "avcodec.h"
+
+/* here we can use OS dependant allocation functions */
+#undef malloc
+#undef free
+#undef realloc
+
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
memory allocator. You do not need to suppress this file because the
linker will do it automatically */
-/* memory alloc */
-void *av_malloc(int size)
+/**
+ * Memory allocation of size byte with alignment suitable for all
+ * memory accesses (including vectors if available on the
+ * CPU). av_malloc(0) must return a non NULL pointer.
+ */
+void *av_malloc(unsigned int size)
{
void *ptr;
-#if defined (HAVE_MEMALIGN)
+
+#ifdef MEMALIGN_HACK
+ int diff;
+ ptr = malloc(size+16+1);
+ diff= ((-(int)ptr - 1)&15) + 1;
+ ptr += diff;
+ ((char*)ptr)[-1]= diff;
+#elif defined (HAVE_MEMALIGN)
ptr = memalign(16,size);
/* Why 64?
Indeed, we should align it:
#else
ptr = malloc(size);
#endif
- if (!ptr)
- return NULL;
-//fprintf(stderr, "%X %d\n", (int)ptr, size);
- /* NOTE: this memset should not be present */
- memset(ptr, 0, size);
return ptr;
}
+/**
+ * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
+ * identical to malloc(size). If size is zero, it is identical to
+ * free(ptr) and NULL is returned.
+ */
+void *av_realloc(void *ptr, unsigned int size)
+{
+#ifdef MEMALIGN_HACK
+ //FIXME this isnt aligned correctly though it probably isnt needed
+ int diff;
+ if(!ptr) return av_malloc(size);
+ diff= ((char*)ptr)[-1];
+ return realloc(ptr - diff, size + diff) + diff;
+#else
+ return realloc(ptr, size);
+#endif
+}
+
/* NOTE: ptr = NULL is explicetly allowed */
void av_free(void *ptr)
{
/* XXX: this test should not be needed on most libcs */
if (ptr)
+#ifdef MEMALIGN_HACK
+ free(ptr - ((char*)ptr)[-1]);
+#else
free(ptr);
+#endif
}