\fIFree\fR can be used on pointers returned by these routines.
.PP
\fISt_alloc\fR and \fIst_free\fR provide a mechanism for maintaining free lists
-of structures, whose first field is a pointer called \fBnext\fR.
-This field is used to chain free structures together.
+of structures.
\fISt_alloc\fR takes three parameters: \fIphead\fR is a pointer to a field
containing the head of the free list, \fIsize\fR contains the size of the
structures, and \fIcount\fR indicates how many new structures must be allocated
call a routine \fINo_Mem\fR if there is no memory available. This routine
is not supposed to return. A default one, that
gives an error message and stops execution, is provided.
+.SH BUGS
+The
+.I st_alloc
+mechanism only works for structures that are large enough to contain one
+pointer.
+Also,
+.I st_free
+actually is a macro, and references its arguments more than once, so they
+better not have side-effects.
/* S T R U C T U R E - S T O R A G E D E F I N I T I O N S */
+typedef struct _ALLOC_ {
+ struct _ALLOC_ *_A_next;
+} *_PALLOC_;
+
+#define _A_st_free(ptr, phead, size) (((_PALLOC_)ptr)->_A_next = \
+ (_PALLOC_)(*phead), \
+ *((_PALLOC_ *)phead) = \
+ (_PALLOC_) ptr)
#ifndef BOTCH_FREE
-#define st_free(ptr, phead, size) (ptr->next = *phead, *phead = ptr)
+#define st_free(ptr, phead, size) _A_st_free(ptr, phead, size)
#else def BOTCH_FREE
#define st_free(ptr, phead, size) (botch((char *)(ptr), size), \
- ptr->next = *phead, *phead = ptr)
+ _A_st_free(ptr, phead, size))
#endif BOTCH_FREE
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* st_alloc - get a structure from a free list. If no structures left,
- create new ones. The structures for which this works are
- supposed to have as their first tag the string "next", which
- should be a pointer type.
+ create new ones.
The counterpart, st_free, is a macro, defined in alloc.h
*/
#include "alloc.h"
-struct xxx {
- char *next;
-};
-
char *
st_alloc(phead, size, count)
char **phead;
if (*phead == 0) {
p = Malloc(size * count);
- ((struct xxx *) p)->next = 0;
+ ((_PALLOC_) p)->_A_next = 0;
while (--count) {
p += size;
- ((struct xxx *) p)->next = p - size;
+ ((_PALLOC_) p)->_A_next = p - size;
}
*phead = p;
}
else p = *phead;
- *phead = ((struct xxx *)p)->next;
+ *phead = ((_PALLOC_)p)->_A_next;
retval = p;
if (size >= sizeof(long)) {
q = (long *) p;
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* st_alloc - get a structure from a free list. If no structures left,
- create new ones. The structures for which this works are
- supposed to have as their first tag the string "next", which
- should be a pointer type.
+ create new ones.
The counterpart, st_free, is a macro, defined in alloc.h
*/
#include "alloc.h"
-struct xxx {
- char *next;
-};
-
char *
std_alloc(phead, size, count, pcnt)
char **phead;
p = Malloc(size * count);
*pcnt += count;
- ((struct xxx *) p)->next = 0;
+ ((_PALLOC_) p)->_A_next = 0;
while (--count) {
p += size;
- ((struct xxx *) p)->next = p - size;
+ ((_PALLOC_) p)->_A_next = p - size;
}
*phead = p;
}
else p = *phead;
- *phead = ((struct xxx *) p)->next;
+ *phead = ((_PALLOC_) p)->_A_next;
p += size;
while (size--) *--p = 0;
return p;