mvLru.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*******************************************************************************
  2. Copyright (C) Marvell International Ltd. and its affiliates
  3. This software file (the "File") is owned and distributed by Marvell
  4. International Ltd. and/or its affiliates ("Marvell") under the following
  5. alternative licensing terms. Once you have made an election to distribute the
  6. File under one of the following license alternatives, please (i) delete this
  7. introductory statement regarding license alternatives, (ii) delete the two
  8. license alternatives that you have not elected to use and (iii) preserve the
  9. Marvell copyright notice above.
  10. ********************************************************************************
  11. Marvell Commercial License Option
  12. If you received this File from Marvell and you have entered into a commercial
  13. license agreement (a "Commercial License") with Marvell, the File is licensed
  14. to you under the terms of the applicable Commercial License.
  15. ********************************************************************************
  16. Marvell GPL License Option
  17. If you received this File from Marvell, you may opt to use, redistribute and/or
  18. modify this File in accordance with the terms and conditions of the General
  19. Public License Version 2, June 1991 (the "GPL License"), a copy of which is
  20. available along with the File in the license.txt file or by writing to the Free
  21. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
  22. on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
  23. THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
  25. DISCLAIMED. The GPL License provides additional details about this warranty
  26. disclaimer.
  27. ********************************************************************************
  28. Marvell BSD License Option
  29. If you received this File from Marvell, you may opt to use, redistribute and/or
  30. modify this File under the following licensing terms.
  31. Redistribution and use in source and binary forms, with or without modification,
  32. are permitted provided that the following conditions are met:
  33. * Redistributions of source code must retain the above copyright notice,
  34. this list of conditions and the following disclaimer.
  35. * Redistributions in binary form must reproduce the above copyright
  36. notice, this list of conditions and the following disclaimer in the
  37. documentation and/or other materials provided with the distribution.
  38. * Neither the name of Marvell nor the names of its contributors may be
  39. used to endorse or promote products derived from this software without
  40. specific prior written permission.
  41. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  42. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  45. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  48. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *******************************************************************************/
  52. #include "mvOs.h"
  53. #include "mvLru.h"
  54. /* LRU Cache support */
  55. /* Init LRU cache database */
  56. MV_LRU_CACHE* mvLruCacheInit(int numOfEntries)
  57. {
  58. int i;
  59. MV_LRU_CACHE* pLruCache;
  60. pLruCache = mvOsMalloc(sizeof(MV_LRU_CACHE));
  61. if(pLruCache == NULL)
  62. {
  63. return NULL;
  64. }
  65. memset(pLruCache, 0, sizeof(MV_LRU_CACHE));
  66. pLruCache->table = mvOsMalloc(numOfEntries*sizeof(MV_LRU_ENTRY));
  67. if(pLruCache->table == NULL)
  68. {
  69. mvOsFree(pLruCache);
  70. return NULL;
  71. }
  72. memset(pLruCache->table, 0, numOfEntries*sizeof(MV_LRU_ENTRY));
  73. pLruCache->tableSize = numOfEntries;
  74. for(i=0; i<numOfEntries; i++)
  75. {
  76. pLruCache->table[i].next = i+1;
  77. pLruCache->table[i].prev = i-1;
  78. }
  79. pLruCache->least = 0;
  80. pLruCache->most = numOfEntries-1;
  81. return pLruCache;
  82. }
  83. void mvLruCacheFinish(MV_LRU_CACHE* pLruCache)
  84. {
  85. mvOsFree(pLruCache->table);
  86. mvOsFree(pLruCache);
  87. }
  88. /* Update LRU cache database after using cache Index */
  89. void mvLruCacheIdxUpdate(MV_LRU_CACHE* pLruHndl, int cacheIdx)
  90. {
  91. int prev, next;
  92. if(cacheIdx == pLruHndl->most)
  93. return;
  94. next = pLruHndl->table[cacheIdx].next;
  95. if(cacheIdx == pLruHndl->least)
  96. {
  97. pLruHndl->least = next;
  98. }
  99. else
  100. {
  101. prev = pLruHndl->table[cacheIdx].prev;
  102. pLruHndl->table[next].prev = prev;
  103. pLruHndl->table[prev].next = next;
  104. }
  105. pLruHndl->table[pLruHndl->most].next = cacheIdx;
  106. pLruHndl->table[cacheIdx].prev = pLruHndl->most;
  107. pLruHndl->most = cacheIdx;
  108. }
  109. /* Delete LRU cache entry */
  110. void mvLruCacheIdxDelete(MV_LRU_CACHE* pLruHndl, int cacheIdx)
  111. {
  112. int prev, next;
  113. if(cacheIdx == pLruHndl->least)
  114. return;
  115. prev = pLruHndl->table[cacheIdx].prev;
  116. if(cacheIdx == pLruHndl->most)
  117. {
  118. pLruHndl->most = prev;
  119. }
  120. else
  121. {
  122. next = pLruHndl->table[cacheIdx].next;
  123. pLruHndl->table[next].prev = prev;
  124. pLruHndl->table[prev].next = next;
  125. }
  126. pLruHndl->table[pLruHndl->least].prev = cacheIdx;
  127. pLruHndl->table[cacheIdx].next = pLruHndl->least;
  128. pLruHndl->least = cacheIdx;
  129. }