//
// File: hzSMAR.h
//
// Legal Notice: This file is part of the HadronZoo C++ Class Library.
//
// Copyright 2025 HadronZoo Project (http://www.hadronzoo.com)
//
// The HadronZoo C++ Class Library 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 any later version.
//
// The HadronZoo C++ Class Library 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 the HadronZoo C++ Class Library. If not, see http://www.gnu.org/licenses.
//
/*
** SMAR: General Purpose Memory Allocation and Management Regime. This is described in the HadronZoo Library Manual, Chapter 3.2 "Allocation Regimes".
*/
#ifndef hzSMAR_h
#define hzSMAR_h
#include <string.h>
#include "hzErrcode.h"
#include "hzLock.h"
#include "hzLock.h"
#include "hzTmplArray.h"
#include "hzTmplMapS.h"
#define SMAR_BLOC_MASK 0xffff0000 // Block part of address
#define SMAR_SLOT_MASK 0xffff // Slot part of address
#define SMAR_BLOC_SPACE 65536 // Total size of the string space blocks in multiples of 8 bytes
#define SMAR_OVERSIZE 16380 // Allocations above this size are from the heap
/*
** Definitions
*/
struct _smarBloc
{
// Small string space superblock
uint32_t m_blkSelf ; // Address of block
uint32_t m_Usage ; // Space used (so position of free space)
uint32_t m_Space[SMAR_BLOC_SPACE] ; // Areas for string spaces
_smarBloc (void)
{
m_blkSelf = 0 ;
m_Usage = 0 ;
}
} ;
class hzSMAR
{
// Category: Memory Allocation
//
// Segmented Memory Allocation Regime
// Make sure hzSMAR (uint32_t nCode) is called
hzSMAR (void) ;
public:
hzMapS <uint32_t,_smarBloc*> m_Super ; // Map of 256K superblocks (max of 64K addressable so 16Gb space)
hzMapS <uint32_t,void*> m_Heap ; // Heap allocated oversized strings
uint32_t m_arrFL[4096] ; // Array of free lists for each size
_smarBloc* m_pTopBlock ; // Latest small string space superblock (only one from which new string spaces can be allocated)
uint64_t m_nAllocNew ; // Total number of new allocations
uint64_t m_nAllocOld ; // Total number of allocations from free list
uint64_t m_nReleases ; // Total number of releases
// Locks
hzLockS m_lockFlist ; // Lock for m_Flists
hzLockS m_lockSbloc ; // Lock for allocating superblocks
hzLockS m_lockOsize ; // Lock for allocating/freeing of oversize strings
uint32_t m_nLive ; // Live string population
uint32_t m_nCode ; // Diagnostics
hzSMAR (uint32_t nCode) ;
~hzSMAR (void) {}
// Memory operations
uchar* Xlate (uint32_t ssrAddr) ;
uint32_t Alloc (uint32_t nSize) ;
hzEcode Free (uint32_t strAddr, uint32_t nSize) ;
// Integrity Report
void Report (hzChain& report) ;
} ;
#endif // hzSMAR_h