ref: 8a26e53e24fd1fca9cc2ff6e60d60d69c25d602f
parent: 348489368dec95826593e6a8faecf09c0f421cfc
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jan 13 00:27:50 EST 2025
add force-aligned dumb memory wrappers for macos/m68k
--- a/3rd/mp/mpaux.c
+++ b/3rd/mp/mpaux.c
@@ -57,7 +57,7 @@
n = DIGITS(n);
if(n < mpmindigits)
n = mpmindigits;
- b = calloc(1, sizeof(mpint) + n*Dbytes);
+ b = MEM_CALLOC(1, sizeof(mpint) + n*Dbytes);
if(b == nil){
fprintf(stderr, "mpnew: no memory\n");
exit(2);
--- a/3rd/mp/mpmul.c
+++ b/3rd/mp/mpmul.c
@@ -39,7 +39,7 @@
v1 = b + v0len;
// room for the partial products
- t = calloc(1, Dbytes*5*(2*n+1));
+ t = MEM_CALLOC(1, Dbytes*5*(2*n+1));
if(t == nil){
fprintf(stderr, "mpkaratsuba: no memory\n");
exit(2);
--- a/3rd/mp/test.c
+++ b/3rd/mp/test.c
@@ -76,7 +76,7 @@
fprintf(stderr, "A strtomp(%s) -> nil\n", p);
exit(2);
}
- free(p);
+ MEM_FREE(p);
printf("%s\n", MPB(base[i], b));
switch(base[i]){
@@ -90,7 +90,7 @@
fprintf(stderr, "B strtomp(%s) -> nil\n", p);
exit(2);
}
- free(p);
+ MEM_FREE(p);
printf("%s\n", MPB(base[i], b));
break;
}
--- a/macos/femtolispm68k.r
+++ b/macos/femtolispm68k.r
@@ -18,5 +18,5 @@
reserved,
reserved,
4 * 1024 * 1024,
- 2 * 1024 * 1024
+ 1 * 1024 * 1024
};
--- a/macos/platform.h
+++ b/macos/platform.h
@@ -25,14 +25,9 @@
#include <unistd.h>
#include <wctype.h>
#include <wchar.h>
+#include "mem.h"
#define __os_name__ "macos"
-
-#define MEM_CALLOC(n, sz) calloc((size_t)(n), (size_t)(sz))
-#define MEM_ALLOC(n) malloc((size_t)(n))
-#define MEM_REALLOC(p, n) realloc((p), (size_t)(n))
-#define MEM_FREE(x) free(x)
-#define MEM_STRDUP(s) strdup(s)
#define sadd_overflow __builtin_add_overflow
#define sadd_overflow_64 __builtin_add_overflow
--- /dev/null
+++ b/mem.c
@@ -1,0 +1,55 @@
+#include "platform.h"
+
+#if defined(MEM_NEED_ALIGNED)
+void *
+fl_malloc(size_t sz)
+{
+ uint8_t *p = malloc(sz+1+7);
+ uint8_t *a = (uint8_t*)(((uintptr_t)p+1+7) & ~(uintptr_t)7);
+ a[-1] = a-p;
+ return a;
+}
+
+void *
+fl_calloc(size_t n, size_t esz)
+{
+ size_t sz = n*esz;
+ assert(sz >= esz);
+ uint8_t *p = fl_malloc(sz);
+ memset(p, 0, sz);
+ return p;
+}
+
+void *
+fl_realloc(void *a_, size_t sz)
+{
+ uint8_t *a = a_;
+ assert(((uintptr_t)a & 7) == 0);
+ uint8_t *p = a == nil ? nil : a-a[-1];
+ if((p = realloc(p, sz+1+7)) == nil)
+ return nil;
+ a = (void*)(((uintptr_t)p+1+7) & ~(uintptr_t)7);
+ a[-1] = a-p;
+ return a;
+}
+
+void
+fl_free(void *a_)
+{
+ uint8_t *a = a_;
+ if(a == nil)
+ return;
+ assert(((uintptr_t)a & 7) == 0);
+ uint8_t *p = a - a[-1];
+ free(p);
+}
+
+char *
+fl_strdup(const char *s)
+{
+ size_t sz = strlen(s)+1;
+ char *p = fl_malloc(sz);
+ memcpy(p, s, sz);
+ return p;
+}
+#endif
--- /dev/null
+++ b/mem.h
@@ -1,0 +1,18 @@
+#if defined(MEM_NEED_ALIGNED)
+void *fl_malloc(size_t sz);
+void *fl_calloc(size_t n, size_t esz);
+void *fl_realloc(void *p0, size_t sz);
+void fl_free(void *p0);
+char *fl_strdup(const char *s);
+#define MEM_CALLOC(n, sz) fl_calloc((size_t)(n), (size_t)(sz))
+#define MEM_ALLOC(n) fl_malloc((size_t)(n))
+#define MEM_REALLOC(p, n) fl_realloc((p), (size_t)(n))
+#define MEM_FREE(x) fl_free(x)
+#define MEM_STRDUP(s) fl_strdup(s)
+#else
+#define MEM_CALLOC(n, sz) calloc((size_t)(n), (size_t)(sz))
+#define MEM_ALLOC(n) malloc((size_t)(n))
+#define MEM_REALLOC(p, n) realloc((p), (size_t)(n))
+#define MEM_FREE(x) free(x)
+#define MEM_STRDUP(s) strdup(s)
+#endif
--- a/meson.build
+++ b/meson.build
@@ -71,11 +71,23 @@
)
cpp = meson.get_compiler('cpp')
add_project_arguments(
- '-DINITIAL_HEAP_SIZE=64*1024',
- '-DALLOC_LIMIT_TRIGGER=128*1024',
+ '-DINITIAL_HEAP_SIZE=128*1024',
+ '-DALLOC_LIMIT_TRIGGER=64*1024',
'-D__thread=', # does not make sense anyway
language: 'c',
)
+ if host_machine.cpu() == 'm68k'
+ add_project_arguments(
+ # don't expect allocated pointers to be aligned by 8 bytes
+ '-DMEM_NEED_ALIGNED',
+ language: 'c',
+ )
+ else
+ add_project_arguments(
+ '-DCOMPUTED_GOTO',
+ language: 'c',
+ )
+ endif
extras = [
cpp.find_library('RetroConsole', required: true),
]
@@ -162,6 +174,16 @@
),
)
+common = static_library(
+ 'common',
+ [
+ 'mem.c',
+ ],
+ include_directories: include_directories(
+ platform,
+ ),
+)
+
mp = static_library(
'mp',
[
@@ -236,6 +258,7 @@
),
link_with: [
brieflz,
+ common,
mp,
utf,
],
@@ -306,6 +329,7 @@
platform,
),
link_with: [
+ common,
mp,
],
)
@@ -324,6 +348,7 @@
platform,
),
link_with: [
+ common,
mp,
],
)
--- a/plan9/platform.h
+++ b/plan9/platform.h
@@ -4,6 +4,7 @@
#include <libc.h>
#include <ctype.h>
#include <mp.h>
+#include "mem.h"
#ifdef NDEBUG
#undef assert
@@ -25,12 +26,6 @@
#define __builtin_prefetch(x)
int __builtin_clz(unsigned int x);
-
-#define MEM_CALLOC(n, sz) calloc((n), (sz))
-#define MEM_ALLOC(n) malloc(n)
-#define MEM_REALLOC(p, n) realloc((p), (n))
-#define MEM_FREE(x) free(x)
-#define MEM_STRDUP(s) strdup(s)
/* FIXME(sigrid): s*_overflow_* can be more optimal */
--- a/posix/platform.h
+++ b/posix/platform.h
@@ -26,6 +26,7 @@
#include <unistd.h>
#include <wctype.h>
#include <wchar.h>
+#include "mem.h"
#if defined(__linux__)
#define __os_name__ "linux"
@@ -40,12 +41,6 @@
#else
#define __os_name__ "unknown"
#endif
-
-#define MEM_CALLOC(n, sz) calloc((size_t)(n), (size_t)(sz))
-#define MEM_ALLOC(n) malloc((size_t)(n))
-#define MEM_REALLOC(p, n) realloc((p), (size_t)(n))
-#define MEM_FREE(x) free(x)
-#define MEM_STRDUP(s) strdup(s)
#ifndef __SIZEOF_POINTER__
#error pointer size unknown