File indexing completed on 2024-04-28 05:41:26

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2002-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 #ifndef POOL_H
0010 #define POOL_H
0011 
0012 /**
0013  * Pool objects: containers for many small objects.
0014  */
0015 
0016 struct SpaceChunk;
0017 
0018 /**
0019  * FixPool
0020  *
0021  * For objects with fixed size and life time
0022  * ending with that of the pool.
0023  */
0024 class FixPool
0025 {
0026 public:
0027     FixPool();
0028     ~FixPool();
0029 
0030     /**
0031      * Take @p size bytes from the pool
0032      * @param size is the number of bytes
0033      */
0034     void* allocate(unsigned int size);
0035 
0036     /**
0037      * Reserve space. If you call allocateReservedSpace(realsize)
0038      * with realSize < reserved size directly after, you
0039      * will get the same memory area.
0040      */
0041     void* reserve(unsigned int size);
0042 
0043     /**
0044      * Before calling this, you have to reserve at least @p size bytes
0045      * with reserveSpace().
0046      * @param size is the number of bytes
0047      */
0048     bool allocateReserved(unsigned int size);
0049 
0050 private:
0051     /* Checks that there is enough space in the last chunk.
0052      * Returns false if this is not possible.
0053      */
0054     bool ensureSpace(unsigned int);
0055 
0056     struct SpaceChunk *_first, *_last;
0057     unsigned int _reservation;
0058     int _count, _size;
0059 };
0060 
0061 /**
0062  * DynPool
0063  *
0064  * For objects which probably need to be resized
0065  * in the future. Objects also can be deleted to free up space.
0066  * As objects can also be moved in a defragmentation step,
0067  * access has to be done via the given pointer object.
0068  */
0069 class DynPool
0070 {
0071 public:
0072     DynPool();
0073     ~DynPool();
0074 
0075     /**
0076      * Take @p size bytes from the pool, changing @p *ptr
0077      * to point to this allocated space.
0078      * @param size is the number of bytes
0079      * @param *ptr will be changed if the object is moved.
0080      * Returns false if no space available.
0081      */
0082     bool allocate(char** ptr, unsigned int size);
0083 
0084     /**
0085      * To resize, first allocate new space, and free old
0086      * afterwards.
0087      */
0088     void free(char** ptr);
0089 
0090 private:
0091     /* Checks that there is enough space. If not,
0092      * it compactifies, possibly moving objects.
0093      */
0094     bool ensureSpace(unsigned int);
0095 
0096     char* _data;
0097     unsigned int _used, _size;
0098 };
0099 
0100 #endif // POOL_H