next up previous contents index
Next: Copying Instances Up: Memory Management Previous: "new-instance""delete" and "free"

"list-alloc" and "list-free"

 

You might have noticed that the "new-instance" method of Rot2d used a method called "list-alloc" to allocate memory for the coordinate lists of the curve and axis triangulations.

Most geometry classes have got the methods "list-alloc" and "list-free" for allocating and freeing memory for their pointer type instance variables, for example coordinates lists, connectivity array or even instances.

   GRAPE(inst, "list-alloc")(...)
INSTANCE * inst
allocates memory for the pointer variables of the instance inst. The parameters of this method depend upon the class of the instance, you should be aware of the problems that are created by these different parameter lists. If the method is called on an instance of a subclass, there might be parameters missing or parameter types may be wrong. Sometimes a G_CAST_CLASS to cast the object to the right class might help to ensure that the call is done correctly.

For all geometry classes the functionality of "list-alloc" is now identical, the sizes of the lists will be changed to exactly the given new sizes. You have to make sure that the currently used space (for triang class the number_of_points/number_of_elements) doesn't exceed the new maximum size. These "list-alloc" methods use the smart realloc functions described in section 10.2.1, therefore existing data is preserved if possible.

   GRAPE(inst, "list-free")()
INSTANCE * inst
frees all memory that was allocated by "list-alloc" for the pointer type instance variables. This method needs no parameters, those classes providing "list-free" usually call it in their "free" method.

For geometry classes calling "list-free" is identical to setting the size of the used space to zero and then calling "list-alloc" with zero sizes, e.g. for TRIANG2D *tr2d:

tr2d->number_of_points = tr2d->number_of_elements = 0;
GRAPE(tr2d, "list-alloc")(0, 0);
This is not recommended because it may cause trouble for subclasses, you should always use "list-free" if it is available.

We could have written these methods for our class Rot2d though we don't have any memory lists. In this case "list-alloc" would have been used to create default curve and axis, "list-free" to free them again. Then "delete" would have called "list-free" to delete curve and axis instead of deleting them itself.


next up previous contents index
Next: Copying Instances Up: Memory Management Previous: "new-instance""delete" and "free"

SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.