Skip to content

Commit 3250444

Browse files
committed
Added max_align_t and some backports from Olaf's clib2
1 parent f3b354f commit 3250444

File tree

13 files changed

+53
-17
lines changed

13 files changed

+53
-17
lines changed

GNUmakefile.os4

+2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,10 @@ compile-tests:
358358
install:
359359
$(DELETE) $(INSTALL_PREFIX)/include/*
360360
$(DELETE) $(INSTALL_PREFIX)/lib/*
361+
$(DELETE) $(INSTALL_PREFIX)/clib2.library*
361362
$(COPY) $(OUTPUT_LIB)/* $(INSTALL_PREFIX)/lib/
362363
$(COPY) libs/libauto.a $(INSTALL_PREFIX)/lib/
364+
$(COPY) $(BUILD_DIR)/clib2.library* $(INSTALL_PREFIX)
363365
$(COPY) $(LIB_ROOT)/library/include/* $(INSTALL_PREFIX)/include/
364366

365367
release:

library/include/stddef.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __BEGIN_DECLS
2020

2121
typedef int ptrdiff_t;
2222
typedef unsigned int size_t;
23+
typedef struct { long long __ll; long double __ld; } max_align_t;
2324

2425
/* wchar_t is a built-in type in C++ */
2526
#ifndef __cplusplus

library/posix/raise.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $Id: signal_raise.c,v 1.10 2006-01-08 12:04:24 clib2devs Exp $
2+
* $Id: signal_raise.c,v 1.11 2023-09-12 12:04:24 clib2devs Exp $
33
*/
44

