ref: f19b5f51f9a9249f5dbf524346a07e1a65eff5b1
parent: 48299e12fec43f510d42016b22dadd3823b7462f
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Mar 6 22:00:07 EST 2025
split non-plan9 functionality further out
--- a/meson.build
+++ b/meson.build
@@ -112,12 +112,8 @@
)
cpp = meson.get_compiler('cpp')
if cpu == 'm68k'
- add_project_arguments(
- # don't expect NewPtr-returned pointers to be aligned by 8 bytes
- # hence just use dlmalloc, which is also a much faster way to go about it
- '-DUSE_DLMALLOC',
- language: 'c',
- )
+ # don't expect NewPtr-returned pointers to be aligned by 8 bytes
+ # hence just use dlmalloc, which is also a much faster way to go about it
src_common += [
'src/mem.c',
]
--- a/src/cc.h
+++ /dev/null
@@ -1,74 +1,0 @@
-#pragma once
-
-#ifdef __GNUC__
-
-typedef uint8_t u8int;
-typedef uint16_t u16int;
-typedef uint32_t u32int;
-typedef uint64_t u64int;
-typedef uintptr_t uintptr;
-
-typedef int8_t s8int;
-typedef int16_t s16int;
-typedef int32_t s32int;
-typedef int64_t s64int;
-typedef intptr_t intptr;
-
-typedef ssize_t ssize;
-typedef size_t usize;
-typedef off_t soffset;
-
-#define sl_unlikely(x) __builtin_expect(!!(x), 0)
-#define sl_likely(x) __builtin_expect(!!(x), 1)
-#define sl_printfmt(x, y) __attribute__((format(printf, x, y)))
-#if defined(NDEBUG) && !defined(__macos__) && !defined(__dos__)
-#define sl_thread(x) __thread x
-#else
-#define sl_thread(x) x
-#endif
-#define sl_prefetch(x) __builtin_prefetch(x)
-#define sl_constfn __attribute__((const))
-#define sl_purefn __attribute__((pure))
-#define sl_hotfn __attribute__((hot))
-#define sl_aligned(x) __attribute__((aligned(x)))
-#define sl_popcount(x) __builtin_popcount(x)
-#define sl_clz(x) __builtin_clz(x)
-#define sadd_overflow __builtin_add_overflow
-#define sadd_overflow_64 __builtin_add_overflow
-#define smul_overflow_64 __builtin_mul_overflow
-
-#else
-
-#define sl_unlikely(x) (x)
-#define sl_likely(x) (x)
-#define sl_printfmt(x, y)
-#define sl_thread(x) x
-#define sl_prefetch(x)
-#define sl_constfn
-#define sl_purefn
-#define sl_hotfn
-#define sl_aligned(x)
-
-/* FIXME(sigrid): s*_overflow_* can be more optimal */
-#define sadd_overflow_64(a, b, c) ( \
- (b < 1) ? \
- ((INT64_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
- ((INT64_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
-)
-#define smul_overflow_64(a, b, c) ( \
- ((a)>0 ? ((b)>0 ? (a)>INT64_MAX/(b) : (b)<INT64_MIN/(a)) \
- : ((b)>0 ? (a)<INT64_MIN/(b) : ((a)!=0 && (b)<INT64_MAX/(a)))) \
- ? 1 \
- : ((*(c)=(a)*(b)), 0) \
-)
-#if defined(BITS64)
-#define sadd_overflow(a, b, c) sadd_overflow_64(a, b, c)
-#else
-#define sadd_overflow(a, b, c) ( \
- (b < 1) ? \
- ((INT32_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
- ((INT32_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
-)
-#endif
-
-#endif
--- /dev/null
+++ b/src/cc_gnuc.h
@@ -1,0 +1,34 @@
+typedef uint8_t u8int;
+typedef uint16_t u16int;
+typedef uint32_t u32int;
+typedef uint64_t u64int;
+typedef uintptr_t uintptr;
+
+typedef int8_t s8int;
+typedef int16_t s16int;
+typedef int32_t s32int;
+typedef int64_t s64int;
+typedef intptr_t intptr;
+
+typedef ssize_t ssize;
+typedef size_t usize;
+typedef off_t soffset;
+
+#define sl_unlikely(x) __builtin_expect(!!(x), 0)
+#define sl_likely(x) __builtin_expect(!!(x), 1)
+#define sl_printfmt(x, y) __attribute__((format(printf, x, y)))
+#if defined(NDEBUG) && !defined(__macos__) && !defined(__dos__)
+#define sl_thread(x) __thread x
+#else
+#define sl_thread(x) x
+#endif
+#define sl_prefetch(x) __builtin_prefetch(x)
+#define sl_constfn __attribute__((const))
+#define sl_purefn __attribute__((pure))
+#define sl_hotfn __attribute__((hot))
+#define sl_aligned(x) __attribute__((aligned(x)))
+#define sl_popcount(x) __builtin_popcount(x)
+#define sl_clz(x) __builtin_clz(x)
+#define sadd_overflow __builtin_add_overflow
+#define sadd_overflow_64 __builtin_add_overflow
+#define smul_overflow_64 __builtin_mul_overflow
--- a/src/dos/platform.h
+++ b/src/dos/platform.h
@@ -43,7 +43,7 @@
#define MEM_UNALIGNED_ACCESS
-#include "cc.h"
+#include "cc_gnuc.h"
#include "mem.h"
#include "mp.h"
#include "utf.h"
--- a/src/macos/platform.h
+++ b/src/macos/platform.h
@@ -50,7 +50,8 @@
#define HEAP_SIZE0 128*1024
#define STACK_SIZE0 8*1024
-#include "cc.h"
+#include "cc_gnuc.h"
#include "mem.h"
+#include "mem_dlmalloc.h"
#include "mp.h"
#include "utf.h"
--- a/src/mem.h
+++ b/src/mem.h
@@ -10,21 +10,7 @@
#define ALLOC_LIMIT_TRIGGER HEAP_SIZE0
#endif
-#if defined(USE_DLMALLOC)
-void *sl_malloc(usize);
-void sl_free(void *);
-void *sl_calloc(usize, usize);
-void *sl_realloc(void *, usize);
-char *sl_strdup(const char *s);
-#define MEM_CALLOC(n, sz) sl_calloc((usize)(n), (usize)(sz))
-#define MEM_ALLOC(n) sl_malloc((usize)(n))
-#define MEM_REALLOC(p, n) sl_realloc((p), (usize)(n))
-#define MEM_FREE(x) sl_free(x)
-#define MEM_STRDUP(s) sl_strdup(s)
-#define sl_segalloc(sz) MEM_ALLOC((usize)sz)
-#define sl_segfree(s, sz) MEM_FREE(s)
-#define sl_segused(s, sz, used) do{}while(0)
-#else
+#if !defined(MEM_CALLOC)
#define MEM_CALLOC(n, sz) calloc((usize)(n), (usize)(sz))
#define MEM_ALLOC(n) malloc((usize)(n))
#define MEM_REALLOC(p, n) realloc((p), (usize)(n))
--- /dev/null
+++ b/src/mem_dlmalloc.h
@@ -1,0 +1,13 @@
+void *sl_malloc(usize);
+void sl_free(void *);
+void *sl_calloc(usize, usize);
+void *sl_realloc(void *, usize);
+char *sl_strdup(const char *s);
+#define MEM_CALLOC(n, sz) sl_calloc((usize)(n), (usize)(sz))
+#define MEM_ALLOC(n) sl_malloc((usize)(n))
+#define MEM_REALLOC(p, n) sl_realloc((p), (usize)(n))
+#define MEM_FREE(x) sl_free(x)
+#define MEM_STRDUP(s) sl_strdup(s)
+#define sl_segalloc(sz) MEM_ALLOC((usize)sz)
+#define sl_segfree(s, sz) MEM_FREE(s)
+#define sl_segused(s, sz, used) do{}while(0)
--- a/src/plan9/platform.h
+++ b/src/plan9/platform.h
@@ -122,5 +122,36 @@
int ftruncate(int f, soffset sz);
-#include "cc.h"
+#define sl_unlikely(x) (x)
+#define sl_likely(x) (x)
+#define sl_printfmt(x, y)
+#define sl_thread(x) x
+#define sl_prefetch(x)
+#define sl_constfn
+#define sl_purefn
+#define sl_hotfn
+#define sl_aligned(x)
+
+/* FIXME(sigrid): s*_overflow_* can be more optimal */
+#define sadd_overflow_64(a, b, c) ( \
+ (b < 1) ? \
+ ((INT64_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
+ ((INT64_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
+)
+#define smul_overflow_64(a, b, c) ( \
+ ((a)>0 ? ((b)>0 ? (a)>INT64_MAX/(b) : (b)<INT64_MIN/(a)) \
+ : ((b)>0 ? (a)<INT64_MIN/(b) : ((a)!=0 && (b)<INT64_MAX/(a)))) \
+ ? 1 \
+ : ((*(c)=(a)*(b)), 0) \
+)
+#if defined(BITS64)
+#define sadd_overflow(a, b, c) sadd_overflow_64(a, b, c)
+#else
+#define sadd_overflow(a, b, c) ( \
+ (b < 1) ? \
+ ((INT32_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
+ ((INT32_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
+)
+#endif
+
#include "mem.h"
--- a/src/posix/platform.h
+++ b/src/posix/platform.h
@@ -95,7 +95,7 @@
#define MEM_UNALIGNED_ACCESS
#endif
-#include "cc.h"
+#include "cc_gnuc.h"
#include "mem.h"
#include "mp.h"
#include "utf.h"