glpdmp.c 7.6 KB

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