mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-04-23 14:32:12 -07:00
Mps hw-gc example: use vm arena, better reporting.
Copied from Perforce Change: 159053 ServerID: perforce.ravenbrook.com
This commit is contained in:
parent
e9417a4285
commit
b64f2699cf
2 changed files with 49 additions and 37 deletions
|
|
@ -1,4 +1,5 @@
|
|||
CODE=../../code
|
||||
LIBS=$CODE/xcppgc/ci
|
||||
gcc hwgc01.c -o hwgc01 -I$CODE $LIBS/mps.a $LIBS/mpsplan.a
|
||||
# ./a.out
|
||||
FLAGS="-std=iso9899:1990 -pedantic -Wimplicit -Wmissing-prototypes"
|
||||
gcc hwgc01.c -o hwgc01 -I$CODE $LIBS/mps.a $LIBS/mpsplan.a $FLAGS
|
||||
./hwgc01
|
||||
|
|
|
|||
|
|
@ -1,27 +1,56 @@
|
|||
/* Demo File: 05alloc.c */
|
||||
/* Simple C client of the MPS */
|
||||
/* Simple C client of the MPS, with MV pool and VM arena */
|
||||
|
||||
#include <stdlib.h> /* for malloc */
|
||||
#include <stdio.h> /* for printf */
|
||||
#include <string.h> /* for strcpy */
|
||||
|
||||
#include "mps.h"
|
||||
#include "mpsacl.h" /* for mps_arena_class_cl */
|
||||
#include "mpsavm.h" /* for mps_arena_class_vm */
|
||||
#include "mpscmv.h" /* for mps_class_mv */
|
||||
|
||||
static void reportArena(mps_arena_t Arena);
|
||||
static void reportPoolmv(mps_pool_t Pool);
|
||||
|
||||
static void report(char* comment, mps_arena_t Arena, mps_pool_t Pool)
|
||||
{
|
||||
printf("%s:\n", comment);
|
||||
if (Arena)
|
||||
reportArena(Arena);
|
||||
if (Pool)
|
||||
reportPoolmv(Pool);
|
||||
printf("--\n\n");
|
||||
}
|
||||
|
||||
static void reportArena(mps_arena_t Arena)
|
||||
{
|
||||
size_t cbArenaReserved = 0;
|
||||
size_t cbArenaCommitted = 0;
|
||||
size_t cbArenaSpareCommitted = 0;
|
||||
cbArenaReserved = mps_arena_reserved(Arena);
|
||||
cbArenaCommitted = mps_arena_committed(Arena);
|
||||
cbArenaSpareCommitted = mps_arena_spare_committed(Arena);
|
||||
printf(" Arena has:\n %lu bytes reserved;\n %lu bytes committed;\n %lu bytes spare committed.\n",
|
||||
(unsigned long)cbArenaReserved,
|
||||
(unsigned long)cbArenaCommitted,
|
||||
(unsigned long)cbArenaSpareCommitted
|
||||
);
|
||||
}
|
||||
|
||||
static void reportPoolmv(mps_pool_t Pool)
|
||||
{
|
||||
size_t cbPoolFree = mps_mv_free_size(Pool);
|
||||
printf(
|
||||
"Pool has %lu bytes free.\n",
|
||||
(unsigned long)cbPoolFree
|
||||
);
|
||||
size_t cbPoolSize = 0;
|
||||
size_t cbPoolFree = 0;
|
||||
cbPoolSize = mps_mv_size(Pool);
|
||||
cbPoolFree = mps_mv_free_size(Pool);
|
||||
printf(" Pool has:\n %lu bytes in use;\n %lu bytes free.\n",
|
||||
(unsigned long)cbPoolSize,
|
||||
(unsigned long)cbPoolFree
|
||||
);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
void *pBlock = NULL;
|
||||
size_t cbBlock = 1024 * 1024;
|
||||
size_t cbArena = 1 * 1024 * 1024 * 1024; /* 1GB, ie. one quarter of 32-bit address space */
|
||||
mps_arena_t ArenaDemo = NULL;
|
||||
mps_pool_t PoolDemo = NULL;
|
||||
|
||||
|
|
@ -30,22 +59,13 @@ int main(void)
|
|||
{
|
||||
/* Create arena */
|
||||
|
||||
pBlock = malloc(cbBlock);
|
||||
if(pBlock == NULL) {
|
||||
printf("malloc failed!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
res = mps_arena_create(
|
||||
&ArenaDemo,
|
||||
mps_arena_class_cl(),
|
||||
cbBlock,
|
||||
pBlock
|
||||
);
|
||||
res = mps_arena_create(&ArenaDemo, mps_arena_class_vm(), cbArena);
|
||||
if (res != MPS_RES_OK) {
|
||||
printf("mps_arena_create: failed with res %d.\n", res);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
report("Created arena", ArenaDemo, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -55,22 +75,13 @@ int main(void)
|
|||
size_t cbObjectAvg = 32;
|
||||
size_t cbPoolMax = 64 * 1024;
|
||||
|
||||
size_t cbPoolFree = 0;
|
||||
|
||||
res = mps_pool_create(
|
||||
&PoolDemo,
|
||||
ArenaDemo,
|
||||
mps_class_mv(),
|
||||
cbPoolExtend,
|
||||
cbObjectAvg,
|
||||
cbPoolMax
|
||||
);
|
||||
res = mps_pool_create(&PoolDemo, ArenaDemo, mps_class_mv(), cbPoolExtend, cbObjectAvg, cbPoolMax);
|
||||
if (res != MPS_RES_OK) {
|
||||
printf("mps_pool_create: failed with res %d.\n", res);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
reportPoolmv(PoolDemo);
|
||||
report("Created pool", ArenaDemo, PoolDemo);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -85,7 +96,7 @@ int main(void)
|
|||
exit(2);
|
||||
}
|
||||
|
||||
reportPoolmv(PoolDemo);
|
||||
report("Allocated 100 bytes", ArenaDemo, PoolDemo);
|
||||
|
||||
{
|
||||
/* Show that it really is memory */
|
||||
|
|
@ -101,7 +112,7 @@ int main(void)
|
|||
|
||||
printf(
|
||||
"Success: The hello-world example code successfully allocated\n"
|
||||
"some memory using mps_alloc(), in an MV pool, in a CL arena.\n"
|
||||
"some memory using mps_alloc(), in an MV pool, in a VM arena.\n"
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue