removed the limitation of the next field

This commit is contained in:
ceriel 1987-08-03 09:07:25 +00:00
parent 84bce837a0
commit 3b038786ad
4 changed files with 28 additions and 24 deletions

View file

@ -67,8 +67,7 @@ All these routines use \fImalloc\fR and \fIrealloc\fR.
\fIFree\fR can be used on pointers returned by these routines. \fIFree\fR can be used on pointers returned by these routines.
.PP .PP
\fISt_alloc\fR and \fIst_free\fR provide a mechanism for maintaining free lists \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. of structures.
This field is used to chain free structures together.
\fISt_alloc\fR takes three parameters: \fIphead\fR is a pointer to a field \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 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 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 call a routine \fINo_Mem\fR if there is no memory available. This routine
is not supposed to return. A default one, that is not supposed to return. A default one, that
gives an error message and stops execution, is provided. 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.

View file

@ -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 */ /* 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 #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 #else def BOTCH_FREE
#define st_free(ptr, phead, size) (botch((char *)(ptr), size), \ #define st_free(ptr, phead, size) (botch((char *)(ptr), size), \
ptr->next = *phead, *phead = ptr) _A_st_free(ptr, phead, size))
#endif BOTCH_FREE #endif BOTCH_FREE

View file

@ -4,18 +4,12 @@
* See the copyright notice in the ACK home directory, in the file "Copyright". * 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, /* st_alloc - get a structure from a free list. If no structures left,
create new ones. The structures for which this works are create new ones.
supposed to have as their first tag the string "next", which
should be a pointer type.
The counterpart, st_free, is a macro, defined in alloc.h The counterpart, st_free, is a macro, defined in alloc.h
*/ */
#include "alloc.h" #include "alloc.h"
struct xxx {
char *next;
};
char * char *
st_alloc(phead, size, count) st_alloc(phead, size, count)
char **phead; char **phead;
@ -28,15 +22,15 @@ st_alloc(phead, size, count)
if (*phead == 0) { if (*phead == 0) {
p = Malloc(size * count); p = Malloc(size * count);
((struct xxx *) p)->next = 0; ((_PALLOC_) p)->_A_next = 0;
while (--count) { while (--count) {
p += size; p += size;
((struct xxx *) p)->next = p - size; ((_PALLOC_) p)->_A_next = p - size;
} }
*phead = p; *phead = p;
} }
else p = *phead; else p = *phead;
*phead = ((struct xxx *)p)->next; *phead = ((_PALLOC_)p)->_A_next;
retval = p; retval = p;
if (size >= sizeof(long)) { if (size >= sizeof(long)) {
q = (long *) p; q = (long *) p;

View file

@ -4,18 +4,12 @@
* See the copyright notice in the ACK home directory, in the file "Copyright". * 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, /* st_alloc - get a structure from a free list. If no structures left,
create new ones. The structures for which this works are create new ones.
supposed to have as their first tag the string "next", which
should be a pointer type.
The counterpart, st_free, is a macro, defined in alloc.h The counterpart, st_free, is a macro, defined in alloc.h
*/ */
#include "alloc.h" #include "alloc.h"
struct xxx {
char *next;
};
char * char *
std_alloc(phead, size, count, pcnt) std_alloc(phead, size, count, pcnt)
char **phead; char **phead;
@ -28,15 +22,15 @@ std_alloc(phead, size, count, pcnt)
p = Malloc(size * count); p = Malloc(size * count);
*pcnt += count; *pcnt += count;
((struct xxx *) p)->next = 0; ((_PALLOC_) p)->_A_next = 0;
while (--count) { while (--count) {
p += size; p += size;
((struct xxx *) p)->next = p - size; ((_PALLOC_) p)->_A_next = p - size;
} }
*phead = p; *phead = p;
} }
else p = *phead; else p = *phead;
*phead = ((struct xxx *) p)->next; *phead = ((_PALLOC_) p)->_A_next;
p += size; p += size;
while (size--) *--p = 0; while (size--) *--p = 0;
return p; return p;