ilobject.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: ilobject.c /main/3 1995/10/23 15:58:21 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1991 Hewlett-Packard Co.
  27. ***
  28. *** RESTRICTED RIGHTS LEGEND
  29. *** Use, duplication, or disclosure by the U.S. Government is subject to
  30. *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
  31. *** Technical Data and Computer Software clause in DFARS 252.227-7013.
  32. *** Hewlett-Packard Company
  33. *** 3000 Hanover Street
  34. *** Palo Alto, CA 94304 U.S.A.
  35. *** Rights for non-DOD U.S. Government Departments and Agencies are as set
  36. *** forth in FAR 52.227-19(c)(1,2).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. /* /ilc/ilobject.c : General object handling code, e.g. ilDestroyObject().
  40. */
  41. #include <stdlib.h>
  42. #include "ilint.h"
  43. #include "ilcontext.h"
  44. #include "ilerrors.h"
  45. /* ------------------------ ilCreateObject ----------------------------------- */
  46. /* Creates an object of size "sizeInBytes" and returns a ptr to it, or null
  47. if failure (the error code in "context" is set in either case). The object
  48. is added to the given "context". "sizeInBytes" must be a minimum of
  49. "sizeof(ilObjectRec)".
  50. The function "Destroy" will be called by ilDestroyObject(), as:
  51. Destroy (ilObjectPtr pObject);
  52. Before destroying this object. The Destroy() function must free any
  53. object-specific data etc. It MUST not touch the fields in the object header
  54. (ilObjectRec) or free *pObject.
  55. */
  56. IL_PRIVATE ilObjectPtr _ilCreateObject (
  57. ilContext context, /* context to add object to */
  58. int objectType, /* code for object, e.g. IL_PIPE */
  59. void (*Destroy)(), /* destroy function; see above */
  60. /* Use portable type, bug report OSF_QAR#32082 */
  61. size_t sizeInBytes /* size of object to create */
  62. )
  63. {
  64. ilContextPtr pContext;
  65. ilObjectPtr pObject;
  66. /* Allocate object rec, return if failure.
  67. */
  68. pContext = (ilContextPtr)context;
  69. pObject = (ilObjectPtr)IL_MALLOC (sizeInBytes);
  70. if (!pObject) {
  71. pContext->p.error = IL_ERROR_MALLOC;
  72. return (ilObjectPtr)NULL;
  73. }
  74. /* Fill in object header, init refCount to 1 => no one attached to it yet.
  75. */
  76. pObject->p.context = (ilContext)pContext;
  77. pObject->p.objectType = objectType;
  78. pObject->p.pPrivate = (ilPtr)NULL;
  79. pObject->Destroy = Destroy;
  80. pObject->refCount = 1;
  81. /* Add the object to the front of the list of objects.
  82. */
  83. pObject->pNext = pContext->objectHead.pNext;
  84. pObject->pPrev = (ilPtr)&pContext->objectHead;
  85. ((ilObjectPtr)pContext->objectHead.pNext)->pPrev = (ilPtr)pObject;
  86. pContext->objectHead.pNext = (ilPtr)pObject;
  87. pContext->p.error = IL_OK;
  88. return pObject;
  89. }
  90. /* ------------------------ ilDestroyObject -------------------------------- */
  91. /* Public function: see spec.
  92. */
  93. ilBool ilDestroyObject (
  94. ilObject object
  95. )
  96. {
  97. ilObjectPtr pObject;
  98. /* Downcount refCount, exit if not zero - object is still attached to others.
  99. Call object-specific destroy function, free client private if present,
  100. remove object from the linked list, free the object.
  101. */
  102. pObject = (ilObjectPtr)object;
  103. if ((pObject->p.objectType == IL_NULL_OBJECT)
  104. || (pObject->p.objectType > IL_MAX_OBJECT_TYPE)) {
  105. pObject->p.context->error = IL_ERROR_OBJECT_TYPE;
  106. return FALSE;
  107. }
  108. pObject->p.context->error = 0;
  109. if (--pObject->refCount > 0)
  110. return TRUE; /* object still attached; EXIT */
  111. (*pObject->Destroy) (pObject);
  112. if (pObject->p.pPrivate)
  113. free (pObject->p.pPrivate); /* note: caller used malloc, not IL_MALLOC() */
  114. ((ilObjectPtr)pObject->pPrev)->pNext = (ilPtr)pObject->pNext;
  115. ((ilObjectPtr)pObject->pNext)->pPrev = (ilPtr)pObject->pPrev;
  116. IL_FREE (pObject);
  117. return TRUE;
  118. }
  119. /* ------------------------ ilObjectInitContext ------------------------- */
  120. /* Called by ilCreateContext() to create object data in the given context.
  121. Returns: true if add was ok, else error: caller should free all.
  122. */
  123. IL_PRIVATE ilBool _ilObjectInitContext (
  124. ilContextPtr pContext
  125. )
  126. {
  127. /* pCOD->head points to the list of objects; init list to null (point to self).
  128. */
  129. pContext->objectHead.p.context = (ilContext)pContext;
  130. pContext->objectHead.pPrev = pContext->objectHead.pNext = (ilPtr)&pContext->objectHead;
  131. return TRUE;
  132. }
  133. /* ------------------------ ilObjectDestroyContext ------------------------- */
  134. /* Called by ilDestroyContext() to destroy object data in the given context.
  135. */
  136. IL_PRIVATE void _ilObjectDestroyContext (
  137. ilContextPtr pContext
  138. )
  139. {
  140. ilObjectPtr pObject, pNextObject, pObjectHead;
  141. /* Loop thru and destroy all objects until list points back to head (done).
  142. */
  143. pObjectHead = &pContext->objectHead;
  144. pObject = (ilObjectPtr)pObjectHead->pNext;
  145. while (pObject != pObjectHead) {
  146. pNextObject = (ilObjectPtr)pObject->pNext;
  147. ilDestroyObject ((ilObject)pObject);
  148. pObject = pNextObject;
  149. }
  150. }