]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - processor_audio_sdk_1_00_00_00/pasdk/common/paf_alg_create.c
Move location of alpha folder
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / common / paf_alg_create.c
2 /*
3 * Copyright (C) 2004-2014 Texas Instruments Incorporated - http://www.ti.com/
4 * All rights reserved.  
5 *
6 *  Redistribution and use in source and binary forms, with or without
7 *  modification, are permitted provided that the following conditions
8 *  are met:
9 *
10 *    Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 *    Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the
16 *    distribution.
17 *
18 *    Neither the name of Texas Instruments Incorporated nor the names of
19 *    its contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 //
37 //
38 // Performance Audio Algorithm Creation.
39 //
40 //
41 //
43 #include <stdlib.h>
44 #include <xdc/std.h>
45 #include <xdc/runtime/Error.h>
46 #include <xdc/runtime/IHeap.h>
47 #include <xdc/runtime/Memory.h>
48 #include "alg.h"
50 //#include "pafhjt.h"
51 #include "paf_alg.h"
52 #include "paf_alg_priv.h"
54 //extern far int SDRAM;
55 //far int *mem_mallocseg_ptr=&SDRAM;
56 //#define MALLOC(size) MEM_alloc(*mem_mallocseg_ptr,(size), 8)
57 //#define FREE(addr,size) MEM_free(*mem_mallocseg_ptr,(addr),(size))
58 extern HeapMem_Handle heapMemDdr3;
59 #define HEAPMALLOC (IHeap_Handle)heapMemDdr3
61 /*
62  * The PAF_ALG_alloc_ function collects the Common Memory Data Requirements
63  * from all the Algorithms. The function returns 0 on success. If the 
64  * call to malloc() fails then it returns PAF_ALGERR_MEMTAB_MALLOC. If a 
65  * particular Algorithm requests 0 or less memTabs then the function returns 
66  * an error value of PAF_ALGERR_MEMTAB_COUNT. 
67  */
69 Int 
70 PAF_ALG_alloc_ (
71     const PAF_ALG_AllocInit *pInit, 
72     Int sizeofInit, 
73     IALG_MemRec common[])
74 {
75     IALG_MemRec *memTab;
76     Int commonSize[PAF_IALG_COMMON_MEMN+1];
77     Int commonCount[PAF_IALG_COMMON_MEMN+1];
78     Int i,n;
79     Error_Block eb;
81     // Initialize error block
82     Error_init(&eb); 
84     for ( ; pInit && pInit->ialg_fxns; 
85         pInit=(const PAF_ALG_AllocInit *)((char *)pInit+sizeofInit)) {
86 #ifdef _TMS320C6X
87 #pragma UNROLL(1)
88 #endif
89         for(i=PAF_IALG_COMMON_MEM0;i<=PAF_IALG_COMMON_MEMN;++i) {
90             commonSize[i]=0;
91             commonCount[i]=0;
92         }
93         n = pInit->ialg_fxns->algNumAlloc != NULL ? pInit->ialg_fxns->algNumAlloc() 
94                                                   : IALG_DEFMEMRECS;
95         //if((memTab = (IALG_MemRec *)MALLOC(n * sizeof(IALG_MemRec)))) {
96         if ((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof(IALG_MemRec), 8, &eb)))
97         {
98             n=pInit->ialg_fxns->algAlloc(pInit->ialg_prms,
99                                          (IALG_Fxns ** )&(pInit->ialg_fxns),memTab);
100             if(n<=0)
101                 return PAF_ALGERR_MEMTAB_COUNT;
103             for(i=0;i<n;++i){
104                 if((memTab[i].attrs == PAF_IALG_COMMON_MEM0) || 
105                    (memTab[i].attrs >= PAF_IALG_COMMON_MEM1 && 
106                     memTab[i].attrs <= PAF_IALG_COMMON_MEMN)) {
107                     commonSize[memTab[i].attrs] += memTab[i].size + 
108                         (commonCount[memTab[i].attrs] ? memTab[i].alignment : 0);
109                     if(!commonCount[memTab[i].attrs] && 
110                         memTab[i].alignment > common[memTab[i].attrs].alignment)
111                         common[memTab[i].attrs].alignment = memTab[i].alignment;
112                     commonCount[memTab[i].attrs]++;
113                     if (common[memTab[i].attrs].space == PAF_IALG_NONE)
114                         /* creating common memory section */
115                         common[memTab[i].attrs].space = memTab[i].space;
116                     else if (common[memTab[i].attrs].space == memTab[i].space)
117                         /* existing common memory section: space okay */
118                         ;
119                     else {
120                         /* existing common memory section: space conflict */
121                         /* Greater Memory Space Rule (tends to external)  */
122                         if (common[memTab[i].attrs].space < memTab[i].space)
123                             common[memTab[i].attrs].space = memTab[i].space;
124                         else 
125                             ;
126                     }
127                 }
128             }
129             //FREE(memTab,(n * sizeof(IALG_MemRec)));
130             Memory_free(HEAPMALLOC,memTab,(n*sizeof(IALG_MemRec)));
131             if(common){
132                 for(i=PAF_IALG_COMMON_MEM0;i<=PAF_IALG_COMMON_MEMN;++i) {
133                     if(commonSize[i] > common[i].size) {
134                         common[i].size = commonSize[i];
135                         common[i].attrs = (IALG_MemAttrs )i;
136                     }
137                 }
138             }
139         }
140         else
141             return PAF_ALGERR_MEMTAB_MALLOC;
142     }
143     return 0;
146 /*
147  * The PAF_ALG_create_ is derived from standard XDAIS ALG_create function defined in the 
148  * file alg_create.c version "XDAS 2.2.1 12-07-01". 
149  * The PAF_ALG_create_ is designed to handle the Performance Audio Specific "Common"
150  * memory and also to share the scratch memory accross the Performance Audio
151  * System.
152  * The PAF_ALG_create_ function returns the Handle to the Algorithm if the Algorithm is 
153  * created successfully otherwise it returns NULL.
154  */
156 ALG_Handle PAF_ALG_create_ (
157     const IALG_Fxns *fxns,
158     IALG_Handle p,
159     const IALG_Params *params,
160     const IALG_MemRec common[],
161     PAF_IALG_Config *pafConfig)
163     IALG_MemRec *memTab;
164     Int n;
165     ALG_Handle alg;
166     IALG_Fxns *fxnsPtr;
167     Error_Block eb;
169     // Initialize error block
170     Error_init(&eb); 
172     if(fxns != NULL){
173         n = fxns->algNumAlloc != NULL ? fxns->algNumAlloc() : IALG_DEFMEMRECS;
174         //if((memTab = (IALG_MemRec *)MALLOC(n * sizeof (IALG_MemRec)))) 
175         if((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof (IALG_MemRec), 8, &eb)))
176         {
177             n = fxns->algAlloc(params, &fxnsPtr, memTab);
178             if(n<=0)
179                 return NULL;
180             if(!PAF_ALG_allocMemory(memTab,n,common,pafConfig)){
181                 alg = (IALG_Handle)memTab[0].base;
182                 alg->fxns = (IALG_Fxns * )fxns;
183                 if(fxns->algInit(alg, memTab, p, params) == IALG_EOK){
184                     //FREE(memTab,(n * sizeof(IALG_MemRec)));
185                     Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
186                     return alg;
187                 }
188                 fxns->algFree(alg, memTab);
189                 PAF_ALG_freeMemory(memTab, n);
190             }
191             //FREE(memTab,(n * sizeof(IALG_MemRec)));
192             Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
193         }
194     }
195     return (NULL);
198 /*
199  * The PAF_ALG_delete is derived from standard XDAIS ALG_delete function defined in the 
200  * file alg_create.c version "XDAS 2.2.1 12-07-01". 
201  * The PAF_ALG_create is designed to handle the Performance Audio Specific "Common"
202  * memory and also scratch memory.
203  */
205 /* DO NOT REMOVE THIS CODE_SECTION. --Kurt */
206 #ifdef _TMS320C6X
207 #pragma CODE_SECTION(PAF_ALG_delete,".text:_PAF_ALG_delete")
208 #endif
210 Void PAF_ALG_delete(ALG_Handle alg)
212     IALG_MemRec *memTab;
213     Int n;
214     IALG_Fxns *fxns;
215     Error_Block eb;
217 #if defined(_TMS320C6X) && !defined(__TI_EABI__)
218     asm (" .clink");
219 #endif    
221     // Initialize error block
222     Error_init(&eb); 
224     if (alg != NULL && alg->fxns != NULL) {
225         fxns = alg->fxns;
226         n = fxns->algNumAlloc != NULL ? fxns->algNumAlloc() : IALG_DEFMEMRECS;
228         //if ((memTab = (IALG_MemRec *)MALLOC(n * sizeof (IALG_MemRec)))) 
229         if ((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof (IALG_MemRec), 8, &eb)))
230         {
231             memTab[0].base = alg;
232             n = fxns->algFree(alg, memTab);
233             PAF_ALG_freeMemory(memTab, n);
234             //FREE(memTab,(n * sizeof(IALG_MemRec)));
235             Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
236         }
237     }
240 struct PAF_ALG_Fxns PAF_ALG_fxns =
242     PAF_ALG_alloc_,
243     PAF_ALG_create_,
244     PAF_ALG_init_,
245     PAF_ALG_allocMemory_,
246     PAF_ALG_mallocMemory_,
247     PAF_ALG_memSpaceToHeapId_,    
248     PAF_ALG_memSpaceToHeap_,
249     PAF_ALG_setup_
250 };