@niivue/niivue 0.12.2 → 0.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/assets/worker.9df7e894.js +1 -0
- package/dist/niivue.es.js +113 -5
- package/dist/niivue.umd.js +22 -22
- package/package.json +11 -11
- package/src/heap-verifier.js +51 -0
- package/src/index.html +3 -3
- package/src/linear-memory.js +33 -0
- package/src/niivue.js +107 -5
- package/src/nvimage.js +59 -0
- package/src/process-image.wasm +0 -0
- package/src/wasm/core.c +959 -0
- package/src/wasm/core.h +187 -0
- package/src/wasm/core32.c +3 -0
- package/src/wasm/core32.h +17 -0
- package/src/wasm/core64.c +3 -0
- package/src/wasm/core64.h +18 -0
- package/src/wasm/coreFLT.c +6285 -0
- package/src/wasm/nifti2_wasm.c +27 -0
- package/src/wasm/nifti2_wasm.h +76 -0
- package/src/wasm/walloc.c +466 -0
- package/src/worker.js +72 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#include <math.h>
|
|
2
|
+
#include "nifti2_wasm.h"
|
|
3
|
+
|
|
4
|
+
void nifti_image_free( nifti_image *nim )
|
|
5
|
+
{
|
|
6
|
+
if( nim == NULL ) return ;
|
|
7
|
+
//if( nim->fname != NULL ) free(nim->fname) ;
|
|
8
|
+
//if( nim->iname != NULL ) free(nim->iname) ;
|
|
9
|
+
if( nim->data != NULL ) free(nim->data ) ;
|
|
10
|
+
//(void)nifti_free_extensions( nim ) ;
|
|
11
|
+
free(nim) ; return ;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/*----------------------------------------------------------------------*/
|
|
15
|
+
/*! compute the determinant of a 3x3 matrix
|
|
16
|
+
*//*--------------------------------------------------------------------*/
|
|
17
|
+
float nifti_mat33_determ( mat33 R ) /* determinant of 3x3 matrix */
|
|
18
|
+
{
|
|
19
|
+
double r11,r12,r13,r21,r22,r23,r31,r32,r33 ;
|
|
20
|
+
/* INPUT MATRIX: */
|
|
21
|
+
r11 = R.m[0][0]; r12 = R.m[0][1]; r13 = R.m[0][2]; /* [ r11 r12 r13 ] */
|
|
22
|
+
r21 = R.m[1][0]; r22 = R.m[1][1]; r23 = R.m[1][2]; /* [ r21 r22 r23 ] */
|
|
23
|
+
r31 = R.m[2][0]; r32 = R.m[2][1]; r33 = R.m[2][2]; /* [ r31 r32 r33 ] */
|
|
24
|
+
|
|
25
|
+
return (float)(r11*r22*r33-r11*r32*r23-r21*r12*r33
|
|
26
|
+
+r21*r32*r13+r31*r12*r23-r31*r22*r13) ;
|
|
27
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Data structures for using niimath core32 API.
|
|
3
|
+
*/
|
|
4
|
+
#ifndef _NIFTI2_WASM_HEADER_
|
|
5
|
+
#define _NIFTI2_WASM_HEADER_
|
|
6
|
+
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
#include <math.h>
|
|
11
|
+
#include <limits.h>
|
|
12
|
+
#include <ctype.h>
|
|
13
|
+
#include <inttypes.h>
|
|
14
|
+
|
|
15
|
+
/*=================*/
|
|
16
|
+
#ifdef __cplusplus
|
|
17
|
+
extern "C" {
|
|
18
|
+
#endif
|
|
19
|
+
/*=================*/
|
|
20
|
+
|
|
21
|
+
#define DT_UNSIGNED_CHAR 2 /* unsigned char (8 bits/voxel) */
|
|
22
|
+
#define DT_SIGNED_SHORT 4 /* signed short (16 bits/voxel) */
|
|
23
|
+
#define DT_FLOAT 16 /* float (32 bits/voxel) */
|
|
24
|
+
#define DT_FLOAT32 16
|
|
25
|
+
|
|
26
|
+
typedef struct { /** 4x4 matrix struct **/
|
|
27
|
+
float m[4][4] ;
|
|
28
|
+
} mat44 ;
|
|
29
|
+
|
|
30
|
+
typedef struct { /** 3x3 matrix struct **/
|
|
31
|
+
float m[3][3] ;
|
|
32
|
+
} mat33 ;
|
|
33
|
+
|
|
34
|
+
typedef struct { /** 4x4 matrix struct (double) **/
|
|
35
|
+
double m[4][4] ;
|
|
36
|
+
} nifti_dmat44 ;
|
|
37
|
+
|
|
38
|
+
typedef struct { /** 3x3 matrix struct (double) **/
|
|
39
|
+
double m[3][3] ;
|
|
40
|
+
} nifti_dmat33 ;
|
|
41
|
+
|
|
42
|
+
typedef struct { /*!< Image storage struct **/
|
|
43
|
+
|
|
44
|
+
int64_t ndim ; /*!< last dimension greater than 1 (1..7) */
|
|
45
|
+
int64_t nx ; /*!< dimensions of grid array */
|
|
46
|
+
int64_t ny ; /*!< dimensions of grid array */
|
|
47
|
+
int64_t nz ; /*!< dimensions of grid array */
|
|
48
|
+
int64_t nt ; /*!< dimensions of grid array */
|
|
49
|
+
int64_t nvox ; /*!< number of voxels = nx*ny*nz*...*nw */
|
|
50
|
+
//int nbyper ; /*!< bytes per voxel, matches datatype */
|
|
51
|
+
int datatype ; /*!< type of data in voxels: DT_* code */
|
|
52
|
+
|
|
53
|
+
double dx ; /*!< grid spacings */
|
|
54
|
+
double dy ; /*!< grid spacings */
|
|
55
|
+
double dz ; /*!< grid spacings */
|
|
56
|
+
double dt ; /*!< grid spacings */
|
|
57
|
+
|
|
58
|
+
double scl_slope ; /*!< scaling parameter - slope */
|
|
59
|
+
double scl_inter ; /*!< scaling parameter - intercept */
|
|
60
|
+
double cal_min ; /*!< calibration parameter, minimum */
|
|
61
|
+
double cal_max ; /*!< calibration parameter, maximum */
|
|
62
|
+
void *data ; /*!< pointer to data: nbyper*nvox bytes */
|
|
63
|
+
|
|
64
|
+
} nifti_image ;
|
|
65
|
+
|
|
66
|
+
void nifti_image_free ( nifti_image *nim);
|
|
67
|
+
float nifti_mat33_determ ( mat33 R ) ;
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/*=================*/
|
|
71
|
+
#ifdef __cplusplus
|
|
72
|
+
}
|
|
73
|
+
#endif
|
|
74
|
+
/*=================*/
|
|
75
|
+
|
|
76
|
+
#endif /* _NIFTI2_IO_HEADER_ */
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
// walloc.c: a small malloc implementation for use in WebAssembly targets
|
|
2
|
+
// Copyright (c) 2020 Igalia, S.L.
|
|
3
|
+
//
|
|
4
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
5
|
+
// copy of this software and associated documentation files (the
|
|
6
|
+
// "Software"), to deal in the Software without restriction, including
|
|
7
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
// permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
// the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included
|
|
13
|
+
// in all copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
19
|
+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
20
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
21
|
+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
typedef __SIZE_TYPE__ size_t;
|
|
24
|
+
typedef __UINTPTR_TYPE__ uintptr_t;
|
|
25
|
+
typedef __UINT8_TYPE__ uint8_t;
|
|
26
|
+
|
|
27
|
+
#define NULL ((void *) 0)
|
|
28
|
+
|
|
29
|
+
#define STATIC_ASSERT_EQ(a, b) _Static_assert((a) == (b), "eq")
|
|
30
|
+
|
|
31
|
+
#ifndef NDEBUG
|
|
32
|
+
#define ASSERT(x) do { if (!(x)) __builtin_trap(); } while (0)
|
|
33
|
+
#else
|
|
34
|
+
#define ASSERT(x) do { } while (0)
|
|
35
|
+
#endif
|
|
36
|
+
#define ASSERT_EQ(a,b) ASSERT((a) == (b))
|
|
37
|
+
|
|
38
|
+
static inline size_t max(size_t a, size_t b) {
|
|
39
|
+
return a < b ? b : a;
|
|
40
|
+
}
|
|
41
|
+
static inline uintptr_t align(uintptr_t val, uintptr_t alignment) {
|
|
42
|
+
return (val + alignment - 1) & ~(alignment - 1);
|
|
43
|
+
}
|
|
44
|
+
#define ASSERT_ALIGNED(x, y) ASSERT((x) == align((x), y))
|
|
45
|
+
|
|
46
|
+
#define CHUNK_SIZE 256
|
|
47
|
+
#define CHUNK_SIZE_LOG_2 8
|
|
48
|
+
#define CHUNK_MASK (CHUNK_SIZE - 1)
|
|
49
|
+
STATIC_ASSERT_EQ(CHUNK_SIZE, 1 << CHUNK_SIZE_LOG_2);
|
|
50
|
+
|
|
51
|
+
#define PAGE_SIZE 65536
|
|
52
|
+
#define PAGE_SIZE_LOG_2 16
|
|
53
|
+
#define PAGE_MASK (PAGE_SIZE - 1)
|
|
54
|
+
STATIC_ASSERT_EQ(PAGE_SIZE, 1 << PAGE_SIZE_LOG_2);
|
|
55
|
+
|
|
56
|
+
#define CHUNKS_PER_PAGE 256
|
|
57
|
+
STATIC_ASSERT_EQ(PAGE_SIZE, CHUNK_SIZE * CHUNKS_PER_PAGE);
|
|
58
|
+
|
|
59
|
+
#define GRANULE_SIZE 8
|
|
60
|
+
#define GRANULE_SIZE_LOG_2 3
|
|
61
|
+
#define LARGE_OBJECT_THRESHOLD 256
|
|
62
|
+
#define LARGE_OBJECT_GRANULE_THRESHOLD 32
|
|
63
|
+
|
|
64
|
+
STATIC_ASSERT_EQ(GRANULE_SIZE, 1 << GRANULE_SIZE_LOG_2);
|
|
65
|
+
STATIC_ASSERT_EQ(LARGE_OBJECT_THRESHOLD,
|
|
66
|
+
LARGE_OBJECT_GRANULE_THRESHOLD * GRANULE_SIZE);
|
|
67
|
+
|
|
68
|
+
struct chunk {
|
|
69
|
+
char data[CHUNK_SIZE];
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// There are small object pages for allocations of these sizes.
|
|
73
|
+
#define FOR_EACH_SMALL_OBJECT_GRANULES(M) \
|
|
74
|
+
M(1) M(2) M(3) M(4) M(5) M(6) M(8) M(10) M(16) M(32)
|
|
75
|
+
|
|
76
|
+
enum chunk_kind {
|
|
77
|
+
#define DEFINE_SMALL_OBJECT_CHUNK_KIND(i) GRANULES_##i,
|
|
78
|
+
FOR_EACH_SMALL_OBJECT_GRANULES(DEFINE_SMALL_OBJECT_CHUNK_KIND)
|
|
79
|
+
#undef DEFINE_SMALL_OBJECT_CHUNK_KIND
|
|
80
|
+
|
|
81
|
+
SMALL_OBJECT_CHUNK_KINDS,
|
|
82
|
+
FREE_LARGE_OBJECT = 254,
|
|
83
|
+
LARGE_OBJECT = 255
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
static const uint8_t small_object_granule_sizes[] =
|
|
87
|
+
{
|
|
88
|
+
#define SMALL_OBJECT_GRANULE_SIZE(i) i,
|
|
89
|
+
FOR_EACH_SMALL_OBJECT_GRANULES(SMALL_OBJECT_GRANULE_SIZE)
|
|
90
|
+
#undef SMALL_OBJECT_GRANULE_SIZE
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
static enum chunk_kind granules_to_chunk_kind(unsigned granules) {
|
|
94
|
+
#define TEST_GRANULE_SIZE(i) if (granules <= i) return GRANULES_##i;
|
|
95
|
+
FOR_EACH_SMALL_OBJECT_GRANULES(TEST_GRANULE_SIZE);
|
|
96
|
+
#undef TEST_GRANULE_SIZE
|
|
97
|
+
return LARGE_OBJECT;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static unsigned chunk_kind_to_granules(enum chunk_kind kind) {
|
|
101
|
+
switch (kind) {
|
|
102
|
+
#define CHUNK_KIND_GRANULE_SIZE(i) case GRANULES_##i: return i;
|
|
103
|
+
FOR_EACH_SMALL_OBJECT_GRANULES(CHUNK_KIND_GRANULE_SIZE);
|
|
104
|
+
#undef CHUNK_KIND_GRANULE_SIZE
|
|
105
|
+
default:
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Given a pointer P returned by malloc(), we get a header pointer via
|
|
111
|
+
// P&~PAGE_MASK, and a chunk index via (P&PAGE_MASK)/CHUNKS_PER_PAGE. If
|
|
112
|
+
// chunk_kinds[chunk_idx] is [FREE_]LARGE_OBJECT, then the pointer is a large
|
|
113
|
+
// object, otherwise the kind indicates the size in granules of the objects in
|
|
114
|
+
// the chunk.
|
|
115
|
+
struct page_header {
|
|
116
|
+
uint8_t chunk_kinds[CHUNKS_PER_PAGE];
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
struct page {
|
|
120
|
+
union {
|
|
121
|
+
struct page_header header;
|
|
122
|
+
struct chunk chunks[CHUNKS_PER_PAGE];
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
#define PAGE_HEADER_SIZE (sizeof (struct page_header))
|
|
127
|
+
#define FIRST_ALLOCATABLE_CHUNK 1
|
|
128
|
+
STATIC_ASSERT_EQ(PAGE_HEADER_SIZE, FIRST_ALLOCATABLE_CHUNK * CHUNK_SIZE);
|
|
129
|
+
|
|
130
|
+
static struct page* get_page(void *ptr) {
|
|
131
|
+
return (struct page*) (char*) (((uintptr_t) ptr) & ~PAGE_MASK);
|
|
132
|
+
}
|
|
133
|
+
static unsigned get_chunk_index(void *ptr) {
|
|
134
|
+
return (((uintptr_t) ptr) & PAGE_MASK) / CHUNK_SIZE;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
struct freelist {
|
|
138
|
+
struct freelist *next;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
struct large_object {
|
|
142
|
+
struct large_object *next;
|
|
143
|
+
size_t size;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
#define LARGE_OBJECT_HEADER_SIZE (sizeof (struct large_object))
|
|
147
|
+
|
|
148
|
+
static inline void* get_large_object_payload(struct large_object *obj) {
|
|
149
|
+
return ((char*) obj) + LARGE_OBJECT_HEADER_SIZE;
|
|
150
|
+
}
|
|
151
|
+
static inline struct large_object* get_large_object(void *ptr) {
|
|
152
|
+
return (struct large_object*) (((char*) ptr) - LARGE_OBJECT_HEADER_SIZE);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static struct freelist *small_object_freelists[SMALL_OBJECT_CHUNK_KINDS];
|
|
156
|
+
static struct large_object *large_objects;
|
|
157
|
+
|
|
158
|
+
extern void __heap_base;
|
|
159
|
+
static size_t walloc_heap_size;
|
|
160
|
+
|
|
161
|
+
static struct page*
|
|
162
|
+
allocate_pages(size_t payload_size, size_t *n_allocated) {
|
|
163
|
+
size_t needed = payload_size + PAGE_HEADER_SIZE;
|
|
164
|
+
size_t heap_size = __builtin_wasm_memory_size(0) * PAGE_SIZE;
|
|
165
|
+
uintptr_t base = heap_size;
|
|
166
|
+
uintptr_t preallocated = 0, grow = 0;
|
|
167
|
+
|
|
168
|
+
if (!walloc_heap_size) {
|
|
169
|
+
// We are allocating the initial pages, if any. We skip the first 64 kB,
|
|
170
|
+
// then take any additional space up to the memory size.
|
|
171
|
+
uintptr_t heap_base = align((uintptr_t)&__heap_base, PAGE_SIZE);
|
|
172
|
+
preallocated = heap_size - heap_base; // Preallocated pages.
|
|
173
|
+
walloc_heap_size = preallocated;
|
|
174
|
+
base -= preallocated;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (preallocated < needed) {
|
|
178
|
+
// Always grow the walloc heap at least by 50%.
|
|
179
|
+
grow = align(max(walloc_heap_size / 2, needed - preallocated),
|
|
180
|
+
PAGE_SIZE);
|
|
181
|
+
ASSERT(grow);
|
|
182
|
+
if (__builtin_wasm_memory_grow(0, grow >> PAGE_SIZE_LOG_2) == -1) {
|
|
183
|
+
return NULL;
|
|
184
|
+
}
|
|
185
|
+
walloc_heap_size += grow;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
struct page *ret = (struct page *)base;
|
|
189
|
+
size_t size = grow + preallocated;
|
|
190
|
+
ASSERT(size);
|
|
191
|
+
ASSERT_ALIGNED(size, PAGE_SIZE);
|
|
192
|
+
*n_allocated = size / PAGE_SIZE;
|
|
193
|
+
return ret;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
static char*
|
|
197
|
+
allocate_chunk(struct page *page, unsigned idx, enum chunk_kind kind) {
|
|
198
|
+
page->header.chunk_kinds[idx] = kind;
|
|
199
|
+
return page->chunks[idx].data;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// It's possible for splitting to produce a large object of size 248 (256 minus
|
|
203
|
+
// the header size) -- i.e. spanning a single chunk. In that case, push the
|
|
204
|
+
// chunk back on the GRANULES_32 small object freelist.
|
|
205
|
+
static void maybe_repurpose_single_chunk_large_objects_head(void) {
|
|
206
|
+
if (large_objects->size < CHUNK_SIZE) {
|
|
207
|
+
unsigned idx = get_chunk_index(large_objects);
|
|
208
|
+
char *ptr = allocate_chunk(get_page(large_objects), idx, GRANULES_32);
|
|
209
|
+
large_objects = large_objects->next;
|
|
210
|
+
struct freelist* head = (struct freelist *)ptr;
|
|
211
|
+
head->next = small_object_freelists[GRANULES_32];
|
|
212
|
+
small_object_freelists[GRANULES_32] = head;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// If there have been any large-object frees since the last large object
|
|
217
|
+
// allocation, go through the freelist and merge any adjacent objects.
|
|
218
|
+
static int pending_large_object_compact = 0;
|
|
219
|
+
static struct large_object**
|
|
220
|
+
maybe_merge_free_large_object(struct large_object** prev) {
|
|
221
|
+
struct large_object *obj = *prev;
|
|
222
|
+
while (1) {
|
|
223
|
+
char *end = get_large_object_payload(obj) + obj->size;
|
|
224
|
+
ASSERT_ALIGNED((uintptr_t)end, CHUNK_SIZE);
|
|
225
|
+
unsigned chunk = get_chunk_index(end);
|
|
226
|
+
if (chunk < FIRST_ALLOCATABLE_CHUNK) {
|
|
227
|
+
// Merging can't create a large object that newly spans the header chunk.
|
|
228
|
+
// This check also catches the end-of-heap case.
|
|
229
|
+
return prev;
|
|
230
|
+
}
|
|
231
|
+
struct page *page = get_page(end);
|
|
232
|
+
if (page->header.chunk_kinds[chunk] != FREE_LARGE_OBJECT) {
|
|
233
|
+
return prev;
|
|
234
|
+
}
|
|
235
|
+
struct large_object *next = (struct large_object*) end;
|
|
236
|
+
|
|
237
|
+
struct large_object **prev_prev = &large_objects, *walk = large_objects;
|
|
238
|
+
while (1) {
|
|
239
|
+
ASSERT(walk);
|
|
240
|
+
if (walk == next) {
|
|
241
|
+
obj->size += LARGE_OBJECT_HEADER_SIZE + walk->size;
|
|
242
|
+
*prev_prev = walk->next;
|
|
243
|
+
if (prev == &walk->next) {
|
|
244
|
+
prev = prev_prev;
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
prev_prev = &walk->next;
|
|
249
|
+
walk = walk->next;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
static void
|
|
254
|
+
maybe_compact_free_large_objects(void) {
|
|
255
|
+
if (pending_large_object_compact) {
|
|
256
|
+
pending_large_object_compact = 0;
|
|
257
|
+
struct large_object **prev = &large_objects;
|
|
258
|
+
while (*prev) {
|
|
259
|
+
prev = &(*maybe_merge_free_large_object(prev))->next;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Allocate a large object with enough space for SIZE payload bytes. Returns a
|
|
265
|
+
// large object with a header, aligned on a chunk boundary, whose payload size
|
|
266
|
+
// may be larger than SIZE, and whose total size (header included) is
|
|
267
|
+
// chunk-aligned. Either a suitable allocation is found in the large object
|
|
268
|
+
// freelist, or we ask the OS for some more pages and treat those pages as a
|
|
269
|
+
// large object. If the allocation fits in that large object and there's more
|
|
270
|
+
// than an aligned chunk's worth of data free at the end, the large object is
|
|
271
|
+
// split.
|
|
272
|
+
//
|
|
273
|
+
// The return value's corresponding chunk in the page as starting a large
|
|
274
|
+
// object.
|
|
275
|
+
static struct large_object*
|
|
276
|
+
allocate_large_object(size_t size) {
|
|
277
|
+
maybe_compact_free_large_objects();
|
|
278
|
+
struct large_object *best = NULL, **best_prev = &large_objects;
|
|
279
|
+
size_t best_size = -1;
|
|
280
|
+
for (struct large_object **prev = &large_objects, *walk = large_objects;
|
|
281
|
+
walk;
|
|
282
|
+
prev = &walk->next, walk = walk->next) {
|
|
283
|
+
if (walk->size >= size && walk->size < best_size) {
|
|
284
|
+
best_size = walk->size;
|
|
285
|
+
best = walk;
|
|
286
|
+
best_prev = prev;
|
|
287
|
+
if (best_size + LARGE_OBJECT_HEADER_SIZE
|
|
288
|
+
== align(size + LARGE_OBJECT_HEADER_SIZE, CHUNK_SIZE))
|
|
289
|
+
// Not going to do any better than this; just return it.
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!best) {
|
|
295
|
+
// The large object freelist doesn't have an object big enough for this
|
|
296
|
+
// allocation. Allocate one or more pages from the OS, and treat that new
|
|
297
|
+
// sequence of pages as a fresh large object. It will be split if
|
|
298
|
+
// necessary.
|
|
299
|
+
size_t size_with_header = size + sizeof(struct large_object);
|
|
300
|
+
size_t n_allocated = 0;
|
|
301
|
+
struct page *page = allocate_pages(size_with_header, &n_allocated);
|
|
302
|
+
if (!page) {
|
|
303
|
+
return NULL;
|
|
304
|
+
}
|
|
305
|
+
char *ptr = allocate_chunk(page, FIRST_ALLOCATABLE_CHUNK, LARGE_OBJECT);
|
|
306
|
+
best = (struct large_object *)ptr;
|
|
307
|
+
size_t page_header = ptr - ((char*) page);
|
|
308
|
+
best->next = large_objects;
|
|
309
|
+
best->size = best_size =
|
|
310
|
+
n_allocated * PAGE_SIZE - page_header - LARGE_OBJECT_HEADER_SIZE;
|
|
311
|
+
ASSERT(best_size >= size_with_header);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
allocate_chunk(get_page(best), get_chunk_index(best), LARGE_OBJECT);
|
|
315
|
+
|
|
316
|
+
struct large_object *next = best->next;
|
|
317
|
+
*best_prev = next;
|
|
318
|
+
|
|
319
|
+
size_t tail_size = (best_size - size) & ~CHUNK_MASK;
|
|
320
|
+
if (tail_size) {
|
|
321
|
+
// The best-fitting object has 1 or more aligned chunks free after the
|
|
322
|
+
// requested allocation; split the tail off into a fresh aligned object.
|
|
323
|
+
struct page *start_page = get_page(best);
|
|
324
|
+
char *start = get_large_object_payload(best);
|
|
325
|
+
char *end = start + best_size;
|
|
326
|
+
|
|
327
|
+
if (start_page == get_page(end - tail_size - 1)) {
|
|
328
|
+
// The allocation does not span a page boundary; yay.
|
|
329
|
+
ASSERT_ALIGNED((uintptr_t)end, CHUNK_SIZE);
|
|
330
|
+
} else if (size < PAGE_SIZE - LARGE_OBJECT_HEADER_SIZE - CHUNK_SIZE) {
|
|
331
|
+
// If the allocation itself smaller than a page, split off the head, then
|
|
332
|
+
// fall through to maybe split the tail.
|
|
333
|
+
ASSERT_ALIGNED((uintptr_t)end, PAGE_SIZE);
|
|
334
|
+
size_t first_page_size = PAGE_SIZE - (((uintptr_t)start) & PAGE_MASK);
|
|
335
|
+
struct large_object *head = best;
|
|
336
|
+
allocate_chunk(start_page, get_chunk_index(start), FREE_LARGE_OBJECT);
|
|
337
|
+
head->size = first_page_size;
|
|
338
|
+
head->next = large_objects;
|
|
339
|
+
large_objects = head;
|
|
340
|
+
|
|
341
|
+
maybe_repurpose_single_chunk_large_objects_head();
|
|
342
|
+
|
|
343
|
+
struct page *next_page = start_page + 1;
|
|
344
|
+
char *ptr = allocate_chunk(next_page, FIRST_ALLOCATABLE_CHUNK, LARGE_OBJECT);
|
|
345
|
+
best = (struct large_object *) ptr;
|
|
346
|
+
best->size = best_size = best_size - first_page_size - CHUNK_SIZE - LARGE_OBJECT_HEADER_SIZE;
|
|
347
|
+
ASSERT(best_size >= size);
|
|
348
|
+
start = get_large_object_payload(best);
|
|
349
|
+
tail_size = (best_size - size) & ~CHUNK_MASK;
|
|
350
|
+
} else {
|
|
351
|
+
// A large object that spans more than one page will consume all of its
|
|
352
|
+
// tail pages. Therefore if the split traverses a page boundary, round up
|
|
353
|
+
// to page size.
|
|
354
|
+
ASSERT_ALIGNED((uintptr_t)end, PAGE_SIZE);
|
|
355
|
+
size_t first_page_size = PAGE_SIZE - (((uintptr_t)start) & PAGE_MASK);
|
|
356
|
+
size_t tail_pages_size = align(size - first_page_size, PAGE_SIZE);
|
|
357
|
+
size = first_page_size + tail_pages_size;
|
|
358
|
+
tail_size = best_size - size;
|
|
359
|
+
}
|
|
360
|
+
best->size -= tail_size;
|
|
361
|
+
|
|
362
|
+
unsigned tail_idx = get_chunk_index(end - tail_size);
|
|
363
|
+
while (tail_idx < FIRST_ALLOCATABLE_CHUNK && tail_size) {
|
|
364
|
+
// We would be splitting in a page header; don't do that.
|
|
365
|
+
tail_size -= CHUNK_SIZE;
|
|
366
|
+
tail_idx++;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (tail_size) {
|
|
370
|
+
struct page *page = get_page(end - tail_size);
|
|
371
|
+
char *tail_ptr = allocate_chunk(page, tail_idx, FREE_LARGE_OBJECT);
|
|
372
|
+
struct large_object *tail = (struct large_object *) tail_ptr;
|
|
373
|
+
tail->next = large_objects;
|
|
374
|
+
tail->size = tail_size - LARGE_OBJECT_HEADER_SIZE;
|
|
375
|
+
ASSERT_ALIGNED((uintptr_t)(get_large_object_payload(tail) + tail->size), CHUNK_SIZE);
|
|
376
|
+
large_objects = tail;
|
|
377
|
+
|
|
378
|
+
maybe_repurpose_single_chunk_large_objects_head();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
ASSERT_ALIGNED((uintptr_t)(get_large_object_payload(best) + best->size), CHUNK_SIZE);
|
|
383
|
+
return best;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
static struct freelist*
|
|
387
|
+
obtain_small_objects(enum chunk_kind kind) {
|
|
388
|
+
struct freelist** whole_chunk_freelist = &small_object_freelists[GRANULES_32];
|
|
389
|
+
void *chunk;
|
|
390
|
+
if (*whole_chunk_freelist) {
|
|
391
|
+
chunk = *whole_chunk_freelist;
|
|
392
|
+
*whole_chunk_freelist = (*whole_chunk_freelist)->next;
|
|
393
|
+
} else {
|
|
394
|
+
chunk = allocate_large_object(0);
|
|
395
|
+
if (!chunk) {
|
|
396
|
+
return NULL;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
char *ptr = allocate_chunk(get_page(chunk), get_chunk_index(chunk), kind);
|
|
400
|
+
char *end = ptr + CHUNK_SIZE;
|
|
401
|
+
struct freelist *next = NULL;
|
|
402
|
+
size_t size = chunk_kind_to_granules(kind) * GRANULE_SIZE;
|
|
403
|
+
for (size_t i = size; i <= CHUNK_SIZE; i += size) {
|
|
404
|
+
struct freelist *head = (struct freelist*) (end - i);
|
|
405
|
+
head->next = next;
|
|
406
|
+
next = head;
|
|
407
|
+
}
|
|
408
|
+
return next;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
static inline size_t size_to_granules(size_t size) {
|
|
412
|
+
return (size + GRANULE_SIZE - 1) >> GRANULE_SIZE_LOG_2;
|
|
413
|
+
}
|
|
414
|
+
static struct freelist** get_small_object_freelist(enum chunk_kind kind) {
|
|
415
|
+
ASSERT(kind < SMALL_OBJECT_CHUNK_KINDS);
|
|
416
|
+
return &small_object_freelists[kind];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
static void*
|
|
420
|
+
allocate_small(enum chunk_kind kind) {
|
|
421
|
+
struct freelist **loc = get_small_object_freelist(kind);
|
|
422
|
+
if (!*loc) {
|
|
423
|
+
struct freelist *freelist = obtain_small_objects(kind);
|
|
424
|
+
if (!freelist) {
|
|
425
|
+
return NULL;
|
|
426
|
+
}
|
|
427
|
+
*loc = freelist;
|
|
428
|
+
}
|
|
429
|
+
struct freelist *ret = *loc;
|
|
430
|
+
*loc = ret->next;
|
|
431
|
+
return (void *) ret;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
static void*
|
|
435
|
+
allocate_large(size_t size) {
|
|
436
|
+
struct large_object *obj = allocate_large_object(size);
|
|
437
|
+
return obj ? get_large_object_payload(obj) : NULL;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
void*
|
|
441
|
+
xmalloc(size_t size) {
|
|
442
|
+
size_t granules = size_to_granules(size);
|
|
443
|
+
enum chunk_kind kind = granules_to_chunk_kind(granules);
|
|
444
|
+
return (kind == LARGE_OBJECT) ? allocate_large(size) : allocate_small(kind);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
void
|
|
448
|
+
xfree(void *ptr) {
|
|
449
|
+
if (!ptr) return;
|
|
450
|
+
struct page *page = get_page(ptr);
|
|
451
|
+
unsigned chunk = get_chunk_index(ptr);
|
|
452
|
+
uint8_t kind = page->header.chunk_kinds[chunk];
|
|
453
|
+
if (kind == LARGE_OBJECT) {
|
|
454
|
+
struct large_object *obj = get_large_object(ptr);
|
|
455
|
+
obj->next = large_objects;
|
|
456
|
+
large_objects = obj;
|
|
457
|
+
allocate_chunk(page, chunk, FREE_LARGE_OBJECT);
|
|
458
|
+
pending_large_object_compact = 1;
|
|
459
|
+
} else {
|
|
460
|
+
size_t granules = kind;
|
|
461
|
+
struct freelist **loc = get_small_object_freelist(granules);
|
|
462
|
+
struct freelist *obj = ptr;
|
|
463
|
+
obj->next = *loc;
|
|
464
|
+
*loc = obj;
|
|
465
|
+
}
|
|
466
|
+
}
|
package/src/worker.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import init from "./process-image.wasm";
|
|
2
|
+
import { LinearMemory } from "./linear-memory.js";
|
|
3
|
+
|
|
4
|
+
let linearMemory = new LinearMemory({ initial: 256, maximum: 2048 });
|
|
5
|
+
let wasmReady = init({ env: linearMemory.env() });
|
|
6
|
+
|
|
7
|
+
addEventListener("message", (e) => {
|
|
8
|
+
wasmReady.then((niimathWasm) => {
|
|
9
|
+
const imageMetadata = e.data[0];
|
|
10
|
+
const imageBytes = e.data[1];
|
|
11
|
+
const cmd = e.data[2];
|
|
12
|
+
const isNewLayer = e.data[3];
|
|
13
|
+
|
|
14
|
+
let cptr = niimathWasm.walloc(cmd.length + 1);
|
|
15
|
+
linearMemory.record_malloc(cptr, cmd.length + 1);
|
|
16
|
+
let cmdstr = new Uint8Array(cmd.length + 1);
|
|
17
|
+
for (let i = 0; i < cmd.length; i++) cmdstr[i] = cmd.charCodeAt(i);
|
|
18
|
+
let cstr = new Uint8Array(niimathWasm.memory.buffer, cptr, cmd.length + 1);
|
|
19
|
+
cstr.set(cmdstr);
|
|
20
|
+
//allocate WASM image data
|
|
21
|
+
let nvox =
|
|
22
|
+
imageMetadata.nx * imageMetadata.ny * imageMetadata.nz * imageMetadata.nt;
|
|
23
|
+
let ptr = niimathWasm.walloc(nvox * imageMetadata.bpv);
|
|
24
|
+
linearMemory.record_malloc(ptr, nvox * imageMetadata.bpv);
|
|
25
|
+
let cimg = new Uint8Array(
|
|
26
|
+
niimathWasm.memory.buffer,
|
|
27
|
+
ptr,
|
|
28
|
+
nvox * imageMetadata.bpv
|
|
29
|
+
);
|
|
30
|
+
cimg.set(new Uint8Array(imageBytes));
|
|
31
|
+
// console.log(niimathWasm);
|
|
32
|
+
let ok = niimathWasm.niimath(
|
|
33
|
+
ptr,
|
|
34
|
+
imageMetadata.datatypeCode,
|
|
35
|
+
imageMetadata.nx,
|
|
36
|
+
imageMetadata.ny,
|
|
37
|
+
imageMetadata.nz,
|
|
38
|
+
imageMetadata.nt,
|
|
39
|
+
imageMetadata.dx,
|
|
40
|
+
imageMetadata.dy,
|
|
41
|
+
imageMetadata.dz,
|
|
42
|
+
imageMetadata.dt,
|
|
43
|
+
cptr
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (ok != 0) {
|
|
47
|
+
console.error(" -> '", cmd, " generated a fatal error: ", ok);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
cimg = new Uint8Array(
|
|
51
|
+
niimathWasm.memory.buffer,
|
|
52
|
+
ptr,
|
|
53
|
+
nvox * imageMetadata.bpv
|
|
54
|
+
);
|
|
55
|
+
// https://stackoverflow.com/questions/59705741/why-memory-could-not-be-cloned
|
|
56
|
+
let clone = new Uint8Array(cimg, 0, nvox * imageMetadata.bpv);
|
|
57
|
+
|
|
58
|
+
// post modified bytes and id of
|
|
59
|
+
postMessage({
|
|
60
|
+
id: imageMetadata.id,
|
|
61
|
+
imageBytes: clone,
|
|
62
|
+
cmd,
|
|
63
|
+
isNewLayer,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
//free WASM memory
|
|
67
|
+
linearMemory.record_free(cptr);
|
|
68
|
+
niimathWasm.wfree(cptr);
|
|
69
|
+
linearMemory.record_free(ptr);
|
|
70
|
+
niimathWasm.wfree(ptr);
|
|
71
|
+
});
|
|
72
|
+
});
|