FreeList.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither name of Intel Corporation nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  26. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ******************************************************************************/
  31. #ifndef FREE_LIST_H
  32. #define FREE_LIST_H
  33. /*!
  34. * \file
  35. */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "ithread.h"
  40. #include <errno.h>
  41. /*!
  42. * Free list node. points to next free item.
  43. * Memory for node is borrowed from allocated items.
  44. * \internal
  45. */
  46. typedef struct FREELISTNODE
  47. {
  48. struct FREELISTNODE *next;
  49. } FreeListNode;
  50. /*!
  51. * Stores head and size of free list, as well as mutex for protection.
  52. * \internal
  53. */
  54. typedef struct FREELIST
  55. {
  56. FreeListNode *head;
  57. size_t element_size;
  58. int maxFreeListLength;
  59. int freeListLength;
  60. } FreeList;
  61. /*!
  62. * \brief Initializes Free List.
  63. *
  64. * Must be called first and only once for FreeList.
  65. *
  66. * \return:
  67. * \li \c 0 on success.
  68. * \li \c EINVAL on failure.
  69. */
  70. int FreeListInit(
  71. /*! Must be valid, non null, pointer to a linked list. */
  72. FreeList *free_list,
  73. /*! Size of elements to store in free list. */
  74. size_t elementSize,
  75. /*! Max size that the free list can grow to before returning
  76. * memory to O.S. */
  77. int maxFreeListLength);
  78. /*!
  79. * \brief Allocates chunk of set size.
  80. *
  81. * If a free item is available in the list, returnes the stored item,
  82. * otherwise calls the O.S. to allocate memory.
  83. *
  84. * \return Non NULL on success. NULL on failure.
  85. */
  86. void *FreeListAlloc(
  87. /*! Must be valid, non null, pointer to a linked list. */
  88. FreeList *free_list);
  89. /*!
  90. * \brief Returns an item to the Free List.
  91. *
  92. * If the free list is smaller than the max size then adds the item to the
  93. * free list, otherwise returns the item to the O.S.
  94. *
  95. * \return:
  96. * \li \c 0 on success.
  97. * \li \c EINVAL on failure.
  98. */
  99. int FreeListFree(
  100. /*! Must be valid, non null, pointer to a free list. */
  101. FreeList *free_list,
  102. /*! Must be a pointer allocated by FreeListAlloc. */
  103. void *element);
  104. /*!
  105. * \brief Releases the resources stored with the free list.
  106. *
  107. * \return:
  108. * \li \c 0 on success.
  109. * \li \c EINVAL on failure.
  110. */
  111. int FreeListDestroy(
  112. /*! Must be valid, non null, pointer to a linked list. */
  113. FreeList *free_list);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* FREE_LIST_H */