[Scummvm-cvs-logs] SF.net SVN: scummvm:[45428] scummvm/trunk/engines/tinsel

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Tue Oct 27 01:37:54 CET 2009


Revision: 45428
          http://scummvm.svn.sourceforge.net/scummvm/?rev=45428&view=rev
Author:   fingolfin
Date:     2009-10-27 00:37:54 +0000 (Tue, 27 Oct 2009)

Log Message:
-----------
TINSEL: Make HeapCompact local to heapmem.cpp; add code to print heap statistics

Modified Paths:
--------------
    scummvm/trunk/engines/tinsel/handle.cpp
    scummvm/trunk/engines/tinsel/heapmem.cpp
    scummvm/trunk/engines/tinsel/heapmem.h

Modified: scummvm/trunk/engines/tinsel/handle.cpp
===================================================================
--- scummvm/trunk/engines/tinsel/handle.cpp	2009-10-27 00:37:23 UTC (rev 45427)
+++ scummvm/trunk/engines/tinsel/handle.cpp	2009-10-27 00:37:54 UTC (rev 45428)
@@ -41,7 +41,7 @@
 //----------------- EXTERNAL GLOBAL DATA --------------------
 
 #ifdef DEBUG
-bool bLockedScene = 0;
+static bool bLockedScene = 0;
 #endif
 
 
@@ -391,9 +391,6 @@
 
 	pH = handleTable + handle;
 
