1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm-allocator.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct MemInfo {
void* real_pointer; /* pointer as returned from malloc */
size_t size;
} MemInfo;
#ifndef NDEBUG
static WasmBool is_power_of_two(size_t x) {
return x && ((x & (x - 1)) == 0);
}
static WasmBool is_aligned(void* p, size_t align) {
return ((intptr_t)p & (align - 1)) == 0;
}
#endif /* NDEBUG */
static void* align_up(void* p, size_t align) {
return (void*)(((intptr_t)p + align - 1) & ~(align - 1));
}
static void* libc_alloc(WasmAllocator* allocator,
size_t size,
size_t align,
const char* file,
int line) {
assert(is_power_of_two(align));
if (align < sizeof(void*))
align = sizeof(void*);
void* p = malloc(size + sizeof(MemInfo) + align - 1);
if (!p)
return NULL;
void* aligned = align_up((void*)((size_t)p + sizeof(MemInfo)), align);
MemInfo* mem_info = (MemInfo*)aligned - 1;
assert(is_aligned(mem_info, sizeof(void*)));
mem_info->real_pointer = p;
mem_info->size = size;
return aligned;
}
static void libc_free(WasmAllocator* allocator,
void* p,
const char* file,
int line) {
if (!p)
return;
MemInfo* mem_info = (MemInfo*)p - 1;
free(mem_info->real_pointer);
}
/* nothing to destroy */
static void libc_destroy(WasmAllocator* allocator) {}
static void* libc_realloc(WasmAllocator* allocator,
void* p,
size_t size,
size_t align,
const char* file,
int line) {
if (!p)
return libc_alloc(allocator, size, align, NULL, 0);
MemInfo* mem_info = (MemInfo*)p - 1;
void* new_p = libc_alloc(allocator, size, align, NULL, 0);
if (!new_p)
return NULL;
size_t copy_size = size > mem_info->size ? mem_info->size : size;
memcpy(new_p, p, copy_size);
free(mem_info->real_pointer);
return new_p;
}
/* mark/reset_to_mark are not supported by the libc allocator */
static WasmAllocatorMark libc_mark(WasmAllocator* allocator) {
return NULL;
}
static void libc_reset_to_mark(WasmAllocator* allocator,
WasmAllocatorMark mark) {}
static void libc_print_stats(WasmAllocator* allocator) {
/* TODO(binji): nothing for now, implement later */
}
WasmAllocator g_wasm_libc_allocator = {
libc_alloc, libc_realloc, libc_free, libc_destroy,
libc_mark, libc_reset_to_mark, libc_print_stats};
|