/* *********************************************************************
* file: memory.cc - 1/21/96 -- Christopher J. Dickey *
* This is a set of classes to handle memory for the mud. There's *
* not a whole lot of fancy stuff behind them, but they do help in *
* efficiency. Basically, we never really delete any memory unless *
* absolutely necessary, and we keep stacks of the most used *
* objects. *
* (c) 1996-1997 Christopher J. Dickey, Andrew Hynek, *
* (c) 2001 The AwakeMUD Consortium *
********************************************************************* */
#include <assert.h>
#include <stdio.h>
#include "memory.h"
#include "structs.h"
#include "awake.h"
#include "db.h"
#include "utils.h"
memoryClass::memoryClass()
{
Room = new stackClass<struct room_data>;
Obj = new stackClass<struct obj_data>;
Ch = new stackClass<struct char_data>;
}
memoryClass::memoryClass(const memoryClass & mClass)
{
Obj = new stackClass<obj_data>;
Obj = mClass.Obj;
Ch = new stackClass<char_data>;
Ch = mClass.Ch;
}
memoryClass::~memoryClass()
{
}
struct obj_data *memoryClass::GetObject()
{
if (Obj->StackIsEmpty()) {
struct obj_data *temp = new obj_data;
clear_object(temp);
return temp;
} else return Obj->Pop();
}
struct char_data *memoryClass::GetCh()
{
if (Ch->StackIsEmpty())
{
struct char_data *temp = new char_data;
clear_char(temp);
return temp;
}
else
return Ch->Pop();
}
struct room_data *memoryClass::GetRoom()
{
if (Room->StackIsEmpty())
{
struct room_data *temp = new room_data;
// clear room just zeros the room structure, which is all we need to
// do since it's fresh
clear_room(temp);
return temp;
}
else
return Room->Pop();
}
void memoryClass::DeleteObject(struct obj_data *obj)
{
// we want to do this so that when we pop em off, they are usable
free_obj(obj);
Obj->Push(obj);
}
void memoryClass::DeleteCh(struct char_data *ch)
{
free_char(ch);
Ch->Push(ch);
}
void memoryClass::DeleteRoom(struct room_data *room)
{
free_room(room);
Room->Push(room);
}
void memoryClass::ClearObject(struct obj_data *obj)
{
clear_object(obj);
Obj->Push(obj);
}
void memoryClass::ClearCh(struct char_data *ch)
{
clear_char(ch);
Ch->Push(ch);
}
void memoryClass::ClearRoom(struct room_data *room)
{
clear_room(room);
Room->Push(room);
}
bool memoryClass::ClearStacks()
{
// if all the stacks are empty, return FALSE
if (Obj->StackIsEmpty() && Ch->StackIsEmpty() && Room->StackIsEmpty())
return FALSE;
// and here we just clean up the stacks, hoping to free some memory
while (!Obj->StackIsEmpty())
Obj->PopDelete();
while (!Ch->StackIsEmpty())
Ch->PopDelete();
while (!Room->StackIsEmpty())
Room->PopDelete();
return TRUE;
}