From 0f75cc09ad5b3caef4536997fa667cf90e21cd64 Mon Sep 17 00:00:00 2001 From: carl Date: Tue, 19 Feb 2019 00:31:58 +0800 Subject: [PATCH] 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 --- modules/src/alloc/Amake.srclist | 26 ++--- modules/src/alloc/CMakeLists.txt | 29 +++++ modules/src/alloc/Malloc.c | 4 +- modules/src/alloc/No_Mem.c | 11 +- modules/src/alloc/Realloc.c | 5 +- modules/src/alloc/Salloc.c | 5 +- modules/src/alloc/Srealloc.c | 5 +- modules/src/alloc/alloc.3 | 189 ++++++++++++++----------------- modules/src/alloc/botch.c | 5 +- modules/src/alloc/clear.c | 5 +- modules/src/alloc/st_alloc.c | 12 +- modules/src/alloc/std_alloc.c | 6 +- 12 files changed, 143 insertions(+), 159 deletions(-) create mode 100644 modules/src/alloc/CMakeLists.txt diff --git a/modules/src/alloc/Amake.srclist b/modules/src/alloc/Amake.srclist index 377a0a5ba..829f2183b 100644 --- a/modules/src/alloc/Amake.srclist +++ b/modules/src/alloc/Amake.srclist @@ -1,13 +1,13 @@ -L_ACK_MODULES_ALLOC = { - $PWD/Malloc.c, - $PWD/Salloc.c, - $PWD/Srealloc.c, - $PWD/Realloc.c, - $PWD/botch.c, - $PWD/clear.c, - $PWD/st_alloc.c, - $PWD/std_alloc.c, - $PWD/No_Mem.c, - $PWD/alloc.h -}; - +L_ACK_MODULES_ALLOC = { + $PWD/Malloc.c, + $PWD/Salloc.c, + $PWD/Srealloc.c, + $PWD/Realloc.c, + $PWD/botch.c, + $PWD/clear.c, + $PWD/st_alloc.c, + $PWD/std_alloc.c, + $PWD/No_Mem.c, + $PWD/alloc.h +}; + diff --git a/modules/src/alloc/CMakeLists.txt b/modules/src/alloc/CMakeLists.txt new file mode 100644 index 000000000..0592965de --- /dev/null +++ b/modules/src/alloc/CMakeLists.txt @@ -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) + diff --git a/modules/src/alloc/Malloc.c b/modules/src/alloc/Malloc.c index 6077829ba..567b2dc08 100644 --- a/modules/src/alloc/Malloc.c +++ b/modules/src/alloc/Malloc.c @@ -17,9 +17,7 @@ extern char *malloc(); #endif #include "alloc.h" -char * -Malloc(sz) - unsigned int sz; +char *Malloc(unsigned int sz) { register char *res = malloc(sz); diff --git a/modules/src/alloc/No_Mem.c b/modules/src/alloc/No_Mem.c index 8011c1fdc..2cd3fd066 100644 --- a/modules/src/alloc/No_Mem.c +++ b/modules/src/alloc/No_Mem.c @@ -1,14 +1,15 @@ /* $Id$ */ /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". */ -#include +#include +#include #include "alloc.h" -void -No_Mem() +void No_Mem(void) { - sys_write(STDERR, "Out of memory\n", 14); - sys_stop(S_EXIT); + fprintf(stderr,"Out of memory\n"); + exit(1); } diff --git a/modules/src/alloc/Realloc.c b/modules/src/alloc/Realloc.c index 0399dc0aa..35c6fbf1d 100644 --- a/modules/src/alloc/Realloc.c +++ b/modules/src/alloc/Realloc.c @@ -19,10 +19,7 @@ extern char *realloc(); #include "alloc.h" -char * -Realloc(ptr, sz) - char ptr[]; - unsigned int sz; +char *Realloc(char ptr[], unsigned int sz) { register char *mptr; diff --git a/modules/src/alloc/Salloc.c b/modules/src/alloc/Salloc.c index 7f98690d0..1422b1878 100644 --- a/modules/src/alloc/Salloc.c +++ b/modules/src/alloc/Salloc.c @@ -19,10 +19,7 @@ extern char *malloc(); #include "alloc.h" -char * -Salloc(str, sz) - register char *str; - register unsigned int sz; +char *Salloc(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. diff --git a/modules/src/alloc/Srealloc.c b/modules/src/alloc/Srealloc.c index d799f6f8e..32ddf9a92 100644 --- a/modules/src/alloc/Srealloc.c +++ b/modules/src/alloc/Srealloc.c @@ -12,10 +12,7 @@ #include "alloc.h" -char * -Srealloc(str, sz) - char str[]; - unsigned int sz; +char *Srealloc(char str[], unsigned int sz) { return Realloc(str, sz); } diff --git a/modules/src/alloc/alloc.3 b/modules/src/alloc/alloc.3 index f7c963cad..cd7bfbf5e 100644 --- a/modules/src/alloc/alloc.3 +++ b/modules/src/alloc/alloc.3 @@ -1,107 +1,82 @@ -.TH ALLOC 3 "$Revision$" -.ad -.SH NAME -Malloc, Salloc, Realloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation routines -.SH SYNOPSIS -.B #include -.PP -.B char *Malloc(size) -.br -.B unsigned int size; -.PP -.B char *Salloc(str, size) -.br -.B char *str; -.B unsigned int size; -.PP -.B char *Realloc(ptr, size) -.B char *buf; -.B unsigned int size; -.PP -.B char *Srealloc(str, size) -.br -.B char *str; -.br -.B unsigned int size; -.PP -.B char *st_alloc(phead, size, count) -.br -.B char **phead; -.br -.B unsigned int size; -.PP -.B st_free(ptr, phead, size) -.br -.B char *ptr; -.br -.B char **phead; -.br -.B unsigned int size; -.PP -.B void clear(ptr, size) -.br -.B char *ptr; -.br -.B unsigned int size; -.PP -.void No_Mem() -.PP -.SH DESCRIPTION -This set of routines provides a checking memory allocation mechanism. -.PP -\fIMalloc\fR returns a pointer to a block of at least \fIsize\fR -bytes, beginning on a boundary suitable for any data type. -.PP -\fISalloc\fR returns a pointer to a block of at least \fIsize\fR -bytes, initialized with the null-terminated string \fIstr\fR. -.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. If \fIbuf\fP is a null pointer, \fIRealloc\fP -behaves as \fIMalloc\fP. -.PP -\fISrealloc\fR reallocates -the string at \fIstr\fR to \fIsize\fR bytes. -It actually does the same as \fIRealloc\fP, and exists only for -backwards compatibility. -.PP -All these routines use \fImalloc\fR and \fIrealloc\fR. -The routine \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. -\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 -in case the free list is exhausted. -It returns a pointer to a zero-initialized structure. -\fISt_free\fR also takes three parameters: \fIptr\fR is a pointer to -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 -of the structures. -These last two routines are best used in a macro. -.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. +.TH ALLOC 3 "$Revision$" +.ad +.SH NAME +Malloc, Salloc, Realloc, Srealloc, st_alloc, st_free\ \-\ low level memory allocation routines +.SH SYNOPSIS +.B #include +.PP +.B char *Malloc(unsigned int size) +.PP +.B char *Salloc(char *str, unsigned int size) +.PP +.B char *Realloc(char *buf, unsigned int size) +.PP +.B char *Srealloc(char *str, unsigned int size) +.PP +.B char *st_alloc(char **phead, unsigned int size, int count) +.PP +.B st_free(char *ptr, char **phead, unsigned int size) +.PP +.B void clear(char *ptr, unsigned int size) +.PP +.void No_Mem() +.PP +.SH DESCRIPTION +This set of routines provides a checking memory allocation mechanism. +.PP +\fIMalloc\fR returns a pointer to a block of at least \fIsize\fR +bytes, beginning on a boundary suitable for any data type. +.PP +\fISalloc\fR returns a pointer to a block of at least \fIsize\fR +bytes, initialized with the null-terminated string \fIstr\fR. +.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. If \fIbuf\fP is a null pointer, \fIRealloc\fP +behaves as \fIMalloc\fP. +.PP +\fISrealloc\fR reallocates +the string at \fIstr\fR to \fIsize\fR bytes. +It actually does the same as \fIRealloc\fP, and exists only for +backwards compatibility. +.PP +All these routines use \fImalloc\fR and \fIrealloc\fR. +The routine \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. +\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 +in case the free list is exhausted. +It returns a pointer to a zero-initialized structure. +\fISt_free\fR also takes three parameters: \fIptr\fR is a pointer to +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 +of the structures. +These last two routines are best used in a macro. +.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. diff --git a/modules/src/alloc/botch.c b/modules/src/alloc/botch.c index 0365f75df..094627252 100644 --- a/modules/src/alloc/botch.c +++ b/modules/src/alloc/botch.c @@ -9,10 +9,7 @@ #include "alloc.h" -void -botch(ptr, n) - register char *ptr; - register unsigned int n; +void botch(register char *ptr, register unsigned int n) { while (n >= sizeof (long)) { /* high-speed botch loop */ diff --git a/modules/src/alloc/clear.c b/modules/src/alloc/clear.c index 40642238d..4cf0c87de 100644 --- a/modules/src/alloc/clear.c +++ b/modules/src/alloc/clear.c @@ -10,10 +10,7 @@ /* instead of Calloc: */ -void -clear(ptr, n) - register char *ptr; - register unsigned int n; +void clear(register char *ptr, register unsigned int n) { register long *q = (long *) ptr; diff --git a/modules/src/alloc/st_alloc.c b/modules/src/alloc/st_alloc.c index 704b853a6..0457420cc 100644 --- a/modules/src/alloc/st_alloc.c +++ b/modules/src/alloc/st_alloc.c @@ -12,16 +12,16 @@ #include #else extern char *malloc(); +#ifndef NULL +#define NULL 0 +#endif #endif #include "alloc.h" -char * -st_alloc(phead, size, count) - char **phead; - register unsigned int size; +char *st_alloc(char **phead, register unsigned int size, int count) { - register char *p; + register char *p = NULL; register long *q; char *retval; @@ -29,7 +29,7 @@ st_alloc(phead, size, count) while (count >= 1 && (p = malloc(size * count)) == 0) { count >>= 1; } - if (p == 0) { + if (p == NULL) { No_Mem(); } ((_PALLOC_) p)->_A_next = 0; diff --git a/modules/src/alloc/std_alloc.c b/modules/src/alloc/std_alloc.c index 1628c9f55..c66eb8d11 100644 --- a/modules/src/alloc/std_alloc.c +++ b/modules/src/alloc/std_alloc.c @@ -17,11 +17,7 @@ extern char *malloc(); #include "alloc.h" -char * -std_alloc(phead, size, count, pcnt) - char **phead; - register unsigned int size; - int *pcnt; +char *std_alloc(char **phead, register unsigned int size, int count, int *pcnt) { register char *p; register long *q; -- 2.34.1