diff options
| author | Richard Kistruck | 2006-06-08 14:45:57 +0100 |
|---|---|---|
| committer | Richard Kistruck | 2006-06-08 14:45:57 +0100 |
| commit | 0bcd2a1cfd1b1e9063d9c8f237348e8164faac1d (patch) | |
| tree | 6c3cedea75000fc448dff5b0c64cc05dbad894bb | |
| parent | 61435c32097243f7af71820fc00a53cbffc954d1 (diff) | |
| download | emacs-0bcd2a1cfd1b1e9063d9c8f237348e8164faac1d.tar.gz emacs-0bcd2a1cfd1b1e9063d9c8f237348e8164faac1d.zip | |
Mps example hwgc02.c: move from mps_alloc to ap: mps_reserve..mps_commit
Copied from Perforce
Change: 159152
ServerID: perforce.ravenbrook.com
| -rw-r--r-- | mps/example/hw-gc/hwgc02.c | 38 |
1 files 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 @@ | |||
| 1 | /* Simple C client of the MPS, with LO pool and VM arena */ | 1 | /* Simple C client of the MPS, with ap in LO pool and VM arena */ |
| 2 | /* $Id$ */ | 2 | /* $Id$ */ |
| 3 | 3 | ||
| 4 | #include <stdlib.h> /* for malloc */ | 4 | #include <stdlib.h> /* for malloc */ |
| @@ -60,6 +60,7 @@ int main(void) | |||
| 60 | mps_arena_t ArenaDemo = NULL; | 60 | mps_arena_t ArenaDemo = NULL; |
| 61 | mps_fmt_t FormatDemo = NULL; | 61 | mps_fmt_t FormatDemo = NULL; |
| 62 | mps_pool_t PoolDemo = NULL; | 62 | mps_pool_t PoolDemo = NULL; |
| 63 | mps_ap_t ApDemo = NULL; | ||
| 63 | 64 | ||
| 64 | mps_res_t res; | 65 | mps_res_t res; |
| 65 | 66 | ||
| @@ -108,20 +109,38 @@ int main(void) | |||
| 108 | 109 | ||
| 109 | report("Created pool", ArenaDemo, PoolDemo); | 110 | report("Created pool", ArenaDemo, PoolDemo); |
| 110 | } | 111 | } |
| 112 | |||
| 113 | { | ||
| 114 | /* Create ap */ | ||
| 115 | |||
| 116 | res = mps_ap_create(&ApDemo, PoolDemo); | ||
| 117 | if (res != MPS_RES_OK) { | ||
| 118 | printf("mps_ap_create: failed with res %d.\n", res); | ||
| 119 | exit(2); | ||
| 120 | } | ||
| 121 | |||
| 122 | report("Created ap", ArenaDemo, PoolDemo); | ||
| 123 | } | ||
| 111 | 124 | ||
| 112 | { | 125 | { |
| 113 | /* Allocate memory */ | 126 | /* Allocate memory */ |
| 114 | 127 | ||
| 115 | size_t cbBuffer = 100; | 128 | size_t cbBuffer = 256; |
| 116 | void *p = NULL; | 129 | void *p = NULL; |
| 117 | 130 | ||
| 118 | res = mps_alloc(&p, PoolDemo, cbBuffer); | 131 | do { |
| 119 | if (res != MPS_RES_OK) { | 132 | res = mps_reserve(&p, ApDemo, cbBuffer); |
| 120 | printf("mps_alloc: failed with res %d.\n", res); | 133 | if (res != MPS_RES_OK) { |
| 121 | exit(2); | 134 | printf("mps_reserve: failed with res %d.\n", res); |
| 122 | } | 135 | exit(2); |
| 136 | } | ||
| 137 | /* Initialise my object here -- ie. make it valid. */ | ||
| 138 | /* (In this example, memory is manually managed, so even | ||
| 139 | uninitialized memory is already a 'valid object'. So | ||
| 140 | we can call commit immediately.) */ | ||
| 141 | } while (! mps_commit(ApDemo, p, cbBuffer)); | ||
| 123 | 142 | ||
| 124 | report("Allocated 100 bytes", ArenaDemo, PoolDemo); | 143 | report("Allocated 256 bytes", ArenaDemo, PoolDemo); |
| 125 | 144 | ||
| 126 | { | 145 | { |
| 127 | /* Show that it really is memory */ | 146 | /* Show that it really is memory */ |
| @@ -137,7 +156,8 @@ int main(void) | |||
| 137 | 156 | ||
| 138 | printf( | 157 | printf( |
| 139 | "Success: The hello-world example code successfully allocated\n" | 158 | "Success: The hello-world example code successfully allocated\n" |
| 140 | "some memory using mps_alloc(), in an LO pool, in a VM arena.\n" | 159 | "some memory using an allocation point with\n" |
| 160 | "mps_reserve()..mps_commit(), in an LO pool, in a VM arena.\n" | ||
| 141 | ); | 161 | ); |
| 142 | return 0; | 162 | return 0; |
| 143 | } | 163 | } |