Home | History | Annotate | Download | only in src
      1 /*
      2  ** Copyright 2003-2010, VisualOn, Inc.
      3  **
      4  ** Licensed under the Apache License, Version 2.0 (the "License");
      5  ** you may not use this file except in compliance with the License.
      6  ** You may obtain a copy of the License at
      7  **
      8  **     http://www.apache.org/licenses/LICENSE-2.0
      9  **
     10  ** Unless required by applicable law or agreed to in writing, software
     11  ** distributed under the License is distributed on an "AS IS" BASIS,
     12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  ** See the License for the specific language governing permissions and
     14  ** limitations under the License.
     15  */
     16 
     17 /*******************************************************************************
     18 	File:		mem_align.c
     19 
     20 	Content:	Memory alloc alignments functions
     21 
     22 *******************************************************************************/
     23 
     24 
     25 #include	"memalign.h"
     26 #ifdef _MSC_VER
     27 #include	<stddef.h>
     28 #else
     29 #include	<stdint.h>
     30 #endif
     31 
     32 /*****************************************************************************
     33 *
     34 * function name: mem_malloc
     35 * description:  malloc the alignments memory
     36 * returns:      the point of the memory
     37 *
     38 **********************************************************************************/
     39 void *
     40 mem_malloc(VO_MEM_OPERATOR *pMemop, unsigned int size, unsigned char alignment, unsigned int CodecID)
     41 {
     42 	int ret;
     43 	unsigned char *mem_ptr;
     44 	VO_MEM_INFO MemInfo;
     45 
     46 	if (!alignment) {
     47 
     48 		MemInfo.Flag = 0;
     49 		MemInfo.Size = size + 1;
     50 		ret = pMemop->Alloc(CodecID, &MemInfo);
     51 		if(ret != 0)
     52 			return 0;
     53 		mem_ptr = (unsigned char *)MemInfo.VBuffer;
     54 
     55 		pMemop->Set(CodecID, mem_ptr, 0, size + 1);
     56 
     57 		*mem_ptr = (unsigned char)1;
     58 
     59 		return ((void *)(mem_ptr+1));
     60 	} else {
     61 		unsigned char *tmp;
     62 
     63 		MemInfo.Flag = 0;
     64 		MemInfo.Size = size + alignment;
     65 		ret = pMemop->Alloc(CodecID, &MemInfo);
     66 		if(ret != 0)
     67 			return 0;
     68 
     69 		tmp = (unsigned char *)MemInfo.VBuffer;
     70 
     71 		pMemop->Set(CodecID, tmp, 0, size + alignment);
     72 
     73 		mem_ptr =
     74 			(unsigned char *) ((intptr_t) (tmp + alignment - 1) &
     75 					(~((intptr_t) (alignment - 1))));
     76 
     77 		if (mem_ptr == tmp)
     78 			mem_ptr += alignment;
     79 
     80 		*(mem_ptr - 1) = (unsigned char) (mem_ptr - tmp);
     81 
     82 		return ((void *)mem_ptr);
     83 	}
     84 
     85 	return(0);
     86 }
     87 
     88 
     89 /*****************************************************************************
     90 *
     91 * function name: mem_free
     92 * description:  free the memory
     93 *
     94 *******************************************************************************/
     95 void
     96 mem_free(VO_MEM_OPERATOR *pMemop, void *mem_ptr, unsigned int CodecID)
     97 {
     98 
     99 	unsigned char *ptr;
    100 
    101 	if (mem_ptr == 0)
    102 		return;
    103 
    104 	ptr = mem_ptr;
    105 
    106 	ptr -= *(ptr - 1);
    107 
    108 	pMemop->Free(CodecID, ptr);
    109 }
    110 
    111 
    112 
    113