hdb.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <assert.h>
  37. #include "hdb.h"
  38. enum SA_HANDLE_STATE {
  39. SA_HANDLE_STATE_EMPTY,
  40. SA_HANDLE_STATE_PENDINGREMOVAL,
  41. SA_HANDLE_STATE_ACTIVE
  42. };
  43. struct saHandle {
  44. int state;
  45. void *instance;
  46. int refCount;
  47. };
  48. SaErrorT
  49. saHandleCreate (
  50. struct saHandleDatabase *handleDatabase,
  51. int instanceSize,
  52. unsigned int *handleOut)
  53. {
  54. int handle;
  55. void *newHandles;
  56. int found = 0;
  57. void *instance;
  58. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  59. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  60. found = 1;
  61. break;
  62. }
  63. }
  64. if (found == 0) {
  65. handleDatabase->handleCount += 1;
  66. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  67. sizeof (struct saHandle) * handleDatabase->handleCount);
  68. if (newHandles == 0) {
  69. return (SA_ERR_NO_MEMORY);
  70. }
  71. handleDatabase->handles = newHandles;
  72. }
  73. instance = malloc (instanceSize);
  74. if (instance == 0) {
  75. return (SA_ERR_NO_MEMORY);
  76. }
  77. memset (instance, 0, instanceSize);
  78. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  79. handleDatabase->handles[handle].instance = instance;
  80. handleDatabase->handles[handle].refCount = 1;
  81. *handleOut = handle;
  82. return (SA_OK);
  83. }
  84. SaErrorT
  85. saHandleDestroy (
  86. struct saHandleDatabase *handleDatabase,
  87. unsigned int handle)
  88. {
  89. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  90. saHandleInstancePut (handleDatabase, handle);
  91. return (SA_OK);
  92. }
  93. SaErrorT
  94. saHandleInstanceGet (
  95. struct saHandleDatabase *handleDatabase,
  96. unsigned int handle,
  97. void **instance)
  98. {
  99. if (handle > handleDatabase->handleCount) {
  100. return (SA_ERR_BAD_HANDLE);
  101. }
  102. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  103. return (SA_ERR_BAD_HANDLE);
  104. }
  105. *instance = handleDatabase->handles[handle].instance;
  106. handleDatabase->handles[handle].refCount += 1;
  107. return (SA_OK);
  108. }
  109. SaErrorT
  110. saHandleInstancePut (
  111. struct saHandleDatabase *handleDatabase,
  112. unsigned int handle)
  113. {
  114. void *instance;
  115. handleDatabase->handles[handle].refCount -= 1;
  116. assert (handleDatabase->handles[handle].refCount >= 0);
  117. if (handleDatabase->handles[handle].refCount == 0) {
  118. instance = (handleDatabase->handles[handle].instance);
  119. if (handleDatabase->handleInstanceDestructor) {
  120. handleDatabase->handleInstanceDestructor (instance);
  121. }
  122. free (instance);
  123. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  124. }
  125. return (SA_OK);
  126. }