Skip to content

Commit

Permalink
browser gc test
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed May 16, 2012
1 parent dec9d8f commit aec9ccf
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/library_gc.js
Expand Up @@ -18,7 +18,7 @@ if (GC_SUPPORT) {
GC.initted = true;
#if GENERATING_HTML
setInterval(function() {
GC.maybeGC();
GC.maybeCollect();
}, 1000);
#else
// No HTML intervals, so you need to call GC.maybeCollect() or GC.collect() manually
Expand Down
2 changes: 1 addition & 1 deletion src/settings.js
Expand Up @@ -216,7 +216,7 @@ var FAKE_X86_FP80 = 1; // Replaces x86_fp80 with double. This loses precision. I
// if you can, to get the original source code to build without x86_fp80
// (which is nonportable anyhow).

var GC_SUPPORT = 0; // Enables GC, see gc.h
var GC_SUPPORT = 1; // Enables GC, see gc.h (this does not add overhead, so it is on by default)

// Compiler debugging options
var DEBUG_TAGS_SHOWING = [];
Expand Down
96 changes: 96 additions & 0 deletions tests/browser_gc.cpp
@@ -0,0 +1,96 @@
#include <stdio.h>
#include <gc.h>
#include <assert.h>
#include <emscripten.h>

void *global;

int freed = 0;

void finalizer(void *ptr, void *arg) {
printf("finalizing %d (global == %d)\n", (int)arg, ptr == global);
freed++;
if (ptr == global) global = 0;
}

int stage = 0;
float start = 0;

void waiter() {
if (stage == 0) { // wait for a while, see no GCing
assert(global);
if (emscripten_get_now() - start > 2100) {
GC_MALLOC(1024*1024*2); // allocate enough to trigger a GC
start = emscripten_get_now();
stage = 1;
printf("stage 1\n");
}
} else if (stage == 1) {
assert(global);
if (freed > 0) {
GC_FREE(global);
stage = 2;
start = emscripten_get_now();
printf("stage 2\n");
}
if (emscripten_get_now() - start > 2100) {
printf("fail, too much time passed (a)\n");
return;
}
} else if (stage == 2) {
if (emscripten_get_now() - start > 2100) { // wait and see that no gc'ing happens yet
GC_MALLOC(1024*1024*2); // allocate enough to trigger a GC
stage = 3;
start = emscripten_get_now();
printf("stage 3\n");
}
} else if (stage == 3) {
assert(!global);
if (freed == 5) {
printf("Ok.\n");
int result = 1;
REPORT_RESULT();
return;
}
if (emscripten_get_now() - start > 2100) {
printf("fail, too much time passed (b)\n");
return;
}
}

emscripten_async_call(waiter, 100);
}

int main() {
start = emscripten_get_now();

GC_INIT();

void *local, *local2, *local3, *local4;

global = GC_MALLOC(12);
GC_REGISTER_FINALIZER_NO_ORDER(global, finalizer, 0, 0, 0);
local = GC_MALLOC(12);
GC_REGISTER_FINALIZER_NO_ORDER(local, finalizer, (void*)1, 0, 0);
local2 = GC_MALLOC_ATOMIC(12);
GC_REGISTER_FINALIZER_NO_ORDER(local2, finalizer, (void*)2, 0, 0);
local3 = GC_MALLOC(12);
GC_REGISTER_FINALIZER_NO_ORDER(local3, finalizer, (void*)3, 0, 0);
local4 = GC_MALLOC(12);
GC_REGISTER_FINALIZER_NO_ORDER(local4, finalizer, (void*)4, 0, 0);

void **globalData = (void**)global;
globalData[0] = local;
globalData[1] = local2;

void **localData = (void**)local;
localData[0] = local3;

void **local2Data = (void**)local2;
local2Data[0] = local4; // actually ignored, because local2 is atomic, so 4 is freeable

emscripten_async_call(waiter, 100);

return 0;
}

3 changes: 3 additions & 0 deletions tests/runner.py
Expand Up @@ -7431,6 +7431,9 @@ def btest(self, filename, expected=None, reference=None, args=[]): # TODO: use i
def test_emscripten_api(self):
self.btest('emscripten_api_browser.cpp', '1')

def test_gc(self):
self.btest('browser_gc.cpp', '1')

def test_sdlglshader(self):
self.btest('sdlglshader.c', reference='sdlglshader.png', args=['--closure', '1'])

Expand Down

0 comments on commit aec9ccf

Please sign in to comment.