From 0bcd2a1cfd1b1e9063d9c8f237348e8164faac1d Mon Sep 17 00:00:00 2001 From: Richard Kistruck Date: Thu, 8 Jun 2006 14:45:57 +0100 Subject: [PATCH] Mps example hwgc02.c: move from mps_alloc to ap: mps_reserve..mps_commit Copied from Perforce Change: 159152 ServerID: perforce.ravenbrook.com --- mps/example/hw-gc/hwgc02.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/mps/example/hw-gc/hwgc02.c b/mps/example/hw-gc/hwgc02.c index 4560acd35ff..1221f64a066 100644 --- a/mps/example/hw-gc/hwgc02.c +++ b/mps/example/hw-gc/hwgc02.c @@ -1,4 +1,4 @@ -/* Simple C client of the MPS, with LO pool and VM arena */ +/* Simple C client of the MPS, with ap in LO pool and VM arena */ /* $Id$ */ #include /* for malloc */ @@ -60,6 +60,7 @@ int main(void) mps_arena_t ArenaDemo = NULL; mps_fmt_t FormatDemo = NULL; mps_pool_t PoolDemo = NULL; + mps_ap_t ApDemo = NULL; mps_res_t res; @@ -108,20 +109,38 @@ int main(void) report("Created pool", ArenaDemo, PoolDemo); } + + { + /* Create ap */ + + res = mps_ap_create(&ApDemo, PoolDemo); + if (res != MPS_RES_OK) { + printf("mps_ap_create: failed with res %d.\n", res); + exit(2); + } + + report("Created ap", ArenaDemo, PoolDemo); + } { /* Allocate memory */ - size_t cbBuffer = 100; + size_t cbBuffer = 256; void *p = NULL; - res = mps_alloc(&p, PoolDemo, cbBuffer); - if (res != MPS_RES_OK) { - printf("mps_alloc: failed with res %d.\n", res); - exit(2); - } + do { + res = mps_reserve(&p, ApDemo, cbBuffer); + if (res != MPS_RES_OK) { + printf("mps_reserve: failed with res %d.\n", res); + exit(2); + } + /* Initialise my object here -- ie. make it valid. */ + /* (In this example, memory is manually managed, so even + uninitialized memory is already a 'valid object'. So + we can call commit immediately.) */ + } while (! mps_commit(ApDemo, p, cbBuffer)); - report("Allocated 100 bytes", ArenaDemo, PoolDemo); + report("Allocated 256 bytes", ArenaDemo, PoolDemo); { /* Show that it really is memory */ @@ -137,7 +156,8 @@ int main(void) printf( "Success: The hello-world example code successfully allocated\n" - "some memory using mps_alloc(), in an LO pool, in a VM arena.\n" + "some memory using an allocation point with\n" + "mps_reserve()..mps_commit(), in an LO pool, in a VM arena.\n" ); return 0; }