Skip to content
Merged
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
22 changes: 22 additions & 0 deletions test/core/test_emmalloc_memory_statistics.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <emscripten/emmalloc.h>

size_t round_to_4k(size_t val) {
return (val + 4095) & ~4095;
}

int main() {
void *ptr = malloc(32*1024*1024);
void *ptr2 = malloc(4*1024*1024);
void *ptr3 = malloc(64*1024*1024);
void *ptr4 = malloc(16*1024);
void *ptr5 = malloc(2*1024*1024);
printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5));
free(ptr2);
free(ptr4);
printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions());
printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size());
printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory());
emmalloc_dump_free_dynamic_memory_fragmentation_map();
printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
}
24 changes: 0 additions & 24 deletions test/core/test_emmalloc_memory_statistics.cpp

This file was deleted.

55 changes: 55 additions & 0 deletions test/core/test_mallinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

// Tests that we can use the dlmalloc mallinfo() function to obtain information
// about malloc()ed blocks and compute how much memory is used/freed.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <malloc.h>
#include <emscripten/em_asm.h>

size_t getTotalMemory() {
return (size_t)EM_ASM_PTR(return HEAP8.length);
}

size_t getFreeMemory() {
struct mallinfo i = mallinfo();
uintptr_t totalMemory = getTotalMemory();
uintptr_t dynamicTop = (uintptr_t)sbrk(0);
return totalMemory - dynamicTop + i.fordblks;
}

int main() {
size_t total_mem = getTotalMemory();
size_t free_mem = getFreeMemory();

printf("Before allocation:\n");
printf("Total memory: %zu bytes\n", getTotalMemory());
printf("Free memory: %zu bytes\n", getFreeMemory());
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
assert(getTotalMemory() == total_mem);
assert(getFreeMemory() == free_mem);

void *ptr = malloc(1024*1024);
printf("\nAfter 1MB allocation: %p\n", ptr);
printf("Total memory: %zu bytes\n", getTotalMemory());
printf("Free memory: %zu bytes\n", getFreeMemory());
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
assert(getTotalMemory() == total_mem);
assert(getFreeMemory() < free_mem);

free(ptr);
printf("\nAfter freeing:\n");
printf("Total memory: %zu bytes\n", getTotalMemory());
printf("Free memory: %zu bytes\n", getFreeMemory());
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
assert(getTotalMemory() == total_mem);
assert(getFreeMemory() == free_mem);

printf("OK.\n");
}
1 change: 1 addition & 0 deletions test/core/test_mallinfo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK.
21 changes: 11 additions & 10 deletions test/core/test_memorygrowth.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@

int main(int argc, char **argv)
{
char *buf1 = (char*)malloc(100);
char *data1 = (char*)"hello";
char *buf1 = malloc(100);
char *data1 = "hello";
memcpy(buf1, data1, strlen(data1)+1);

float *buf2 = (float*)malloc(100);
float *buf2 = malloc(100);
float pie = 4.955;
memcpy(buf2, &pie, sizeof(float));

printf("*pre: %s,%.3f*\n", buf1, buf2[0]);

int totalMemory = EM_ASM_INT({ return HEAP8.length });
char *buf3 = (char*)malloc(totalMemory+1);
buf3[argc] = (long)buf2;
if (argc % 7 == 6) printf("%ld\n", (long)memcpy(buf3, buf1, argc));
char *buf4 = (char*)malloc(100);
float *buf5 = (float*)malloc(100);
size_t totalMemory = (size_t)EM_ASM_PTR({ return HEAP8.length });
//printf("totalMemory: %zu, argc: %d\n", totalMemory, argc);
char *buf3 = malloc(totalMemory+1);
assert(buf3 && "fail to perform large allocation");
if (argc % 7 == 6) printf("%p\n", memcpy(buf3, buf1, argc));
char *buf4 = malloc(100);
float *buf5 = malloc(100);
//printf("totalMemory: %d bufs: %d,%d,%d,%d,%d\n", totalMemory, buf1, buf2, buf3, buf4, buf5);
assert((long)buf4 > (long)totalMemory && (long)buf5 > (long)totalMemory);
assert((intptr_t)buf4 > (intptr_t)totalMemory && (intptr_t)buf5 > (intptr_t)totalMemory);

printf("*%s,%.3f*\n", buf1, buf2[0]); // the old heap data should still be there

Expand Down
49 changes: 17 additions & 32 deletions test/embind/test_i64_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,13 @@
using namespace emscripten;
using namespace std;

void fail()
{
cout << "fail\n";
}

void pass()
{
cout << "pass\n";
}
#define assert_js(X) assert(run_js(X))

void test(string message)
{
cout << "test:\n" << message << "\n";
}

void ensure(bool value)
{
if (value)
pass();
else
fail();
}

void execute_js(string js_code)
{
Expand All @@ -46,22 +31,22 @@ void execute_js(string js_code)
}, js_code_pointer);
}

