1 /*
2 * Copyright (c) 2012-2013, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== HeapMemMP.xs ========
34 *
35 */
37 var HeapMemMP = null;
38 var SharedRegion = null;
39 var NameServer = null;
40 var Ipc = null;
41 var Cache = null;
42 var GateMP = null;
44 var instCount = 0; /* use to determine if processing last instance */
46 /*
47 * ======== module$use ========
48 * Initialize module values.
49 */
50 function module$use(mod, params)
51 {
52 HeapMemMP = this;
53 NameServer = xdc.useModule('ti.sdo.utils.NameServer');
54 SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');
55 Ipc = xdc.useModule('ti.sdo.ipc.Ipc');
56 GateMP = xdc.useModule('ti.sdo.ipc.GateMP');
57 Cache = xdc.useModule('ti.sysbios.hal.Cache');
58 }
60 /*
61 * ======== module$static$init ========
62 */
63 function module$static$init(mod, params)
64 {
65 mod.nameServer = null;
67 /* initialize the NameServer param to be used now or later */
68 HeapMemMP.nameSrvPrms.maxRuntimeEntries = params.maxRuntimeEntries;
69 HeapMemMP.nameSrvPrms.tableSection = params.tableSection;
70 HeapMemMP.nameSrvPrms.maxNameLen = params.maxNameLen;
72 /*
73 * Get the current number of created static instances of this module.
74 * Note: if user creates a static instance after this point and
75 * expects to use NameServer, this is a problem.
76 */
77 var instCount = this.$instances.length;
79 /* create NameServer here only if no static instances are created */
80 if (instCount == 0) {
81 mod.nameServer = NameServer.create("HeapMemMP",
82 HeapMemMP.nameSrvPrms);
83 }
84 }
86 /*
87 * ======== viewInitBasic ========
88 */
89 function viewInitBasic(view, obj)
90 {
91 var NameServer = xdc.useModule('ti.sdo.utils.NameServer');
92 var SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');
93 var Ipc = xdc.useModule('ti.sdo.ipc.Ipc');
95 view.name = NameServer.getName$view("HeapMemMP",
96 SharedRegion.getSRPtrMeta$view(obj.attrs));
98 view.buf = $addr(obj.buf);
99 view.totalSize = "0x" + obj.bufSize.toString(16);
101 view.objType = Ipc.getObjTypeStr$view(obj.objType);
102 if (view.objType == null) {
103 Program.displayError(view, "objType",
104 "Corrupted state: The value of 'objType' is not " +
105 "one of known types");
106 }
108 view.gate = obj.gate;
109 }
111 /*
112 * ======== viewInitDetailed ========
113 * Initialize the 'Detailed' HeapMemMP instance view.
114 */
115 function viewInitDetailed(view, obj)
116 {
117 var HeapMemMP = xdc.useModule('ti.sdo.ipc.heaps.HeapMemMP');
118 var SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');
120 // first get the Basic view:
121 viewInitBasic(view, obj);
123 /* view.attrs */
124 view.attrs = "0x" + Number(obj.attrs.$addr).toString(16);
126 /* view.cacheEnabled */
127 view.cacheEnabled = obj.cacheEnabled;
129 /* view.totalFreeSize */
131 var totalFreeSize = 0;
132 var largestFreeSize = 0;
134 /* get the free list head; it should always exist even if list is empty */
135 var currElem;
136 var attrs;
138 try {
139 attrs = Program.fetchStruct(HeapMemMP.Attrs$fetchDesc,
140 obj.attrs.$addr, false);
142 currElem = Program.fetchStruct(HeapMemMP.Header$fetchDesc,
143 attrs.head.$addr, false);
144 }
145 catch (e) {
146 view.$status["totalFreeSize"] =
147 "Error: could not access head element of free list: " + e;
149 throw (e);
150 }
152 /*
153 * Traverse free list and add up all of the sizes. This loop condition
154 * ensures that we don't try to access a null element (i.e. it avoids trav-
155 * ersing past the header on an empty list and it ends once the end of a non
156 * empty list is reached).
157 */
158 while (currElem.next != SharedRegion.getSRPtrMeta$view(0)) {
159 // Fetch the next free block (next linked list element)
160 try {
161 var currElem = Program.fetchStruct(
162 HeapMemMP.Header$fetchDesc,
163 SharedRegion.getPtrMeta$view(currElem.next),
164 false);
165 }
166 catch (e) {
167 view.$status["totalFreeSize"] = "Error: could not access" +
168 " next element linked list: " + e;
169 throw (e);
170 }
172 // add up the total free space
173 totalFreeSize += currElem.size;
175 // check for a new max free size
176 if (currElem.size > largestFreeSize) {
177 largestFreeSize = currElem.size;
178 }
179 }
181 view.totalFreeSize = "0x" + Number(totalFreeSize).toString(16);
182 view.largestFreeSize = "0x" + Number(largestFreeSize).toString(16);
184 if (totalFreeSize > obj.bufSize) {
185 view.$status["totalFreeSize"] = "Error: totalFreeSize (" +
186 view.totalFreeSize + ") > totalSize (" + view.totalSize +
187 ")!";
188 }
190 if (totalFreeSize < 0) {
191 view.$status["totalFreeSize"] = "Error: totalFreeSize (" +
192 view.totalFreeSize + ") is negative!";
193 }
195 if (largestFreeSize > obj.bufSize) {
196 view.$status["largestFreeSize"] = "Error: largestFreeSize (" +
197 view.largestFreeSize + ") > totalSize (" + view.totalSize +
198 ")!";
199 }
201 if (largestFreeSize < 0) {
202 view.$status["largestFreeSize"] = "Error: largestFreeSize (" +
203 view.largestFreeSize + ") is negative!";
204 }
205 }
207 /*
208 * ======== viewInitData ========
209 */
210 function viewInitData(view, obj)
211 {
212 var NameServer = xdc.useModule('ti.sdo.utils.NameServer');
214 view.label = Program.getShortName(obj.$label);
215 if (view.label == "") {
216 // split the label to rid of the long full package name that precedes
217 // format will be: "HeapMemMP@0x12345678"
218 var splitStr = String(obj.$label).split(".");
220 view.label = splitStr[splitStr.length - 1];
221 }
222 view.label += "-freeList";
224 if (obj.attrs != 0) {
225 view.elements = getFreeList(obj);
226 }
228 return(view);
229 }
231 /*
232 * ======== getFreeList ========
233 */
234 function getFreeList(obj)
235 {
236 var HeapMemMP = xdc.useModule('ti.sdo.ipc.heaps.HeapMemMP');
237 var SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');
239 try {
240 var attrs = Program.fetchStruct(HeapMemMP.Attrs$fetchDesc,
241 obj.attrs.$addr, false);
242 var header = Program.fetchStruct(HeapMemMP.Header$fetchDesc,
243 attrs.head.$addr, false);
244 }
245 catch (e) {
246 print("Error: Caught exception from fetchStruct: " +
247 e.toString());
248 throw (e);
249 }
252 var freeList = new Array();
254 /* For each block on the free list... */
255 while (true) {
256 /* Break if this is the last block */
257 if (header.next == SharedRegion.getSRPtrMeta$view(0)) {
258 break;
259 }
261 /* Fetch the next block */
262 try {
263 var header = Program.fetchStruct(
264 HeapMemMP.Header$fetchDesc,
265 SharedRegion.getPtrMeta$view(header.next),
266 false);
267 }
268 catch (e) {
269 print("Error: Caught exception from fetchStruct: " +
270 e.toString());
271 throw (e);
272 }
274 /* Add this block to the list */
275 var memBlock = Program.newViewStruct('ti.sdo.ipc.heaps.HeapMemMP',
276 'FreeList');
277 memBlock.size = "0x" + Number(header.size).toString(16);
278 memBlock.address = "0x" + Number(header.$addr).toString(16);
280 freeList[freeList.length] = memBlock;
281 }
283 return (freeList);
284 }