Changeset 1985


Ignore:
Timestamp:
Oct 7, 2012, 2:34:59 PM (5 years ago)
Author:
sam
Message:

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

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/dict.cpp

    r1942 r1985  
    3737public:
    3838    DictData() :
    39         entities(0),
    40         maxid(0),
    4139        nentities(0)
    4240    {
     
    5048            Log::Error("still %i entities in dict\n", nentities);
    5149#endif
    52         free(entities);
    5350    }
    5451
    5552private:
    56     Entity **entities;
    57     int maxid, nentities;
     53    Array<Entity *> m_entities;
     54    int nentities;
    5855};
    5956
     
    7875    /* If the entry is already registered, remember its ID. Look for an
    7976     * empty slot at the same time. */
    80     for (slotid = 0; slotid < data->maxid; slotid++)
     77    for (slotid = 0; slotid < data->m_entities.Count(); slotid++)
    8178    {
    82         Entity *e = data->entities[slotid];
     79        Entity *e = data->m_entities[slotid];
    8380        if (!e)
    8481        {
     
    105102
    106103    /* If this is a new entry, create a new slot for it. */
    107     if (slotid == data->maxid || !data->entities[slotid])
     104    if (slotid == data->m_entities.Count() || !data->m_entities[slotid])
    108105    {
    109         if (slotid == data->maxid)
     106        if (slotid == data->m_entities.Count())
    110107        {
    111             empty = data->maxid++;
    112             data->entities = (Entity **)realloc(data->entities,
    113                                                 data->maxid * sizeof(Entity *));
     108            empty = data->m_entities.Count();
     109            data->m_entities.Push(NULL);
    114110        }
    115111
    116         data->entities[empty] = NULL;
     112        data->m_entities[empty] = NULL;
    117113        slotid = empty;
    118114        data->nentities++;
     
    120116    else
    121117    {
    122         Ticker::Ref(data->entities[slotid]);
     118        Ticker::Ref(data->m_entities[slotid]);
    123119    }
    124120
     
    128124void Dict::RemoveSlot(int slotid)
    129125{
    130     if (Ticker::Unref(data->entities[slotid]) == 0)
     126    if (Ticker::Unref(data->m_entities[slotid]) == 0)
    131127    {
    132         data->entities[slotid] = NULL;
     128        data->m_entities[slotid] = NULL;
    133129        data->nentities--;
    134130    }
     
    137133void Dict::RemoveSlot(Entity *entity)
    138134{
    139     for (int slotid = 0; slotid < data->maxid; slotid++)
    140         if (data->entities[slotid] == entity)
     135    for (int slotid = 0; slotid < data->m_entities.Count(); slotid++)
     136        if (data->m_entities[slotid] == entity)
    141137        {
    142138            RemoveSlot(slotid);
     
    153149{
    154150    Ticker::Ref(entity);
    155     data->entities[slotid] = entity;
     151    data->m_entities[slotid] = entity;
    156152}
    157153
    158154Entity *Dict::GetEntity(int slotid)
    159155{
    160     return data->entities[slotid];
     156    return data->m_entities[slotid];
    161157}
    162158
  • trunk/src/map.cpp

    r1513 r1985  
    3939    int ntilers;
    4040
    41     Layer **layers;
    42     int nlayers;
     41    Array<Layer *> m_layers;
    4342
    4443    int width, height;
     
    5352{
    5453    data->ntilers = 0;
    55     data->layers = NULL;
    56     data->nlayers = 0;
    5754    data->width = 0;
    5855    data->height = 0;
     
    121118                Layer *l = new Layer(data->width, data->height,
    122119                                     level, orientation, tiles);
    123                 data->layers[data->nlayers] = l;
    124                 data->nlayers++;
     120                data->m_layers.Push(l);
    125121                tiles = NULL;
    126122                //Log::Debug("new layer %ix%i\n", data->width, data->height);
     
    144140        {
    145141            /* This is a layer description. Prepare to read the data. */
    146             data->layers = (Layer **)realloc(data->layers,
    147                                        sizeof(Layer **) * (data->nlayers + 1));
    148142            orientation = toupper(a) == 'V' ? 1 : 0;
    149143            level = i * 32;
     
    162156    for (int i = 0; i < data->ntilers; i++)
    163157        Tiler::Deregister(data->tilesets[i]);
    164     for (int i = 0; i < data->nlayers; i++)
    165         delete data->layers[i];
    166     free(data->layers);
     158    for (int i = 0; i < data->m_layers.Count(); i++)
     159        delete data->m_layers[i];
    167160    delete data;
    168161}
     
    170163void Map::Render(int x, int y, int z)
    171164{
    172     for (int i = 0; i < data->nlayers; i++)
    173         data->layers[i]->Render(x, y, z);
     165    for (int i = 0; i < data->m_layers.Count(); i++)
     166        data->m_layers[i]->Render(x, y, z);
    174167}
    175168
Note: See TracChangeset for help on using the changeset viewer.