1956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/* 2956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** Copyright 2003-2010, VisualOn, Inc. 3956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** 4956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** Licensed under the Apache License, Version 2.0 (the "License"); 5956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** you may not use this file except in compliance with the License. 6956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** You may obtain a copy of the License at 7956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** 8956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** http://www.apache.org/licenses/LICENSE-2.0 9956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** 10956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** Unless required by applicable law or agreed to in writing, software 11956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** distributed under the License is distributed on an "AS IS" BASIS, 12956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** See the License for the specific language governing permissions and 14956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ** limitations under the License. 15956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong */ 16956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 17956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/******************************************************************************* 18956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong File: mem_align.c 19956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 20956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong Content: Memory alloc alignments functions 21956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 22956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*******************************************************************************/ 23956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 24956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 25956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#include "memalign.h" 26267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo#ifdef _MSC_VER 27267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo#include <stddef.h> 28267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo#else 29267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo#include <stdint.h> 30267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo#endif 31956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 32956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/***************************************************************************** 33956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* 34956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* function name: mem_malloc 35b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard* description: malloc the alignments memory 36956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* returns: the point of the memory 37956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* 38956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong**********************************************************************************/ 39956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid * 40956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongmem_malloc(VO_MEM_OPERATOR *pMemop, unsigned int size, unsigned char alignment, unsigned int CodecID) 41956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{ 42956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong int ret; 43956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong unsigned char *mem_ptr; 44956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong VO_MEM_INFO MemInfo; 45956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 46956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong if (!alignment) { 47956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 48956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong MemInfo.Flag = 0; 49956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong MemInfo.Size = size + 1; 50956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ret = pMemop->Alloc(CodecID, &MemInfo); 51956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong if(ret != 0) 52956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return 0; 53956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong mem_ptr = (unsigned char *)MemInfo.VBuffer; 54956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 55956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong pMemop->Set(CodecID, mem_ptr, 0, size + 1); 56956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 57956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong *mem_ptr = (unsigned char)1; 58956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 59956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return ((void *)(mem_ptr+1)); 60956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong } else { 61956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong unsigned char *tmp; 62956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 63956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong MemInfo.Flag = 0; 64956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong MemInfo.Size = size + alignment; 65956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ret = pMemop->Alloc(CodecID, &MemInfo); 66956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong if(ret != 0) 67956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return 0; 68956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 69956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong tmp = (unsigned char *)MemInfo.VBuffer; 70956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 71956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong pMemop->Set(CodecID, tmp, 0, size + alignment); 72956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 73956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong mem_ptr = 74267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo (unsigned char *) ((intptr_t) (tmp + alignment - 1) & 75267dbd822ff060843ce409c0f1b50f6d5468ea31Martin Storsjo (~((intptr_t) (alignment - 1)))); 76956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 77956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong if (mem_ptr == tmp) 78956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong mem_ptr += alignment; 79956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 80956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong *(mem_ptr - 1) = (unsigned char) (mem_ptr - tmp); 81956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 82956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return ((void *)mem_ptr); 83956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong } 84956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 85956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return(0); 86956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong} 87956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 88956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 89956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/***************************************************************************** 90956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* 91956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* function name: mem_free 92956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* description: free the memory 93956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* 94956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*******************************************************************************/ 95956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid 96956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongmem_free(VO_MEM_OPERATOR *pMemop, void *mem_ptr, unsigned int CodecID) 97956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{ 98956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 99956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong unsigned char *ptr; 100956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 101956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong if (mem_ptr == 0) 102956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong return; 103956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 104956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ptr = mem_ptr; 105956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 106956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong ptr -= *(ptr - 1); 107956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 108956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong pMemop->Free(CodecID, ptr); 109956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong} 110956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 111956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 112956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong 113