1 /*******************************************************************************
2 * Copyright (C) 2018 Cadence Design Systems, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to use this Software with Cadence processor cores only and
7 * not with any other processors and platforms, subject to
8 * the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 ******************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "xa_type_def.h"
26 
27 /* ...debugging facility */
28 #include "xaf-utils-test.h"
29 
30 mem_obj_t g_mem_obj;
31 
mem_malloc(int size,int id)32 void* mem_malloc(int size, int id)
33 {
34     int index;
35     void* heap_ptr = NULL;
36 
37     if(id == XAF_MEM_ID_DEV)
38     {
39         index = g_mem_obj.num_malloc_dev;
40 	if(index >= MEM_NUM_MEM_ALLOC-1)
41 	{
42 	    heap_ptr = NULL;
43 	}
44 	else
45 	{
46 	    heap_ptr = malloc(size);
47             g_mem_obj.num_malloc_dev++;
48             g_mem_obj.mem_dev[index].heap_ptr = heap_ptr;
49             g_mem_obj.mem_dev[index].size = size;
50             g_mem_obj.persi_mem_dev += size;
51 	}
52     }
53     else if(id == XAF_MEM_ID_COMP)
54     {
55         index = g_mem_obj.num_malloc_comp;
56 	if(index >= MEM_NUM_MEM_ALLOC-1)
57 	{
58 	    heap_ptr = NULL;
59 	}
60 	else
61 	{
62 	    heap_ptr = malloc(size);
63             g_mem_obj.num_malloc_comp++;
64             g_mem_obj.mem_comp[index].heap_ptr = heap_ptr;
65             g_mem_obj.mem_comp[index].size = size;
66             g_mem_obj.persi_mem_comp += size;
67         }
68     }
69     return heap_ptr;
70 }
71 
get_heap_ptr_index(void * p_heap,int id)72 int get_heap_ptr_index(void* p_heap, int id)
73 {
74     int idx;
75 
76     idx = -1;
77     if(id == XAF_MEM_ID_DEV)
78     {
79         for(idx = 0; idx < MEM_NUM_MEM_ALLOC; idx++)
80         {
81             if(g_mem_obj.mem_dev[idx].heap_ptr == p_heap)
82                 break;
83         }
84     }
85 
86     else if(id == XAF_MEM_ID_COMP)
87     {
88         for(idx = 0; idx < MEM_NUM_MEM_ALLOC; idx++)
89         {
90             if(g_mem_obj.mem_comp[idx].heap_ptr == p_heap)
91                 break;
92         }
93     }
94     return idx;
95 }
96 
mem_free(void * heap_ptr,int id)97 void mem_free(void * heap_ptr, int id)
98 {
99     int index;
100     int size;
101 
102     index = get_heap_ptr_index(heap_ptr, id);
103 
104     if (index != -1)
105     {
106         if(id == XAF_MEM_ID_DEV)
107         {
108 	    size=g_mem_obj.mem_dev[index].size;
109             g_mem_obj.mem_dev[index].size = 0;
110             g_mem_obj.num_malloc_dev--;
111             free(heap_ptr);
112             g_mem_obj.mem_dev[index].heap_ptr = NULL;
113         }
114         else if(id == XAF_MEM_ID_COMP)
115         {
116 	    size=g_mem_obj.mem_comp[index].size;
117             g_mem_obj.mem_comp[index].size = 0;
118             g_mem_obj.num_malloc_comp--;
119             free(heap_ptr);
120             g_mem_obj.mem_comp[index].heap_ptr = NULL;
121         }
122     }
123     return;
124 }
125 
mem_get_alloc_size(mem_obj_t * pmem_handle,int id)126 int mem_get_alloc_size(mem_obj_t* pmem_handle, int id)
127 {
128     int mem_size = 0;
129     if(id == XAF_MEM_ID_DEV)
130         mem_size =  pmem_handle->persi_mem_dev;
131     else if(id == XAF_MEM_ID_COMP)
132         mem_size = pmem_handle->persi_mem_comp;
133     return mem_size;
134 }
135 
mem_init()136 void* mem_init()
137 {
138     void* ptr;
139     ptr = &g_mem_obj;
140     return ptr;
141 }
142 
mem_exit()143 void mem_exit()
144 {
145     if((g_mem_obj.num_malloc_dev != 0)||(g_mem_obj.num_malloc_comp != 0))
146     {
147         fprintf(stdout,"Memory leaks\n");
148     }
149     return;
150 }
151