Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / gmxlib / selection / mempool.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2009, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \internal \file
39  * \brief Memory pooling for selection evaluation.
40  *
41  * \todo
42  * Document these functions.
43  */
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #include <assert.h>
49 #include <stdlib.h>
50
51 #include <gmx_fatal.h>
52 #include <smalloc.h>
53
54 #include <indexutil.h>
55
56 #include "mempool.h"
57
58 #define ALIGN_STEP 8
59
60 typedef struct gmx_sel_mempool_block_t
61 {
62     void                       *ptr;
63     size_t                      size;
64 } gmx_sel_mempool_block_t;
65
66 struct gmx_sel_mempool_t
67 {
68     size_t                      currsize;
69     size_t                      freesize;
70     char                       *buffer;
71     char                       *freeptr;
72     int                         nblocks;
73     gmx_sel_mempool_block_t    *blockstack;
74     int                         blockstack_nalloc;
75     size_t                      maxsize;
76 };
77
78 int
79 _gmx_sel_mempool_create(gmx_sel_mempool_t **mpp)
80 {
81     gmx_sel_mempool_t *mp;
82
83     snew(mp, 1);
84     mp->currsize          = 0;
85     mp->freesize          = 0;
86     mp->buffer            = NULL;
87     mp->freeptr           = NULL;
88     mp->nblocks           = 0;
89     mp->blockstack        = NULL;
90     mp->blockstack_nalloc = 0;
91     mp->maxsize           = 0;
92     *mpp = mp;
93     return 0;
94 }
95
96 void
97 _gmx_sel_mempool_destroy(gmx_sel_mempool_t *mp)
98 {
99     if (!mp->buffer)
100     {
101         int  i;
102
103         for (i = 0; i < mp->nblocks; ++i)
104         {
105             sfree(mp->blockstack[i].ptr);
106         }
107     }
108     sfree(mp->buffer);
109     sfree(mp->blockstack);
110     sfree(mp);
111 }
112
113 int
114 _gmx_sel_mempool_alloc(gmx_sel_mempool_t *mp, void **ptrp, size_t size)
115 {
116     void   *ptr = NULL;
117     size_t  size_walign;
118
119     *ptrp = NULL;
120     size_walign = ((size + ALIGN_STEP - 1) / ALIGN_STEP) * ALIGN_STEP;
121     if (mp->buffer)
122     {
123         if (mp->freesize < size)
124         {
125             gmx_bug("out of memory pool memory");
126             return ENOMEM;
127         }
128         ptr = mp->freeptr;
129         mp->freeptr  += size_walign;
130         mp->freesize -= size_walign;
131         mp->currsize += size_walign;
132     }
133     else
134     {
135         ptr = malloc(size);
136         if (!ptr)
137         {
138             gmx_mem("out of memory");
139             return ENOMEM;
140         }
141         mp->currsize += size_walign;
142         if (mp->currsize > mp->maxsize)
143         {
144             mp->maxsize = mp->currsize;
145         }
146     }
147
148     if (mp->nblocks >= mp->blockstack_nalloc)
149     {
150         mp->blockstack_nalloc = mp->nblocks + 10;
151         srenew(mp->blockstack, mp->blockstack_nalloc);
152     }
153     mp->blockstack[mp->nblocks].ptr  = ptr;
154     mp->blockstack[mp->nblocks].size = size_walign;
155     mp->nblocks++;
156
157     *ptrp = ptr;
158     return 0;
159 }
160
161 void
162 _gmx_sel_mempool_free(gmx_sel_mempool_t *mp, void *ptr)
163 {
164     int size;
165
166     if (ptr == NULL)
167     {
168         return;
169     }
170     assert(mp->nblocks > 0 && mp->blockstack[mp->nblocks - 1].ptr == ptr);
171     mp->nblocks--;
172     size = mp->blockstack[mp->nblocks].size;
173     mp->currsize -= size;
174     if (mp->buffer)
175     {
176         mp->freeptr = (char *)ptr;
177         mp->freesize += size;
178     }
179     else
180     {
181         sfree(ptr);
182     }
183 }
184
185 int
186 _gmx_sel_mempool_reserve(gmx_sel_mempool_t *mp, size_t size)
187 {
188     assert(mp->nblocks == 0 && !mp->buffer);
189     if (size == 0)
190     {
191         size = mp->maxsize;
192     }
193     mp->buffer = (char *)malloc(size);
194     if (!mp->buffer)
195     {
196         gmx_mem("out of memory");
197         return ENOMEM;
198     }
199     mp->freesize = size;
200     mp->freeptr  = mp->buffer;
201     return 0;
202 }
203
204 int
205 _gmx_sel_mempool_alloc_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g,
206                              int isize)
207 {
208     return _gmx_sel_mempool_alloc(mp, (void **)&g->index,
209                                   sizeof(*g->index)*isize);
210 }
211
212 void
213 _gmx_sel_mempool_free_group(gmx_sel_mempool_t *mp, gmx_ana_index_t *g)
214 {
215     _gmx_sel_mempool_free(mp, g->index);
216     g->index = NULL;
217 }