src/glpdmp.c
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
/* glpdmp.c (dynamic memory pool) */
alpar@1
     2
alpar@1
     3
/***********************************************************************
alpar@1
     4
*  This code is part of GLPK (GNU Linear Programming Kit).
alpar@1
     5
*
alpar@1
     6
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
alpar@1
     7
*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
alpar@1
     8
*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
alpar@1
     9
*  E-mail: <mao@gnu.org>.
alpar@1
    10
*
alpar@1
    11
*  GLPK is free software: you can redistribute it and/or modify it
alpar@1
    12
*  under the terms of the GNU General Public License as published by
alpar@1
    13
*  the Free Software Foundation, either version 3 of the License, or
alpar@1
    14
*  (at your option) any later version.
alpar@1
    15
*
alpar@1
    16
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@1
    17
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@1
    18
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@1
    19
*  License for more details.
alpar@1
    20
*
alpar@1
    21
*  You should have received a copy of the GNU General Public License
alpar@1
    22
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@1
    23
***********************************************************************/
alpar@1
    24
alpar@1
    25
#include "glpdmp.h"
alpar@1
    26
alpar@1
    27
#if 1 /* 29/VIII-2008 */
alpar@1
    28
/* some processors need data to be properly aligned; the macro
alpar@1
    29
   align_datasize enlarges the specified size of a data item to provide
alpar@1
    30
   a proper alignment of immediately following data */
alpar@1
    31
alpar@1
    32
#define align_datasize(size) ((((size) + 7) / 8) * 8)
alpar@1
    33
/* 8 bytes is sufficient in both 32- and 64-bit environments */
alpar@1
    34
#endif
alpar@1
    35
alpar@1
    36
#ifdef GLP_DEBUG
alpar@1
    37
struct info
alpar@1
    38
{     DMP *pool;
alpar@1
    39
      int size;
alpar@1
    40
};
alpar@1
    41
#endif
alpar@1
    42
alpar@1
    43
/***********************************************************************
alpar@1
    44
*  NAME
alpar@1
    45
*
alpar@1
    46
*  dmp_create_pool - create dynamic memory pool
alpar@1
    47
*
alpar@1
    48
*  SYNOPSIS
alpar@1
    49
*
alpar@1
    50
*  #include "glpdmp.h"
alpar@1
    51
*  DMP *dmp_create_pool(void);
alpar@1
    52
*
alpar@1
    53
*  DESCRIPTION
alpar@1
    54
*
alpar@1
    55
*  The routine dmp_create_pool creates a dynamic memory pool.
alpar@1
    56
*
alpar@1
    57
*  RETURNS
alpar@1
    58
*
alpar@1
    59
*  The routine returns a pointer to the memory pool created. */
alpar@1
    60
alpar@1
    61
DMP *dmp_create_pool(void)
alpar@1
    62
{     DMP *pool;
alpar@1
    63
      int k;
alpar@1
    64
#ifdef GLP_DEBUG
alpar@1
    65
      xprintf("dmp_create_pool: warning: debug mode enabled\n");
alpar@1
    66
#endif
alpar@1
    67
      pool = xmalloc(sizeof(DMP));
alpar@1
    68
#if 0
alpar@1
    69
      pool->size = 0;
alpar@1
    70
#endif
alpar@1
    71
      for (k = 0; k <= 31; k++) pool->avail[k] = NULL;
alpar@1
    72
      pool->block = NULL;
alpar@1
    73
      pool->used = DMP_BLK_SIZE;
alpar@1
    74
      pool->count.lo = pool->count.hi = 0;
alpar@1
    75
      return pool;
alpar@1
    76
}
alpar@1
    77
alpar@1
    78
/***********************************************************************
alpar@1
    79
*  NAME
alpar@1
    80
*
alpar@1
    81
*  dmp_get_atom - get free atom from dynamic memory pool
alpar@1
    82
*
alpar@1
    83
*  SYNOPSIS
alpar@1
    84
*
alpar@1
    85
*  #include "glpdmp.h"
alpar@1
    86
*  void *dmp_get_atom(DMP *pool, int size);
alpar@1
    87
*
alpar@1
    88
*  DESCRIPTION
alpar@1
    89
*
alpar@1
    90
*  The routine dmp_get_atom obtains a free atom (memory block) from the
alpar@1
    91
*  specified memory pool.
alpar@1
    92
*
alpar@1
    93
*  The parameter size is the atom size, in bytes, 1 <= size <= 256.
alpar@1
    94
*
alpar@1
    95
*  Note that the free atom contains arbitrary data, not binary zeros.
alpar@1
    96
*
alpar@1
    97
*  RETURNS
alpar@1
    98
*
alpar@1
    99
*  The routine returns a pointer to the free atom obtained. */
alpar@1
   100
