added Realloc, split into separate files

This commit is contained in:
ceriel 1987-07-06 14:46:00 +00:00
parent d120b2b73a
commit 8c10914e78
8 changed files with 119 additions and 36 deletions

View file

@ -1,5 +1,8 @@
Makefile Makefile
Malloc.c Malloc.c
Srealloc.c
Realloc.c
Salloc.c
alloc.3 alloc.3
alloc.h alloc.h
botch.c botch.c

View file

@ -7,6 +7,9 @@ INCLUDES = -I. -I$(HDIR)
CFLAGS = -O $(INCLUDES) CFLAGS = -O $(INCLUDES)
CSRC = Malloc.c\ CSRC = Malloc.c\
Salloc.c\
Srealloc.c\
Realloc.c\
botch.c\ botch.c\
clear.c\ clear.c\
st_alloc.c\ st_alloc.c\
@ -15,7 +18,8 @@ CSRC = Malloc.c\
SOURCES = alloc.h\ SOURCES = alloc.h\
$(CSRC) $(CSRC)
OBJECTS = botch.o clear.o st_alloc.o Malloc.o std_alloc.o No_Mem.o OBJECTS = botch.o clear.o st_alloc.o Malloc.o Salloc.o \
Srealloc.o Realloc.o std_alloc.o No_Mem.o
all: liballoc.a all: liballoc.a

View file

@ -8,9 +8,6 @@
/* The memory allocation routines offered in this file are: /* The memory allocation routines offered in this file are:
char *Malloc(n) : allocate n bytes char *Malloc(n) : allocate n bytes
char *Srealloc(ptr, n) : reallocate buffer to n bytes
char *Salloc(str, n) : allocate n bytes, initialized with the string
str
This file imports routines from "system". This file imports routines from "system".
*/ */
@ -28,30 +25,3 @@ Malloc(sz)
if (res == 0) No_Mem(); if (res == 0) No_Mem();
return res; return res;
} }
EXPORT char *
Salloc(str, sz)
register char str[];
register unsigned int sz;
{
/* Salloc() is not a primitive function: it just allocates a
piece of storage and copies a given string into it.
*/
char *res = malloc(sz);
register char *m = res;
if (m == 0) No_Mem();
while (sz--)
*m++ = *str++;
return res;
}
EXPORT char *
Srealloc(str, sz)
char str[];
unsigned int sz;
{
str = realloc(str, sz);
if (str == 0) No_Mem();
return str;
}

View file

@ -0,0 +1,27 @@
/* $Header$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* M E M O R Y A L L O C A T I O N R O U T I N E S */
/* The memory allocation routines offered in this file are:
char *Realloc(ptr, n) : reallocate buffer to n bytes
This file imports routines from "system".
*/
#include <system.h>
#include "in_all.h"
#include "alloc.h"
EXPORT char *
Realloc(ptr, sz)
char ptr[];
unsigned int sz;
{
ptr = realloc(ptr, sz);
if (ptr == 0) No_Mem();
return ptr;
}

View file

@ -0,0 +1,35 @@
/* $Header$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* M E M O R Y A L L O C A T I O N R O U T I N E S */
/* The memory allocation routines offered in this file are:
char *Salloc(str, n) : allocate n bytes, initialized with the string
str
This file imports routines from "system".
*/
#include <system.h>
#include "in_all.h"
#include "alloc.h"
EXPORT char *
Salloc(str, sz)
register char str[];
register unsigned int sz;
{
/* Salloc() is not a primitive function: it just allocates a
piece of storage and copies a given string into it.
*/
char *res = malloc(sz);
register char *m = res;
if (m == 0) No_Mem();
while (sz--)
*m++ = *str++;
return res;
}

View file

@ -0,0 +1,25 @@
/* $Header$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* M E M O R Y A L L O C A T I O N R O U T I N E S */
/* The memory allocation routines offered in this file are:
char *Srealloc(ptr, n) : reallocate buffer to n bytes
This file imports routines from "system".
*/
#include <system.h>
#include "in_all.h"
#include "alloc.h"
EXPORT char *
Srealloc(str, sz)
char str[];
unsigned int sz;
{
return Realloc(str, sz);
}

