3 #include <time.h> /* for time() in srand() */
7 int main(int argc
, char *argv
[])
13 /* Check argument count */
15 fprintf(stderr
, "usage: %s nrows ncols\n", argv
[0]);
19 /* Initialize memory pool with 1024 bytes */
20 mpret
= mpool_init(&mpool
, 10, 5);
21 if (mpret
== MPOOL_ENOMEM
) {
22 fprintf(stderr
, "mpool: not enough memory\n");
25 else if (mpret
== MPOOL_EBADVAL
) {
26 fprintf(stderr
, "mpool: bad value passed to mpool_init()\n");
30 /* Initialize random number generator */
33 /* Allocate memory for rows */
34 if ((array
= mpool_alloc(mpool
, atoi(argv
[1]) * sizeof *array
)) == NULL
) {
35 fprintf(stderr
, "mpool: no available block in pool\n");
40 /* Allocate memory for columns */
41 for (i
= 0; i
< atoi(argv
[1]); i
++) {
42 if ((array
[i
] = mpool_alloc(mpool
, atoi(argv
[2]) * sizeof **array
)) == NULL
) {
43 fprintf(stderr
, "mpool: no available block in pool\n");
49 /* Fill in matrix with random values and print it */
50 for (i
= 0; i
< atoi(argv
[1]); i
++) {
51 for (j
= 0; j
< atoi(argv
[2]); j
++) {
52 array
[i
][j
] = rand() % 100;
53 printf("%2d\t", array
[i
][j
]);
59 * Destroy memory pool and free all resources
61 * One, normally, would have to explicitly call mpool_free(),
62 * in order to liberate the allocated blocks one by one.
63 * But since we terminate anyway, just nuke the whole
64 * pool with mpool_destroy()