Skip to content

Commit

Permalink
Refactor particles allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Nov 3, 2017
1 parent e1a7760 commit 252a735
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/xrParticles/particle_effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct ParticleEffect
u32 max_particles; // Max particles allowed in effect.
u32 particles_allocated; // Actual allocated size.
Particle* particles; // Actually, num_particles in size
void* real_ptr; // Base, possible not aligned pointer
OnBirthParticleCB b_cb;
OnDeadParticleCB d_cb;
void* owner;
Expand All @@ -27,15 +26,11 @@ struct ParticleEffect
max_particles = mp;
particles_allocated = max_particles;

real_ptr = xr_malloc(sizeof(Particle) * (max_particles + 1));

particles = (Particle*)((uintptr_t)real_ptr + (64 - ((uintptr_t)real_ptr & 63)));
//particles = static_cast<Particle*>(real_ptr);

particles = xr_alloc<Particle>(max_particles);
//Msg("Allocated %u bytes (%u particles) with base address 0x%p", max_particles * sizeof(Particle), max_particles, particles);
}

~ParticleEffect() { xr_free(real_ptr); }
~ParticleEffect() { xr_free(particles); }

int Resize(u32 max_count)
{
Expand All @@ -52,23 +47,20 @@ struct ParticleEffect
}

// Allocate particles.
void* new_real_ptr = xr_malloc(sizeof(Particle) * (max_count + 1));

if (new_real_ptr == nullptr)
Particle* new_particles = xr_alloc<Particle>(max_count);
if (!new_particles)
{
// ERROR - Not enough memory. Just give all we've got.
max_particles = particles_allocated;
return max_particles;
}

Particle* new_particles = (Particle*)((uintptr_t)new_real_ptr + (64 - ((uintptr_t)new_real_ptr & 63)));
Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);
//Msg("Re-allocated %u bytes (%u particles) with base address 0x%p", max_count * sizeof(Particle), max_count, new_particles);

CopyMemory(new_particles, particles, p_count * sizeof(Particle));
xr_free(real_ptr);
xr_free(particles);

particles = new_particles;
real_ptr = new_real_ptr;

max_particles = max_count;
particles_allocated = max_count;
Expand Down

0 comments on commit 252a735

Please sign in to comment.