diff: memory allocation bug fix
Fergus Henderson
fjh at cs.mu.OZ.AU
Thu Jan 28 22:45:41 AEDT 1999
Estimated hours taken: 6
Fix a memory allocation bug reported by
Warwick Harvey <wharvey at cs.monash.edu.au>.
library/std_util.m:
runtime/mercury_type_info.c:
Use `newmem()' rather than `malloc()' when allocating memory
that will contain Mercury terms or types. This is needed
because the Boehm collector does not trace memory allocated
with malloc().
Index: library/std_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/std_util.m,v
retrieving revision 1.139
diff -u -r1.139 std_util.m
--- std_util.m 1999/01/05 17:05:06 1.139
+++ std_util.m 1999/01/28 11:42:25
@@ -2174,9 +2174,13 @@
** Expand the given data using its type_info, find its
** functor, arity, argument vector and type_info vector.
**
-** The info.type_info_vector is allocated using malloc
-** It is the responsibility of the caller to free this
-** memory, and to copy any fields of this vector to
+** The info.type_info_vector is allocated using newmem().
+** (We need to use newmem() rather than malloc(), since this
+** vector may contain pointers into the Mercury heap, and
+** memory allocated with malloc will not be traced by the
+** Boehm collector.)
+** It is the responsibility of the caller to deallocate this
+** memory (using oldmem()), and to copy any fields of this vector to
** the Mercury heap. The type_infos that the elements of
** this vector point to are either
** - already allocated on the heap.
@@ -2281,8 +2285,7 @@
if (info->need_args) {
info->argument_vector = (Word *) data_value;
- info->type_info_vector = checked_malloc(
- info->arity * sizeof(Word));
+ info->type_info_vector = newmem(info->arity * sizeof(Word));
for (i = 0; i < info->arity ; i++) {
Word *arg_pseudo_type_info;
@@ -2319,8 +2322,7 @@
*/
info->argument_vector = (Word *) data_word_ptr;
- info->type_info_vector = checked_malloc(
- info->arity * sizeof(Word));
+ info->type_info_vector = newmem(info->arity * sizeof(Word));
for (i = 0; i < info->arity ; i++) {
Word *arg_pseudo_type_info;
@@ -2529,7 +2531,7 @@
** Free the allocated type_info_vector, since we just copied
** the stuff we want out of it.
*/
- free(info.type_info_vector);
+ oldmem(info.type_info_vector);
return success;
}
@@ -2726,7 +2728,7 @@
* all its arguments onto the heap.
*/
- free(info.type_info_vector);
+ oldmem(info.type_info_vector);
}").
Index: runtime/mercury_type_info.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_type_info.c,v
retrieving revision 1.14
diff -u -r1.14 mercury_type_info.c
--- mercury_type_info.c 1998/11/11 02:14:19 1.14
+++ mercury_type_info.c 1999/01/28 11:44:50
@@ -173,7 +173,7 @@
**
** NOTE: If you are changing this code, you might also need
** to change the code in MR_make_type_info in this module
- ** which does much the same thing, only allocating using malloc
+ ** which does much the same thing, only allocating using newmem
** instead of on the heap.
*/
@@ -409,8 +409,8 @@
{
while (allocated != NULL) {
MR_MemoryList next = allocated->next;
- free(allocated->data);
- free(allocated);
+ oldmem(allocated->data);
+ oldmem(allocated);
allocated = next;
}
}
@@ -430,19 +430,22 @@
** arg_pseudo_type_info with all the type variables filled in.
** If there are no type variables to fill in, we return the
** arg_pseudo_type_info, unchanged. Otherwise, we allocate
- ** memory using malloc(). Any such memory allocated will be
+ ** memory using newmem(). Any such memory allocated will be
** inserted into the list of allocated memory cells.
** It is the caller's responsibility to free these cells
** by calling MR_deallocate() on the list when they are no longer
** needed.
**
** This code could be tighter. In general, we want to
- ** handle our own allocations rather than using malloc().
+ ** handle our own allocations rather than using newmem().
+ ** (Note: we need to use newmem() rather than malloc()
+ ** because the Boehm collector doesn't trace memory
+ ** allocated with malloc().)
**
** NOTE: If you are changing this code, you might also need
** to change the code in MR_create_type_info (defined above),
** which does much the same thing, only allocating on the
- ** heap instead of using malloc.
+ ** heap instead of using newmem.
*/
Word *
@@ -507,9 +510,9 @@
MR_MemoryList node;
/*
** allocate a new type_info and copy the
- ** data across from arg_pseduo_type_info
+ ** data across from arg_pseudo_type_info
*/
- type_info = checked_malloc(
+ type_info = newmem(
(arity + extra_args) * sizeof(Word));
memcpy(type_info, arg_pseudo_type_info,
(arity + extra_args) * sizeof(Word));
@@ -518,7 +521,7 @@
** list of allocated memory cells, so we can
** free it later on
*/
- node = checked_malloc(sizeof(*node));
+ node = newmem(sizeof(*node));
node->data = type_info;
node->next = *allocated;
*allocated = node;
--
Fergus Henderson <fjh at cs.mu.oz.au> | "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh> | but source code lives forever"
PGP: finger fjh at 128.250.37.3 | -- leaked Microsoft memo.
More information about the developers
mailing list