]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/common/paf_alg_create.c
Build #59: Update submodules + Merge files from PASDK-218 HSR Work.
[processor-sdk/performance-audio-sr.git] / pasdk / common / paf_alg_create.c
2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
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 // Algorithm Creation
38 //
40 #include <stdlib.h>
41 #include <xdc/std.h>
42 #include <xdc/runtime/Error.h>
43 #include <xdc/runtime/IHeap.h>
44 #include <xdc/runtime/Memory.h>
45 #include "alg.h"
47 //#include "pafhjt.h"
48 #include "paf_alg.h"
49 #include "paf_alg_priv.h"
51 //extern far int SDRAM;
52 //far int *mem_mallocseg_ptr=&SDRAM;
53 //#define MALLOC(size) MEM_alloc(*mem_mallocseg_ptr,(size), 8)
54 //#define FREE(addr,size) MEM_free(*mem_mallocseg_ptr,(addr),(size))
55 extern HeapMem_Handle heapMemDdr3;
56 #define HEAPMALLOC (IHeap_Handle)heapMemDdr3
58 /*
59  * The PAF_ALG_alloc_ function collects the Common Memory Data Requirements
60  * from all the Algorithms. The function returns 0 on success. If the 
61  * call to malloc() fails then it returns PAF_ALGERR_MEMTAB_MALLOC. If a 
62  * particular Algorithm requests 0 or less memTabs then the function returns 
63  * an error value of PAF_ALGERR_MEMTAB_COUNT. 
64  */
66 Int 
67 PAF_ALG_alloc_ (
68     const PAF_ALG_AllocInit *pInit, 
69     Int sizeofInit, 
70     IALG_MemRec common[])
71 {
72     IALG_MemRec *memTab;
73     Int commonSize[PAF_IALG_COMMON_MEMN+1];
74     Int commonCount[PAF_IALG_COMMON_MEMN+1];
75     Int i,n;
76     Error_Block eb;
78     // Initialize error block
79     Error_init(&eb); 
81     for ( ; pInit && pInit->ialg_fxns; 
82         pInit=(const PAF_ALG_AllocInit *)((char *)pInit+sizeofInit)) {
83 #ifdef _TMS320C6X
84 #pragma UNROLL(1)
85 #endif
86         for(i=PAF_IALG_COMMON_MEM0;i<=PAF_IALG_COMMON_MEMN;++i) {
87             commonSize[i]=0;
88             commonCount[i]=0;
89         }
90         n = pInit->ialg_fxns->algNumAlloc != NULL ? pInit->ialg_fxns->algNumAlloc() 
91                                                   : IALG_DEFMEMRECS;
92         //if((memTab = (IALG_MemRec *)MALLOC(n * sizeof(IALG_MemRec)))) {
93         if ((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof(IALG_MemRec), 8, &eb)))
94         {
95             n=pInit->ialg_fxns->algAlloc(pInit->ialg_prms,
96                                          (IALG_Fxns ** )&(pInit->ialg_fxns),memTab);
97             if(n<=0)
98                 return PAF_ALGERR_MEMTAB_COUNT;
100             for(i=0;i<n;++i){
101                 if((memTab[i].attrs == PAF_IALG_COMMON_MEM0) || 
102                    (memTab[i].attrs >= PAF_IALG_COMMON_MEM1 && 
103                     memTab[i].attrs <= PAF_IALG_COMMON_MEMN)) {
104                     commonSize[memTab[i].attrs] += memTab[i].size + 
105                         (commonCount[memTab[i].attrs] ? memTab[i].alignment : 0);
106                     if(!commonCount[memTab[i].attrs] && 
107                         memTab[i].alignment > common[memTab[i].attrs].alignment)
108                         common[memTab[i].attrs].alignment = memTab[i].alignment;
109                     commonCount[memTab[i].attrs]++;
110                     if (common[memTab[i].attrs].space == PAF_IALG_NONE)
111                         /* creating common memory section */
112                         common[memTab[i].attrs].space = memTab[i].space;
113                     else if (common[memTab[i].attrs].space == memTab[i].space)
114                         /* existing common memory section: space okay */
115                         ;
116                     else {
117                         /* existing common memory section: space conflict */
118                         /* Greater Memory Space Rule (tends to external)  */
119                         if (common[memTab[i].attrs].space < memTab[i].space)
120                             common[memTab[i].attrs].space = memTab[i].space;
121                         else 
122                             ;
123                     }
124                 }
125             }
126             //FREE(memTab,(n * sizeof(IALG_MemRec)));
127             Memory_free(HEAPMALLOC,memTab,(n*sizeof(IALG_MemRec)));
128             if(common){
129                 for(i=PAF_IALG_COMMON_MEM0;i<=PAF_IALG_COMMON_MEMN;++i) {
130                     if(commonSize[i] > common[i].size) {
131                         common[i].size = commonSize[i];
132                         common[i].attrs = (IALG_MemAttrs )i;
133                     }
134                 }
135             }
136         }
137         else
138             return PAF_ALGERR_MEMTAB_MALLOC;
139     }
140     return 0;
143 /*
144  * The PAF_ALG_create_ is derived from standard XDAIS ALG_create function defined in the 
145  * file alg_create.c version "XDAS 2.2.1 12-07-01". 
146  * The PAF_ALG_create_ is designed to handle the Performance Audio Specific "Common"
147  * memory and also to share the scratch memory accross the Performance Audio
148  * System.
149  * The PAF_ALG_create_ function returns the Handle to the Algorithm if the Algorithm is 
150  * created successfully otherwise it returns NULL.
151  */
153 ALG_Handle PAF_ALG_create_ (
154     const IALG_Fxns *fxns,
155     IALG_Handle p,
156     const IALG_Params *params,
157     const IALG_MemRec common[],
158     PAF_IALG_Config *pafConfig)
160     IALG_MemRec *memTab;
161     Int n;
162     ALG_Handle alg;
163     IALG_Fxns *fxnsPtr;
164     Error_Block eb;
166     // Initialize error block
167     Error_init(&eb); 
169     if(fxns != NULL){
170         n = fxns->algNumAlloc != NULL ? fxns->algNumAlloc() : IALG_DEFMEMRECS;
171         //if((memTab = (IALG_MemRec *)MALLOC(n * sizeof (IALG_MemRec)))) 
172         if((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof (IALG_MemRec), 8, &eb)))
173         {
174             n = fxns->algAlloc(params, &fxnsPtr, memTab);
175             if(n<=0)
176                 return NULL;
177             if(!PAF_ALG_allocMemory(memTab,n,common,pafConfig)){
178                 alg = (IALG_Handle)memTab[0].base;
179                 alg->fxns = (IALG_Fxns * )fxns;
180                 if(fxns->algInit(alg, memTab, p, params) == IALG_EOK){
181                     //FREE(memTab,(n * sizeof(IALG_MemRec)));
182                     Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
183                     return alg;
184                 }
185                 fxns->algFree(alg, memTab);
186                 PAF_ALG_freeMemory(memTab, n);
187             }
188             //FREE(memTab,(n * sizeof(IALG_MemRec)));
189             Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
190         }
191     }
192     return (NULL);
195 /*
196  * The PAF_ALG_delete is derived from standard XDAIS ALG_delete function defined in the 
197  * file alg_create.c version "XDAS 2.2.1 12-07-01". 
198  * The PAF_ALG_create is designed to handle the Performance Audio Specific "Common"
199  * memory and also scratch memory.
200  */
202 /* DO NOT REMOVE THIS CODE_SECTION. --Kurt */
203 #ifdef _TMS320C6X
204 #pragma CODE_SECTION(PAF_ALG_delete,".text:_PAF_ALG_delete")
205 #endif
207 Void PAF_ALG_delete(ALG_Handle alg)
209     IALG_MemRec *memTab;
210     Int n;
211     IALG_Fxns *fxns;
212     Error_Block eb;
214 #if defined(_TMS320C6X) && !defined(__TI_EABI__)
215     asm (" .clink");
216 #endif    
218     // Initialize error block
219     Error_init(&eb); 
221     if (alg != NULL && alg->fxns != NULL) {
222         fxns = alg->fxns;
223         n = fxns->algNumAlloc != NULL ? fxns->algNumAlloc() : IALG_DEFMEMRECS;
225         //if ((memTab = (IALG_MemRec *)MALLOC(n * sizeof (IALG_MemRec)))) 
226         if ((memTab = (IALG_MemRec *)Memory_alloc(HEAPMALLOC, n*sizeof (IALG_MemRec), 8, &eb)))
227         {
228             memTab[0].base = alg;
229             n = fxns->algFree(alg, memTab);
230             PAF_ALG_freeMemory(memTab, n);
231             //FREE(memTab,(n * sizeof(IALG_MemRec)));
232             Memory_free(HEAPMALLOC,memTab,(n * sizeof(IALG_MemRec)));
233         }
234     }
237 struct PAF_ALG_Fxns PAF_ALG_fxns =
239     PAF_ALG_alloc_,
240     PAF_ALG_create_,
241     PAF_ALG_init_,
242     PAF_ALG_allocMemory_,
243     PAF_ALG_mallocMemory_,
244     PAF_ALG_memSpaceToHeapId_,    
245     PAF_ALG_memSpaceToHeap_,
246     PAF_ALG_setup_
247 };