Merge remote branch 'origin/release-4-5-patches'
[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 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <assert.h>
43 #include <stdlib.h>
44
45 #include <gmx_fatal.h>
46 #include <smalloc.h>
47
48 #include "gromacs/selection/indexutil.h"
49
50 #include "mempool.h"
51
52 //! Alignment in bytes for all returned blocks.
53 #define ALIGN_STEP 8
54
55 /*! \internal \brief
56  * Describes a single block allocated from the memory pool.
57  */
58 typedef struct gmx_sel_mempool_block_t
59 {
60     //! Pointer to the start of the block (as returned to the user).
61     void                       *ptr;
62     //! Size of the block, including padding required to align next block.
63     size_t                      size;
64 } gmx_sel_mempool_block_t;
65
66 /*! \internal \brief
67  * Describes a memory pool.
68  */
69 struct gmx_sel_mempool_t
70 {
71     //! Number of bytes currently allocated from the pool.
72     size_t                      currsize;
73     //! Number of bytes free in the pool, or 0 if \a buffer is NULL.
74     size_t                      freesize;
75     //! Memory area allocated for the pool, or NULL if not yet reserved.
76     char                       *buffer;
77     //! Pointer to the first free byte (aligned at ::ALIGN_STEP) in \a buffer.
78     char                       *freeptr;
79     //! Number of blocks allocated from the pool.
80     int                         nblocks;
81     //! Array describing the allocated blocks.
82     gmx_sel_mempool_block_t    *blockstack;
83     //! Number of elements allocated for the \a blockstack array.
84     int                         blockstack_nalloc;
85     /*! \brief
86      * Maximum number of bytes that have been reserved from the pool
87      * simultaneously.
88      */
89     size_t                      maxsize;
90 };
91
92 int
93 _gmx_sel_mempool_create(gmx_sel_mempool_t **mpp)
94 {
95     gmx_sel_mempool_t *mp;
96
97     snew(mp, 1);
98     mp->currsize          = 0;
99     mp->freesize          = 0;
100     mp->buffer            = NULL;
101     mp->freeptr           = NULL;
102     mp->nblocks           = 0;
103     mp->blockstack        = NULL;
104     mp->blockstack_nalloc = 0;
105     mp->maxsize           = 0;
106     *mpp = mp;
107     return 0;
108 }
109
110 void
111 _gmx_sel_mempool_destroy(gmx_sel_mempool_t *mp)
112 {
113     if (!mp->buffer)
114     {
115         int  i;
116
117         for (i = 0; i < mp->nblocks; ++i)
118         {
119             sfree(mp->blockstack[i].ptr);
120         }
121     }
122     sfree(mp->buffer);
123     sfree(mp->blockstack);
124     sfree(mp);
125 }
126
127 int
128 _gmx_sel_mempool_alloc(gmx_sel_mempool_t *mp, void **ptrp, size_t size)
129 {
130     void   *ptr = NULL;
131     size_t  size_walign;
132
133     *ptrp = NULL;
134     size_walign = ((size + ALIGN_STEP - 1) / ALIGN_STEP) * ALIGN_STEP;
135     if (mp->buffer)
136     {
137         if (mp->freesize < size)
138         {
139             gmx_bug("out of memory pool memory");
140             return ENOMEM;
141         }
142         ptr = mp->freeptr;
143         mp->freeptr  += size_walign;
144         mp->freesize -= size_walign;
145         mp->currsize += size_walign;
146     }
147     else
148     {
149         ptr = malloc(size);
150         if (!ptr)
151         {
152             gmx_mem("out of memory");
153             return ENOMEM;
154         }
155         mp->currsize += size_walign;
156         if (mp->currsize > mp->maxsize)
157         {
158             mp->maxsize = mp->currsize;
159         }
160     }
161
162     if (mp->nblocks >= mp->blockstack_nalloc)
163     {
164         mp->blockstack_nalloc = mp->nblocks + 10;
165         srenew(mp->blockstack, mp->blockstack_nalloc);
166     }
167     mp->blockstack[mp->nblocks].ptr  = ptr;
168     mp->blockstack[mp->nblocks].size = size_walign;
169     mp->nblocks++;
170
171     *ptrp = ptr;
172     return 0;
173 }
174
175 void
176 _gmx_sel_mempool_free(gmx_sel_mempool_t *mp, void *ptr)
177 {
178     int size;
179
180     if (ptr == NULL)
181     {
182         return;
183     }
184     assert(mp->nblocks > 0 && mp->blockstack[mp->nblocks - 1].ptr == ptr);
185     mp->nblocks--;
186     size = mp->blockstack[mp->nblocks].size;
187     mp->currsize -= size;
188     if (mp->buffer)
189     {
190         mp->freeptr = (char *)ptr;
191         mp->freesize += size;
192     }
193     else
194     {
195         sfree(ptr);
196     }
197 }
198
199 int
200 _gmx_sel_mempool_reserve(gmx_sel_mempool_t *mp, size_t size)
201 {
202     assert(mp->nblocks == 0 && !mp->buffer);
203     if (size == 0)
204     {
205         size = mp->maxsize;
206     }
207     mp->buffer = (char *)malloc(size);
208     if (!mp->buffer)
209     {
210         gmx_mem("out of memory");
211         return ENOMEM;
212     }
213     mp->freesize = size;
214     mp->freeptr  = mp->buffer;
215     return 0;
216 }
217
218 int
219 _gmx_sel_mempool_alloc_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g,
220                              int isize)
221 {
222     return _gmx_sel_mempool_alloc(mp, (void **)&g->index,
223                                   sizeof(*g->index)*isize);
224 }
225
226 void
227 _gmx_sel_mempool_free_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g)
228 {
229     _gmx_sel_mempool_free(mp, g->index);
230     g->index = NULL;
231 }