Control memory with a mutex instead of a semaphore

Mutex locks, unlike semaphore locks, cannot fail, but they must stay
within their thread.  The memory lock is unlocked at all procedure
boundaries; thus, it is safe to use a normal mutex.


git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1225 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2003-09-13 07:10:43 +00:00
parent ddee2625cb
commit 96e1f955a0
+21 -21
View File
@@ -43,7 +43,7 @@ BOOLEAN leak_debug;
int leak_idx = -1; int leak_idx = -1;
int leak_size = -1; int leak_size = -1;
#endif #endif
Semaphore _MemorySem; static Mutex _MemoryLock;
#define MAX_EXTENTS 100000 #define MAX_EXTENTS 100000
@@ -277,7 +277,7 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority,
{ {
szMemoryNode *node; szMemoryNode *node;
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
#ifdef MEM_DEBUG #ifdef MEM_DEBUG
CheckMemory(); CheckMemory();
@@ -307,11 +307,11 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority,
node = node; node = node;
#endif /* LEAK_DEBUG */ #endif /* LEAK_DEBUG */
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (node->handle); return (node->handle);
} }
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
(void) priority; /* Satisfying compiler (unused parameter) */ (void) priority; /* Satisfying compiler (unused parameter) */
(void) usage; /* Satisfying compiler (unused parameter) */ (void) usage; /* Satisfying compiler (unused parameter) */
return (0); return (0);
@@ -342,13 +342,13 @@ mem_allocate (MEM_SIZE coreSize, MEM_FLAGS flags, MEM_PRIORITY priority,
MEM_SIZE MEM_SIZE
mem_get_size (MEM_HANDLE h) mem_get_size (MEM_HANDLE h)
{ {
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h)
{ {
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return ((int)extents[h - 1].size); return ((int)extents[h - 1].size);
} }
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (0); return (0);
} }
@@ -382,8 +382,8 @@ mem_init (void)
{ {
int i; int i;
_MemorySem = CreateSemaphore (1, "Memory"); _MemoryLock = CreateMutex ();
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
freeListHead = &extents[0]; freeListHead = &extents[0];
for (i=0; i<MAX_EXTENTS; i++) for (i=0; i<MAX_EXTENTS; i++)
@@ -395,7 +395,7 @@ mem_init (void)
} }
extents[MAX_EXTENTS-1].next = NULL; extents[MAX_EXTENTS-1].next = NULL;
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return TRUE; return TRUE;
} }
@@ -425,7 +425,7 @@ mem_uninit(void)
{ {
int i; int i;
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
for (i=0; i<MAX_EXTENTS; i++) for (i=0; i<MAX_EXTENTS; i++)
{ {
@@ -446,7 +446,7 @@ mem_uninit(void)
} }
freeListHead = 0; freeListHead = 0;
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (TRUE); return (TRUE);
} }
@@ -479,7 +479,7 @@ mem_release(MEM_HANDLE h)
if (h == 0) if (h == 0)
return (TRUE); return (TRUE);
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
--h; --h;
if (h < 0 || h >= MAX_EXTENTS) if (h < 0 || h >= MAX_EXTENTS)
@@ -501,11 +501,11 @@ mem_release(MEM_HANDLE h)
extents[h].handle = -1; extents[h].handle = -1;
extents[h].next = freeListHead; extents[h].next = freeListHead;
freeListHead = &extents[h]; freeListHead = &extents[h];
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return TRUE; return TRUE;
} }
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return FALSE; return FALSE;
} }
@@ -535,16 +535,16 @@ mem_release(MEM_HANDLE h)
void * void *
mem_simple_access(MEM_HANDLE h) mem_simple_access(MEM_HANDLE h)
{ {
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h)
{ {
++extents[h - 1].refcount; ++extents[h - 1].refcount;
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (extents[h - 1].memory); return (extents[h - 1].memory);
} }
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (0); return (0);
} }
@@ -574,16 +574,16 @@ mem_simple_access(MEM_HANDLE h)
MEM_BOOL MEM_BOOL
mem_simple_unaccess(MEM_HANDLE h) mem_simple_unaccess(MEM_HANDLE h)
{ {
SetSemaphore (_MemorySem); LockMutex (_MemoryLock);
if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h) if (h > 0 && h <= MAX_EXTENTS && extents[h - 1].handle == h)
{ {
if (extents[h - 1].refcount) if (extents[h - 1].refcount)
--extents[h - 1].refcount; --extents[h - 1].refcount;
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (1); return (1);
} }
ClearSemaphore (_MemorySem); UnlockMutex (_MemoryLock);
return (0); return (0);
} }