void ensure_js(string js_code)
int run_js(string js_code)
{
js_code.append(";");
const char* js_code_pointer = js_code.c_str();
ensure(EM_ASM_INT({
return EM_ASM_INT({
var js_code = UTF8ToString($0);
return eval(js_code);
}, js_code_pointer));
}, js_code_pointer);
}

void ensure_js_throws(string js_code, string error_type)
{
js_code.append(";");
const char* js_code_pointer = js_code.c_str();
const char* error_type_pointer = error_type.c_str();
ensure(EM_ASM_INT({
assert(EM_ASM_INT({
var js_code = UTF8ToString($0);
var error_type = UTF8ToString($1);
try {
Expand Down Expand Up @@ -91,14 +76,14 @@ int main()
test("vector<int64_t>");
val myval(std::vector<int64_t>{1, 2, 3, -4});
val::global().set("v64", myval);
ensure_js("v64.get(0) === 1n");
ensure_js("v64.get(1) === 2n");
ensure_js("v64.get(2) === 3n");
ensure_js("v64.get(3) === -4n");
assert_js("v64.get(0) === 1n");
assert_js("v64.get(1) === 2n");
assert_js("v64.get(2) === 3n");
assert_js("v64.get(3) === -4n");

execute_js("v64.push_back(1234n)");
ensure_js("v64.size() === 5");
ensure_js("v64.get(4) === 1234n");
assert_js("v64.size() === 5");
assert_js("v64.get(4) === 1234n");

test("vector<int64_t> Cannot convert number to int64_t");
ensure_js_throws("v64.push_back(1234)", "TypeError");
Expand All @@ -109,14 +94,14 @@ int main()
test("vector<uint64_t>");
val myval2(vector<uint64_t>{1, 2, 3, 4});
val::global().set("vU64", myval2);
ensure_js("vU64.get(0) === 1n");
ensure_js("vU64.get(1) === 2n");
ensure_js("vU64.get(2) === 3n");
ensure_js("vU64.get(3) === 4n");
assert_js("vU64.get(0) === 1n");
assert_js("vU64.get(1) === 2n");
assert_js("vU64.get(2) === 3n");
assert_js("vU64.get(3) === 4n");

execute_js("vU64.push_back(1234n)");
ensure_js("vU64.size() === 5");
ensure_js("vU64.get(4) === 1234n");
assert_js("vU64.size() === 5");
assert_js("vU64.get(4) === 1234n");

test("vector<uint64_t> Cannot convert number to uint64_t");
ensure_js_throws("vU64.push_back(1234)", "TypeError");
Expand Down
17 changes: 0 additions & 17 deletions test/embind/test_i64_binding.out
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
start
test:
vector<int64_t>
pass
pass
pass
pass
pass
pass
test:
vector<int64_t> Cannot convert number to int64_t
pass
test:
vector<int64_t> Cannot convert bigint that is too big
pass
test:
vector<uint64_t>
pass
pass
pass
pass
pass
pass
test:
vector<uint64_t> Cannot convert number to uint64_t
pass
test:
vector<uint64_t> Cannot convert bigint that is too big
pass
test:
vector<uint64_t> Cannot convert bigint that is negative
pass
end
70 changes: 0 additions & 70 deletions test/mallinfo.cpp

This file was deleted.

Loading