sis_ds.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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,
  137. int size)
  138. {
  139. PMemBlock blocks;
  140. if (size <= 0)
  141. return NULL;
  142. blocks = (TMemBlock *)drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
  143. if (blocks != NULL) {
  144. blocks->ofs = ofs;
  145. blocks->size = size;
  146. blocks->free = 1;
  147. return (memHeap_t *)blocks;
  148. } else
  149. return NULL;
  150. }
  151. /* Checks if a pointer 'b' is part of the heap 'heap' */
  152. int mmBlockInHeap(memHeap_t *heap, PMemBlock b)
  153. {
  154. TMemBlock *p;
  155. if (heap == NULL || b == NULL)
  156. return 0;
  157. p = heap;
  158. while (p != NULL && p != b) {
  159. p = p->next;
  160. }
  161. if (p == b)
  162. return 1;
  163. else
  164. return 0;
  165. }
  166. static TMemBlock* SliceBlock(TMemBlock *p,
  167. int startofs, int size,
  168. int reserved, int alignment)
  169. {
  170. TMemBlock *newblock;
  171. /* break left */
  172. if (startofs > p->ofs) {
  173. newblock = (TMemBlock*) drm_calloc(1, sizeof(TMemBlock),
  174. DRM_MEM_DRIVER);
  175. newblock->ofs = startofs;
  176. newblock->size = p->size - (startofs - p->ofs);
  177. newblock->free = 1;
  178. newblock->next = p->next;
  179. p->size -= newblock->size;
  180. p->next = newblock;
  181. p = newblock;
  182. }
  183. /* break right */
  184. if (size < p->size) {
  185. newblock = (TMemBlock*) drm_calloc(1, sizeof(TMemBlock),
  186. DRM_MEM_DRIVER);
  187. newblock->ofs = startofs + size;
  188. newblock->size = p->size - size;
  189. newblock->free = 1;
  190. newblock->next = p->next;
  191. p->size = size;
  192. p->next = newblock;
  193. }
  194. /* p = middle block */
  195. p->align = alignment;
  196. p->free = 0;
  197. p->reserved = reserved;
  198. return p;
  199. }
  200. PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int startSearch)
  201. {
  202. int mask,startofs, endofs;
  203. TMemBlock *p;
  204. if (heap == NULL || align2 < 0 || size <= 0)
  205. return NULL;
  206. mask = (1 << align2)-1;
  207. startofs = 0;
  208. p = (TMemBlock *)heap;
  209. while (p != NULL) {
  210. if (ISFREE(p)) {
  211. startofs = (p->ofs + mask) & ~mask;
  212. if ( startofs < startSearch ) {
  213. startofs = startSearch;
  214. }
  215. endofs = startofs+size;
  216. if (endofs <= (p->ofs+p->size))
  217. break;
  218. }
  219. p = p->next;
  220. }
  221. if (p == NULL)
  222. return NULL;
  223. p = SliceBlock(p,startofs,size,0,mask+1);
  224. p->heap = heap;
  225. return p;
  226. }
  227. static __inline__ int Join2Blocks(TMemBlock *p)
  228. {
  229. if (p->free && p->next && p->next->free) {
  230. TMemBlock *q = p->next;
  231. p->size += q->size;
  232. p->next = q->next;
  233. drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. int mmFreeMem(PMemBlock b)
  239. {
  240. TMemBlock *p, *prev;
  241. if (b == NULL)
  242. return 0;
  243. if (b->heap == NULL)
  244. return -1;
  245. p = b->heap;
  246. prev = NULL;
  247. while (p != NULL && p != b) {
  248. prev = p;
  249. p = p->next;
  250. }
  251. if (p == NULL || p->free || p->reserved)
  252. return -1;
  253. p->free = 1;
  254. Join2Blocks(p);
  255. if (prev)
  256. Join2Blocks(prev);
  257. return 0;
  258. }