Skip to content

Commit

Permalink
Added max_align_t and some backports from Olaf's clib2
Browse files Browse the repository at this point in the history
  • Loading branch information
afxgroup committed Sep 12, 2023
1 parent f3b354f commit 3250444
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 17 deletions.
2 changes: 2 additions & 0 deletions GNUmakefile.os4
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,10 @@ compile-tests:
install:
$(DELETE) $(INSTALL_PREFIX)/include/*
$(DELETE) $(INSTALL_PREFIX)/lib/*
$(DELETE) $(INSTALL_PREFIX)/clib2.library*
$(COPY) $(OUTPUT_LIB)/* $(INSTALL_PREFIX)/lib/
$(COPY) libs/libauto.a $(INSTALL_PREFIX)/lib/
$(COPY) $(BUILD_DIR)/clib2.library* $(INSTALL_PREFIX)
$(COPY) $(LIB_ROOT)/library/include/* $(INSTALL_PREFIX)/include/

release:
Expand Down
1 change: 1 addition & 0 deletions library/include/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ __BEGIN_DECLS

typedef int ptrdiff_t;
typedef unsigned int size_t;
typedef struct { long long __ll; long double __ld; } max_align_t;

/* wchar_t is a built-in type in C++ */
#ifndef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions library/posix/raise.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* $Id: signal_raise.c,v 1.10 2006-01-08 12:04:24 clib2devs Exp $
* $Id: signal_raise.c,v 1.11 2023-09-12 12:04:24 clib2devs Exp $
*/

#ifndef _SIGNAL_HEADERS_H
Expand Down Expand Up @@ -106,7 +106,7 @@ raise(int sig) {
/* Drop straight into abort(), which might call signal()
again but is otherwise guaranteed to eventually
land us in _exit(). */
abort();
__abort();
}
/* If we have a SIGALRM without associated handler don't call abort but exit directly */
if (sig == SIGALRM) {
Expand Down
1 change: 1 addition & 0 deletions library/stdio/fclose.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fclose(FILE *stream) {
if (file->iob_CustomBuffer != NULL) {
SHOWMSG("Delete allocated buffer");
FreeVec(file->iob_CustomBuffer);
file->iob_CustomBuffer = NULL;
}

/* Free the lock semaphore now. */
Expand Down
5 changes: 3 additions & 2 deletions library/stdio/fgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ fgets(char *buf, int n, FILE *stream) {
/* Copy the remainder of the read buffer into the
string buffer, including the terminating line
feed character. */
memcpy(s, buffer, num_characters_in_line);
memmove(s, buffer, num_characters_in_line - 1);
s += num_characters_in_line - 1;

file->iob_BufferPosition += num_characters_in_line;
s[num_characters_in_line] = 0;
/* And that concludes the line read operation. */
goto out;
}

memcpy(s, buffer, num_bytes_in_buffer);
memmove(s, buffer, num_bytes_in_buffer);
s += num_bytes_in_buffer;

file->iob_BufferPosition += num_bytes_in_buffer;
Expand Down
8 changes: 3 additions & 5 deletions library/stdio/fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ fread(void *ptr, size_t element_size, size_t count, FILE *stream) {
if (__fgetc_check((FILE *) file) < 0)
goto out;

/* Check for overflow. */
total_size = element_size * count;
if (element_size != (total_size / count))
goto out;

SHOWVALUE(total_size);

Expand Down Expand Up @@ -128,11 +131,6 @@ fread(void *ptr, size_t element_size, size_t count, FILE *stream) {
total_size -= num_bytes_in_buffer;
if (total_size == 0)
break;

/* If the read buffer is now empty and there is still enough data
to be read, try to optimize the read operation. */
if (file->iob_BufferReadBytes == 0 && total_size >= (size_t) file->iob_BufferSize)
continue;
}

c = __getc(file);
Expand Down
3 changes: 3 additions & 0 deletions library/stdio/fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ fwrite(const void *ptr, size_t element_size, size_t count, FILE *stream) {
size_t total_bytes_written = 0;
size_t total_size;

/* Check for overflow. */
total_size = element_size * count;
if (element_size != (total_size / count))
goto out;

/* If this is an unbuffered interactive stream, we will switch
to line buffered mode in order to improve readability of
Expand Down
1 change: 1 addition & 0 deletions library/stdio/gets.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ gets(char *s) {
string buffer, including the terminating line
feed character. */
memmove(s, buffer, num_characters_in_line);
s += num_characters_in_line;

file->iob_BufferPosition += num_characters_in_line;
s[num_characters_in_line] = 0;
Expand Down
2 changes: 1 addition & 1 deletion library/stdio/setvbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ setvbuf(FILE *stream, char *buf, int bufmode, size_t size) {
allocate some memory for it. */
if (size > 0 && buf == NULL) {
/* Allocate a little more memory than necessary. */
new_buffer = malloc(size + (__clib2->__cache_line_size - 1));
new_buffer = AllocVecTags(size + (__clib2->__cache_line_size - 1), AVT_Type, MEMF_SHARED, TAG_DONE);
if (new_buffer == NULL) {
__set_errno(ENOBUFS);
goto out;
Expand Down
17 changes: 12 additions & 5 deletions library/stdlib/abort.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
#endif /* _STDIO_HEADERS_H */

void
abort(void) {
__abort(void) {
ENTER();
struct _clib2 *__clib2 = __CLIB2;

/* Try to call the signal handler that might be in charge of
handling cleanup operations, etc. */
raise(SIGABRT);

/* If the signal handler returns it means that we still have
to terminate the program. */

Expand All @@ -36,3 +32,14 @@ abort(void) {
does not trigger the exit trap. */
_exit(EXIT_FAILURE);
}

void
abort(void) {
/* Try to call the signal handler that might be in charge of
handling cleanup operations, etc. */
raise(SIGABRT);

/* If the signal handler returns it means that we still have
to terminate the program. */
__abort();
}
4 changes: 2 additions & 2 deletions library/stdlib/calloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ __calloc(size_t num_elements, size_t element_size) {
D(("calloc(num_elements=%ld, element_size=%ld) overflow"));
}

return (result);
return result;
}

void *
Expand All @@ -44,5 +44,5 @@ calloc(size_t num_elements, size_t element_size) {

result = __calloc(num_elements, element_size);

return (result);
return result;
}
2 changes: 2 additions & 0 deletions library/stdlib/stdlib_protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extern void reent_exit(struct _clib2 *__clib2, BOOL fallback);
extern void __check_abort(void);
extern int32 _start(STRPTR argstring, int32 arglen, struct ExecBase *sysbase);

extern void __abort(void);

/* stdlib_assertion_failure.c */
extern void __assertion_failure(const char *file_name, int line_number, const char *expression);

Expand Down
20 changes: 20 additions & 0 deletions test_programs/misc/max_align.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stddef.h>
#include <stdalign.h>
#include <stdlib.h>
#include <inttypes.h>

int main(void) {
size_t sz = sizeof(max_align_t);
printf("Size of max_align_t is %zu (%#zx)\n", sz, sz);

size_t a = alignof(max_align_t);
printf("Alignment of max_align_t is %zu (%#zx)\n", a, a);

void *p = malloc(123);
printf("The address obtained from malloc(123) is %#"
PRIxPTR
"\n",
(uintptr_t) p);
free(p);
}

0 comments on commit 3250444

Please sign in to comment.