First steps towards consistent use of HMalloc.

DRAWABLE, FONT, and a few others which mix memory handles with flags still remain; this patch catches the low-hanging fruit.

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2890 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2007-12-31 00:42:33 +00:00
parent c804dcfce7
commit 91e0374f9e
19 changed files with 83 additions and 158 deletions
+4 -4
View File
@@ -51,7 +51,7 @@ FreeKernel (void)
UninitPlayerInput ();
DestroyDrawable (ReleaseDrawable (Screen));
DestroyContext (ReleaseContext (ScreenContext));
DestroyContext (ScreenContext);
UninitVideoPlayer ();
UninitSound ();
@@ -63,9 +63,9 @@ UninitContexts (void)
{
UninitQueue (&disp_q);
DestroyContext (ReleaseContext (OffScreenContext));
DestroyContext (ReleaseContext (SpaceContext));
DestroyContext (ReleaseContext (StatusContext));
DestroyContext (OffScreenContext);
DestroyContext (SpaceContext);
DestroyContext (StatusContext);
}
static void
+4 -4
View File
@@ -1238,7 +1238,7 @@ DoCommunication (ENCOUNTER_STATE *pES)
CommData.AlienTransitionDesc.AnimFlags &= ~(TALK_INTRO | TALK_DONE);
SetContext (SpaceContext);
DestroyContext (ReleaseContext (TaskContext));
DestroyContext (TaskContext);
TaskContext = 0;
UnlockMutex (GraphicsLock);
@@ -1333,7 +1333,7 @@ HailAlien (void)
SubtitleText.align = CommData.AlienTextAlign;
// init subtitle cache context
TextCacheContext = CaptureContext (CreateContext ());
TextCacheContext = CreateContext ();
TextCacheFrame = CaptureDrawable (
CreateDrawable (WANT_PIXMAP, SIS_SCREEN_WIDTH,
SIS_SCREEN_HEIGHT - SLIDER_Y - SLIDER_HEIGHT + 2, 1));
@@ -1354,7 +1354,7 @@ HailAlien (void)
{
RECT r;
TaskContext = CaptureContext (CreateContext ());
TaskContext = CreateContext ();
SetContext (TaskContext);
SetContextFGFrame (Screen);
GetFrameRect (CommData.AlienFrame, &r);
@@ -1420,7 +1420,7 @@ HailAlien (void)
DestroyFont (ReleaseFont (CommData.AlienFont));
DestroyDrawable (ReleaseDrawable (CommData.AlienFrame));
DestroyContext (ReleaseContext (TextCacheContext));
DestroyContext (TextCacheContext);
DestroyDrawable (ReleaseDrawable (TextCacheFrame));
SetContext (SpaceContext);
+4 -4
View File
@@ -314,8 +314,8 @@ credit_roll_task (void *data)
TextBack = BUILD_COLOR (MAKE_RGB15 (0x00, 0x00, 0x00), 0x00);
TextFore = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F);
LocalContext = CaptureContext (CreateContext ());
DrawContext = CaptureContext (CreateContext ());
LocalContext = CreateContext ();
DrawContext = CreateContext ();
total_h = disp_h = SCREEN_HEIGHT;
@@ -530,8 +530,8 @@ credit_roll_task (void *data)
}
}
DestroyContext (ReleaseContext (DrawContext));
DestroyContext (ReleaseContext (LocalContext));
DestroyContext (DrawContext);
DestroyContext (LocalContext);
// free remaining frames
DestroyDrawable (ReleaseDrawable (Frame));
+5 -2
View File
@@ -19,6 +19,10 @@
#include "displist.h"
#include "libs/log.h"
#ifdef QUEUE_TABLE
#define NULL_HANDLE NULL
#endif
/*
* This file contains code for generic doubly linked lists.
* If QUEUE_TABLE is defined, each lists has its own preallocated
@@ -40,7 +44,7 @@ InitQueue (QUEUE *pq, COUNT num_elements, OBJ_SIZE size)
log_add (log_Debug, "InitQueue(): num_elements = %d (%d)",
num_elements, (BYTE)num_elements);
#endif
if (AllocQueueTab (pq, num_elements) && LockQueueTab (pq))
if (AllocQueueTab (pq, num_elements) != NULL)
{
do
FreeLink (pq, GetLinkAddr (pq, num_elements));
@@ -60,7 +64,6 @@ UninitQueue (QUEUE *pq)
SetHeadLink (pq, NULL_HANDLE);
SetTailLink (pq, NULL_HANDLE);
SetFreeList (pq, NULL_HANDLE);
UnlockQueueTab (pq);
FreeQueueTab (pq);
return (TRUE);
+7 -9
View File
@@ -20,8 +20,9 @@
#define _DISPLIST_H
#include <assert.h>
#include "libs/memlib.h"
#include "port.h"
#include "compiler.h"
#include "misc.h"
// Note that we MUST use the QUEUE_TABLE variant at this time, because
// certain gameplay elements depend on it. Namely, the maximum number
@@ -34,6 +35,7 @@
#ifdef QUEUE_TABLE
typedef void* QUEUE_HANDLE;
#else /* !QUEUE_TABLE */
#include "libs/memlib.h"
typedef MEM_HANDLE QUEUE_HANDLE;
#endif /* QUEUE_TABLE */
@@ -55,7 +57,6 @@ typedef struct /* queue */
#ifdef QUEUE_TABLE
BYTE *pq_tab;
HLINK free_list;
MEM_HANDLE hq_tab;
#endif
COUNT object_size;
#ifdef QUEUE_TABLE
@@ -89,16 +90,13 @@ UnlockLink (const QUEUE *pq, HLINK h)
#define GetFreeList(pq) (pq)->free_list
#define SetFreeList(pq, h) (pq)->free_list = (h)
#define AllocQueueTab(pq,n) \
((pq)->hq_tab = mem_allocate ((MEM_SIZE)((COUNT)(pq)->object_size * \
(COUNT)((pq)->num_objects = (BYTE)(n))), \
MEM_PRIMARY))
#define LockQueueTab(pq) ((pq)->pq_tab = (BYTE*)mem_lock ((pq)->hq_tab))
#define UnlockQueueTab(pq) mem_unlock ((pq)->hq_tab)
#define FreeQueueTab(pq) mem_release ((pq)->hq_tab); (pq)->hq_tab = 0
((pq)->pq_tab = HMalloc (((COUNT)(pq)->object_size * \
(COUNT)((pq)->num_objects = (BYTE)(n)))))
#define FreeQueueTab(pq) HFree ((pq)->pq_tab); (pq)->pq_tab = NULL
#define SizeQueueTab(pq) (COUNT)((pq)->num_objects)
#define GetLinkAddr(pq,i) (HLINK)((pq)->pq_tab + ((pq)->object_size * ((i) - 1)))
#else /* !QUEUE_TABLE */
#define AllocLink(pq) (HLINK)mem_request ((pq)->object_size)
#define AllocLink(pq) (HLINK)mem_allocate ((pq)->object_size, DEFAULT_MEM_FLAGS)
#define LockLink(pq, h) (LINK*)mem_lock (h)
#define UnlockLink(pq, h) mem_unlock (h)
#define FreeLink(pq,h) mem_release (h)
+2 -13
View File
@@ -84,7 +84,7 @@ enum
MEM_HANDLE hData;
which_res = GetResFileChar (fp);
hData = mem_request (sizeof (CODERES_STRUCT));
hData = mem_allocate (sizeof (CODERES_STRUCT), DEFAULT_MEM_FLAGS);
if (hData)
{
RACE_DESC *RDPtr;
@@ -373,15 +373,4 @@ ReleaseCodeRes (void *CodeRef)
}
return (0);
}
DRAWABLE
CreatePixmapRegion (FRAME Frame, POINT *lpOrg, SIZE width, SIZE height)
{
(void) lpOrg; /* Satisfying compiler (unused parameter) */
(void) width; /* Satisfying compiler (unused parameter) */
(void) height; /* Satisfying compiler (unused parameter) */
return (GetFrameHandle (Frame));
}
}
+2 -2
View File
@@ -125,7 +125,7 @@ CreateRadar (void)
RECT r;
CONTEXT OldContext;
RadarContext = CaptureContext (CreateContext ());
RadarContext = CreateContext ();
OldContext = SetContext (RadarContext);
SetContextFGFrame (Screen);
r.corner.x = RADAR_X;
@@ -372,7 +372,7 @@ InitSIS (void)
void
FreeSC2Data (void)
{
DestroyContext (ReleaseContext (RadarContext));
DestroyContext (RadarContext);
RadarContext = 0;
DestroyDrawable (ReleaseDrawable (FontGradFrame));
FontGradFrame = 0;
+6 -9
View File
@@ -29,6 +29,7 @@
#include <string.h>
#include <ctype.h>
#include "libs/decomp/lzh.h"
#include "reslib.h"
PLZHCODE_DESC _lpCurCodeDesc;
STREAM_TYPE _StreamType;
@@ -103,7 +104,6 @@ StartHuff (void)
DECODE_REF
copen (void *InStream, STREAM_TYPE SType, STREAM_MODE SMode)
{
MEM_HANDLE h;
DWORD StreamLength;
_StreamType = SType;
@@ -132,17 +132,14 @@ copen (void *InStream, STREAM_TYPE SType, STREAM_MODE SMode)
StreamLength = MAKE_DWORD (loword, hiword);
}
h = 0;
if (StreamLength == 0xFFFFFFFF
|| (h = AllocCodeDesc ()) == 0
|| (_lpCurCodeDesc = LockCodeDesc (h)) == 0)
|| (_lpCurCodeDesc = AllocCodeDesc ()) == NULL)
{
_lpCurCodeDesc = 0;
FreeCodeDesc (h);
FreeCodeDesc (_lpCurCodeDesc);
_lpCurCodeDesc = NULL;
}
else
{
_lpCurCodeDesc->fh = h;
_lpCurCodeDesc->Stream = _Stream;
_lpCurCodeDesc->StreamType = _StreamType;
_lpCurCodeDesc->StreamMode = SMode;
@@ -168,8 +165,8 @@ cclose (PLZHCODE_DESC lpCodeDesc)
(*_lpCurCodeDesc->CleanupFunc) ();
StreamIndex = lpCodeDesc->StreamIndex;
UnlockCodeDesc (lpCodeDesc->fh);
FreeCodeDesc (lpCodeDesc->fh);
FreeCodeDesc (lpCodeDesc);
_lpCurCodeDesc = NULL;
return (StreamIndex);
}
+14 -19
View File
@@ -26,38 +26,34 @@
#include <stdio.h>
#include "lzh.h"
#include "reslib.h"
static UWORD match_position, match_length;
static SWORD *lson;
static SWORD *rson;
static SWORD *dad;
static SWORD *encode_arrays;
#define AllocEncodeArrays() \
mem_allocate ( \
HCalloc ( \
(MEM_SIZE)(((N + 1) + (N + 257) + (N + 1)) \
* sizeof (lson[0]) + sizeof (MEM_HANDLE)), \
MEM_ZEROINIT)
#define LockCodeArrays (SWORD*)mem_lock
#define UnlockCodeArrays mem_unlock
#define FreeCodeArrays mem_release
* sizeof (lson[0])))
#define FreeCodeArrays HFree
static BOOLEAN
InitTree (void)
{
MEM_HANDLE h;
if ((h = AllocEncodeArrays ()) == 0
|| (lson = LockCodeArrays (h)) == 0)
if ((encode_arrays = AllocEncodeArrays ()) == NULL)
{
FreeCodeArrays (h);
FreeCodeArrays (encode_arrays);
encode_arrays = NULL;
return (FALSE);
}
else
{
SWORD i;
*(MEM_HANDLE *)lson = h;
lson = (SWORD*)((BYTE*)lson + sizeof (h));
lson = encode_arrays;
rson = lson + (N + 1);
dad = rson + (N + 257);
@@ -277,18 +273,17 @@ EncodePosition (UWORD c)
static void
UninitTree (void)
{
MEM_HANDLE h;
if (_workbuflen)
{
OutChar ((BYTE)(_workbuf >> 8));
++_lpCurCodeDesc->StreamIndex;
}
lson = (SWORD*)((BYTE*)lson - sizeof (h));
h = *(MEM_HANDLE *)lson;
UnlockCodeArrays (h);
FreeCodeArrays (h);
FreeCodeArrays (encode_arrays);
encode_arrays = NULL;
lson = NULL;
rson = NULL;
dad = NULL;
}
static void
+3 -8
View File
@@ -19,8 +19,8 @@
#ifndef _LZH_H
#define _LZH_H
#include "reslib.h"
#include "declib.h"
#include "misc.h"
/* LZSS Parameters */
@@ -41,8 +41,6 @@
struct _LZHCODE_DESC
{
MEM_HANDLE fh;
COUNT buf_index, restart_index, bytes_left;
BYTE text_buf[N + F - 1];
/* reconstruct freq tree */
@@ -77,11 +75,8 @@ typedef LZHCODE_DESC *PLZHCODE_DESC;
(*_Stream++ = (BYTE)(c)))
#define AllocCodeDesc() \
mem_allocate ((MEM_SIZE)sizeof (LZHCODE_DESC), MEM_ZEROINIT)
#define LockCodeDesc (PLZHCODE_DESC)mem_lock
#define UnlockCodeDesc mem_unlock
#define FreeCodeDesc mem_release
#define AllocCodeDesc() HCalloc ((MEM_SIZE)sizeof (LZHCODE_DESC))
#define FreeCodeDesc HFree
extern void _update (COUNT c);
extern void StartHuff (void);
+2 -6
View File
@@ -131,8 +131,6 @@ typedef struct
STAMP IntersectStamp;
} INTERSECT_CONTROL;
typedef MEM_HANDLE CONTEXT_REF;
typedef DWORD DRAWABLE;
#define BUILD_DRAWABLE(h,i) ((DRAWABLE)MAKE_DWORD(h,i))
@@ -172,8 +170,6 @@ extern BOOLEAN InitGraphics (int argc, char *argv[], COUNT
extern void UninitGraphics (void);
extern CONTEXT SetContext (CONTEXT Context);
extern CONTEXT CaptureContext (CONTEXT_REF ContextRef);
extern CONTEXT_REF ReleaseContext (CONTEXT Context);
extern COLOR SetContextForeGroundColor (COLOR Color);
extern COLOR SetContextBackGroundColor (COLOR Color);
extern FRAME SetContextFGFrame (FRAME Frame);
@@ -196,8 +192,8 @@ extern void UnbatchGraphics (void);
extern void FlushGraphics (void);
extern void ClearBackGround (RECT *pClipRect);
extern void ClearDrawable (void);
extern CONTEXT_REF CreateContext (void);
extern BOOLEAN DestroyContext (CONTEXT_REF ContextRef);
extern CONTEXT CreateContext (void);
extern BOOLEAN DestroyContext (CONTEXT ContextRef);
extern DRAWABLE CreateDisplay (CREATE_FLAGS CreateFlags, SIZE *pwidth,
SIZE *pheight);
extern DRAWABLE CreateDrawable (CREATE_FLAGS CreateFlags, SIZE width,
+11 -38
View File
@@ -65,67 +65,40 @@ SetContext (CONTEXT Context)
return (LastContext);
}
CONTEXT_REF
CONTEXT
CreateContext (void)
{
CONTEXT_REF ContextRef;
CONTEXT NewContext;
ContextRef = AllocContext ();
if (ContextRef)
NewContext = AllocContext ();
if (NewContext)
{
CONTEXT OldContext;
/* initialize context */
OldContext = SetContext (CaptureContext (ContextRef));
OldContext = SetContext (NewContext);
SetContextForeGroundColor (
BUILD_COLOR (MAKE_RGB15 (0x1F, 0x1F, 0x1F), 0x0F));
SetContextBackGroundColor (
BUILD_COLOR (MAKE_RGB15 (0x00, 0x00, 0x00), 0x00));
SetContextClipping (TRUE);
ReleaseContext (SetContext (OldContext));
SetContext (OldContext);
}
return (ContextRef);
return (NewContext);
}
BOOLEAN
DestroyContext (CONTEXT_REF ContextRef)
DestroyContext (CONTEXT ContextRef)
{
if (ContextRef == 0)
return (FALSE);
if (_pCurContext && _pCurContext->ContextRef == ContextRef)
if (_pCurContext && _pCurContext == ContextRef)
SetContext ((CONTEXT)0);
return (FreeContext (ContextRef));
}
CONTEXT
CaptureContext (CONTEXT_REF ContextRef)
{
CONTEXT ContextPtr;
ContextPtr = LockContext (ContextRef);
if (ContextPtr)
ContextPtr->ContextRef = ContextRef;
return (ContextPtr);
}
CONTEXT_REF
ReleaseContext (CONTEXT ContextPtr)
{
if (ContextPtr)
{
CONTEXT_REF ContextRef;
ContextRef = ContextPtr->ContextRef;
UnlockContext (ContextRef);
return (ContextRef);
}
return (0);
FreeContext (ContextRef);
return TRUE;
}
COLOR
+2 -9
View File
@@ -27,7 +27,6 @@ typedef UWORD FBK_FLAGS;
struct context_desc
{
CONTEXT_REF ContextRef;
UWORD Flags;
COLOR ForeGroundColor, BackGroundColor;
@@ -42,14 +41,8 @@ struct context_desc
};
#define CONTEXT_PRIORITY DEFAULT_MEM_PRIORITY
#define AllocContext() \
(CONTEXT_REF)mem_allocate ((MEM_SIZE)sizeof (CONTEXT_DESC), \
MEM_ZEROINIT | MEM_PRIMARY)
#define LockContext (CONTEXT)mem_lock
#define UnlockContext mem_unlock
#define FreeContext mem_release
#define AllocContext() HCalloc ((MEM_SIZE)sizeof (CONTEXT_DESC))
#define FreeContext HFree
extern CONTEXT _pCurContext;
extern PRIMITIVE _locPrim;
-9
View File
@@ -44,21 +44,12 @@ typedef struct mem_header {
#define GET_MEM_HEADER(addr) ((MEM_HEADER *) \
(((char *) addr) - sizeof (MEM_HEADER)))
//Newer verion from w_memlib.c to follow...
/*
extern MEM_BOOL mem_init (MEM_SIZE core_size, PMEM_SIZE pmin_addressable,
PSTR disk_name);*/
extern BOOLEAN mem_init (void);
extern BOOLEAN mem_uninit (void);
extern MEM_HANDLE mem_allocate (MEM_SIZE size, MEM_FLAGS flags);
extern BOOLEAN mem_release (MEM_HANDLE handle);
#define mem_request(size) \
mem_allocate((MEM_SIZE)(size), DEFAULT_MEM_FLAGS)
extern void* mem_lock (MEM_HANDLE handle);
extern BOOLEAN mem_unlock (MEM_HANDLE handle);
+2 -2
View File
@@ -113,7 +113,7 @@ GenerateChmmr (BYTE control)
CaptureStringTable (
LoadStringTable (CHMMR_BASE_STRTAB));
ScanContext = CaptureContext (CreateContext ());
ScanContext = CreateContext ();
SetContext (ScanContext);
SetContextFGFrame (Screen);
r.corner.x = (SIS_ORG_X + SIS_SCREEN_WIDTH) - MAP_WIDTH;
@@ -125,7 +125,7 @@ GenerateChmmr (BYTE control)
DoDiscoveryReport (MenuSounds);
SetContext (SpaceContext);
DestroyContext (ReleaseContext (ScanContext));
DestroyContext (ScanContext);
ScanContext = 0;
DestroyStringTable (ReleaseStringTable (
+2 -2
View File
@@ -152,7 +152,7 @@ LoadPlanet (FRAME SurfDefFrame)
StopMusic ();
TaskContext = CaptureContext (CreateContext ());
TaskContext = CreateContext ();
pPlanetDesc = pSolarSysState->pOrbitalDesc;
@@ -263,7 +263,7 @@ FreePlanet (void)
Orbit->ScratchArray = 0;
}
DestroyContext (ReleaseContext (TaskContext));
DestroyContext (TaskContext);
TaskContext = 0;
DestroyStringTable (ReleaseStringTable (
+2 -2
View File
@@ -1229,7 +1229,7 @@ ScanSystem (void)
(MAP_HEIGHT >> 1) << MAG_SHIFT;
LockMutex (GraphicsLock);
ScanContext = CaptureContext (CreateContext ());
ScanContext = CreateContext ();
SetContext (ScanContext);
MenuState.flash_rect0.extent.width = FLASH_WIDTH;
MenuState.flash_rect0.extent.height = FLASH_HEIGHT;
@@ -1263,7 +1263,7 @@ ScanSystem (void)
LockMutex (GraphicsLock);
SetContext (SpaceContext);
DestroyDrawable (ReleaseDrawable (MenuState.flash_frame0));
DestroyContext (ReleaseContext (ScanContext));
DestroyContext (ScanContext);
ScanContext = 0;
UnlockMutex (GraphicsLock);
}
+7 -12
View File
@@ -618,24 +618,21 @@ BOOLEAN
SaveGame (COUNT which_game, SUMMARY_DESC *SummPtr)
{
BOOLEAN success, made_room;
void *out_fp;
MEM_HANDLE h;
void *out_fp, *h;
DECODE_REF fh;
success = TRUE;
made_room = FALSE;
RetrySave:
h = mem_request (10 * 1024);
out_fp = mem_lock (h);
if (out_fp == 0
|| (fh = copen (out_fp, MEMORY_STREAM, STREAM_WRITE)) == 0)
h = HMalloc (10 * 1024);
if (h == 0
|| (fh = copen (h, MEMORY_STREAM, STREAM_WRITE)) == 0)
{
if (success)
{
success = FALSE;
made_room = TRUE;
mem_unlock (h);
mem_release (h);
HFree (h);
FreeSC2Data ();
log_add (log_Debug, "Insufficient room for save buffers"
@@ -821,11 +818,10 @@ RetrySave:
success = SaveSummary (SummPtr, out_fp);
// Then write the rest of the data.
if (success && WriteResFile (mem_lock (h), (COUNT)flen, 1,
if (success && WriteResFile (h, (COUNT)flen, 1,
out_fp) == 0)
success = FALSE;
mem_unlock (h);
if (res_CloseResFile ((uio_Stream *)out_fp) == 0)
success = FALSE;
@@ -837,8 +833,7 @@ RetrySave:
DeleteResFile (saveDir, file);
}
mem_unlock (h);
mem_release (h);
HFree (h);
if (made_room)
LoadSC2Data ();
+4 -4
View File
@@ -98,7 +98,7 @@ LoadKernel (int argc, char *argv[])
InitSound (argc, argv);
InitVideoPlayer (TRUE);
ScreenContext = CaptureContext (CreateContext ());
ScreenContext = CreateContext ();
if (ScreenContext == NULL)
return FALSE;
@@ -150,7 +150,7 @@ InitContexts (void)
{
RECT r;
StatusContext = CaptureContext (CreateContext ());
StatusContext = CreateContext ();
if (StatusContext == NULL)
return FALSE;
@@ -162,11 +162,11 @@ InitContexts (void)
r.extent.height = STATUS_HEIGHT;
SetContextClipRect (&r);
SpaceContext = CaptureContext (CreateContext ());
SpaceContext = CreateContext ();
if (SpaceContext == NULL)
return FALSE;
OffScreenContext = CaptureContext (CreateContext ());
OffScreenContext = CreateContext ();
if (OffScreenContext == NULL)
return FALSE;