mempoolcontainer.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:22k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  * mempoolcontainer.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  * MAUI
  44.  *
  45.  * Description:
  46.  * ------------
  47.  * This file implements memry pool realted functions
  48.  *
  49.  * Author:
  50.  * -------
  51.  * -------
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * removed!
  58.  *
  59.  * removed!
  60.  * removed!
  61.  * removed!
  62.  *
  63.  *------------------------------------------------------------------------------
  64.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  65.  *============================================================================
  66.  ****************************************************************************/
  67. /**
  68.  * Copyright Notice
  69.  * ?2002 - 2003, Pixtel Communications, Inc., 1489 43rd Ave. W.,
  70.  * Vancouver, B.C. V6M 4K8 Canada. All Rights Reserved.
  71.  *  (It is illegal to remove this copyright notice from this software or any
  72.  *  portion of it)
  73.  */
  74. /**************************************************************
  75.    FILENAME : MemPoolContainer.c
  76.    PURPOSE     : Container for various memory Pools
  77.    REMARKS     :  Manages the various memory pools and tries to keep a contiguous
  78.                range of pools
  79.    AUTHOR      : Vikram
  80.    DATE     : 
  81. **************************************************************/
  82. /**************************************************************/
  83. /***********  Include: MMI header file **************/
  84. #include "MMI_features.h"
  85. #include "PixtelDataTypes.h"
  86. #include "MemPoolContainerProt.h"
  87. #include "OSLMemory.h"
  88. #include "DebugInitDEf.h"
  89. #ifdef MMI_ON_HARDWARE_P
  90. #include "L4Dr.h"
  91. #endif 
  92. /***************************************************************************** 
  93. * Define
  94. *****************************************************************************/
  95. /***************************************************************************** 
  96. * Typedef 
  97. *****************************************************************************/
  98. /***************************************************************************** 
  99. * Local Variable (Local to this file - static)
  100. *****************************************************************************/
  101. /***************************************************************************** 
  102. * Local Function (local to this file - static)
  103. *****************************************************************************/
  104. /***************************************************************************** 
  105. * Global Variable
  106. *****************************************************************************/
  107. /***************************************************************************** 
  108. * Global Function
  109. *****************************************************************************/
  110. /*****************************************************************************
  111.  * FUNCTION
  112.  *  mmi_mem_create_container
  113.  * DESCRIPTION
  114.  *  Create memory container to use
  115.  * PARAMETERS
  116.  *  num_pool                [IN]        Number of memory pool
  117.  *  unit_per_pool           [IN]        Number of memory unit per pool
  118.  *  buffer                  [IN]        Address point to whole pool
  119.  *  unit_size               [IN]        Size of each memory unit
  120.  *  mem_pool_container      [OUT]       Address point to the container to be initialized
  121.  * RETURNS
  122.  *  void
  123.  *****************************************************************************/
  124. void mmi_mem_create_container(
  125.         S16 num_pool,
  126.         S16 unit_per_pool,
  127.         void *buffer,
  128.         S16 unit_size,
  129.         mem_pool_container_struct *mem_pool_container)
  130. {
  131.     /*----------------------------------------------------------------*/
  132.     /* Local Variables                                                */
  133.     /*----------------------------------------------------------------*/
  134.     S32 i;
  135.     /*----------------------------------------------------------------*/
  136.     /* Code Body                                                      */
  137.     /*----------------------------------------------------------------*/
  138.     /* initialize pool container */
  139.     mem_pool_container->num_pool = num_pool;
  140.     mem_pool_container->unit_per_pool = unit_per_pool;
  141.     mem_pool_container->num_used_pool = 0;
  142.     mem_pool_container->pBuffer = buffer;
  143.     mem_pool_container->unit_size = unit_size;
  144.     /* why +1 ? keep a buffer for memory shifting */
  145.     mem_pool_container->mem_pools = (mem_pool_struct*) OslMalloc(sizeof(mem_pool_struct) * (num_pool + 1));
  146.     for (i = 0; i < num_pool + 1; ++i)
  147.     {
  148.         mem_pool_container->mem_pools[i].nMinId = -1;
  149.         mem_pool_container->mem_pools[i].nMaxId = -1;
  150.         mem_pool_container->mem_pools[i].unit_list = (void **)OslMalloc(sizeof(S8*) * unit_per_pool);
  151.     }
  152. }
  153. /*****************************************************************************
  154.  * FUNCTION
  155.  *  mmi_mem_destroy_container
  156.  * DESCRIPTION
  157.  *  Destory a memory pool container
  158.  * PARAMETERS
  159.  *  mem_pool_container      [IN]        Container to be destroyed
  160.  * RETURNS
  161.  *  void
  162.  *****************************************************************************/
  163. void mmi_mem_destroy_container(mem_pool_container_struct *mem_pool_container)
  164. {
  165.     /*----------------------------------------------------------------*/
  166.     /* Local Variables                                                */
  167.     /*----------------------------------------------------------------*/
  168.     S32 i;
  169.     /*----------------------------------------------------------------*/
  170.     /* Code Body                                                      */
  171.     /*----------------------------------------------------------------*/
  172.     if (mem_pool_container->mem_pools)
  173.     {
  174.         for (i = 0; i < mem_pool_container->num_pool + 1; ++i)
  175.         {
  176.             OslMfree(mem_pool_container->mem_pools[i].unit_list);
  177.         }
  178.         OslMfree(mem_pool_container->mem_pools);
  179.     }
  180. }
  181. /*****************************************************************************
  182.  * FUNCTION
  183.  *  mmi_mem_insert_pool
  184.  * DESCRIPTION
  185.  *  Insert a memory pool into container
  186.  * PARAMETERS
  187.  *  mem_pool_container      [IN]        Container to insert
  188.  *  mem_pool                [IN]        Memory pool to be inserted
  189.  * RETURNS
  190.  *  void
  191.  *****************************************************************************/
  192. void mmi_mem_insert_pool(mem_pool_container_struct *mem_pool_container, mem_pool_struct *mem_pool)
  193. {
  194.     /*----------------------------------------------------------------*/
  195.     /* Local Variables                                                */
  196.     /*----------------------------------------------------------------*/
  197.     S32 nIndex;
  198.     S32 nTemp;
  199.     /*----------------------------------------------------------------*/
  200.     /* Code Body                                                      */
  201.     /*----------------------------------------------------------------*/
  202.     nIndex = mmi_mem_get_free_pool(mem_pool_container, mem_pool);
  203.     mem_pool_container->mem_pools[nIndex].nMaxId = mem_pool->nMaxId;
  204.     mem_pool_container->mem_pools[nIndex].nMinId = mem_pool->nMinId;
  205.     /* copy list of unit address to memory pool */
  206.     memcpy(
  207.         (S8 **) (mem_pool_container->mem_pools[nIndex].unit_list),
  208.         (S8 **) (mem_pool->unit_list),
  209.         (mem_pool_container->unit_per_pool * sizeof(S8 **)));
  210.     if (nIndex > 0)
  211.     {
  212.         /* If the pool returned is the first pool then no need to move the data up. Leave the last pool e28t? */
  213.         if (mem_pool_container->num_used_pool >= mem_pool_container->num_pool)
  214.         {
  215.             nTemp = nIndex - 1;
  216.             if (mem_pool->nMinId > mem_pool_container->mem_pools[0].nMaxId + 1) /* Delete the 0th Data only if u do not require it. Else remove the last one */
  217.             {
  218.                 for (nTemp = 0; nTemp < mem_pool_container->num_pool; ++nTemp)
  219.                 {
  220.                     mem_pool_container->mem_pools[nTemp].nMaxId = mem_pool_container->mem_pools[nTemp + 1].nMaxId;
  221.                     mem_pool_container->mem_pools[nTemp].nMinId = mem_pool_container->mem_pools[nTemp + 1].nMinId;
  222.                     memcpy(
  223.                         (S8 **) (mem_pool_container->mem_pools[nTemp].unit_list),
  224.                         (S8 **) (mem_pool_container->mem_pools[nTemp + 1].unit_list),
  225.                         (mem_pool_container->unit_per_pool * sizeof(S8 **)));
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     if (mem_pool_container->num_used_pool < mem_pool_container->num_pool)
  231.     {
  232.         ++mem_pool_container->num_used_pool;
  233.     }
  234. }
  235. /*****************************************************************************
  236.  * FUNCTION
  237.  *  mmi_mem_delete_pool
  238.  * DESCRIPTION
  239.  *  Delete a memory pool from container
  240.  * PARAMETERS
  241.  *  mem_pool_container      [IN]        Container to delete
  242.  *  nMinId                  [IN]        Min id of memory pool to be deleted
  243.  *  nMaxId                  [IN]        Max id of memory pool to be deleted
  244.  * RETURNS
  245.  *  TRUE means delete success; FALSE means delete failure
  246.  *****************************************************************************/
  247. BOOL mmi_mem_delete_pool(mem_pool_container_struct *mem_pool_container, S32 nMinId, S32 nMaxId)
  248. {
  249.     /*----------------------------------------------------------------*/
  250.     /* Local Variables                                                */
  251.     /*----------------------------------------------------------------*/
  252.     S32 nCount = 0;
  253.     S32 nToCopy;
  254.     /*----------------------------------------------------------------*/
  255.     /* Code Body                                                      */
  256.     /*----------------------------------------------------------------*/
  257.     /* copy/shift memory pool down and insert current pool into best position */
  258. while(nCount < mem_pool_container->num_used_pool)
  259. {
  260. if(mem_pool_container->mem_pools[nCount].nMinId == nMinId && mem_pool_container->mem_pools[nCount].nMaxId == nMaxId)
  261. {
  262. if(nCount != (mem_pool_container->num_used_pool - 1))
  263. {
  264. nToCopy = nCount + 1;
  265. while (nCount < (mem_pool_container->num_used_pool - 1))
  266. {
  267. mem_pool_container->mem_pools[nCount].nMaxId = mem_pool_container->mem_pools[nToCopy].nMaxId;
  268. mem_pool_container->mem_pools[nCount].nMinId = mem_pool_container->mem_pools[nToCopy].nMinId;
  269. memcpy(
  270. (S8**)(mem_pool_container->mem_pools[nCount].unit_list),
  271. (S8**)(mem_pool_container->mem_pools[nToCopy].unit_list),
  272. (mem_pool_container->unit_per_pool * sizeof(S8**)));
  273. ++nCount;
  274. ++nToCopy;
  275. }
  276. }
  277. --mem_pool_container->num_used_pool;
  278. return TRUE;
  279. }
  280. ++nCount;
  281. }
  282.     return FALSE;
  283. }
  284. /*****************************************************************************
  285.  * FUNCTION
  286.  *  mmi_mem_get_free_pool
  287.  * DESCRIPTION
  288.  *  Get a free/appropriate position for new interting pool
  289.  * PARAMETERS
  290.  *  mem_pool_container      [OUT]       Container of memory pool
  291.  *  mem_pools               [IN]        Memory pool to be inserted
  292.  * RETURNS
  293.  *  void
  294.  *****************************************************************************/
  295. /* There is one extra buffer then the total pools. So all the pools are shifted one down to get the correct buffer
  296.    The basic advantage is to remove all the checks and always get the maximum continuous data.
  297.    Consider a case of three pools with data 12-35, 36-47, 48-59. Now if a user asks to add pool with id 1, then 
  298.    the resulting data should be 0-11, 12-35, 36-47 instead of having 0-11, 36-47, 48-59 */
  299. S32 mmi_mem_get_free_pool(mem_pool_container_struct *mem_pool_container, mem_pool_struct *mem_pools)
  300. {
  301.     /*----------------------------------------------------------------*/
  302.     /* Local Variables                                                */
  303.     /*----------------------------------------------------------------*/
  304.     S32 nCount = mem_pool_container->num_used_pool - 1;
  305.     S32 nToCopy;
  306.     /*----------------------------------------------------------------*/
  307.     /* Code Body                                                      */
  308.     /*----------------------------------------------------------------*/
  309.     /* pool is empty, return first pool */
  310.     if (nCount < 0)
  311.     {
  312.         return 0;
  313.     }
  314.     nToCopy = nCount + 1;
  315.     /* copy/shift memory pool down and insert current pool into best position */
  316.     while (mem_pool_container->mem_pools[nCount].nMinId > mem_pools->nMinId && nCount >= 0)
  317.     {
  318.         mem_pool_container->mem_pools[nToCopy].nMaxId = mem_pool_container->mem_pools[nCount].nMaxId;
  319.         mem_pool_container->mem_pools[nToCopy].nMinId = mem_pool_container->mem_pools[nCount].nMinId;
  320.         memcpy(
  321.             (S8 **) (mem_pool_container->mem_pools[nToCopy].unit_list),
  322.             (S8 **) (mem_pool_container->mem_pools[nCount].unit_list),
  323.             (mem_pool_container->unit_per_pool * sizeof(S8 **)));
  324.         --nCount;
  325.         --nToCopy;
  326.     }
  327.     return nToCopy;
  328. }
  329. /*****************************************************************************
  330.  * FUNCTION
  331.  *  mmi_mem_get_free_mem
  332.  * DESCRIPTION
  333.  *  Get address of free memory location in read buffer of memory units
  334.  * PARAMETERS
  335.  *  mem_pool_container      [OUT]       Container of memory pool
  336.  *  mem_pools               [IN]        Memory pool to be inserted
  337.  * RETURNS
  338.  *  void
  339.  *****************************************************************************/
  340. void *mmi_mem_get_free_mem(mem_pool_container_struct *mem_pool_container, mem_pool_struct *mem_pools)
  341. {
  342.     /*----------------------------------------------------------------*/
  343.     /* Local Variables                                                */
  344.     /*----------------------------------------------------------------*/
  345.     S32 nCount = mem_pool_container->num_used_pool - 1;
  346.     S32 nToCopy;
  347.     S32 nTemp;
  348.     /*----------------------------------------------------------------*/
  349.     /* Code Body                                                      */
  350.     /*----------------------------------------------------------------*/
  351.     if (mem_pool_container->num_used_pool < mem_pool_container->num_pool)
  352.     {
  353.         nToCopy = mem_pool_container->num_used_pool;
  354.     }
  355.     else
  356.     {
  357.         nToCopy = nCount + 1;
  358.         while (mem_pool_container->mem_pools[nCount].nMinId > mem_pools->nMinId && nCount >= 0)
  359.         {
  360.             --nCount;
  361.             --nToCopy;
  362.         }
  363.         if (nToCopy > 0)    /* If finally we plan to use a buffer greater then the first buffer */
  364.         {
  365.             if (mem_pools->nMinId > mem_pool_container->mem_pools[0].nMaxId + 1)        /* Delete the 0th Data only if u do not require it. Else remove the last one */
  366.             {
  367.                 /* This means the index is 0 that will be freed. Return the buffer pointed by the zero index pool */
  368.                 return (S8*) mem_pool_container->mem_pools[0].unit_list[0];
  369.             }
  370.             else
  371.             {
  372.                 return (S8*) mem_pool_container->mem_pools[mem_pool_container->num_used_pool - 1].unit_list[0];
  373.             }
  374.         }
  375.         else
  376.         {
  377.             return (S8*) mem_pool_container->mem_pools[mem_pool_container->num_used_pool - 1].unit_list[0];
  378.         }
  379.     }
  380.     nTemp = nToCopy * mem_pool_container->unit_size * mem_pool_container->unit_per_pool;
  381.     return ((S8*) (mem_pool_container->pBuffer) + nTemp);
  382. }
  383. /*****************************************************************************
  384.  * FUNCTION
  385.  *  mmi_mem_get_values
  386.  * DESCRIPTION
  387.  *  Retrieve real body of memory pool units if available
  388.  * PARAMETERS
  389.  *  mem_pool_container      [IN]        Container of memory pool
  390.  *  nMinId                  [IN]        Min boundary of searching
  391.  *  nMaxId                  [IN]        Max boundary of searching
  392.  *  pValueList              [OUT]       Buffer to store search result
  393.  *  pNotAvailPool           [OUT]       E28t?
  394.  * RETURNS
  395.  *  void
  396.  *****************************************************************************/
  397. mmi_mem_pool_ret_enum mmi_mem_get_values(
  398.                         mem_pool_container_struct *mem_pool_container,
  399.                         S16 nMinId,
  400.                         S16 nMaxId,
  401.                         void **pValueList,
  402.                         S16 *pNotAvailPool)
  403. {
  404.     /*----------------------------------------------------------------*/
  405.     /* Local Variables                                                */
  406.     /*----------------------------------------------------------------*/
  407.     S16 nSearchId = nMinId;
  408.     S16 nIndex;
  409.     S16 n_units_found = 0;  /* number of units found */
  410.     S16 nStrtIndex = 0;
  411.     /*----------------------------------------------------------------*/
  412.     /* Code Body                                                      */
  413.     /*----------------------------------------------------------------*/
  414.     while ((nIndex = mmi_mem_search_unit(mem_pool_container, nSearchId)) >= 0)
  415.     {
  416.         nStrtIndex = nSearchId - mem_pool_container->mem_pools[nIndex].nMinId;
  417.         if (nMaxId <= mem_pool_container->mem_pools[nIndex].nMaxId)
  418.         {
  419.             memcpy(
  420.                 ((S8 **) (pValueList) + n_units_found),
  421.                 ((S8 **) (mem_pool_container->mem_pools[nIndex].unit_list) + nStrtIndex),
  422.                 (nMaxId - nSearchId + 1) * sizeof(S8 **));
  423.             n_units_found += (nMaxId - nSearchId + 1);
  424.         }
  425.         else
  426.         {
  427.             memcpy(
  428.                 ((S8 **) (pValueList) + n_units_found),
  429.                 ((S8 **) (mem_pool_container->mem_pools[nIndex].unit_list) + nStrtIndex),
  430.                 (mem_pool_container->mem_pools[nIndex].nMaxId - nSearchId + 1) * sizeof(S8 **));
  431.             n_units_found += (mem_pool_container->mem_pools[nIndex].nMaxId - nSearchId + 1);
  432.         }
  433.         if (n_units_found == nMaxId - nMinId + 1)   /* number of units found equals to num of units required */
  434.         {
  435.             return MEM_CT_SUCCESS;
  436.         }
  437.         else
  438.         {
  439.             nSearchId = mem_pool_container->mem_pools[nIndex].nMaxId + 1;       /* search next index */
  440.         }
  441.     }
  442.     *pNotAvailPool = (nSearchId) / mem_pool_container->unit_per_pool;   /* index of not available pool */
  443.     return MEM_CT_POOL_NOT_AVAILABLE;
  444. }
  445. /*****************************************************************************
  446.  * FUNCTION
  447.  *  mmi_mem_search_unit
  448.  * DESCRIPTION
  449.  *  Search memory pool index in which a unit locates
  450.  * PARAMETERS
  451.  *  mem_pool_container      [IN]        Container of memory pool
  452.  *  nId                     [IN]        Index of the unit
  453.  * RETURNS
  454.  *  Index of pool if found; otherwisre, (-1)
  455.  *****************************************************************************/
  456. S32 mmi_mem_search_unit(mem_pool_container_struct *mem_pool_container, S16 nId)
  457. {
  458.     /*----------------------------------------------------------------*/
  459.     /* Local Variables                                                */
  460.     /*----------------------------------------------------------------*/
  461.     S32 i;
  462.     /*----------------------------------------------------------------*/
  463.     /* Code Body                                                      */
  464.     /*----------------------------------------------------------------*/
  465.     for (i = 0; i < mem_pool_container->num_used_pool; ++i)
  466.         if (nId >= mem_pool_container->mem_pools[i].nMinId && nId <= mem_pool_container->mem_pools[i].nMaxId)
  467.         {
  468.             return i;
  469.         }
  470.     return (-1);
  471. }