|
|
1.1 root 1: /*****************************************************************************
2: * XGA Memory Manager
3: *
4: * Copyright (c) 1992 Microsoft Corporation
5: ****************************************************************************/
6:
7: #include "driver.h"
8:
9:
10:
11:
12: /******************************************************************************
13: * CpAlloc - Coprocessor Memory Alloc
14: *
15: * ENTRY: nSize - The requested allocation size.
16: * ulFlags - Allocation control flags.
17: *
18: * XGA_ZERO_MEM - Zero allocated memory.
19: * XGA_LOCK_MEM - Lock the allocated memory.
20: * The returned handle is really a
21: * pointer to allocation node.
22: *
23: * EXIT: hXgaMem - A handle for the memory just allocated.
24: * If XGA_LOCK_MEM is set then this is a pointer
25: * to the allocation node.
26: *
27: * On an error, this will be NULL.
28: *****************************************************************************/
29: HANDLE hCpAlloc(PPDEV ppdev, ULONG nSize, ULONG ulFlags)
30: {
31: PCPALLOCNODE pcpan,
32: pcpanSearch,
33: pcpanNew,
34: pcpanLast ;
35:
36:
37: // This will be returned as the handle.
38:
39: pcpanNew = NULL ;
40:
41: // Note: Is is possible that the previous allocation was the
42: // the last piece of memory available from the free list,
43: // and it was an exact fit. In this case the free list root
44: // will be NULL. In this case we should fail the allocation
45: // request.
46:
47: if (ppdev->pFreeListRoot == NULL)
48: {
49: DISPDBG((1, "XGA.DLL!CpAlloc - pFreeListRoot NULL (normal condition)\n")) ;
50: return (NULL) ;
51: }
52:
53: // Traverse the free list, looking for the first piece of memory
54: // that is larger enough to satisfy the memory request.
55:
56: pcpanSearch = ppdev->pFreeListRoot ;
57: pcpanLast = pcpanSearch ;
58: do
59: {
60: if (pcpanSearch->ulLength >= nSize)
61: {
62:
63: // Take care of the case when the node we find is exactly
64: // the size we need.
65:
66: if (pcpanSearch->ulLength > nSize)
67: {
68: // Allocate a new node to hold the information about this
69: // memory request.
70:
71: pcpanNew = (PCPALLOCNODE) LocalAlloc(LPTR, sizeof(CPALLOCNODE)) ;
72: if (pcpanNew == NULL)
73: {
74: DISPDBG((1, "XGA.DLL!CpAlloc - LocalAlloc failed\n")) ;
75: return (NULL) ;
76: }
77:
78: // Initialize the new node. We take memory the beginning
79: // of the piece of memory we found.
80:
81: pcpanNew->pcpanNext = NULL ;
82: pcpanNew->ulFlags = 0 ;
83: pcpanNew->hCpAllocNode = pcpanNew ;
84: pcpanNew->ulLength = nSize ;
85: pcpanNew->pCpLinearMemory = pcpanSearch->pCpLinearMemory ;
86: pcpanNew->ulCpPhysicalMemory = pcpanSearch->ulCpPhysicalMemory ;
87:
88: // Reduce the size of this node, the amount of memory
89: // we're taking.
90:
91: pcpanSearch->ulLength -= nSize ;
92: (PBYTE) pcpanSearch->pCpLinearMemory += nSize ;
93: pcpanSearch->ulCpPhysicalMemory += nSize ;
94: }
95: else
96: {
97: // We found a node that is exactly the size we need.
98: // Remove it from the Free List and initialize it's links.
99:
100: if (pcpanLast == ppdev->pFreeListRoot)
101: {
102: ppdev->pFreeListRoot = (PCPALLOCNODE) pcpanSearch->pcpanNext ;
103: }
104: else
105: {
106: pcpanLast->pcpanNext = pcpanSearch->pcpanNext ;
107: }
108:
109: pcpanNew = pcpanSearch ;
110: pcpanNew->pcpanNext = NULL ;
111: }
112:
113: // Set the flags, and take care of initialization of the
114: // Allocated XGA memory if necessary.
115:
116: pcpanNew->ulFlags = ulFlags ;
117: if (ulFlags & XGA_ZERO_INIT)
118: {
119: memset(pcpanNew->pCpLinearMemory, 0, nSize) ;
120: }
121:
122: // Add (or move) the new (or existing) node to the
123: // Allocated list.
124: // Note: New nodes are always added to the end of the Allocated
125: // list.
126:
127: if (ppdev->pAllocatedListRoot == NULL)
128: {
129: ppdev->pAllocatedListRoot = pcpanNew ;
130: }
131: else
132: {
133: for (pcpan = ppdev->pAllocatedListRoot ;
134: pcpan->pcpanNext != NULL ;
135: pcpan = pcpan->pcpanNext) ;
136:
137: pcpan->pcpanNext = pcpanNew ;
138:
139: }
140:
141: break ;
142: }
143:
144: pcpanLast = pcpanSearch ;
145: pcpanSearch = pcpanSearch->pcpanNext ;
146:
147: } while (pcpanSearch != NULL) ;
148:
149: return(pcpanNew) ;
150:
151:
152:
153: }
154:
155:
156: /******************************************************************************
157: * CpFree - Coprocessor Memory Free
158: * ENTRY: hXgaMem - A handle for an allocated node of memory.
159: * The node may be locked.
160: *
161: * EXIT: hXgaMem - If the Free was successfull then this will be NULL.
162: * If there is any erro the original hXgaMem will be
163: * returned.
164: *****************************************************************************/
165: HANDLE hCpFree(PPDEV ppdev, HANDLE hXgaMem)
166: {
167: PCPALLOCNODE pcpan,
168: pcpanSearch,
169: pcpanLast ;
170: ULONG ulLength ;
171:
172:
173: // Make sure the handle passed in is not NULL.
174:
175: pcpanSearch = (PCPALLOCNODE) hXgaMem ;
176:
177: if (pcpanSearch == NULL)
178: {
179: DISPDBG((1, "XGA.DLL!CpFree - hXgaMem NULL (invalid)\n")) ;
180: return (hXgaMem) ;
181: }
182:
183: // Find the Allocation node on the Allocated List.
184:
185: pcpanLast = ppdev->pAllocatedListRoot ;
186: for (pcpan = ppdev->pAllocatedListRoot ;
187: pcpan != pcpanSearch ;
188: pcpan = pcpan->pcpanNext)
189: {
190: if (pcpan == NULL)
191: {
192: DISPDBG((1, "XGA.DLL!CpFree - hXgaMem not Found\n")) ;
193: return (hXgaMem) ;
194: }
195:
196: pcpanLast = pcpan ;
197: }
198:
199: // Remove the Old Node from the Allocated List.
200:
201: if (pcpanLast == ppdev->pAllocatedListRoot)
202: {
203: ppdev->pAllocatedListRoot = pcpan->pcpanNext ;
204: }
205: else
206: {
207: pcpanLast->pcpanNext = pcpan->pcpanNext ;
208: }
209:
210: // Find the position in the Free List for this node.
211:
212: ulLength = pcpanSearch->ulLength ;
213:
214: pcpanLast = ppdev->pFreeListRoot ;
215: for (pcpan = ppdev->pFreeListRoot ; pcpan != NULL ; pcpan = pcpan->pcpanNext)
216: {
217: if (pcpan->ulLength > ulLength)
218: break ;
219: pcpanLast = pcpan ;
220: }
221:
222: // Add the Node to the Free List.
223:
224: if (pcpanLast == ppdev->pFreeListRoot)
225: {
226: pcpanSearch->pcpanNext = ppdev->pFreeListRoot ;
227: ppdev->pFreeListRoot = pcpanSearch ;
228: }
229: else
230: {
231: pcpanSearch->pcpanNext = pcpanLast->pcpanNext ;
232: pcpanLast->pcpanNext = pcpanSearch ;
233: }
234:
235: return (NULL) ;
236:
237:
238: }
239:
240: /******************************************************************************
241: * CpMemLock - Lock an Allocated piece of memory
242: *
243: * ENTRY: hXgaMem - A handle for an allocated node of memory.
244: *
245: * EXIT: pXgaMem - A pointer the memory allocation node.
246: * On Error NULL is returned.
247: *****************************************************************************/
248: PVOID pCpMemLock(PPDEV ppdev, HANDLE hXgaMem, ULONG ulFlags)
249: {
250: PCPALLOCNODE pcpan,
251: pcpanLast,
252: pcpanSearch ;
253:
254:
255: // Find the Allocation node on the Allocated List.
256:
257: pcpanSearch = (PCPALLOCNODE) hXgaMem ;
258:
259: pcpanLast = ppdev->pAllocatedListRoot ;
260: for (pcpan = ppdev->pAllocatedListRoot ;
261: pcpan != pcpanSearch ;
262: pcpan = pcpan->pcpanNext)
263: {
264: if (pcpan == NULL)
265: {
266: DISPDBG((1, "XGA.DLL!CpMemLock - hXgaMem not Found\n")) ;
267: return (NULL) ;
268: }
269: }
270:
271: // Set the locked flag.
272:
273: pcpan->ulFlags |= XGA_LOCK_MEM ;
274:
275: return(pcpan) ;
276:
277: }
278:
279: /******************************************************************************
280: * CpMemUnLock - UnLock an Allocated piece of memory
281: *
282: * ENTRY: hXgaMem - A handle for an allocated node of memory.
283: *
284: * EXIT: TRUE - If the node was found and unlocked
285: * FALSE - If the node was not found.
286: *****************************************************************************/
287: BOOL bCpMemUnLock(PPDEV ppdev, HANDLE hXgaMem)
288: {
289: PCPALLOCNODE pcpan,
290: pcpanLast,
291: pcpanSearch ;
292:
293:
294: // Find the Allocation node on the Allocated List.
295:
296: pcpanSearch = (PCPALLOCNODE) hXgaMem ;
297:
298: pcpanLast = ppdev->pAllocatedListRoot ;
299: for (pcpan = ppdev->pAllocatedListRoot ;
300: pcpan != pcpanSearch ;
301: pcpan = pcpan->pcpanNext)
302: {
303: if (pcpan == NULL)
304: {
305: DISPDBG((1, "XGA.DLL!CpMemUnLock - hXgaMem not Found\n")) ;
306: return (FALSE) ;
307: }
308: }
309:
310: // ReSet the locked flag.
311:
312: pcpan->ulFlags &= ~XGA_LOCK_MEM ;
313:
314: return(TRUE) ;
315:
316:
317:
318: }
319:
320: /******************************************************************************
321: * CpMmInitHeap - Coprocessor Memory Manger Heap Init
322: *
323: * ENTRY: None
324: *
325: * EXIT: TRUE - If the Heap was created.
326: * FALSE - If there was any problem
327: *****************************************************************************/
328: BOOL bCpMmInitHeap(PPDEV ppdev)
329: {
330:
331:
332:
333: ppdev->pFreeListRoot = (PCPALLOCNODE) LocalAlloc(LPTR, sizeof(CPALLOCNODE)) ;
334: if (ppdev->pFreeListRoot == NULL)
335: {
336: DISPDBG((1, "XGA.DLL!CpMmInitHeap - LocalAlloc failed\n")) ;
337: return(FALSE) ;
338: }
339:
340: // Init the CoProcessor Allocation Node's fields.
341:
342: ppdev->pFreeListRoot->pcpanNext = NULL ;
343: ppdev->pFreeListRoot->ulFlags = 0 ;
344: ppdev->pFreeListRoot->hCpAllocNode = ppdev->pFreeListRoot ;
345: ppdev->pFreeListRoot->ulLength = ppdev->ulVideoMemorySize -
346: ppdev->ulScreenSize ;
347: ppdev->pFreeListRoot->pCpLinearMemory = ppdev->pjScreen +
348: ppdev->ulScreenSize ;
349: ppdev->pFreeListRoot->ulCpPhysicalMemory = ppdev->ulPhysFrameBuffer +
350: ppdev->ulScreenSize ;
351:
352: return (TRUE) ;
353:
354:
355: }
356:
357:
358: /******************************************************************************
359: * CpMmDestroyHeap - Coprocessor Memory Manger Heap Destroy
360: *
361: * ENTRY: None
362: *
363: * EXIT: TRUE - If the Heap was destroyed.
364: * FALSE - If there was any problem
365: *****************************************************************************/
366: BOOL bCpMmDestroyHeap(PPDEV ppdev)
367: {
368: PCPALLOCNODE pcpan,
369: pcpanNext ;
370:
371: // Run through the Free List, and free all the allocted nodes.
372:
373: pcpan = ppdev->pFreeListRoot ;
374: while(pcpan != NULL)
375: {
376: pcpanNext = pcpan->pcpanNext ;
377: LocalFree(pcpan) ;
378: pcpan = pcpanNext ;
379: }
380:
381: ppdev->pFreeListRoot = NULL ;
382:
383: // Run through the Allocated List and free all the nodes.
384:
385: pcpan = ppdev->pAllocatedListRoot ;
386: while(pcpan != NULL)
387: {
388: pcpanNext = pcpan->pcpanNext ;
389: LocalFree(pcpan) ;
390: pcpan = pcpanNext ;
391: }
392:
393: ppdev->pAllocatedListRoot = NULL ;
394:
395: return (TRUE) ;
396:
397: }
398:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.