Better ANSI C compatibility and portability - part 1:

+ Addition of function prototypes.
+ Change function definitions to ANSI C style.
+ Convert to sed scripts some shell scripts for better portability.
+ Reduce usage of em_path.h
This commit is contained in:
carl 2019-02-19 00:31:58 +08:00
parent 4555c1c8cf
commit 0f75cc09ad
12 changed files with 143 additions and 159 deletions

View file

@ -1,13 +1,13 @@
L_ACK_MODULES_ALLOC = { L_ACK_MODULES_ALLOC = {
$PWD/Malloc.c, $PWD/Malloc.c,
$PWD/Salloc.c, $PWD/Salloc.c,
$PWD/Srealloc.c, $PWD/Srealloc.c,
$PWD/Realloc.c, $PWD/Realloc.c,
$PWD/botch.c, $PWD/botch.c,
$PWD/clear.c, $PWD/clear.c,
$PWD/st_alloc.c, $PWD/st_alloc.c,
$PWD/std_alloc.c, $PWD/std_alloc.c,
$PWD/No_Mem.c, $PWD/No_Mem.c,
$PWD/alloc.h $PWD/alloc.h
}; };

View file

@ -0,0 +1,29 @@
cmake_minimum_required (VERSION 3.0)
project (alloc)
set(SRC
botch.c
clear.c
Malloc.c
No_Mem.c
Realloc.c
Salloc.c
Srealloc.c
std_alloc.c
st_alloc.c
alloc.h
)
add_library(${PROJECT_NAME} ${SRC})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "alloc.h")
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/alloc.3 DESTINATION man OPTIONAL)

View file

