comparison src/aci2/mfw/mfw_mfw.c @ 3:93999a60b835

src/aci2, src/condat2: import of g23m/condat source pieces from TCS211
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 26 Sep 2016 00:29:36 +0000
parents
children
comparison
equal deleted inserted replaced
2:c41a534f33c6 3:93999a60b835
1 /*
2 +--------------------------------------------------------------------+
3 | PROJECT: MMI-Framework (8417) $Workfile:: mfw_mfw.c $|
4 | $Author:: Es $ CONDAT GmbH $Revision:: 11 $|
5 | CREATED: 21.09.98 $Modtime:: 23.03.00 10:47 $|
6 | STATE : code |
7 +--------------------------------------------------------------------+
8
9 MODULE : MFW_MFW
10
11 PURPOSE : element chaining functions
12
13 EXPORT :
14
15 TO DO :
16
17 $History:: mfw_mfw.c $
18
19 Nov 05, 2005 REF: OMAPS00049571 Sumanth Kumar. C
20 Description: eZi Text function is not working
21 Solution: current_mfw_elem variable is updated when any of the elements
22 such as window, keyboard, timer, etc., are deleted so that the
23 current_mfw_element does not point to element that has already been freed.
24
25 Feb 28, 2004 REF: CRR 25608 Deepa M.D
26 The mfwEorc is made const.The changes will be present both in Color and Golite build .
27
28 Jan 27, 2005 REF: CRR MMI-SPR-28300 xnkulkar
29 Description: BMI: memory leak in mfwFree/mfwAlloc ??
30 Solution: Trace output to print memory address and size freed
31 modified in function mfwFree()
32
33 Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
34 Description: MFW memory size variable changed from U16 -> U32
35 Solution: The use of variable U16 has been replaced with U32
36
37 Oct 06, 2004 REF: CRR 25608 Deepa M.D
38 Bug:Change "static MfwHdr mfwEorc =" to const
39 Fix: MfwHdr mfwEorc is made const to save memory.
40 *
41 * ***************** Version 11 *****************
42 * User: Es Date: 23.03.00 Time: 14:43
43 * Updated in $/GSM/Condat/MS/SRC/MFW
44 * Added 'mfwParent(elem)'
45 *
46 * ***************** Version 10 *****************
47 * User: Es Date: 18.02.00 Time: 15:45
48 * Updated in $/GSM/Condat/MS/SRC/MFW
49 * fixed alloc/free bug (shrink)
50 * changed MfwMemLarge macro
51 * added constants for MMI event handler return values
52 *
53 * ***************** Version 9 *****************
54 * User: Kk Date: 10.01.00 Time: 14:03
55 * Updated in $/GSM/Condat/MS/SRC/MFW
56 *
57 * ***************** Version 4 *****************
58 * User: Rm Date: 12/22/99 Time: 4:34p
59 * Updated in $/GSM/Condat/SND-MMI/MFW
60 *
61 * ***************** Version 3 *****************
62 * User: Be Date: 29.11.99 Time: 14:31
63 * Updated in $/GSM/Condat/SND-MMI/MFW
64 * target integration
65 *
66 * ***************** Version 2 *****************
67 * User: Es Date: 22.11.99 Time: 10:24
68 * Updated in $/GSM/Condat/SND-MMI/MFW
69 *
70 * ***************** Version 1 *****************
71 * User: Es Date: 18.11.99 Time: 16:35
72 * Created in $/GSM/Condat/SND-MMI/MFW
73 * Initial
74 *
75 * ***************** Version 6 *****************
76 * User: Es Date: 1.04.99 Time: 17:07
77 * Updated in $/GSM/DEV/MS/SRC/MFW
78 * removed lots of traces
79 *
80 * ***************** Version 5 *****************
81 * User: Es Date: 18.02.99 Time: 17:01
82 * Updated in $/GSM/DEV/MS/SRC/MFW
83 *
84 * ***************** Version 4 *****************
85 * User: Es Date: 17.02.99 Time: 19:11
86 * Updated in $/GSM/DEV/MS/SRC/MFW
87 *
88 * ***************** Version 3 *****************
89 * User: Es Date: 27.01.99 Time: 15:06
90 * Updated in $/GSM/DEV/MS/SRC/MFW
91 *
92 * ***************** Version 2 *****************
93 * User: Es Date: 23.12.98 Time: 16:19
94 * Updated in $/GSM/DEV/MS/SRC/MFW
95 */
96 #define ENTITY_MFW
97
98 #define TRACE_MEMORY
99
100 #include <stdio.h>
101 #include <string.h>
102 #include <stdlib.h>
103
104 #if defined (NEW_FRAME)
105
106 #include "typedefs.h"
107 #include "vsi.h"
108 #include "pei.h"
109 #include "custom.h"
110 #include "gsm.h"
111
112 #else
113
114 #include "STDDEFS.H"
115 #include "custom.h"
116 #include "gsm.h"
117 #include "vsi.h"
118
119 #endif
120
121 //#include <stdio.h>
122 //#include <string.h>
123
124 #include "mfw_mfw.h"
125 #include "mfw_sys.h"
126 #include "mfw_win.h"
127 #include "mfw_sat.h" /* CQ16435 : Header Required */
128
129 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
130 // Description: MFW memory size variable changed from U16 -> U32
131 // Solution: The use of variable U16 has been replaced with U32
132 U32 mfwMemDynTotal = 0; /* sample memory usage */
133 U32 mfwMemLowWater = 0xffffffff; /* sample memory usage */
134
135 MfwHdr *mfwFocus = 0; /* the focus node */
136 MfwHdr *mfwRoot = 0; /* root of MFW elements */
137 MfwCb mfwCommand [MfwTypMax]; /* MFW commands to modules */
138
139 static MfwHdr *mfwSatIdleWin = 0; /* CQ16435 : When this window gets focus, SAT will be informed */
140
141 static MfwMemHdr mfwMem; /* root of free memory list */
142 static U8 *mfwMemBase; /* mfw dynamic memory pool */
143 static U8 *mfwMemEnd; /* end of mfw memory */
144
145 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
146 // Description: MFW memory size variable changed from U16 -> U32
147 // Solution: The use of variable U16 has been replaced with U32
148 static U32 mfwMemLeft; /* space left in pool */
149
150 //Feb 28, 2005 REF: CRR 25608 Deepa M.D
151 //The mfwEorc is made const.The changes will be present both in Color and Golite build
152 static const MfwHdr mfwEorc = {0, MfwTypMax, 0}; /* end of root chain */
153
154
155 /***************************Go-lite Optimization changes end***********************/
156
157 U8 mfwSignallingMethod = 0; /* default is focus+root */
158
159 EXTERN MfwHdr * current_mfw_elem;
160
161 #define SENTINAL_CHECK 1
162
163 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
164 // Description: MFW memory size variable changed from U16 -> U32
165 // Solution: define SENTINEL_SIZE to a value of 4
166 #define SENTINEL_SIZE 4 //Will, Dec/07/2004-->Ti is 4 bytes
167 /*
168 +--------------------------------------------------------------------+
169 | PROJECT : MMI-Framework (8417) MODULE : MFW |
170 | STATE : code ROUTINE : mfwInit |
171 +--------------------------------------------------------------------+
172
173 PURPOSE : initialize framework
174
175 */
176
177 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
178 // Description: MFW memory size variable changed from U16 -> U32
179 // Solution: The use of variable U16 has been replaced with U32
180 MfwRes mfwInit (void *externalRoot, U32 dynMemSize)
181 {
182 int i;
183
184 mfwSetFocus(0); /* initially no focus */
185 dynMemSize &= ~(sizeof(MfwMemHdr)-1); /* align to header size */
186 mfwMemDynTotal = dynMemSize;
187 if (dynMemSize)
188 {
189
190 /***************************Go-lite Optimization changes start***********************/
191 mfwRoot = (MfwHdr *)&mfwEorc; /* points to end of chain */
192
193 /***************************Go-lite Optimization changes end***********************/
194 mfwMemBase = (U8 *) externalRoot; /* setup memory handler */
195 mfwMemEnd = (U8 *) externalRoot + dynMemSize;
196 mfwMem.next = 0;
197 mfwMem.len = 0;
198 ((MfwMemHdr *) mfwMemBase)->next = dynMemSize;
199 ((MfwMemHdr *) mfwMemBase)->len = dynMemSize;
200 mfwMemLeft = dynMemSize;
201 memset(mfwMemBase+sizeof(MfwMemHdr),0,
202 dynMemSize-sizeof(MfwMemHdr));
203 }
204 else
205 {
206 mfwRoot = (MfwHdr *) externalRoot; /* setup root pointer */
207
208 /***************************Go-lite Optimization changes start***********************/
209 if (!mfwRoot)
210 mfwRoot = (MfwHdr *)&mfwEorc; /* points to end of chain */
211
212 /***************************Go-lite Optimization changes end***********************/
213 mfwMemBase = 0;
214 mfwMemEnd = 0;
215 mfwMem.next = 0;
216 mfwMem.len = 0;
217 }
218 for (i = 0; i < MfwTypMax; i++) /* init command table */
219 mfwCommand[i] = 0;
220
221 return MfwResOk;
222 }
223
224
225 /*
226 +--------------------------------------------------------------------+
227 | PROJECT : MMI-Framework (8417) MODULE : MFW |
228 | STATE : code ROUTINE : mfwExit |
229 +--------------------------------------------------------------------+
230
231 PURPOSE : finalize framework
232
233 */
234
235 void mfwExit (void)
236 {
237 TRACE_FUNCTION("mfwExit()");
238 }
239
240 /*
241 +--------------------------------------------------------------------+
242 | PROJECT : MMI-Framework (8417) MODULE : MFW |
243 | STATE : code ROUTINE : mfwSignallingMethod|
244 +--------------------------------------------------------------------+
245
246 PURPOSE : Defines the signalling method
247
248 */
249
250 MfwRes mfwSetSignallingMethod (U8 method)
251 {
252 mfwSignallingMethod = method;
253 }
254
255 /*
256 +--------------------------------------------------------------------+
257 | PROJECT : MMI-Framework (8417) MODULE : MFW |
258 | STATE : code ROUTINE : mfwSetFocus |
259 +--------------------------------------------------------------------+
260
261 PURPOSE : Assign Focus to the passed window
262
263 */
264
265 void mfwSetFocus(MfwHdr *w)
266 {
267 if (satEvtRegistered(SatEvtIdleScreenActivated) == TRUE)
268 {
269 if ((mfwSatIdleWin != 0) && (mfwSatIdleWin == w))
270 {
271 TRACE_EVENT("NDH >>> CQ16435 : Send the Idle Screen Available Event to the SIM");
272 satEvtDownload(SatEvtIdleScreen);
273 }
274 }
275
276 mfwFocus = w;
277 return;
278 }
279
280 /*
281 +--------------------------------------------------------------------+
282 | PROJECT : MMI-Framework (8417) MODULE : MFW |
283 | STATE : code ROUTINE : mfwSetSatIdle |
284 +--------------------------------------------------------------------+
285
286 PURPOSE : Set the window which will trigger SAT Event Idle Activated if required
287
288 */
289 void mfwSetSatIdle(MfwHdr *w)
290 {
291 MfwWin *win;
292
293 if (!w)
294 {
295 mfwSatIdleWin = 0;
296 return;
297 }
298
299 win = w->data;
300
301 mfwSatIdleWin = win->elems;
302 return;
303 }
304
305 /*
306 +--------------------------------------------------------------------+
307 | PROJECT : MMI-Framework (8417) MODULE : MFW |
308 | STATE : code ROUTINE : mfwInsert |
309 +--------------------------------------------------------------------+
310
311 PURPOSE : chain up framework element
312
313 */
314
315 MfwHdr *mfwInsert (MfwHdr *h, MfwHdr *e)
316 {
317 if (!e) /* nothing to insert */
318 return 0;
319
320 if (!h) /* use root chain */
321 {
322 e->next = mfwRoot;
323 mfwRoot = e;
324 }
325 else if (h->type == MfwTypWin) /* use window chain */
326 {
327 e->next = ((MfwWin *) (h->data))->elems;
328 ((MfwWin *) h->data)->elems = e;
329 if (mfwFocus == e->next) /* was focus window ? */
330 mfwSetFocus(e); /* yes: reassign focus */
331 }
332 else /* insert into any chain */
333 {
334 return 0;
335 }
336
337 return e;
338 }
339
340
341 /*
342 +--------------------------------------------------------------------+
343 | PROJECT : MMI-Framework (8417) MODULE : MFW |
344 | STATE : code ROUTINE : mfwAppend |
345 +--------------------------------------------------------------------+
346
347 PURPOSE : chain up framework element at end of chain
348
349 */
350
351 MfwHdr *mfwAppend (MfwHdr **h, MfwHdr *e)
352 {
353 if (!e) /* nothing to append */
354 return 0;
355
356 if (!h) /* append to header chain */
357 h = &mfwRoot;
358
359 if ((*h)->type == MfwTypMax) /* empty chain */
360 {
361 if (mfwFocus == *h)
362 mfwSetFocus(e);
363 e->next = *h;
364 *h = e;
365 return e;
366 }
367
368 while ((*h)->next && (*h)->next->type != MfwTypMax)
369 h = &((*h)->next);
370
371 e->next = (*h)->next;
372 (*h)->next = e;
373
374 return e;
375 }
376
377
378 /*
379 +--------------------------------------------------------------------+
380 | PROJECT : MMI-Framework (8417) MODULE : MFW |
381 | STATE : code ROUTINE : mfwRemove |
382 +--------------------------------------------------------------------+
383
384 PURPOSE : unchain element and return its root
385
386 */
387
388 MfwHdr **mfwRemove (MfwHdr *e)
389 {
390 MfwHdr **rp, **rpr, *h;
391
392 if (!e) /* nothing to remove */
393 return 0;
394
395 h = e; /* find elements root */
396 while (h->next && h->type != MfwTypMax)
397 h = h->next;
398 if (h->type != MfwTypMax)
399 return 0;
400 h = h->next; /* h points to root element */
401 if (!h) /* remove from header chain */
402 rp = &mfwRoot;
403 else if (h->type == MfwTypWin) /* remove from window chain */
404 rp = &(((MfwWin*)(h->data))->elems);
405 else /* don't remove from normal */
406 return 0;
407
408 rpr = rp;
409 while (*rp && (*rp)->type != MfwTypMax)
410 {
411 if (*rp == e)
412 {
413 *rp = e->next;
414 if (mfwFocus == e) /* first element of window */
415 mfwSetFocus(e->next);
416 /* Nov 05, 2005 REF: OMAPS00049571 Sumanth Kumar. C
417 * Solution: current_mfw_elem variable is updated when any of the elements
418 * such as window, keyboard, timer, etc., are deleted so that the
419 * current_mfw_element does not point to element that has already been freed.
420 */
421 if(e->type == MfwTypWin || e->type == MfwTypKbd ||e->type == MfwTypTim ||
422 e->type == MfwTypEdt || e->type == MfwTypMnu || e->type == MfwTypIcn)
423 current_mfw_elem = e->next;
424 return rpr;
425 }
426 rp = &((*rp)->next);
427 }
428
429 return 0;
430 }
431
432
433 /*
434 +--------------------------------------------------------------------+
435 | PROJECT : MMI-Framework (8417) MODULE : MFW |
436 | STATE : code ROUTINE : mfwParent |
437 +--------------------------------------------------------------------+
438
439 PURPOSE : get parent of given element
440
441 */
442
443 MfwHnd mfwParent (MfwHdr *h)
444 {
445 if (!h) /* no element */
446 return 0;
447
448 while (h->next && h->type != MfwTypMax)
449 {
450 h = h->next;
451 if(!h)
452 {
453 return 0;
454 }
455 }
456 if (h->type != MfwTypMax)
457 return 0; /* ERROR ! */
458 h = h->next; /* h points to root element */
459 if (h && h->type == MfwTypWin) /* parent should be window */
460 return h;
461
462 return 0; /* root element */
463 }
464
465 /*
466 +--------------------------------------------------------------------+
467 | PROJECT : MMI-Framework (8417) MODULE : MFW |
468 | STATE : code ROUTINE : mfwHeader |
469 +--------------------------------------------------------------------+
470
471 PURPOSE : get header of a given element
472
473 */
474
475 MfwHnd mfwHeader (void)
476 {
477 return current_mfw_elem;
478 }
479
480
481 /*
482 +--------------------------------------------------------------------+
483 | PROJECT : MMI-Framework (8417) MODULE : MFW |
484 | STATE : code ROUTINE : mfwControl |
485 +--------------------------------------------------------------------+
486
487 PURPOSE : retrieve element control block
488
489 */
490
491 MfwHnd mfwControl (MfwHdr *h)
492 {
493 if (!h)
494 return 0;
495 if (h->type <= MfwTypNone || h->type >= MfwTypMax)
496 return 0;
497
498 return h->data;
499 }
500
501
502 /*
503 +--------------------------------------------------------------------+
504 | PROJECT : MMI-Framework (8417) MODULE : MFW |
505 | STATE : code ROUTINE : mfwAlloc |
506 +--------------------------------------------------------------------+
507
508 PURPOSE : alloc MFW memory
509
510 */
511
512 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
513 // Description: MFW memory size variable changed from U16 -> U32
514 // Solution: The use of variable U16 has been replaced with U32
515 U8 *mfwAlloc (U32 size)
516 {
517 MfwMemHdr *p, *n, *l;
518 U8 *data_ptr;
519
520 if (!size)
521 {
522 TRACE_ERROR("mfwAlloc(): invalid memory request");
523 return 0;
524 }
525
526 #ifdef SENTINAL_CHECK
527 if (!mfwMemBase)
528 {
529 size += (SENTINEL_SIZE*2);
530 data_ptr = (U8 *) sysAlloc(size);
531 memset(data_ptr, 0x55, SENTINEL_SIZE);
532 memset(data_ptr + (size-SENTINEL_SIZE), 0x55, SENTINEL_SIZE);
533 data_ptr += SENTINEL_SIZE;
534 return data_ptr;
535 }
536
537 size = (U32) mfwMemLarge(size) + (SENTINEL_SIZE*2);
538
539 if (size >= mfwMemLeft)
540 {
541 TRACE_ERROR("mfwAlloc(): no memory");
542 return 0;
543 }
544
545 if (mfwMemBase + mfwMem.next > mfwMemEnd
546 || mfwMemBase + mfwMem.next < mfwMemBase)
547 {
548 TRACE_ERROR("mfwAlloc(): corrupted memory");
549 return 0;
550 }
551
552 p = &mfwMem; /* root of dyn memory */
553 n = (MfwMemHdr *) (mfwMemBase + p->next); /* first free block */
554 while ((U8 *) n < mfwMemEnd)
555 {
556 if (n->len == size) /* fits exactly: */
557 {
558 p->next = n->next; /* just remove from chain */
559
560 mfwMemLeft -= size;
561 if (mfwMemLeft < mfwMemLowWater)
562 mfwMemLowWater = mfwMemLeft;
563
564 data_ptr = (U8 *)n;
565
566 memset(data_ptr, 0x55, SENTINEL_SIZE);
567 memset(data_ptr + size-SENTINEL_SIZE, 0x55, SENTINEL_SIZE);
568 memset(data_ptr + SENTINEL_SIZE, 0, size-(SENTINEL_SIZE*2));
569
570 data_ptr += SENTINEL_SIZE;
571
572 #ifdef TRACE_MEMORY
573 TRACE_EVENT_P2("MFWAlloc %d bytes in address %p",size,n);
574 mfwCheckMemoryLeft();
575 #endif
576 return data_ptr;
577 }
578
579 if (n->len > size) /* take it from a big one */
580 {
581 l = (MfwMemHdr *) ((U8 *) n + size); /* new header */
582 l->next = n->next; /* setup chain to next */
583 l->len = (U32) (n->len - size); /* remaining memory */
584 p->next += size; /* link with previous block */
585
586 mfwMemLeft -= size;
587 if (mfwMemLeft < mfwMemLowWater)
588 mfwMemLowWater = mfwMemLeft;
589
590 data_ptr = (U8 *)n;
591
592 memset(data_ptr, 0x55, SENTINEL_SIZE);
593 memset(data_ptr + size-SENTINEL_SIZE, 0x55, SENTINEL_SIZE);
594 memset(data_ptr + SENTINEL_SIZE, 0, size-(SENTINEL_SIZE*2));
595
596 data_ptr += SENTINEL_SIZE;
597
598 #ifdef TRACE_MEMORY
599 TRACE_EVENT_P2("MFWAlloc %d bytes in address %p",size,n);
600 mfwCheckMemoryLeft();
601 #endif
602 return data_ptr; /* allocated me address */
603 }
604 p = n;
605 n = (MfwMemHdr *) (mfwMemBase + n->next);
606 }
607 #else
608 if (!mfwMemBase)
609 return (U8 *) sysAlloc(size);
610
611 size = (U32) mfwMemLarge(size);
612 if (size >= mfwMemLeft)
613 {
614 TRACE_ERROR("mfwAlloc(): no memory");
615 return 0;
616 }
617
618 if (mfwMemBase + mfwMem.next > mfwMemEnd
619 || mfwMemBase + mfwMem.next < mfwMemBase)
620 {
621 TRACE_ERROR("mfwAlloc(): corrupted memory");
622 return 0;
623 }
624
625 p = &mfwMem; /* root of dyn memory */
626 n = (MfwMemHdr *) (mfwMemBase + p->next); /* first free block */
627 while ((U8 *) n < mfwMemEnd)
628 {
629 if (n->len == size) /* fits exactly: */
630 {
631 p->next = n->next; /* just remove from chain */
632 mfwMemLeft -= size;
633 if (mfwMemLeft < mfwMemLowWater)
634 mfwMemLowWater = mfwMemLeft;
635 memset(n,0,size);
636 #ifdef TRACE_MEMORY
637 TRACE_EVENT_P2("MFWAlloc %d bytes in address %p",size,n);
638 mfwCheckMemoryLeft();
639 #endif
640 return (U8 *) n;
641 }
642 if (n->len > size) /* take it from a big one */
643 {
644 l = (MfwMemHdr *) ((U8 *) n + size); /* new header */
645 l->next = n->next; /* setup chain to next */
646 l->len = (U32) (n->len - size); /* remaining memory */
647 p->next += size; /* link with previous block */
648 mfwMemLeft -= size;
649 if (mfwMemLeft < mfwMemLowWater)
650 mfwMemLowWater = mfwMemLeft;
651 memset(n,0,size);
652 #ifdef TRACE_MEMORY
653 TRACE_EVENT_P2("MFWAlloc %d bytes in address %p",size,n);
654 mfwCheckMemoryLeft();
655 #endif
656 return (U8 *) n; /* allocated me address */
657 }
658 p = n;
659 n = (MfwMemHdr *) (mfwMemBase + n->next);
660 }
661 #endif
662
663 if ((U8 *)n == mfwMemEnd)
664 {
665 TRACE_ERROR("mfwAlloc(): no memory block big enough to allocate size requested");
666 }
667 else
668 {
669 TRACE_ERROR("mfwAlloc(): Error: free list corruption is likely");
670 }
671
672 return 0;
673 }
674
675
676 /*
677 +--------------------------------------------------------------------+
678 | PROJECT : MMI-Framework (8417) MODULE : MFW |
679 | STATE : code ROUTINE : mfwFree |
680 +--------------------------------------------------------------------+
681
682 PURPOSE : free allocated MFW memory
683
684 */
685
686 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
687 // Description: MFW memory size variable changed from U16 -> U32
688 // Solution: The use of variable U16 has been replaced with U32
689 void mfwFree (U8 *mem, U32 size)
690 {
691 MfwMemHdr *p, *n;
692 U8 check_val[SENTINEL_SIZE];
693 // U32 check_val = 0x55555555;
694
695 memset(&check_val, 0x55, SENTINEL_SIZE);
696
697 #ifdef SENTINAL_CHECK
698 mem -= SENTINEL_SIZE;
699 #endif
700
701 if (!size || !mem )
702 {
703 TRACE_ERROR("mfwFree(): invalid");
704 return; /* nothing to free */
705 }
706
707 #ifdef SENTINAL_CHECK
708 /*
709 ** Check the 4 preceeding bytes to ensure they haven't been changed
710 */
711 if (memcmp(mem, &check_val, SENTINEL_SIZE) != 0)
712 {
713 TRACE_ERROR("mfwFree(): Error: Preceeding Sentinal Bytes have been corrupted");
714 }
715 #endif
716
717 #ifdef TRACE_MEMORY
718 // Jan 27, 2005 REF: CRR MMI-SPR-28300 xnkulkar
719 // Description: BMI: memory leak in mfwFree/mfwAlloc ??
720 // Solution: Trace output to print memory address and size freed modified
721 TRACE_EVENT_P2("MFWFree address = %p, size : %d bytes",mem,size);
722 #endif
723
724 if (!mfwMemBase)
725 {
726 void *m = mem;
727
728 #ifdef SENTINAL_CHECK
729 /*
730 ** Check the SENTINEL_SIZE last bytes to ensure they haven't been changed
731 */
732 if (memcmp(mem + size + SENTINEL_SIZE, &check_val, SENTINEL_SIZE) != 0)
733 {
734 TRACE_ERROR("mfwFree(): Error: Following Sentinal Bytes have been corrupted");
735 }
736 #endif
737 sysFree(m);
738 return;
739 }
740
741 #ifdef SENTINAL_CHECK
742
743 size = (U32) mfwMemLarge(size) + (SENTINEL_SIZE*2);
744 #else
745 size = (U32) mfwMemLarge(size);
746 #endif
747
748 if ( mem < mfwMemBase || mem >= mfwMemEnd ||
749 mem + size > mfwMemEnd ||
750 mem + size <= mfwMemBase)
751 {
752 TRACE_ERROR("mfwFree(): invalid");
753 return; /* nothing to free */
754 }
755
756 p = &mfwMem; /* root of dyn memory */
757 n = (MfwMemHdr *) (mfwMemBase + p->next); /* first free block */
758
759 /*
760 ** Skip through the Free Link List until we get to where the current pointer
761 ** should be added
762 */
763 while ((U8 *) n < mfwMemEnd && (U8 *) n < mem)
764 { /* search allocated area */
765 p = n;
766 n = (MfwMemHdr *) (mfwMemBase + n->next);
767 }
768
769 /*
770 ** Check that the select memory isn't already free
771 */
772 if (mem == (U8 *) p || mem == (U8 *) n)
773 {
774 TRACE_ERROR("mfwFree(): already free");
775 return; /* already free */
776 }
777
778 /*
779 ** Memory not already free
780 */
781
782 #ifdef SENTINAL_CHECK
783 /*
784 ** Check the 4 last bytes to ensure they haven't been changed
785 */
786 if (memcmp(mem + size - SENTINEL_SIZE, &check_val, SENTINEL_SIZE) != 0)
787 {
788 TRACE_ERROR("mfwFree(): Error: Following Sentinal Bytes have been corrupted");
789 }
790 #endif
791
792 /*
793 ** This memset should only be performed after we are sure that the memory should be freed
794 */
795 memset(mem, 0, size);
796
797 if (p != &mfwMem && (U8 *) p + p->len == mem)
798 { /* adjacent to left free: */
799 p->len += size; /* just add it */
800 }
801 else
802 {
803 p->next = (U32) (mem - mfwMemBase); /* new free link */
804 p = (MfwMemHdr *) mem; /* to new header */
805 p->next = (U32) ((U8 *) n - mfwMemBase); /* link to next */
806 p->len = size;
807 }
808
809 if ((U8 *) n < mfwMemEnd && ((mem + size) == (U8 *) n))
810 { /* adjacent to right free: */
811 p->next = n->next; /* eliminate link and */
812 p->len += n->len; /* eat up the space */
813 }
814
815 mfwMemLeft += size;
816 #ifdef TRACE_MEMORY
817 mfwCheckMemoryLeft();
818 #endif
819 }
820
821 // Dec 22, 2004 REF: CRR MFW-SPR-27847 xnkulkar
822 // Description: MFW memory size variable changed from U16 -> U32
823 // Solution: The use of variable U16 has been replaced with U32
824 U32 mfwCheckMemoryLeft(void)
825 {
826 TRACE_EVENT_P1("memory left %ld",mfwMemLeft);
827
828 return mfwMemLeft;
829 }
830
831 /*
832 ** This function gives the bounds safety of strncpy, but NULL terminates the string like strcpy
833 **
834 ** parameters : cpyto : the pointer to the destination string
835 ** : cpyfrm : the pointer to the source string
836 ** : len : the maximum length of the destination string including the NULL terminator
837 **
838 ** returns : A character pointer to the destination string if successful, otherwise NULL
839 **
840 ** CQ16507 : strcpy call for string without null termination causing hardware reset
841 */
842
843 char *mfwStrncpy(char *cpyto, const char *cpyfrm, size_t len)
844 {
845
846 if (len <= 0)
847 return (void *)0;
848
849 if (cpyto == (char *)0)
850 return (void *)0;
851
852 strncpy(cpyto, cpyfrm, len-1);
853
854 *(cpyto + (len -1)) = 0x00;
855
856 return cpyto;
857 }
858