1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-25 23:10:47 -08:00

Mps example code: hello-world with gc

Copied from Perforce
 Change: 159000
 ServerID: perforce.ravenbrook.com
This commit is contained in:
Richard Kistruck 2006-05-31 17:02:30 +01:00
parent d05deebf6b
commit f1e87a0b01
2 changed files with 111 additions and 0 deletions

4
mps/example/hw-gc/build Executable file
View file

@ -0,0 +1,4 @@
CODE=../../code
LIBS=$CODE/xcppgc/ci
gcc hwgc01.c -o hwgc01 -I$CODE $LIBS/mps.a $LIBS/mpsplan.a
# ./a.out

107
mps/example/hw-gc/hwgc01.c Normal file
View file

@ -0,0 +1,107 @@
/* Demo File: 05alloc.c */
/* Simple C client of the MPS */
#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 "mpscmv.h" /* for mps_class_mv */
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
);
}
int main(void)
{
void *pBlock = NULL;
size_t cbBlock = 1024 * 1024;
mps_arena_t ArenaDemo = NULL;
mps_pool_t PoolDemo = NULL;
mps_res_t res;
{
/* 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
);
if (res != MPS_RES_OK) {
printf("mps_arena_create: failed with res %d.\n", res);
exit(2);
}
}
{
/* Create pool */
size_t cbPoolExtend = 1024;
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
);
if (res != MPS_RES_OK) {
printf("mps_pool_create: failed with res %d.\n", res);
exit(2);
}
reportPoolmv(PoolDemo);
}
{
/* Allocate memory */
size_t cbBuffer = 100;
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);
}
reportPoolmv(PoolDemo);
{
/* Show that it really is memory */
char *pbBuffer = (char *)p;
strcpy(pbBuffer, "hello--world\n");
pbBuffer[5] = ',';
pbBuffer[6] = ' ';
printf(pbBuffer);
}
}
printf(
"Success: The hello-world example code successfully allocated\n"
"some memory using mps_alloc(), in an MV pool, in a CL arena.\n"
);
return 0;
}