/*
    PIP - Platform Independent Primitives
    Introspection module - implementation of containers
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIINTROSPECTION_CONTAINERS_P_H
#define PIINTROSPECTION_CONTAINERS_P_H

#include "piintrospection_containers.h"
#include "pispinlock.h"
#include "pistring.h"

#include <map>
#include <string>


class PIP_EXPORT PIIntrospectionContainers {
public:
	PIIntrospectionContainers();

	struct TypeInfo;

	// clang-format off
	void containerNew   (const PIIntrospectionContainersType & ti, uint isz);
	void containerDelete(const PIIntrospectionContainersType & ti);
	void containerAlloc (const PIIntrospectionContainersType & ti, ullong cnt);
	void containerFree  (const PIIntrospectionContainersType & ti, ullong cnt);
	void containerUsed  (const PIIntrospectionContainersType & ti, ullong cnt);
	void containerUnused(const PIIntrospectionContainersType & ti, ullong cnt);
	// clang-format on

	PIVector<TypeInfo> getInfo() const;

#pragma pack(push, 1)
	struct PIP_EXPORT _Type {
		uint id          = 0u;
		uint count       = 0u;
		uint item_size   = 0u;
		ullong allocated = 0u;
		ullong used      = 0u;
	};
#pragma pack(pop)

	struct PIP_EXPORT TypeInfo: _Type {
		ullong allocated_bytes = 0u;
		ullong used_bytes      = 0u;
		PIString name;
		PIString item_size_str;
		PIString allocated_str;
		PIString used_str;
	};

	std::map<uint, _Type> data;
	std::map<uint, PIIntrospectionContainersType> types;
	mutable PISpinlock mutex;
};


BINARY_STREAM_WRITE(PIIntrospectionContainers::TypeInfo) {
	s << PIMemoryBlock(&v, sizeof(PIIntrospectionContainers::_Type)) << v.name;
	return s;
}
BINARY_STREAM_READ(PIIntrospectionContainers::TypeInfo) {
	s >> PIMemoryBlock(&v, sizeof(PIIntrospectionContainers::_Type)) >> v.name;
	return s;
}

#endif // PIINTROSPECTION_CONTAINERS_P_H
