ilcontext.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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: ilcontext.c /main/3 1995/10/23 15:43:53 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. /* ilcontext.c - Contains ilCreate/DestroyContext() and related code.
  40. */
  41. #include <stdlib.h>
  42. #include "ilint.h"
  43. #include "ilcontext.h"
  44. #include "ilerrors.h"
  45. /* Functions in /ilc/ilobject.c :
  46. */
  47. /* Called by ilCreateContext() to create object data in the given context.
  48. Returns: true if add was ok, else error: caller should free all.
  49. */
  50. IL_EXTERN ilBool _ilObjectInitContext (
  51. ilContextPtr pContext
  52. );
  53. /* Called by ilDestroyContext() to destroy object data in the given context.
  54. */
  55. IL_EXTERN void _ilObjectDestroyContext (
  56. ilContextPtr pContext
  57. );
  58. /* ------------------------ ilInternalCreateContext ------------------------- */
  59. /* Called by the macro IL_CREATE_CONTEXT() which passes in the version
  60. check number. Only check the low-order 16 bits of the versionCheck;
  61. that way backwards-compatible versions can be differentiated by the
  62. upper 16 bits (which might signal the library to do something different).
  63. */
  64. ilError ilInternalCreateContext (
  65. int versionCheck,
  66. ilContext *pContextReturn, /* RETURNED */
  67. unsigned long mustBeZero
  68. )
  69. {
  70. ilContextPtr pContext;
  71. /* If internal version # > the version # the library was built with, then
  72. error; if <, old IL program using new library: supported.
  73. */
  74. if ((versionCheck & 0xffff) > IL_INTERNAL_VERSION)
  75. return IL_ERROR_VERSION_MISMATCH;
  76. if (mustBeZero != 0)
  77. return IL_ERROR_PAR_NOT_ZERO;
  78. pContext = (ilContextPtr)IL_MALLOC_ZERO (sizeof (ilContextRec));
  79. if (!pContext)
  80. return IL_ERROR_MALLOC;
  81. if (!_ilObjectInitContext (pContext)) {
  82. IL_FREE (pContext);
  83. return IL_ERROR_MALLOC;
  84. }
  85. pContext->p.error = 0;
  86. pContext->p.errorInfo = 0;
  87. /* Init private type code to start at standard image types.
  88. */
  89. pContext->privateType = IL_MAX_TYPE + 1;
  90. *pContextReturn = (ilContext)pContext;
  91. return 0;
  92. }
  93. #ifdef IL_GARBAGE_MALLOC
  94. /* --------------------- ilMallocAndInitWithGarbage ------------------------ */
  95. /* Referenced by IL_MALLOC() macro when IL_GARBAGE_MALLOC defined (should be
  96. defined only during test/debug - not in production product.)
  97. malloc the given nBytes, fill it with garbage and return ptr to it.
  98. */
  99. IL_PRIVATE void *_ilMallocAndInitWithGarbage (
  100. unsigned long nBytes
  101. )
  102. {
  103. ilPtr p, pMalloc;
  104. pMalloc = (ilPtr)malloc (nBytes);
  105. if (p = pMalloc) {
  106. while (nBytes-- > 0)
  107. *p++ = 0xFD;
  108. }
  109. return (void *)pMalloc;
  110. }
  111. #endif
  112. /* ----------------------- ilGetPrivateType ----------------------- */
  113. /* Public function; see spec.
  114. */
  115. unsigned int ilGetPrivateType (
  116. ilContext context
  117. )
  118. {
  119. ilContextPtr pContext;
  120. /* Increment code (but not if it has wrapped to zero! and return it.
  121. */
  122. pContext = IL_CONTEXT_PTR (context);
  123. if (pContext->privateType != 0)
  124. pContext->privateType++;
  125. return pContext->privateType;
  126. }
  127. /* ------------------------ ilDestroyContext ---------------------------- */
  128. /* Public function; see spec.
  129. */
  130. ilBool ilDestroyContext (
  131. ilContext context
  132. )
  133. {
  134. ilContextPtr pContext;
  135. int i;
  136. /* Destroy all objects associated with this context, then free
  137. any data pointed to in pAlloc array.
  138. */
  139. pContext = IL_CONTEXT_PTR (context);
  140. _ilObjectDestroyContext (pContext);
  141. for (i = 0; i < IL_CONTEXT_MAX_ALLOC; i++) {
  142. if (pContext->pAlloc[i])
  143. IL_FREE (pContext->pAlloc[i]);
  144. }
  145. IL_FREE (pContext);
  146. return TRUE;
  147. }
  148. /* ------------------------ ilIntersectRect ------------------------------ */
  149. /* Intersect the rectangle "*pSrcRect" with the rect "*pDstRect",
  150. storing the result in "*pDstRect".
  151. Not in this file for any particular reason; no other logical place for it.
  152. */
  153. IL_PRIVATE void _ilIntersectRect (
  154. ilRect *pSrcRect,
  155. ilRect *pDstRect
  156. )
  157. {
  158. long left, top, right, bottom, i;
  159. /* Change to non-inclusive coords for easier compare.
  160. */
  161. left = pSrcRect->x;
  162. right = left + pSrcRect->width;
  163. top = pSrcRect->y;
  164. bottom = top + pSrcRect->height;
  165. if (pDstRect->x > left)
  166. left = pDstRect->x;
  167. if (pDstRect->y > top)
  168. top = pDstRect->y;
  169. i = pDstRect->x + pDstRect->width; /* pDstRect right */
  170. if (i < right)
  171. right = i;
  172. i = pDstRect->y + pDstRect->height; /* pDstRect bottom */
  173. if (i < bottom)
  174. bottom = i;
  175. /* Store result into pDstRect, with width/height always >= 0.
  176. */
  177. pDstRect->x = left;
  178. pDstRect->y = top;
  179. pDstRect->width = (right <= left) ? 0 : right - left;
  180. pDstRect->height = (bottom <= top) ? 0 : bottom - top;
  181. }