alpar@1
   101
void *dmp_get_atom(DMP *pool, int size)
alpar@1
   102
{     void *atom;
alpar@1
   103
      int k;
alpar@1
   104
#ifdef GLP_DEBUG
alpar@1
   105
      int orig_size = size;
alpar@1
   106
#endif
alpar@1
   107
      if (!(1 <= size && size <= 256))
alpar@1
   108
         xerror("dmp_get_atom: size = %d; invalid atom size\n", size);
alpar@1
   109
#if 0
alpar@1
   110
      if (!(pool->size == 0 || pool->size == size))
alpar@1
   111
         xerror("dmp_get_atom: size = %d; wrong atom size\n", size);
alpar@1
   112
#endif
alpar@1
   113
      /* adjust the size to provide the proper data alignment */
alpar@1
   114
      size = align_datasize(size);
alpar@1
   115
#ifdef GLP_DEBUG
alpar@1
   116
      size += align_datasize(sizeof(struct info));
alpar@1
   117
#endif
alpar@1
   118
      /* adjust the size to make it multiple of 8 bytes, if needed */
alpar@1
   119
      size = ((size + 7) / 8) * 8;
alpar@1
   120
      /* determine the corresponding list of free cells */
alpar@1
   121
      k = size / 8 - 1;
alpar@1
   122
      xassert(0 <= k && k <= 31);
alpar@1
   123
      /* obtain a free atom */
alpar@1
   124
      if (pool->avail[k] == NULL)
alpar@1
   125
      {  /* the list of free cells is empty */
alpar@1
   126
         if (pool->used + size > DMP_BLK_SIZE)
alpar@1
   127
         {  /* allocate a new memory block */
alpar@1
   128
            void *block = xmalloc(DMP_BLK_SIZE);
alpar@1
   129
            *(void **)block = pool->block;
alpar@1
   130
            pool->block = block;
alpar@1
   131
            pool->used = align_datasize(sizeof(void *));
alpar@1
   132
         }
alpar@1
   133
         /* place the atom in the current memory block */
alpar@1
   134
         atom = (char *)pool->block + pool->used;
alpar@1
   135
         pool->used += size;
alpar@1
   136
      }
alpar@1
   137
      else
alpar@1
   138
      {  /* obtain the atom from the list of free cells */
alpar@1
   139
         atom = pool->avail[k];
alpar@1
   140
         pool->avail[k] = *(void **)atom;
alpar@1
   141
      }
alpar@1
   142
      memset(atom, '?', size);
alpar@1
   143
      /* increase the number of atoms which are currently in use */
alpar@1
   144
      pool->count.lo++;
alpar@1
   145
      if (pool->count.lo == 0) pool->count.hi++;
alpar@1
   146
#ifdef GLP_DEBUG
alpar@1
   147
      ((struct info *)atom)->pool = pool;
alpar@1
   148
      ((struct info *)atom)->size = orig_size;
alpar@1
   149
      atom = (char *)atom + align_datasize(sizeof(struct info));
alpar@1
   150
#endif
alpar@1
   151
      return atom;
alpar@1
   152
}
alpar@1
   153
alpar@1
   154
/***********************************************************************
alpar@1
   155
*  NAME
alpar@1
   156
*
alpar@1
   157
*  dmp_free_atom - return atom to dynamic memory pool
alpar@1
   158
*
alpar@1
   159
*  SYNOPSIS
alpar@1
   160
*
alpar@1
   161
*  #include "glpdmp.h"
alpar@1
   162
*  void dmp_free_atom(DMP *pool, void *atom, int size);
alpar@1
   163
*
alpar@1
   164
*  DESCRIPTION
alpar@1
   165
*
alpar@1
   166
*  The routine dmp_free_atom returns the specified atom (memory block)
alpar@1
   167
*  to the specified memory pool, making it free.
alpar@1
   168
*
alpar@1
   169
*  The parameter size is the atom size, in bytes, 1 <= size <= 256.
alpar@1
   170
*
alpar@1
   171
*  Note that the atom can be returned only to the pool, from which it
alpar@1
   172
*  was obtained, and its size must be exactly the same as on obtaining
alpar@1
   173
*  it from the pool. */
alpar@1
   174
alpar@1
   175