View file

@ -1,7 +1,7 @@
.TH ALLOC 3ACK "March 25, 1986" .TH ALLOC 3ACK "March 25, 1986"
.ad .ad
.SH NAME .SH NAME
Malloc, Salloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation routines Malloc, Salloc, Realloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation routines
.SH SYNOPSIS .SH SYNOPSIS
.B #include <alloc.h> .B #include <alloc.h>
.PP .PP
@ -14,6 +14,10 @@ Malloc, Salloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation rou
.B char *str; .B char *str;
.B unsigned int size; .B unsigned int size;
.PP .PP
.B char *Realloc(ptr, size)
.B char *buf;
.B unsigned int size;
.PP
.B char *Srealloc(str, size) .B char *Srealloc(str, size)
.br .br
.B char *str; .B char *str;
@ -34,6 +38,13 @@ Malloc, Salloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation rou
.br .br
.B unsigned int size; .B unsigned int size;
.PP .PP
.br
.B clear(ptr, size)
.br
.B char *ptr;
.br
.B unsigned int size;
.PP
.SH DESCRIPTION .SH DESCRIPTION
This set of routines provides a checking memory allocation mechanism. This set of routines provides a checking memory allocation mechanism.
.PP .PP
@ -43,8 +54,14 @@ bytes, beginning on a boundary suitable for any data type.
\fISalloc\fR returns a pointer to a block of at least \fIsize\fR \fISalloc\fR returns a pointer to a block of at least \fIsize\fR
bytes, initialized with the null-terminated string \fIstr\fR. bytes, initialized with the null-terminated string \fIstr\fR.
.PP .PP
\fIRealloc\fR changes the size of
the block at \fIbuf\fR to \fIsize\fR bytes, and returns a pointer to the
(possibly moved) block.
.PP
\fISrealloc\fR reallocates \fISrealloc\fR reallocates
the string at \fIstr\fR to \fIsize\fR bytes. the string at \fIstr\fR to \fIsize\fR bytes.
It actually does the same as \fIRealloc\fP, and exists only for
backwards compatibility.
.PP .PP
All these routines use \fImalloc\fR and \fIrealloc\fR. 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.
@ -62,6 +79,8 @@ the structure to be freed, \fIphead\fR is again a pointer to a field
containing the head of the free list, and \fIsize\fR again contains the size containing the head of the free list, and \fIsize\fR again contains the size
of the structures. of the structures.
These last two routines are best used in a macro. These last two routines are best used in a macro.
.PP
\fIClear\fR clears \fIsize\fR bytes, starting at \fIptr\fR.
.SH FILES .SH FILES
.nf .nf
~em/modules/h/alloc.h ~em/modules/h/alloc.h
@ -70,7 +89,7 @@ These last two routines are best used in a macro.
.SH "SEE ALSO" .SH "SEE ALSO"
malloc(3) malloc(3)
.SH DIAGNOSTICS .SH DIAGNOSTICS
\fIMalloc\fR, \fISalloc\fR, \fISrealloc\fR, and \fIst_alloc\fR \fIMalloc\fR, \fISalloc\fR, \fIRealloc\fP, \fISrealloc\fR, and \fIst_alloc\fR
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.

View file

@ -11,11 +11,11 @@
char *Malloc(n) allocate n bytes char *Malloc(n) allocate n bytes
char *Salloc(str, n) allocate n bytes and fill them with char *Salloc(str, n) allocate n bytes and fill them with
string str string str
char *Realloc(str, n) reallocate the string at str to n bytes, char *Realloc(str, n) reallocate the block at str to n bytes.
only works if str was last allocated char *Srealloc(str, n) same as Realloc.
*/ */
extern char *Salloc(), *Malloc(), *Srealloc(); extern char *Salloc(), *Malloc(), *Srealloc(), *Realloc();
extern char *malloc(), *realloc(); 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 */