OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / media / libstagefright / codecs / amrwbenc / src / mem_align.c
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        "mem_align.h"
26
27 /*****************************************************************************
28 *
29 * function name: mem_malloc
30 * description:  malloc the alignments memory 
31 * returns:      the point of the memory
32 *
33 **********************************************************************************/
34 void *
35 mem_malloc(VO_MEM_OPERATOR *pMemop, unsigned int size, unsigned char alignment, unsigned int CodecID)
36 {
37         int ret;
38         unsigned char *mem_ptr;
39         VO_MEM_INFO MemInfo;
40
41         if (!alignment) {
42
43                 MemInfo.Flag = 0;
44                 MemInfo.Size = size + 1;
45                 ret = pMemop->Alloc(CodecID, &MemInfo);
46                 if(ret != 0)
47                         return 0;
48                 mem_ptr = (unsigned char *)MemInfo.VBuffer;
49
50                 pMemop->Set(CodecID, mem_ptr, 0, size + 1);
51
52                 *mem_ptr = (unsigned char)1;
53
54                 return ((void *)(mem_ptr+1));
55         } else {
56                 unsigned char *tmp;
57
58                 MemInfo.Flag = 0;
59                 MemInfo.Size = size + alignment;
60                 ret = pMemop->Alloc(CodecID, &MemInfo);
61                 if(ret != 0)
62                         return 0;
63
64                 tmp = (unsigned char *)MemInfo.VBuffer;
65
66                 pMemop->Set(CodecID, tmp, 0, size + alignment);
67
68                 mem_ptr =
69                         (unsigned char *) ((unsigned int) (tmp + alignment - 1) &
70                                         (~((unsigned int) (alignment - 1))));
71
72                 if (mem_ptr == tmp)
73                         mem_ptr += alignment;
74
75                 *(mem_ptr - 1) = (unsigned char) (mem_ptr - tmp);
76
77                 return ((void *)mem_ptr);
78         }
79
80         return(0);
81 }
82
83
84 /*****************************************************************************
85 *
86 * function name: mem_free
87 * description:  free the memory
88 *
89 *******************************************************************************/
90 void
91 mem_free(VO_MEM_OPERATOR *pMemop, void *mem_ptr, unsigned int CodecID)
92 {
93
94         unsigned char *ptr;
95
96         if (mem_ptr == 0)
97                 return;
98
99         ptr = mem_ptr;
100
101         ptr -= *(ptr - 1);
102
103         pMemop->Free(CodecID, ptr);
104 }
105
106
107