From 3b038786ad3b827544f0840f0ce1fe25c822e72b Mon Sep 17 00:00:00 2001 From: ceriel Date: Mon, 3 Aug 1987 09:07:25 +0000 Subject: [PATCH] removed the limitation of the next field --- modules/src/alloc/alloc.3 | 12 ++++++++++-- modules/src/alloc/alloc.h | 12 ++++++++++-- modules/src/alloc/st_alloc.c | 14 ++++---------- modules/src/alloc/std_alloc.c | 14 ++++---------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/modules/src/alloc/alloc.3 b/modules/src/alloc/alloc.3 index 7eac028ec..e2d6fecc7 100644 --- a/modules/src/alloc/alloc.3 +++ b/modules/src/alloc/alloc.3 @@ -67,8 +67,7 @@ All these routines use \fImalloc\fR and \fIrealloc\fR. \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 @@ -93,3 +92,12 @@ malloc(3) 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. diff --git a/modules/src/alloc/alloc.h b/modules/src/alloc/alloc.h index 09916a0d4..612e6b8e8 100644 --- a/modules/src/alloc/alloc.h +++ b/modules/src/alloc/alloc.h @@ -20,9 +20,17 @@ extern char *malloc(), *realloc(); /* 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 diff --git a/modules/src/alloc/st_alloc.c b/modules/src/alloc/st_alloc.c index 9b760509f..56ea4d60e 100644 --- a/modules/src/alloc/st_alloc.c +++ b/modules/src/alloc/st_alloc.c @@ -4,18 +4,12 @@ * 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; @@ -28,15 +22,15 @@ st_alloc(phead, size, count) 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; diff --git a/modules/src/alloc/std_alloc.c b/modules/src/alloc/std_alloc.c index 56426f89f..d32bd1af9 100644 --- a/modules/src/alloc/std_alloc.c +++ b/modules/src/alloc/std_alloc.c @@ -4,18 +4,12 @@ * 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; @@ -28,15 +22,15 @@ std_alloc(phead, size, count, pcnt) 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; -- 2.34.1