source: miniini/miniini/include/allocator.h @ 3:9a4831576dc5

Last change on this file since 3:9a4831576dc5 was 3:9a4831576dc5, checked in by vadim@…, 3 years ago

fixes for WIN32 build

File size: 4.0 KB
Line 
1// Copyright (C) 2009-2010 Ferdinand Majerech
2// This file is part of MiniINI
3// For conditions of distribution and use, see copyright notice in LICENSE.txt
4
5
6#ifndef ALLOCATOR_H_INCLUDED
7#define ALLOCATOR_H_INCLUDED
8
9#include "typedefs.h"
10#include <cassert>
11
12#ifdef GetFreeSpace
13#undef GetFreeSpace
14#endif
15
16namespace miniini_private
17{
18/// @cond PRIVATE
19   
20///Single block of allocated memory in Allocator.
21class Block
22{
23    ///Allocator has direct access so it can work with Block as fast as possible
24    friend class Allocator;
25    private:
26        ///Amount of allocated memory in bytes.
27        ui Allocated;
28        ///Amount of used memory in this block, in bytes.
29        ui Used;
30        ///Amount of pointers to memory given to outside code by this block.
31        ui PtrsGiven;
32        ///Amount of pointers to memory in this block that were deleted.
33        ui PtrsDeleted;
34        ///Allocated memory of the block.
35        c * Data;
36
37    public:
38        ///Constructor.
39        /**
40         * Allocates memory for this block.
41         * @param alloc Amount of memory to allocate
42         */
43        Block(const ui alloc)
44            :Allocated(alloc)
45            ,Used(0)
46            ,PtrsGiven(0)
47            ,PtrsDeleted(0)
48            ,Data(new c [Allocated])
49        {}
50       
51        ///Destructor. Deletes memory of this block.
52        ~Block()
53        {
54            delete [] Data;
55        }
56
57        ///@return free space in this block
58        ui GetFreeSpace() const
59        {
60            return Allocated - Used;
61        }
62
63        ///Reallocates this block.
64        /**
65         * Can only be called before the first time this block is used.
66         * @param size Amount of memory to reallocate this block to.
67         */
68        void Reallocate(const ui size)
69        {
70            //This can only be called before this block is used for the first time.
71            assert(!(Used || PtrsGiven || PtrsDeleted));
72            Allocated = size;
73            delete [] Data;
74            Data = new c [Allocated];
75        }
76
77    private:
78
79        ///No copy construction or assignment.
80
81        Block(const Block &);
82
83        void operator = (const Block &);
84};
85
86///Allocates memory for character data in an INIFile.
87/**
88 * Memory is allocated and stored in large blocks.
89 */
90class Allocator
91{
92    private:
93        ///Number of blocks currently used.
94        ui NumBlocks;
95       
96        ///Block we're currently allocating memory from.
97        /*
98         * When the first block gets filled, we start taking memory from the
99         * second one, and so on.
100         */
101        ui CurrentBlock;
102
103        ///Minimum size of a block (used when allocating new blocks).
104        ui BlockMinSize;
105
106        ///Pointers to blocks of allocated memory.
107        Block * * Blocks;
108
109    public:
110        ///Constructor.
111        /**
112         * Allocates given amount of memory divided into given number of blocks.
113         * @param size Amount of memory to allocate.
114         * @param blocks Number of blocks to divide memory to.
115         */
116        Allocator(const ui size, const ui blocks);
117       
118        ///Removes unused blocks left after construction.
119        void Trim();
120
121        ///Deletes space pointed to by given pointer.
122        /**
123         * Equivalent to delete [] or free()
124         * @param ptr Pointer to space to delete.
125         */
126        void DeleteSpace(c * const ptr);
127
128        ///Allocates space with specified size and returns a pointer to it.
129        /**
130         * @param size Amount of space to allocate in bytes.
131         * @return pointer to allocated space.
132         */
133        c * NewSpace(const ui size);
134
135        ///Destructor. Deallocates any blocks left.
136        ~Allocator();
137
138    private:
139       
140        ///Add new block of memory. Called when the current block is out of space.
141        /**
142         * @param minsize Minimum size to allocate the block with.
143         */
144        void NewBlock(const ui minsize);
145
146        ///No copy construction or assignment.
147
148        Allocator(const Allocator &);
149
150        void operator = (const Allocator &);
151};
152/// @endcond
153
154}
155#endif
Note: See TracBrowser for help on using the repository browser.