]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/audio-preprocessing.git/blobdiff - file_demo/am572x/test/src/mem.c
Resturcture the GIT repository for noise reduction demo:
[processor-sdk/audio-preprocessing.git] / file_demo / am572x / test / src / mem.c
diff --git a/file_demo/am572x/test/src/mem.c b/file_demo/am572x/test/src/mem.c
new file mode 100644 (file)
index 0000000..8e47622
--- /dev/null
@@ -0,0 +1,237 @@
+/*\r
+ *        Copyright (c) 2007 \96 2013 Texas Instruments Incorporated                
+ *                                                                                
+ *              All rights reserved not granted herein.                           
+ *                                                                                
+ *                         Limited License.                                       
+ *                                                                                
+ *  Texas Instruments Incorporated grants a world-wide, royalty-free,             
+ *  non-exclusive license under copyrights and patents it now or hereafter owns   
+ *  or controls to make, have made, use, import, offer to sell and sell           
+ *  ("Utilize") this software subject to the terms herein.  With respect to the   
+ *  foregoing patent license, such license is granted solely to the extent that   
+ *  any such patent is necessary to Utilize the software alone.  The patent       
+ *  license shall not apply to any combinations which include this software,      
+ *  other than combinations with devices manufactured by or for TI (\93TI           
+ *  Devices\94).  No hardware patent is licensed hereunder.                         
+ *                                                                                
+ *  Redistributions must preserve existing copyright notices and reproduce this   
+ *  license (including the above copyright notice and the disclaimer and (if      
+ *  applicable) source code license limitations below) in the documentation       
+ *  and/or other materials provided with the distribution                         
+ *                                                                                
+ *  Redistribution and use in binary form, without modification, are permitted    
+ *  provided that the following conditions are met:                               
+ *                                                                                
+ *    *  No reverse engineering, decompilation, or disassembly of this software   
+ *  is permitted with respect to any software provided in binary form.            
+ *                                                                                
+ *    *  any redistribution and use are licensed by TI for use only with TI       
+ *  Devices.                                                                      
+ *                                                                                
+ *    *  Nothing shall obligate TI to provide you with source code for the        
+ *  software licensed and provided to you in object code.                         
+ *                                                                                
+ *  If software source code is provided to you, modification and redistribution   
+ *  of the source code are permitted provided that the following conditions are   
+ *  met:                                                                          
+ *                                                                                
+ *    *  any redistribution and use of the source code, including any resulting   
+ *  derivative works, are licensed by TI for use only with TI Devices.            
+ *                                                                                
+ *    *  any redistribution and use of any object code compiled from the source   
+ *  code and any resulting derivative works, are licensed by TI for use only      
+ *  with TI Devices.                                                              
+ *                                                                                
+ *  Neither the name of Texas Instruments Incorporated nor the names of its       
+ *  suppliers may be used to endorse or promote products derived from this        
+ *  software without specific prior written permission.                           
+ *                                                                                
+ *  DISCLAIMER.                                                                   
+ *                                                                                
+ *  THIS SOFTWARE IS PROVIDED BY TI AND TI\92S LICENSORS "AS IS" AND ANY EXPRESS    
+ *  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED             
+ *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        
+ *  DISCLAIMED. IN NO EVENT SHALL TI AND TI\92S LICENSORS BE LIABLE FOR ANY         
+ *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    
+ *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR            
+ *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER    
+ *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT            
+ *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY     
+ *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   
+ *  DAMAGE.                                                                       \r
+*/\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <xdc/std.h>\r
+\r
+#include <ti/mas/types/types.h>\r
+#include <ti/mas/util/ecomem.h>\r
+#include <ti/xdais/ialg.h>\r
+#include <ti/mas/vau/test/src/mem.h>\r
+\r
+#define align(Addr, Algnmt) (((Addr)+(Algnmt)-1)&(0xFFFFFFFFUL-(Algnmt)+1))\r
+\r
+typedef struct {\r
+  tword *heapBase;\r
+  tulong indx;\r
+  tulong size;\r
+} siuMemHeap_t;\r
+\r
+void siu_mem_heap_init(siuMemHeap_t *ptr, tword *heapBase, tulong size)\r
+{\r
+  ptr->heapBase = heapBase;\r
+  ptr->size = size;\r
+  ptr->indx = 0;\r
+  \r
+  /* Note : memory is not initialized deliberately to expose \r
+            uninitialized memory read problems */\r
+}\r
+\r
+tword *siu_mem_heap_alloc (siuMemHeap_t *ptr, tint size)\r
+{\r
+  tword *alloc = NULL;\r
+   \r
+  if ((ptr->indx + size) <= ptr->size) {\r
+    alloc = &ptr->heapBase[ptr->indx];\r
+    ptr->indx += size;\r
+  }\r
+  return(alloc);\r
+}\r
+\r
+#define SIU_MEM_VOLATILE_HEAP_SIZE 1000\r
+#define SIU_MEM_NONVOLATILE_HEAP_SIZE 1000\r
+\r
+#pragma DATA_SECTION (siuVolatileMemBufs, ".volatileMemBufs");\r
+tword siuVolatileMemBufs[SIU_MEM_VOLATILE_HEAP_SIZE];\r
+\r
+#pragma DATA_SECTION (siuNonVolatileMemBufs, ".nonVolatileMemBufs");\r
+tword siuNonVolatileMemBufs[SIU_MEM_NONVOLATILE_HEAP_SIZE];\r
+\r
+siuMemHeap_t siuMemHeapVolatile;\r
+siuMemHeap_t siuMemHeapNonVolatile;\r
+\r
+void siu_init_heaps(void)\r
+{\r
+  siu_mem_heap_init ( &siuMemHeapVolatile, siuVolatileMemBufs, SIU_MEM_VOLATILE_HEAP_SIZE);\r
+  siu_mem_heap_init ( &siuMemHeapNonVolatile, siuNonVolatileMemBufs, SIU_MEM_NONVOLATILE_HEAP_SIZE);\r
+}\r
+\r
+tword *siu_malloc (tint size, tbool volat)\r
+{\r
+  if (volat == TRUE) /* Scratchpad type */ {\r
+    return ( siu_mem_heap_alloc (&siuMemHeapVolatile, size) );\r
+  }\r
+  else {\r
+    return ( siu_mem_heap_alloc (&siuMemHeapNonVolatile, size) );\r
+  }\r
+}\r
+\r
+static inline void print_header (void)\r
+{\r
+  printf("+-----------+-----------+-----------+-------------+\n");\r
+  printf("| Buffer ID | Alignment |   Size    | Volatility  |\n");\r
+  printf("|           |  (Bytes)  | (Bytes)   |             |\n");\r
+  printf("+-----------+-----------+-----------+-------------+\n");\r
+/*printf("| %9d | %9d | %9d | %11s |\n");*/\r
+}\r
+\r
+static inline void print_buffer (tint ID, tint linAlign, tint sizeMAUs, tbool isVolat)\r
+{\r
+  printf("| %9d | %9d | %9d | %11s |\n", ID, linAlign * (TYP_TWORD_SIZE/8),\r
+        sizeMAUs * (TYP_TWORD_SIZE/8), (isVolat == TRUE)? "scratch" : "non-scratch" );\r
+}\r
+\r
+static inline void print_footer (void)\r
+{\r
+  printf("+-----------+-----------+-----------+-------------+\n");\r
+}\r
+\r
+void siu_alloc_eco_mem(tint nbufs, const ecomemBuffer_t * sizeBufsPtr,\r
+                   ecomemBuffer_t * ecoBufsPtr)\r
+{\r
+  tint      i;\r
+  void     *allocPtr;\r
+  tulong     linAlign, log2align;\r
+  tbool     isVolat;\r
+\r
+  print_header();\r
+  \r
+  for (i = 0; i < nbufs; i++) {\r
+    ecoBufsPtr[i] = sizeBufsPtr[i];\r
+\r
+    isVolat = ecoBufsPtr[i].volat;\r
+\r
+    log2align = sizeBufsPtr[i].log2align;\r
+    linAlign = (tuint) 1 << log2align;\r
+\r
+    print_buffer (i, linAlign, sizeBufsPtr[i].size, isVolat);\r
+           \r
+    if (sizeBufsPtr[i].size == 0) {\r
+      ecoBufsPtr[i].base = NULL;\r
+      continue;\r
+    }\r
+\r
+    if ((allocPtr = (void *) siu_malloc((sizeBufsPtr[i].size + linAlign - 1), isVolat)) == NULL) {\r
+      printf("Not enough heap, exiting\n");\r
+      exit(0);\r
+    }\r
+    ecoBufsPtr[i].base = (void *) align((tulong) allocPtr, linAlign);\r
+  }\r
+  \r
+  print_footer();\r
+}\r
+\r
+void siu_alloc_xdais_mem(tint nbufs, IALG_MemRec *xdaisBufsPtr, tulong *mallocedSizeCounter)\r
+{\r
+  tint      i;\r
+  void     *allocPtr;\r
+  tulong     linAlign;\r
+  tulong      alignmentAdjustedSize;\r
+  tbool    isVolat;\r
+\r
+  print_header();\r
+  \r
+  for (i = 0; i < nbufs; i++) {\r
+\r
+    isVolat = (xdaisBufsPtr[i].attrs == IALG_SCRATCH) ? TRUE : FALSE;\r
+    linAlign = (tint) xdaisBufsPtr[i].alignment;\r
+    \r
+    print_buffer (i, linAlign, xdaisBufsPtr[i].size, isVolat);\r
+\r
+    if (xdaisBufsPtr[i].size == 0) {\r
+      xdaisBufsPtr[i].base = NULL;\r
+      continue;\r
+    }\r
+\r
+    alignmentAdjustedSize = xdaisBufsPtr[i].size + linAlign - 1;\r
+    if ((allocPtr = (void *) siu_malloc(alignmentAdjustedSize, isVolat)\r
+        ) == NULL) {\r
+      printf("Not enough heap, exiting\n");\r
+      exit(0);\r
+    }\r
+    *mallocedSizeCounter += alignmentAdjustedSize;\r
+    xdaisBufsPtr[i].base = (void *) align((tulong) allocPtr, linAlign);\r
+  }\r
+  \r
+  print_footer();  \r
+}\r
+\r
+void siu_dealloc_xdais_mem (tint nbufs, IALG_MemRec *xdaisBufsPtr, tulong *mallocedSizeCounter)\r
+{\r
+  tint i;\r
+  \r
+  for(i = 0; i < nbufs; i++) {\r
+    *mallocedSizeCounter -= (xdaisBufsPtr[i].size + xdaisBufsPtr[i].alignment - 1);\r
+    if (xdaisBufsPtr[i].base) {\r
+    \r
+    /* Note the free below may not have the right pointers as were in malloc because\r
+       the module was fed aligned pointers, it is not possible to reverse and derive\r
+       original unaligned pointer, so don't do free */\r
+      //free(xdaisBufsPtr[i].base);\r
+    }  \r
+  }\r
+}\r
+\r
+/* Nothing past this point */\r