-	// compact the heap to avoid fragmentation while scene is non-discardable
-	HeapCompact(MAX_INT, false);
-
 	if ((pH->filesize & fPreload) == 0) {
 		// Ensure the scene handle is allocated.
 		MemoryReAlloc(pH->_node, pH->filesize & FSIZE_MASK);

Modified: scummvm/trunk/engines/tinsel/heapmem.cpp
===================================================================
--- scummvm/trunk/engines/tinsel/heapmem.cpp	2009-10-27 00:37:23 UTC (rev 45427)
+++ scummvm/trunk/engines/tinsel/heapmem.cpp	2009-10-27 00:37:54 UTC (rev 45428)
@@ -63,12 +63,6 @@
 // pointer to the linked list of free mnodes
 static MEM_NODE *pFreeMemNodes;
 
-#ifdef DEBUG
-// diagnostic mnode counters
-static int numNodes;
-static int maxNodes;
-#endif
-
 // list of all fixed memory nodes
 MEM_NODE s_fixedMnodesList[5];
 
@@ -78,16 +72,37 @@
 //
 static MEM_NODE *AllocMemNode(void);
 
+#ifdef DEBUG
+static void MemoryStats() {
+	int usedNodes = 0;
+	int allocedNodes = 0;
+	int lockedNodes = 0;
+	int lockedSize = 0;
+	int totalSize = 0;
 
+	const MEM_NODE *pHeap = &heapSentinel;
+	MEM_NODE *pCur;
+
+	for (pCur = pHeap->pNext; pCur != pHeap; pCur = pCur->pNext) {
+		usedNodes++;
+		totalSize += pCur->size;
+		if (pCur->flags)
+			allocedNodes++;
+		if (pCur->flags & DWM_LOCKED) {
+			lockedNodes++;
+			lockedSize += pCur->size;
+		}
+	}
+
+	printf("%d nodes used, %d alloced, %d locked; %d bytes locked, %d used\n",
+			usedNodes, allocedNodes, lockedNodes, lockedSize, totalSize);
+}
+#endif
+
 /**
  * Initialises the memory manager.
  */
 void MemoryInit() {
-#ifdef DEBUG
-	// clear number of nodes in use
-	numNodes = 0;
-#endif
-
 	// place first node on free list
 	pFreeMemNodes = mnodeList;
 
@@ -153,12 +168,6 @@
 	// wipe out the mnode
 	memset(pMemNode, 0, sizeof(MEM_NODE));
 
-#ifdef DEBUG
-	// one more mnode in use
-	if (++numNodes > maxNodes)
-		maxNodes = numNodes;
-#endif
-
 	// return new mnode
 	return pMemNode;
 }
@@ -171,12 +180,6 @@
 	// validate mnode pointer
 	assert(pMemNode >= mnodeList && pMemNode <= mnodeList + NUM_MNODES - 1);
 
-#ifdef DEBUG
-	// one less mnode in use
-	--numNodes;
-	assert(numNodes >= 0);
-#endif
-
 	// place free list in mnode next
 	pMemNode->pNext = pFreeMemNodes;
 
@@ -188,20 +191,15 @@
 /**
  * Tries to make space for the specified number of bytes on the specified heap.
  * @param size			Number of bytes to free up
- * @param bDiscard		When set - will discard blocks to fullfill the request
  * @return true if any blocks were discarded, false otherwise
  */
-bool HeapCompact(long size, bool bDiscard) {
+static bool HeapCompact(long size) {
 	const MEM_NODE *pHeap = &heapSentinel;
 	MEM_NODE *pCur, *pOldest;
 	uint32 oldest;		// time of the oldest discardable block
 
 	while (heapSentinel.size < size) {
 
-		if (!bDiscard)
-			// we cannot free enough without discarding blocks
-			return false;
-
 		// find the oldest discardable block
 		oldest = DwGetCurrentTime();
 		pOldest = NULL;
@@ -241,7 +239,7 @@
 #endif
 
 	// compact the heap to make up room for 'size' bytes, if necessary
-	if (!HeapCompact(size, true))
+	if (!HeapCompact(size))
 		return 0;
 
 	// success! we may allocate a new node of the right size
@@ -259,9 +257,13 @@
 	// Subtract size of new block from total
 	heapSentinel.size -= size;
 
+#ifdef DEBUG
+	MemoryStats();
+#endif
+
 	// Set flags, LRU time and size
 	pNode->flags = DWM_USED;
-	pNode->lruTime = DwGetCurrentTime();
+	pNode->lruTime = DwGetCurrentTime() + 1;
 	pNode->size = size;
 
 	// set mnode at the end of the list
@@ -320,7 +322,7 @@
 			pNode->pPrev = 0;
 			pNode->pBaseAddr = (byte *)malloc(size);
 			pNode->size = size;
-			pNode->lruTime = DwGetCurrentTime();
+			pNode->lruTime = DwGetCurrentTime() + 1;
 			pNode->flags = DWM_USED;
 
 			// Subtract size of new block from total
@@ -351,6 +353,10 @@
 		free(pMemNode->pBaseAddr);
 		heapSentinel.size += pMemNode->size;
 
+#ifdef DEBUG
+		MemoryStats();
+#endif
+
 		// mark the node as discarded
 		pMemNode->flags |= DWM_DISCARDED;
 		pMemNode->pBaseAddr = NULL;
@@ -374,11 +380,34 @@
 	// set the lock flag
 	pMemNode->flags |= DWM_LOCKED;
 
+#ifdef DEBUG
+	MemoryStats();
+#endif
+
 	// return memory objects base address
 	return pMemNode->pBaseAddr;
 }
 
 /**
+ * Unlocks a memory object.
+ * @param pMemNode		Node of the memory object
+ */
+void MemoryUnlock(MEM_NODE *pMemNode) {
+	// make sure memory object is already locked
+	assert(pMemNode->flags & DWM_LOCKED);
+
+	// clear the lock flag
+	pMemNode->flags &= ~DWM_LOCKED;
+
+#ifdef DEBUG
+	MemoryStats();
+#endif
+
+	// update the LRU time
+	pMemNode->lruTime = DwGetCurrentTime();
+}
+
+/**
  * Changes the size of a specified memory object and re-allocate it if necessary.
  * @param pMemNode		Node of the memory object
  * @param size			New size of block
@@ -425,21 +454,6 @@
 }
 
 /**
- * Unlocks a memory object.
- * @param pMemNode		Node of the memory object
- */
-void MemoryUnlock(MEM_NODE *pMemNode) {
-	// make sure memory object is already locked
-	assert(pMemNode->flags & DWM_LOCKED);
-
-	// clear the lock flag
-	pMemNode->flags &= ~DWM_LOCKED;
-
-	// update the LRU time
-	pMemNode->lruTime = DwGetCurrentTime();
-}
-
-/**
  * Touch a memory object by updating its LRU time.
  * @param pMemNode		Node of the memory object
  */

Modified: scummvm/trunk/engines/tinsel/heapmem.h
===================================================================
--- scummvm/trunk/engines/tinsel/heapmem.h	2009-10-27 00:37:23 UTC (rev 45427)
+++ scummvm/trunk/engines/tinsel/heapmem.h	2009-10-27 00:37:54 UTC (rev 45428)
@@ -66,10 +66,6 @@
 // Dereference a given memory node
 uint8 *MemoryDeref(MEM_NODE *pMemNode);
 
-bool HeapCompact(		// Allocates the specified number of bytes from the specified heap
-	long size,		// number of bytes to free up
-	bool bDiscard);		// when set - will discard blocks to fullfill the request
-
 } // End of namespace Tinsel
 
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list