void dmp_free_atom(DMP *pool, void *atom, int size)
alpar@1
   176
{     int k;
alpar@1
   177
      if (!(1 <= size && size <= 256))
alpar@1
   178
         xerror("dmp_free_atom: size = %d; invalid atom size\n", size);
alpar@1
   179
#if 0
alpar@1
   180
      if (!(pool->size == 0 || pool->size == size))
alpar@1
   181
         xerror("dmp_free_atom: size = %d; wrong atom size\n", size);
alpar@1
   182
#endif
alpar@1
   183
      if (pool->count.lo == 0 && pool->count.hi == 0)
alpar@1
   184
         xerror("dmp_free_atom: pool allocation error\n");
alpar@1
   185
#ifdef GLP_DEBUG
alpar@1
   186
      atom = (char *)atom - align_datasize(sizeof(struct info));
alpar@1
   187
      xassert(((struct info *)atom)->pool == pool);
alpar@1
   188
      xassert(((struct info *)atom)->size == size);
alpar@1
   189
#endif
alpar@1
   190
      /* adjust the size to provide the proper data alignment */
alpar@1
   191
      size = align_datasize(size);
alpar@1
   192
#ifdef GLP_DEBUG
alpar@1
   193
      size += align_datasize(sizeof(struct info));
alpar@1
   194
#endif
alpar@1
   195
      /* adjust the size to make it multiple of 8 bytes, if needed */
alpar@1
   196
      size = ((size + 7) / 8) * 8;
alpar@1
   197
      /* determine the corresponding list of free cells */
alpar@1
   198
      k = size / 8 - 1;
alpar@1
   199
      xassert(0 <= k && k <= 31);
alpar@1
   200
      /* return the atom to the list of free cells */
alpar@1
   201
      *(void **)atom = pool->avail[k];
alpar@1
   202
      pool->avail[k] = atom;
alpar@1
   203
      /* decrease the number of atoms which are currently in use */
alpar@1
   204
      pool->count.lo--;
alpar@1
   205
      if (pool->count.lo == 0xFFFFFFFF) pool->count.hi--;
alpar@1
   206
      return;
alpar@1
   207
}
alpar@1
   208
alpar@1
   209
/***********************************************************************
alpar@1
   210
*  NAME
alpar@1
   211
*
alpar@1
   212
*  dmp_in_use - determine how many atoms are still in use
alpar@1
   213
*
alpar@1
   214
*  SYNOPSIS
alpar@1
   215
*
alpar@1
   216
*  #include "glpdmp.h"
alpar@1
   217
*  glp_long dmp_in_use(DMP *pool);
alpar@1
   218
*
alpar@1
   219
*  DESCRIPTION
alpar@1
   220
*
alpar@1
   221
*  The routine dmp_in_use determines how many atoms allocated from the
alpar@1
   222
*  specified memory pool with the routine dmp_get_atom are still in use,
alpar@1
   223
*  i.e. not returned to the pool with the routine dmp_free_atom.
alpar@1
   224
*
alpar@1
   225
*  RETURNS
alpar@1
   226
*
alpar@1
   227
*  The routine returns the number of atoms which are still in use. */
alpar@1
   228
alpar@1
   229
glp_long dmp_in_use(DMP *pool)
alpar@1
   230
{     return
alpar@1
   231
         pool->count;
alpar@1
   232
}
alpar@1
   233
alpar@1
   234
/***********************************************************************
alpar@1
   235
*  NAME
alpar@1
   236
*
alpar@1
   237
*  dmp_delete_pool - delete dynamic memory pool
alpar@1
   238
*
alpar@1
   239
*  SYNOPSIS
alpar@1
   240
*
alpar@1
   241
*  #include "glpdmp.h"
alpar@1
   242
*  void dmp_delete_pool(DMP *pool);
alpar@1
   243
*
alpar@1
   244
*  DESCRIPTION
alpar@1
   245
*
alpar@1
   246
*  The routine dmp_delete_pool deletes the specified dynamic memory
alpar@1
   247
*  pool and frees all the memory allocated to this object. */
alpar@1
   248
alpar@1
   249
void dmp_delete_pool(DMP *pool)
alpar@1
   250
{     while (pool->block != NULL)
alpar@1
   251
      {  void *block = pool->block;
alpar@1
   252
         pool->block = *(void **)block;
alpar@1
   253
         xfree(block);
alpar@1
   254
      }
alpar@1
   255
      xfree(pool);
alpar@1
   256
      return;
alpar@1
   257
}
alpar@1
   258
alpar@1
   259
/* eof */