Remove unnecessary config.h includes in C++ code.
[alexxy/gromacs.git] / src / gromacs / selection / mempool.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements functions in mempool.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_selection
37  */
38 #include <stdlib.h>
39
40 #include <new>
41
42 #include "smalloc.h"
43
44 #include "gromacs/selection/indexutil.h"
45 #include "gromacs/utility/exceptions.h"
46 #include "gromacs/utility/gmxassert.h"
47
48 #include "mempool.h"
49
50 //! Alignment in bytes for all returned blocks.
51 #define ALIGN_STEP 8
52
53 /*! \internal \brief
54  * Describes a single block allocated from the memory pool.
55  */
56 typedef struct gmx_sel_mempool_block_t
57 {
58     //! Pointer to the start of the block (as returned to the user).
59     void                       *ptr;
60     //! Size of the block, including padding required to align next block.
61     size_t                      size;
62 } gmx_sel_mempool_block_t;
63
64 /*! \internal \brief
65  * Describes a memory pool.
66  */
67 struct gmx_sel_mempool_t
68 {
69     //! Number of bytes currently allocated from the pool.
70     size_t                      currsize;
71     //! Number of bytes free in the pool, or 0 if \a buffer is NULL.
72     size_t                      freesize;
73     //! Memory area allocated for the pool, or NULL if not yet reserved.
74     char                       *buffer;
75     //! Pointer to the first free byte (aligned at ::ALIGN_STEP) in \a buffer.
76     char                       *freeptr;
77     //! Number of blocks allocated from the pool.
78     int                         nblocks;
79     //! Array describing the allocated blocks.
80     gmx_sel_mempool_block_t    *blockstack;
81     //! Number of elements allocated for the \a blockstack array.
82     int                         blockstack_nalloc;
83     /*! \brief
84      * Maximum number of bytes that have been reserved from the pool
85      * simultaneously.
86      */
87     size_t                      maxsize;
88 };
89
90 gmx_sel_mempool_t *
91 _gmx_sel_mempool_create()
92 {
93     gmx_sel_mempool_t *mp;
94
95     snew(mp, 1);
96     mp->currsize          = 0;
97     mp->freesize          = 0;
98     mp->buffer            = NULL;
99     mp->freeptr           = NULL;
100     mp->nblocks           = 0;
101     mp->blockstack        = NULL;
102     mp->blockstack_nalloc = 0;
103     mp->maxsize           = 0;
104     return mp;
105 }
106
107 void
108 _gmx_sel_mempool_destroy(gmx_sel_mempool_t *mp)
109 {
110     if (!mp->buffer)
111     {
112         int  i;
113
114         for (i = 0; i < mp->nblocks; ++i)
115         {
116             sfree(mp->blockstack[i].ptr);
117         }
118     }
119     sfree(mp->buffer);
120     sfree(mp->blockstack);
121     sfree(mp);
122 }
123
124 void *
125 _gmx_sel_mempool_alloc(gmx_sel_mempool_t *mp, size_t size)
126 {
127     void   *ptr = NULL;
128     size_t  size_walign;
129
130     size_walign = ((size + ALIGN_STEP - 1) / ALIGN_STEP) * ALIGN_STEP;
131     if (mp->buffer)
132     {
133         if (mp->freesize < size)
134         {
135             GMX_THROW(gmx::InternalError("Out of memory pool memory"));
136         }
137         ptr = mp->freeptr;
138         mp->freeptr  += size_walign;
139         mp->freesize -= size_walign;
140         mp->currsize += size_walign;
141     }
142     else
143     {
144         ptr = malloc(size);
145         if (!ptr)
146         {
147             throw std::bad_alloc();
148         }
149         mp->currsize += size_walign;
150         if (mp->currsize > mp->maxsize)
151         {
152             mp->maxsize = mp->currsize;
153         }
154     }
155
156     if (mp->nblocks >= mp->blockstack_nalloc)
157     {
158         mp->blockstack_nalloc = mp->nblocks + 10;
159         srenew(mp->blockstack, mp->blockstack_nalloc);
160     }
161     mp->blockstack[mp->nblocks].ptr  = ptr;
162     mp->blockstack[mp->nblocks].size = size_walign;
163     mp->nblocks++;
164
165     return ptr;
166 }
167
168 void
169 _gmx_sel_mempool_free(gmx_sel_mempool_t *mp, void *ptr)
170 {
171     int size;
172
173     if (ptr == NULL)
174     {
175         return;
176     }
177     GMX_RELEASE_ASSERT(mp->nblocks > 0 && mp->blockstack[mp->nblocks - 1].ptr == ptr,
178                        "Invalid order of memory pool free calls");
179     mp->nblocks--;
180     size = mp->blockstack[mp->nblocks].size;
181     mp->currsize -= size;
182     if (mp->buffer)
183     {
184         mp->freeptr = (char *)ptr;
185         mp->freesize += size;
186     }
187     else
188     {
189         sfree(ptr);
190     }
191 }
192
193 void
194 _gmx_sel_mempool_reserve(gmx_sel_mempool_t *mp, size_t size)
195 {
196     GMX_RELEASE_ASSERT(mp->nblocks == 0,
197                        "Cannot reserve memory pool when there is something allocated");
198     GMX_RELEASE_ASSERT(!mp->buffer, "Cannot reserve memory pool twice");
199     if (size == 0)
200     {
201         size = mp->maxsize;
202     }
203     mp->buffer = (char *)malloc(size);
204     if (!mp->buffer)
205     {
206         throw std::bad_alloc();
207     }
208     mp->freesize = size;
209     mp->freeptr  = mp->buffer;
210 }
211
212 void
213 _gmx_sel_mempool_alloc_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g,
214                              int isize)
215 {
216     void *ptr = _gmx_sel_mempool_alloc(mp, sizeof(*g->index)*isize);
217     g->index = static_cast<int *>(ptr);
218 }
219
220 void
221 _gmx_sel_mempool_free_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g)
222 {
223     _gmx_sel_mempool_free(mp, g->index);
224     g->index = NULL;
225 }