File indexing completed on 2024-07-14 07:17:24

0001 /*
0002     SPDX-FileCopyrightText: 2008 Akarsh Simha <akarshsimha@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "typedef.h"
0010 
0011 class StarBlock;
0012 
0013 /**
0014  * @class StarBlockFactory
0015  *
0016  * @short A factory that creates StarBlocks and recycles them in an LRU Cache
0017  * @author Akarsh Simha
0018  * @version 0.1
0019  */
0020 
0021 class StarBlockFactory
0022 {
0023   public:
0024     static StarBlockFactory *Instance();
0025 
0026     /**
0027      * Destructor
0028      * Deletes the linked list that maintains the Cache, sets the pointer to nullptr
0029      */
0030     ~StarBlockFactory();
0031 
0032     /**
0033      * @short  Return a StarBlock available for use
0034      *
0035      * This method first checks if there are any cached StarBlocks that are not in use.
0036      * If such a StarBlock is found, it returns the same for use. Else it freshly allocates
0037      * a StarBlock and returns the same. It also moves the StarBlock to the front, marking it
0038      * as the most recently used. If the StarBlock had a parent StarBlockList, this method
0039      * detaches the StarBlock from the StarBlockList
0040      *
0041      * @return A StarBlock that is available for use
0042      */
0043     std::shared_ptr<StarBlock> getBlock();
0044 
0045     /**
0046      * @short  Mark a StarBlock as most recently used and sync its drawID with the current drawID
0047      *
0048      * @return true on success, false if the StarBlock supplied was not on our list at all
0049      */
0050     bool markFirst(std::shared_ptr<StarBlock>& block);
0051 
0052     /**
0053      * @short  Rank a given StarBlock after another given StarBlock in the LRU list
0054      * and sync its drawID with the current drawID
0055      *
0056      * @param  after  The block after which 'block' should be put
0057      * @param  block  The block to mark for use
0058      * @return true on success, false on failure
0059      */
0060     bool markNext(std::shared_ptr<StarBlock>& after, std::shared_ptr<StarBlock>& block);
0061 
0062     /**
0063      * @short  Returns the number of StarBlocks currently produced
0064      *
0065      * @return Number of StarBlocks currently allocated
0066      */
0067     inline int getBlockCount() const { return nBlocks; }
0068 
0069     /**
0070      * @short  Frees all StarBlocks that are in the cache
0071      * @return The number of StarBlocks freed
0072      */
0073     inline int freeAll() { return deleteBlocks(nBlocks); }
0074 
0075     /**
0076      * @short  Frees all StarBlocks that are not used in this draw cycle
0077      * @return The number of StarBlocks freed
0078      */
0079     int freeUnused();
0080 
0081     /**
0082      * @short  Prints the structure of the cache, for debugging
0083      */
0084     void printStructure() const;
0085 
0086     quint32 drawID; // A number identifying the current draw cycle
0087 
0088   private:
0089     /**
0090      * Constructor
0091      * Initializes first and last StarBlock pointers to nullptr
0092      */
0093     StarBlockFactory();
0094 
0095     /**
0096      * @short  Deletes the N least recently used blocks
0097      *
0098      * @param  nblocks  Number of blocks to delete
0099      * @return Number of blocks successfully deleted
0100      */
0101     int deleteBlocks(int nblocks);
0102 
0103     std::shared_ptr<StarBlock> first, last; // Pointers to the beginning and end of the linked list
0104     int nBlocks;             // Number of blocks we currently have in the cache
0105     int nCache;              // Number of blocks to start recycling cached blocks at
0106 
0107     static StarBlockFactory *pInstance;
0108 };