@ -17,9 +17,7 @@ extern char *malloc();
#endif #endif
#include "alloc.h" #include "alloc.h"
char * char *Malloc(unsigned int sz)
Malloc(sz)
unsigned int sz;
{ {
register char *res = malloc(sz); register char *res = malloc(sz);

View file

@ -1,14 +1,15 @@
/* $Id$ */ /* $Id$ */
/* /*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* 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".
*/ */
#include <system.h> #include <stdio.h>
#include <stdlib.h>
#include "alloc.h" #include "alloc.h"
void void No_Mem(void)
No_Mem()
{ {
sys_write(STDERR, "Out of memory\n", 14); fprintf(stderr,"Out of memory\n");
sys_stop(S_EXIT); exit(1);
} }

View file

@ -19,10 +19,7 @@ extern char *realloc();
#include "alloc.h" #include "alloc.h"
char * char *Realloc(char ptr[], unsigned int sz)
Realloc(ptr, sz)
char ptr[];
unsigned int sz;
{ {
register char *mptr; register char *mptr;

View file

@ -19,10 +19,7 @@ extern char *malloc();
#include "alloc.h" #include "alloc.h"
char * char *Salloc(register char *str, register unsigned int sz)
Salloc(str, sz)
register char *str;
register unsigned int sz;
{ {
/* Salloc() is not a primitive function: it just allocates a /* Salloc() is not a primitive function: it just allocates a
piece of storage and copies a given string into it. piece of storage and copies a given string into it.

View file

@ -12,10 +12,7 @@
#include "alloc.h" #include "alloc.h"
char * char *Srealloc(char str[], unsigned int sz)
Srealloc(str, sz)
char str[];
unsigned int sz;
{ {
return Realloc(str, sz); return Realloc(str, sz);
} }

View file

@ -1,107 +1,82 @@
.TH ALLOC 3 "$Revision$" .TH ALLOC 3 "$Revision$"
.ad .ad
.SH NAME .SH NAME
Malloc, Salloc, Realloc, 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
.B char *Malloc(size) .B char *Malloc(unsigned int size)
.br .PP
.B unsigned int size; .B char *Salloc(char *str, unsigned int size)
.PP .PP
.B char *Salloc(str, size) .B char *Realloc(char *buf, unsigned int size)
.br .PP
.B char *str; .B char *Srealloc(char *str, unsigned int size)
.B unsigned int size; .PP
.PP .B char *st_alloc(char **phead, unsigned int size, int count)
.B char *Realloc(ptr, size) .PP
.B char *buf; .B st_free(char *ptr, char **phead, unsigned int size)
.B unsigned int size; .PP
.PP .B void clear(char *ptr, unsigned int size)
.B char *Srealloc(str, size) .PP
.br .void No_Mem()
.B char *str; .PP
.br .SH DESCRIPTION
.B unsigned int size; This set of routines provides a checking memory allocation mechanism.
.PP .PP
.B char *st_alloc(phead, size, count) \fIMalloc\fR returns a pointer to a block of at least \fIsize\fR
.br bytes, beginning on a boundary suitable for any data type.
.B char **phead; .PP
.br \fISalloc\fR returns a pointer to a block of at least \fIsize\fR
.B unsigned int size; bytes, initialized with the null-terminated string \fIstr\fR.
.PP .PP
.B st_free(ptr, phead, size) \fIRealloc\fR changes the size of
.br the block at \fIbuf\fR to \fIsize\fR bytes, and returns a pointer to the
.B char *ptr; (possibly moved) block. If \fIbuf\fP is a null pointer, \fIRealloc\fP
.br behaves as \fIMalloc\fP.
.B char **phead; .PP
.br \fISrealloc\fR reallocates
.B unsigned int size; the string at \fIstr\fR to \fIsize\fR bytes.
.PP It actually does the same as \fIRealloc\fP, and exists only for
.B void clear(ptr, size) backwards compatibility.
.br .PP
.B char *ptr; All these routines use \fImalloc\fR and \fIrealloc\fR.
.br The routine \fIfree\fR can be used on pointers returned by these routines.
.B unsigned int size; .PP
.PP \fISt_alloc\fR and \fIst_free\fR provide a mechanism for maintaining free lists
.void No_Mem() of structures.
.PP \fISt_alloc\fR takes three parameters: \fIphead\fR is a pointer to a field
.SH DESCRIPTION containing the head of the free list, \fIsize\fR contains the size of the
This set of routines provides a checking memory allocation mechanism. structures, and \fIcount\fR indicates how many new structures must be allocated
.PP in case the free list is exhausted.
\fIMalloc\fR returns a pointer to a block of at least \fIsize\fR It returns a pointer to a zero-initialized structure.
bytes, beginning on a boundary suitable for any data type. \fISt_free\fR also takes three parameters: \fIptr\fR is a pointer to
.PP the structure to be freed, \fIphead\fR is again a pointer to a field
\fISalloc\fR returns a pointer to a block of at least \fIsize\fR containing the head of the free list, and \fIsize\fR again contains the size
bytes, initialized with the null-terminated string \fIstr\fR. of the structures.
.PP These last two routines are best used in a macro.
\fIRealloc\fR changes the size of .PP
the block at \fIbuf\fR to \fIsize\fR bytes, and returns a pointer to the \fIClear\fR clears \fIsize\fR bytes, starting at \fIptr\fR.
(possibly moved) block. If \fIbuf\fP is a null pointer, \fIRealloc\fP .SH FILES
behaves as \fIMalloc\fP. .nf
.PP ~em/modules/h/alloc.h
\fISrealloc\fR reallocates ~em/modules/lib/liballoc.a
the string at \fIstr\fR to \fIsize\fR bytes. .fi
It actually does the same as \fIRealloc\fP, and exists only for .SH "MODULES USED"
backwards compatibility. system(3)
.PP .SH "SEE ALSO"
All these routines use \fImalloc\fR and \fIrealloc\fR. malloc(3)
The routine \fIfree\fR can be used on pointers returned by these routines. .SH DIAGNOSTICS
.PP \fIMalloc\fR, \fISalloc\fR, \fIRealloc\fP, \fISrealloc\fR, and \fIst_alloc\fR
\fISt_alloc\fR and \fIst_free\fR provide a mechanism for maintaining free lists call a routine \fINo_Mem\fR if there is no memory available. This routine
of structures. is not supposed to return. A default one, that
\fISt_alloc\fR takes three parameters: \fIphead\fR is a pointer to a field gives an error message and stops execution, is provided.
containing the head of the free list, \fIsize\fR contains the size of the .SH BUGS
structures, and \fIcount\fR indicates how many new structures must be allocated The
in case the free list is exhausted. .I st_alloc
It returns a pointer to a zero-initialized structure. mechanism only works for structures that are large enough to contain one
\fISt_free\fR also takes three parameters: \fIptr\fR is a pointer to pointer.
the structure to be freed, \fIphead\fR is again a pointer to a field Also,
containing the head of the free list, and \fIsize\fR again contains the size .I st_free
of the structures. actually is a macro, and references its arguments more than once, so they
These last two routines are best used in a macro. better not have side-effects.
.PP
\fIClear\fR clears \fIsize\fR bytes, starting at \fIptr\fR.
.SH FILES
.nf
~em/modules/h/alloc.h
~em/modules/lib/liballoc.a
.fi
.SH "MODULES USED"
system(3)
.SH "SEE ALSO"
malloc(3)
.SH DIAGNOSTICS
\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
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.

View file

@ -9,10 +9,7 @@
#include "alloc.h" #include "alloc.h"
void void botch(register char *ptr, register unsigned int n)
botch(ptr, n)
register char *ptr;
register unsigned int n;
{ {
while (n >= sizeof (long)) { while (n >= sizeof (long)) {
/* high-speed botch loop */ /* high-speed botch loop */

View file

@ -10,10 +10,7 @@
/* instead of Calloc: */ /* instead of Calloc: */
void void clear(register char *ptr, register unsigned int n)
clear(ptr, n)
register char *ptr;
register unsigned int n;
{ {
register long *q = (long *) ptr; register long *q = (long *) ptr;

View file

@ -12,16 +12,16 @@
#include <stdlib.h> #include <stdlib.h>
#else #else
extern char *malloc(); extern char *malloc();
#ifndef NULL
#define NULL 0
#endif
#endif #endif
#include "alloc.h" #include "alloc.h"
char * char *st_alloc(char **phead, register unsigned int size, int count)
st_alloc(phead, size, count)
char **phead;
register unsigned int size;
{ {
register char *p; register char *p = NULL;
register long *q; register long *q;
char *retval; char *retval;
@ -29,7 +29,7 @@ st_alloc(phead, size, count)
while (count >= 1 && (p = malloc(size * count)) == 0) { while (count >= 1 && (p = malloc(size * count)) == 0) {
count >>= 1; count >>= 1;
} }
if (p == 0) { if (p == NULL) {
No_Mem(); No_Mem();
} }
((_PALLOC_) p)->_A_next = 0; ((_PALLOC_) p)->_A_next = 0;

View file

@ -17,11 +17,7 @@ extern char *malloc();
#include "alloc.h" #include "alloc.h"
char * char *std_alloc(char **phead, register unsigned int size, int count, int *pcnt)
std_alloc(phead, size, count, pcnt)
char **phead;
register unsigned int size;
int *pcnt;
{ {
register char *p; register char *p;
register long *q; register long *q;