DSMCC  17.9.0
 All Data Structures Files Functions Typedefs
cacheMgr.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2001 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
25 /*---includes for this file--------------------------------------------------*/
26 #include "clDsmSystem.h"
27 #include "cacheMgr.h"
28 
29 #include "linkList.h"
30 #include "loadMgr.h"
31 #include "module.h"
32 
33 
34 /*------------------------------- Local Macros -------------------------------*/
35 
36 
37 
38 /*------------------------------ Exported Data -----------------------------*/
39 
40 
41 
42 /*--------------------------------Local Types --------------------------------*/
43 
44 
45 
46 /*------------------------------- Local Statics ------------------------------*/
47 
48 /*------------------- local prototypes/forward declarations ------------------*/
49 
50 
51 /*---------------------------- Exported Functions ----------------------------*/
52 
53 BOOLEAN DSC_CmMemPurgeCache( P_DsmCoreInst idp, U32BIT maxMemory )
54 {
55  P_Module pModule;
56 
57  do
58  {
59  /* -- Find lowest priority cached module to delete and free up some
60  -- heap space.
61  -- NB. Can only delete CACHED modules in priority list to avoid
62  -- error condition of this memGet trying to create space for
63  -- a load or update of the module being deleted (can only
64  -- occur when turboCaching enabled) */
65 
66  pModule = (P_Module)LLTail( idp->llcModulePriority );
67 
68  while (pModule != NULL)
69  {
70  if (pModule->status == MS_CACHED && pModule->currentUseCount == 0)
71  {
72  break;
73  }
74  pModule = LLPrev( pModule, MODULE_PRIORITY_LIST );
75  }
76  if (pModule == NULL)
77  {
78  dsmDP1(("ERROR: memMgr heap full\n"));
79  return FALSE;
80  }
81 
82  /* -- NB. Module should not be loaded */
83  dsmAssert((pModule->loadedCount == 0));
84 
85  /* -- NB. Module should not have any loadRequests */
86  dsmAssert((pModule->llcLoadRequests == NULL));
87 
88  dsmDP2(("INFO: ModuleId %u deleted to free cache space\n", pModule->moduleInfo.moduleId));
89 
90  /* -- Notify the first time the cache fills up (on any
91  -- given service) */
92  if (idp->cacheFull == FALSE)
93  {
94  dsmDP3(("INFO: *** Cache full on current service ***\n"));
95  idp->cacheFull = TRUE;
96  if (idp->setup.progressFunc)
97  {
98  idp->setup.progressFunc( CLDSM_PROG_CURR_SERVICE_CACHE_FULL, /*args*/ NULL );
99  }
100  }
101  DSC_ModuleDeleteDcTidyUp( idp, pModule );
102  }
103  while (idp->cacheMemoryUsage > maxMemory);
104  return TRUE;
105 }
106 
107 /* /////////////////////////////////////////////////////////////////////////////
108 // DSC_CmMemGet
109 //
110 // Tries to allocate size bytes of memory from memMgr. If memMgr heap is full
111 // it will delete 'old' modules from the cache until is successful (or there
112 // are no more modules it can delete).
113 //
114 // If memory cannot be allocated, it sets returned memory handle to NULL and
115 // returns error.
116 //
118 MemHandle DSC_CmMemAlloc( P_DsmCoreInst idp, U32BIT size, int line )
119 {
120  MemHandle memptr;
121 
122  dsmDP4(("DSC_CmMemGet()\n"));
123  dsmAssert((idp != NULL));
124  dsmAssert((size != 0));
125 
126  if (size > idp->setup.maxMemorySize)
127  {
128  ERRPRINT("ERROR: memMgr size too large %d", size)
129  memptr = NULL;
130  }
131  else if (idp->cacheMemoryUsage + size > idp->setup.maxMemorySize &&
132  !DSC_CmMemPurgeCache( idp, idp->setup.maxMemorySize - size ))
133  {
134  ERRPRINT("ERROR: unable to purge, to alloc size %d", size)
135  memptr = NULL;
136  }
137  else
138  {
139  do
140  {
141  memptr = memAlloc( MEM_CONTEXT, &idp->setup, size, line );
142  if (memptr)
143  {
144  idp->cacheMemoryUsage += size;
145  if (idp->cacheMaximumUsage < idp->cacheMemoryUsage)
146  {
147  idp->cacheMaximumUsage = idp->cacheMemoryUsage;
148  dsmDP3(("INFO: Maximum Memory Usage is %u\n", idp->cacheMaximumUsage));
149  }
150  break;
151  }
152  }
153  while (DSC_CmMemPurgeCache( idp, idp->setup.maxMemorySize ));
154  }
155  return memptr;
156 }
157 
158 /* /////////////////////////////////////////////////////////////////////////////
159 // DSC_CmMemRelease
160 //
162 void DSC_CmMemRelease( P_DsmCoreInst idp, MemHandle hMemArea )
163 {
164  dsmDP4(("DSC_CmMemRelease()\n"));
165  dsmAssert((idp != NULL));
166  dsmAssert((hMemArea != NULL));
167 
168  idp->cacheMemoryUsage -= memRelease( MEM_CONTEXT, &idp->setup, hMemArea );
169 
170  dsmDP4(("exit DSC_CmMemRelease\n"));
171 }
172 
173 /*------------------------------ Local Functions -----------------------------*/
174 
175 
176 /*----------------------------------------------------------------------------*/
General include file for clDsm library internal definitions.
Header to the loadMgr module.
Header to the cacheMgr module.
void DSC_ModuleDeleteDcTidyUp(P_DsmCoreInst idp, P_Module pModule)
Delete module and destroy parent DC if now empty.
Definition: module.c:388
Header to the 'module' module - Functions/methods for creating/destroying and managing attributes of ...