removed the limitation of the next field
This commit is contained in:
parent
84bce837a0
commit
3b038786ad
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue