// Cuenta las reservas de memoria de un programa, reemplazando el operator new global.
// Se incluye en un solo .cpp de cada programa.
#ifndef ALLOC_COUNTER_H
#define ALLOC_COUNTER_H

#include <cstddef>
#include <cstdlib>
#include <new>

// Número de llamadas a operator new desde que empezó el programa.
inline std::size_t& allocation_count() {
    static std::size_t count = 0;
    return count;
}

void* operator new(std::size_t size) {
    allocation_count()++;
    void* block = std::malloc(size == 0 ? 1 : size);
    if (block == nullptr) {
        throw std::bad_alloc();
    }
    return block;
}

void operator delete(void* block) noexcept {
    std::free(block);
}

void operator delete(void* block, std::size_t) noexcept {
    std::free(block);
}

#endif
