Procházet zdrojové kódy

core: disable our rare uses of realloc() with a proper Array<> object.

legacy
Sam Hocevar sam před 12 roky
rodič
revize
96342ddb0c
2 změnil soubory, kde provedl 22 přidání a 33 odebrání
  1. +16
    -20
      src/dict.cpp
  2. +6
    -13
      src/map.cpp

+ 16
- 20
src/dict.cpp Zobrazit soubor

@@ -36,8 +36,6 @@ class DictData

public:
DictData() :
entities(0),
maxid(0),
nentities(0)
{
/* Nothing to do */
@@ -49,12 +47,11 @@ public:
if (nentities)
Log::Error("still %i entities in dict\n", nentities);
#endif
free(entities);
}

private:
Entity **entities;
int maxid, nentities;
Array<Entity *> m_entities;
int nentities;
};

/*
@@ -77,9 +74,9 @@ int Dict::MakeSlot(char const *name)

/* If the entry is already registered, remember its ID. Look for an
* empty slot at the same time. */
for (slotid = 0; slotid < data->maxid; slotid++)
for (slotid = 0; slotid < data->m_entities.Count(); slotid++)
{
Entity *e = data->entities[slotid];
Entity *e = data->m_entities[slotid];
if (!e)
{
empty = slotid;
@@ -104,22 +101,21 @@ int Dict::MakeSlot(char const *name)
}

/* If this is a new entry, create a new slot for it. */
if (slotid == data->maxid || !data->entities[slotid])
if (slotid == data->m_entities.Count() || !data->m_entities[slotid])
{
if (slotid == data->maxid)
if (slotid == data->m_entities.Count())
{
empty = data->maxid++;
data->entities = (Entity **)realloc(data->entities,
data->maxid * sizeof(Entity *));
empty = data->m_entities.Count();
data->m_entities.Push(NULL);
}

data->entities[empty] = NULL;
data->m_entities[empty] = NULL;
slotid = empty;
data->nentities++;
}
else
{
Ticker::Ref(data->entities[slotid]);
Ticker::Ref(data->m_entities[slotid]);
}

return slotid;
@@ -127,17 +123,17 @@ int Dict::MakeSlot(char const *name)

void Dict::RemoveSlot(int slotid)
{
if (Ticker::Unref(data->entities[slotid]) == 0)
if (Ticker::Unref(data->m_entities[slotid]) == 0)
{
data->entities[slotid] = NULL;
data->m_entities[slotid] = NULL;
data->nentities--;
}
}

void Dict::RemoveSlot(Entity *entity)
{
for (int slotid = 0; slotid < data->maxid; slotid++)
if (data->entities[slotid] == entity)
for (int slotid = 0; slotid < data->m_entities.Count(); slotid++)
if (data->m_entities[slotid] == entity)
{
RemoveSlot(slotid);
return;
@@ -152,12 +148,12 @@ void Dict::RemoveSlot(Entity *entity)
void Dict::SetEntity(int slotid, Entity *entity)
{
Ticker::Ref(entity);
data->entities[slotid] = entity;
data->m_entities[slotid] = entity;
}

Entity *Dict::GetEntity(int slotid)
{
return data->entities[slotid];
return data->m_entities[slotid];
}

} /* namespace lol */


+ 6
- 13
src/map.cpp Zobrazit soubor

@@ -38,8 +38,7 @@ private:
TileSet *tilesets[MAX_TILESETS];
int ntilers;

Layer **layers;
int nlayers;
Array<Layer *> m_layers;

int width, height;
};
@@ -52,8 +51,6 @@ Map::Map(char const *path)
: data(new MapData())
{
data->ntilers = 0;
data->layers = NULL;
data->nlayers = 0;
data->width = 0;
data->height = 0;

@@ -120,8 +117,7 @@ Map::Map(char const *path)
{
Layer *l = new Layer(data->width, data->height,
level, orientation, tiles);
data->layers[data->nlayers] = l;
data->nlayers++;
data->m_layers.Push(l);
tiles = NULL;
//Log::Debug("new layer %ix%i\n", data->width, data->height);
}
@@ -143,8 +139,6 @@ Map::Map(char const *path)
"width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
{
/* This is a layer description. Prepare to read the data. */
data->layers = (Layer **)realloc(data->layers,
sizeof(Layer **) * (data->nlayers + 1));
orientation = toupper(a) == 'V' ? 1 : 0;
level = i * 32;
data->width = j;
@@ -161,16 +155,15 @@ Map::~Map()
{
for (int i = 0; i < data->ntilers; i++)
Tiler::Deregister(data->tilesets[i]);
for (int i = 0; i < data->nlayers; i++)
delete data->layers[i];
free(data->layers);
for (int i = 0; i < data->m_layers.Count(); i++)
delete data->m_layers[i];
delete data;
}

void Map::Render(int x, int y, int z)
{
for (int i = 0; i < data->nlayers; i++)
data->layers[i]->Render(x, y, z);
for (int i = 0; i < data->m_layers.Count(); i++)
data->m_layers[i]->Render(x, y, z);
}

int Map::GetWidth()


Načítá se…
Zrušit
Uložit