55
#ifndef _SIGNAL_HEADERS_H
@@ -106,7 +106,7 @@ raise(int sig) {
106106
/* Drop straight into abort(), which might call signal()
107107
again but is otherwise guaranteed to eventually
108108
land us in _exit(). */
109-
abort();
109+
__abort();
110110
}
111111
/* If we have a SIGALRM without associated handler don't call abort but exit directly */
112112
if (sig == SIGALRM) {

library/stdio/fclose.c

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ fclose(FILE *stream) {
8989
if (file->iob_CustomBuffer != NULL) {
9090
SHOWMSG("Delete allocated buffer");
9191
FreeVec(file->iob_CustomBuffer);
92+
file->iob_CustomBuffer = NULL;
9293
}
9394

9495
/* Free the lock semaphore now. */

library/stdio/fgets.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ fgets(char *buf, int n, FILE *stream) {
7171
/* Copy the remainder of the read buffer into the
7272
string buffer, including the terminating line
7373
feed character. */
74-
memcpy(s, buffer, num_characters_in_line);
74+
memmove(s, buffer, num_characters_in_line - 1);
75+
s += num_characters_in_line - 1;
7576

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

82-
memcpy(s, buffer, num_bytes_in_buffer);
83+
memmove(s, buffer, num_bytes_in_buffer);
8384
s += num_bytes_in_buffer;
8485

8586
file->iob_BufferPosition += num_bytes_in_buffer;

library/stdio/fread.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ fread(void *ptr, size_t element_size, size_t count, FILE *stream) {
6767
if (__fgetc_check((FILE *) file) < 0)
6868
goto out;
6969

70+
/* Check for overflow. */
7071
total_size = element_size * count;
72+
if (element_size != (total_size / count))
73+
goto out;
7174

7275
SHOWVALUE(total_size);
7376

@@ -128,11 +131,6 @@ fread(void *ptr, size_t element_size, size_t count, FILE *stream) {
128131
total_size -= num_bytes_in_buffer;
129132
if (total_size == 0)
130133
break;
131-
132-
/* If the read buffer is now empty and there is still enough data
133-
to be read, try to optimize the read operation. */
134-
if (file->iob_BufferReadBytes == 0 && total_size >= (size_t) file->iob_BufferSize)
135-
continue;
136134
}
137135

138136
c = __getc(file);

library/stdio/fwrite.c

+3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ fwrite(const void *ptr, size_t element_size, size_t count, FILE *stream) {
5555
size_t total_bytes_written = 0;
5656
size_t total_size;
5757

58+
/* Check for overflow. */
5859
total_size = element_size * count;
60+
if (element_size != (total_size / count))
61+
goto out;
5962

6063
/* If this is an unbuffered interactive stream, we will switch
6164
to line buffered mode in order to improve readability of

library/stdio/gets.c

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ gets(char *s) {
6161
string buffer, including the terminating line
6262
feed character. */
6363
memmove(s, buffer, num_characters_in_line);
64+
s += num_characters_in_line;
6465

6566
file->iob_BufferPosition += num_characters_in_line;
6667
s[num_characters_in_line] = 0;

library/stdio/setvbuf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ setvbuf(FILE *stream, char *buf, int bufmode, size_t size) {
7777
allocate some memory for it. */
7878
if (size > 0 && buf == NULL) {
7979
/* Allocate a little more memory than necessary. */
80-
new_buffer = malloc(size + (__clib2->__cache_line_size - 1));
80+
new_buffer = AllocVecTags(size + (__clib2->__cache_line_size - 1), AVT_Type, MEMF_SHARED, TAG_DONE);
8181
if (new_buffer == NULL) {
8282
__set_errno(ENOBUFS);
8383
goto out;

library/stdlib/abort.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
#endif /* _STDIO_HEADERS_H */
1616

1717
void
18-
abort(void) {
18+
__abort(void) {
1919
ENTER();
2020
struct _clib2 *__clib2 = __CLIB2;
2121

22-
/* Try to call the signal handler that might be in charge of
23-
handling cleanup operations, etc. */
24-
raise(SIGABRT);
25-
2622
/* If the signal handler returns it means that we still have
2723
to terminate the program. */
2824

@@ -36,3 +32,14 @@ abort(void) {
3632
does not trigger the exit trap. */
3733
_exit(EXIT_FAILURE);
3834
}
35+
36+
void
37+
abort(void) {
38+
/* Try to call the signal handler that might be in charge of
39+
handling cleanup operations, etc. */
40+
raise(SIGABRT);
41+
42+
/* If the signal handler returns it means that we still have
43+
to terminate the program. */
44+
__abort();
45+
}

library/stdlib/calloc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ __calloc(size_t num_elements, size_t element_size) {
3535
D(("calloc(num_elements=%ld, element_size=%ld) overflow"));
3636
}
3737

38-
return (result);
38+
return result;
3939
}
4040

4141
void *
@@ -44,5 +44,5 @@ calloc(size_t num_elements, size_t element_size) {
4444

4545
result = __calloc(num_elements, element_size);
4646

47-
return (result);
47+
return result;
4848
}

library/stdlib/stdlib_protos.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern void reent_exit(struct _clib2 *__clib2, BOOL fallback);
4747
extern void __check_abort(void);
4848
extern int32 _start(STRPTR argstring, int32 arglen, struct ExecBase *sysbase);
4949

50+
extern void __abort(void);
51+
5052
/* stdlib_assertion_failure.c */
5153
extern void __assertion_failure(const char *file_name, int line_number, const char *expression);
5254

test_programs/misc/max_align.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <stddef.h>
3+
#include <stdalign.h>
4+
#include <stdlib.h>
5+
#include <inttypes.h>
6+
7+
int main(void) {
8+
size_t sz = sizeof(max_align_t);
9+
printf("Size of max_align_t is %zu (%#zx)\n", sz, sz);
10+
11+
size_t a = alignof(max_align_t);
12+
printf("Alignment of max_align_t is %zu (%#zx)\n", a, a);
13+
14+
void *p = malloc(123);
15+
printf("The address obtained from malloc(123) is %#"
16+
PRIxPTR
17+
"\n",
18+
(uintptr_t) p);
19+
free(p);
20+
}

0 commit comments

Comments
 (0)