Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion recette/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ICOMMON = vital bps bool list vector srfi4 struct print \
rgc_eval rgc hash module import1 import2 \
cfa2 cell hygiene wind dsssl sua peek callcc fringe \
unicode optim pregexp lalr system date process \
mmap input_mmap_port weakptr crypto crc ssr
mmap mmap_numeric input_mmap_port weakptr crypto crc ssr

OCOMMON = object object_sans \
object1 object1_sans \
Expand Down
2 changes: 2 additions & 0 deletions recette/main.scm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
input-port
input-mmap-port
mmap
mmap-numeric
read
callcc
fringe
Expand Down Expand Up @@ -305,6 +306,7 @@
(if-module 'input-port test-input-port)
(if-module 'input-mmap-port test-input-mmap-port)
(if-module 'mmap test-mmap)
(if-module 'mmap-numeric test-mmap-numeric)
(if-module 'read test-read)
(if *callcc?*
(begin
Expand Down
141 changes: 141 additions & 0 deletions recette/mmap_numeric.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
;*=====================================================================*/
;* serrano/prgm/project/bigloo/recette/mmap_numeric.scm */
;* ------------------------------------------------------------- */
;* Author : User */
;* Creation : Sat Jul 18 2026 */
;* ------------------------------------------------------------- */
;* Test numeric mmap access functions */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;* The module */
;*---------------------------------------------------------------------*/
(module mmap-numeric
(import (main "main.scm"))
(include "test.sch")
(export (test-mmap-numeric)))

;*---------------------------------------------------------------------*/
;* test-mmap-numeric ... */
;*---------------------------------------------------------------------*/
(define (test-mmap-numeric)
(test-module "test-mmap-numeric" "mmap_numeric.scm")

(let ((path "tmp_mmap_numeric.data"))
;; Create a test file with known binary data
(with-output-to-file path
(lambda ()
;; Write 200 bytes of zeros
(let loop ((i 0))
(when (<fx i 200)
(write-byte 0)
(loop (+fx i 1))))))

(let ((mm (open-mmap path :read #t :write #t)))

;; Test 8-bit access
(test "mmap-s8-set!.1"
(begin
(mmap-s8-set! mm #e10 #s8:-42)
(int8->fixnum (mmap-s8-ref mm #e10)))
-42)

(test "mmap-u8-set!.1"
(begin
(mmap-u8-set! mm #e11 #u8:100)
(uint8->fixnum (mmap-u8-ref mm #e11)))
100)

;; Test 16-bit access
(test "mmap-u16-set!.1"
(begin
(mmap-u16-set! mm #e20 #u16:1234)
(uint16->fixnum (mmap-u16-ref mm #e20)))
1234)

(test "mmap-s16-set!.1"
(begin
(mmap-s16-set! mm #e22 #s16:-1000)
(int16->fixnum (mmap-s16-ref mm #e22)))
-1000)

;; Test 32-bit access
(test "mmap-u32-set!.1"
(begin
(mmap-u32-set! mm #e30 #u32:12345678)
(uint32->fixnum (mmap-u32-ref mm #e30)))
12345678)

(test "mmap-s32-set!.1"
(begin
(mmap-s32-set! mm #e34 #s32:-100000)
(int32->fixnum (mmap-s32-ref mm #e34)))
-100000)

;; Test 64-bit access (use typed constants for comparison)
(test "mmap-u64-set!.1"
(begin
(mmap-u64-set! mm #e40 #u64:1234567891234)
(mmap-u64-ref mm #e40))
#u64:1234567891234)

(test "mmap-s64-set!.1"
(begin
(mmap-s64-set! mm #e48 #s64:-9876543210)
(mmap-s64-ref mm #e48))
#s64:-9876543210)

;; Test float access
(test "mmap-f32-set!.1"
(begin
(mmap-f32-set! mm #e60 3.14159)
(< (abs (- (mmap-f32-ref mm #e60) 3.14159)) 0.0001))
#t)

(test "mmap-f64-set!.1"
(begin
(mmap-f64-set! mm #e70 2.718281828459045)
(< (abs (- (mmap-f64-ref mm #e70) 2.718281828459045)) 0.000000001))
#t)

;; Test read position tracking
(test "mmap-s32-ref.position"
(begin
(mmap-read-position-set! mm #e0)
(mmap-s32-ref mm #e0)
(mmap-read-position mm))
#e4)

(test "mmap-f64-ref.position"
(begin
(mmap-f64-ref mm #e10)
(mmap-read-position mm))
#e18)

;; Test write position tracking
(test "mmap-u16-set!.position"
(begin
(mmap-write-position-set! mm #e0)
(mmap-u16-set! mm #e0 #u16:42)
(mmap-write-position mm))
#e2)

;; Test bounds checking
(test "mmap-s32-ref.bounds"
(with-handler
(lambda (e) #t)
(mmap-s32-ref mm #e197) ; Would need bytes 197-200, but only 0-199 exist
#f)
#t)

(test "mmap-f64-set!.bounds"
(with-handler
(lambda (e) #t)
(mmap-f64-set! mm #e195 1.0) ; Would need bytes 195-202
#f)
#t)

(close-mmap mm))

;; Clean up
(delete-file path)))
134 changes: 134 additions & 0 deletions runtime/Clib/cmmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,137 @@ bgl_mmap_nommap_set(obj_t mm, long i, unsigned char c) {
}
}
#endif

#if !defined(__GNUC__)
/*---------------------------------------------------------------------*/
/* Numeric mmap access functions for non-GCC compilers */
/*---------------------------------------------------------------------*/

/*---------------------------------------------------------------------*/
/* 16-bit reads */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF int16_t
bgl_mmap_s16_ref(obj_t mm, long i) {
int16_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(int16_t));
return result;
}

BGL_RUNTIME_DEF uint16_t
bgl_mmap_u16_ref(obj_t mm, long i) {
uint16_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(uint16_t));
return result;
}

/*---------------------------------------------------------------------*/
/* 32-bit reads */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF int32_t
bgl_mmap_s32_ref(obj_t mm, long i) {
int32_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(int32_t));
return result;
}

BGL_RUNTIME_DEF uint32_t
bgl_mmap_u32_ref(obj_t mm, long i) {
uint32_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(uint32_t));
return result;
}

/*---------------------------------------------------------------------*/
/* 64-bit reads */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF int64_t
bgl_mmap_s64_ref(obj_t mm, long i) {
int64_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(int64_t));
return result;
}

BGL_RUNTIME_DEF uint64_t
bgl_mmap_u64_ref(obj_t mm, long i) {
uint64_t result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(uint64_t));
return result;
}

/*---------------------------------------------------------------------*/
/* Float reads */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF float
bgl_mmap_f32_ref(obj_t mm, long i) {
float result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(float));
return result;
}

BGL_RUNTIME_DEF double
bgl_mmap_f64_ref(obj_t mm, long i) {
double result;
memcpy(&result, &BGL_MMAP(mm).map[i], sizeof(double));
return result;
}

/*---------------------------------------------------------------------*/
/* 16-bit writes */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF obj_t
bgl_mmap_s16_set(obj_t mm, long i, int16_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(int16_t));
return BUNSPEC;
}

BGL_RUNTIME_DEF obj_t
bgl_mmap_u16_set(obj_t mm, long i, uint16_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(uint16_t));
return BUNSPEC;
}

/*---------------------------------------------------------------------*/
/* 32-bit writes */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF obj_t
bgl_mmap_s32_set(obj_t mm, long i, int32_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(int32_t));
return BUNSPEC;
}

BGL_RUNTIME_DEF obj_t
bgl_mmap_u32_set(obj_t mm, long i, uint32_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(uint32_t));
return BUNSPEC;
}

/*---------------------------------------------------------------------*/
/* 64-bit writes */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF obj_t
bgl_mmap_s64_set(obj_t mm, long i, int64_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(int64_t));
return BUNSPEC;
}

BGL_RUNTIME_DEF obj_t
bgl_mmap_u64_set(obj_t mm, long i, uint64_t val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(uint64_t));
return BUNSPEC;
}

/*---------------------------------------------------------------------*/
/* Float writes */
/*---------------------------------------------------------------------*/
BGL_RUNTIME_DEF obj_t
bgl_mmap_f32_set(obj_t mm, long i, float val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(float));
return BUNSPEC;
}

BGL_RUNTIME_DEF obj_t
bgl_mmap_f64_set(obj_t mm, long i, double val) {
memcpy(&BGL_MMAP(mm).map[i], &val, sizeof(double));
return BUNSPEC;
}
#endif /* !__GNUC__ */
Loading
Loading