Malloc problems

Peter da Silva peter at ficc.UUCP
Thu Jun 2 00:22:04 AEST 1988


This "memory group" thing sounds like "AllocRemember", a routine that
comes as part of the Amiga Intuition library:

struct RememberKey *Memory;

	Memory = 0;

	...

	space = AllocRemember(&Memory, size, flags);

	...

	morespace = AllocRemember(&Memory, size, flags);


	...

	FreeRemember(&Memory, TRUE);

This has the result of allocating the memory on a linked list. You get
the effect of the memory spaces for free. When you call FreeRemember
it trashes all the memory linked on that particular RememberKey. 

Implementation? Easy:

struct memkey {
	struct memkey *nextkey;
	char data[0];
};

mallocremember(key, size)
struct memkey **key;
int size;
{
	struct memkey *ptr;

	ptr = malloc(sizeof(struct memkey *)+size);
	ptr->nextkey = *key;
	*key = ptr;
	return ptr->data;
}

freeremember(key)
struct memkey **key;
{
	while(*key) {
		ptr = (*key)->nextkey;
		free(*key);
		*key = ptr;
	}
}
-- 
-- Peter da Silva, Ferranti International Controls Corporation.
-- Phone: 713-274-5180. Remote UUCP: uunet!nuchat!sugar!peter.



More information about the Comp.lang.c mailing list