📄 memory.c
字号:
/* memory.c, HAWK game engine
*
* Copyright 1997-1998 by Phil Frisbie, Jr.
* for Hawk Software
*
*/
#ifdef WIN32
#include "hardware.h"
#endif
#include "hawk.h"
#include "internal.h"
#include <io.h>
/* Memory functions */
#define MAX_BLOCKS 4
#define TEMP_BLOCK_SIZE ((256*256 *4)+2<<16) /* largest texture size used */
void *blocks[MAX_BLOCKS];
int blockused[MAX_BLOCKS];
int level_memory = 0;
int game_memory = 0;
#define MAX_TAGS 32000
void **tags = NULL;
int *tagused = NULL;
/* Temp memory is used to speed up common tasks like loading textures */
BOOL initTempMemory(void)
{
int i;
for(i=0;i<MAX_BLOCKS;i++)
{
blocks[i]= TagMalloc(TEMP_BLOCK_SIZE, TAG_GAME);
if(!blocks[i])
return FALSE;
blockused[i] = FALSE;
}
return TRUE;
}
void *TempMalloc(int size)
{
int i;
if(size > TEMP_BLOCK_SIZE)
return(malloc(size));
for(i=0;i<MAX_BLOCKS;i++)
{
if(!blockused[i])
{
blockused[i] = TRUE;
return blocks[i];
}
}
if(i==MAX_BLOCKS) /* we could not find an unused block */
return(malloc(size));
}
void TempFree(void *p)
{
int i;
for(i=0;i<MAX_BLOCKS;i++)
{
if(blockused[i])
{
if(blocks[i] == p)
{
blockused[i] = FALSE;
return;
}
}
}
if(i==MAX_BLOCKS) /* we could not find the block */
{
free(p);
return;
}
}
void *TagMalloc(int size, int tag)
{
static int i = 0;
tagused[i] = tag;
tags[i] = malloc(size);
if(tag==TAG_LEVEL)
level_memory += size;
else if(tag==TAG_GAME)
game_memory += size;
return tags[i++];
}
void FreeTags(int tag)
{
int i;
for(i=0;i<MAX_TAGS;i++)
{
if(tagused[i]==tag)
{
void *t = tags[i];
free(tags[i]);
tags[i] = NULL;
tagused[i] = FALSE;
}
}
}
BOOL initTagMemory(void)
{
int i;
tags = malloc(MAX_TAGS * sizeof(void *));
if(!tags)
{
free(tags);
tags = NULL;
return FALSE;
}
tagused = malloc(MAX_TAGS * sizeof(int));
if(!tagused)
{
free(tags);
tags = NULL;
free(tagused);
tagused = NULL;
return FALSE;
}
for(i=1;i<MAX_TAGS;i++)
{
tagused[i] = FALSE;
tags[i] = NULL;
}
return TRUE;
}
BOOL initMemory(void)
{
if(!initTagMemory())
{
return FALSE;
}
if(!initTempMemory())
{
return FALSE;
}
return TRUE;
}
void memShutdown(void)
{
logf("\nFreeing memory\n");
logf("Level memory allocated: %d\n", level_memory);
logf("Game memory allocated: %d\n", game_memory);
if(tags)
{
FreeTags(TAG_GAME);
FreeTags(TAG_LEVEL);
free(tags);
}
if(tagused)
free(tagused);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -