sis_ds.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* sis_ds.c -- Private header for Direct Rendering Manager -*- linux-c -*-
  2. * Created: Mon Jan 4 10:05:05 1999 by sclin@sis.com.tw
  3. *
  4. * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * Authors:
  27. * Sung-Ching Lin <sclin@sis.com.tw>
  28. *
  29. */
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "sis_ds.h"
  33. /* Set Data Structure, not check repeated value
  34. * temporarily used
  35. */
  36. set_t *setInit(void)
  37. {
  38. int i;
  39. set_t *set;
  40. set = (set_t *) drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
  41. if (set != NULL) {
  42. for (i = 0; i < SET_SIZE; i++) {
  43. set->list[i].free_next = i + 1;
  44. set->list[i].alloc_next = -1;
  45. }
  46. set->list[SET_SIZE - 1].free_next = -1;
  47. set->free = 0;
  48. set->alloc = -1;
  49. set->trace = -1;
  50. }
  51. return set;
  52. }
  53. int setAdd(set_t * set, ITEM_TYPE item)
  54. {
  55. int free = set->free;
  56. if (free != -1) {
  57. set->list[free].val = item;
  58. set->free = set->list[free].free_next;
  59. } else {
  60. return 0;
  61. }
  62. set->list[free].alloc_next = set->alloc;
  63. set->alloc = free;
  64. set->list[free].free_next = -1;
  65. return 1;
  66. }
  67. int setDel(set_t * set, ITEM_TYPE item)
  68. {
  69. int alloc = set->alloc;
  70. int prev = -1;
  71. while (alloc != -1) {
  72. if (set->list[alloc].val == item) {
  73. if (prev != -1)
  74. set->list[prev].alloc_next =
  75. set->list[alloc].alloc_next;
  76. else
  77. set->alloc = set->list[alloc].alloc_next;
  78. break;
  79. }
  80. prev = alloc;
  81. alloc = set->list[alloc].alloc_next;
  82. }
  83. if (alloc == -1)
  84. return 0;
  85. set->list[alloc].free_next = set->free;
  86. set->free = alloc;
  87. set->list[alloc].alloc_next = -1;
  88. return 1;
  89. }
  90. /* setFirst -> setAdd -> setNext is wrong */
  91. int setFirst(set_t * set, ITEM_TYPE * item)
  92. {
  93. if (set->alloc == -1)
  94. return 0;
  95. *item = set->list[set->alloc].val;
  96. set->trace = set->list[set->alloc].alloc_next;
  97. return 1;
  98. }
  99. int setNext(set_t * set, ITEM_TYPE * item)
  100. {
  101. if (set->trace == -1)
  102. return 0;
  103. *item = set->list[set->trace].val;
  104. set->trace = set->list[set->trace].alloc_next;
  105. return 1;
  106. }
  107. int setDestroy(set_t * set)
  108. {
  109. drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
  110. return 1;
  111. }
  112. /*
  113. * GLX Hardware Device Driver common code
  114. * Copyright (C) 1999 Wittawat Yamwong
  115. *
  116. * Permission is hereby granted, free of charge, to any person obtaining a
  117. * copy of this software and associated documentation files (the "Software"),
  118. * to deal in the Software without restriction, including without limitation
  119. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  120. * and/or sell copies of the Software, and to permit persons to whom the
  121. * Software is furnished to do so, subject to the following conditions:
  122. *
  123. * The above copyright notice and this permission notice shall be included
  124. * in all copies or substantial portions of the Software.
  125. *
  126. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  127. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  128. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  129. * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
  130. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  131. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  132. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  133. *
  134. */
  135. #define ISFREE(bptr) ((bptr)->free)
  136. memHeap_t *mmInit(int ofs, int size)
  137. {
  138. PMemBlock blocks;
  139. if (size <= 0)
  140. return NULL;
  141. blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
  142. if (blocks != NULL) {
  143. blocks->ofs = ofs;
  144. blocks->size = size;
  145. blocks->free = 1;
  146. return (memHeap_t *) blocks;
  147. } else
  148. return NULL;
  149. }
  150. /* Checks if a pointer 'b' is part of the heap 'heap' */
  151. int mmBlockInHeap(memHeap_t * heap, PMemBlock b)
  152. {
  153. TMemBlock *p;
  154. if (heap == NULL || b == NULL)
  155. return 0;
  156. p = heap;
  157. while (p != NULL && p != b) {
  158. p = p->next;
  159. }
  160. if (p == b)
  161. return 1;
  162. else
  163. return 0;
  164. }
  165. static TMemBlock *SliceBlock(TMemBlock * p,
  166. int startofs, int size,
  167. int reserved, int alignment)
  168. {
  169. TMemBlock *newblock;
  170. /* break left */
  171. if (startofs > p->ofs) {
  172. newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
  173. DRM_MEM_DRIVER);
  174. newblock->ofs = startofs;
  175. newblock->size = p->size - (startofs - p->ofs);
  176. newblock->free = 1;
  177. newblock->next = p->next;
  178. p->size -= newblock->size;
  179. p->next = newblock;
  180. p = newblock;
  181. }
  182. /* break right */
  183. if (size < p->size) {
  184. newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
  185. DRM_MEM_DRIVER);
  186. newblock->ofs = startofs + size;
  187. newblock->size = p->size - size;
  188. newblock->free = 1;
  189. newblock->next = p->next;
  190. p->size = size;
  191. p->next = newblock;
  192. }
  193. /* p = middle block */
  194. p->align = alignment;
  195. p->free = 0;
  196. p->reserved = reserved;
  197. return p;
  198. }
  199. PMemBlock mmAllocMem(memHeap_t * heap, int size, int align2, int startSearch)
  200. {
  201. int mask, startofs, endofs;
  202. TMemBlock *p;
  203. if (heap == NULL || align2 < 0 || size <= 0)
  204. return NULL;
  205. mask = (1 << align2) - 1;
  206. startofs = 0;
  207. p = (TMemBlock *) heap;
  208. while (p != NULL) {
  209. if (ISFREE(p)) {
  210. startofs = (p->ofs + mask) & ~mask;
  211. if (startofs < startSearch) {
  212. startofs = startSearch;
  213. }
  214. endofs = startofs + size;
  215. if (endofs <= (p->ofs + p->size))
  216. break;
  217. }
  218. p = p->next;
  219. }
  220. if (p == NULL)
  221. return NULL;
  222. p = SliceBlock(p, startofs, size, 0, mask + 1);
  223. p->heap = heap;
  224. return p;
  225. }
  226. static __inline__ int Join2Blocks(TMemBlock * p)
  227. {
  228. if (p->free && p->next && p->next->free) {
  229. TMemBlock *q = p->next;
  230. p->size += q->size;
  231. p->next = q->next;
  232. drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. int mmFreeMem(PMemBlock b)
  238. {
  239. TMemBlock *p, *prev;
  240. if (b == NULL)
  241. return 0;
  242. if (b->heap == NULL)
  243. return -1;
  244. p = b->heap;
  245. prev = NULL;
  246. while (p != NULL && p != b) {
  247. prev = p;
  248. p = p->next;
  249. }
  250. if (p == NULL || p->free || p->reserved)
  251. return -1;
  252. p->free = 1;
  253. Join2Blocks(p);
  254. if (prev)
  255. Join2Blocks(prev);
  256. return 0;
  257. }