Add shell script to copy files to source tree
[eleutheria.git] / malloc / test1.c
blobe03638243cbf361c14630da258ed17854af924a5
1 /*
2 * Compile with:
3 * gcc test1.c mpool.c -o test1 -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
9 #include "mpool.h"
11 int main(void)
13 mpool_t *mpool;
14 mpret_t mpret;
15 char *buffer;
17 /* Initialize memory pool with 1024 bytes */
18 mpret = mpool_init(&mpool, 10, 5);
19 if (mpret == MPOOL_ENOMEM) {
20 fprintf(stderr, "mpool: not enough memory\n");
21 exit(EXIT_FAILURE);
23 else if (mpret == MPOOL_EBADVAL) {
24 fprintf(stderr, "mpool: bad value passed to mpool_init()\n");
25 exit(EXIT_FAILURE);
28 /* Allocate 32 bytes for buffer */
29 if ((buffer = mpool_alloc(mpool, 5)) == NULL) {
30 fprintf(stderr, "mpool: no available block in pool\n");
31 mpool_destroy(mpool);
32 exit(EXIT_FAILURE);
35 /* Read input from standard input stream */
36 fgets(buffer, 1<<5, stdin);
38 /* Print buffer's contents */
39 printf("Buffer: %s", buffer);
42 * Free buffer --
43 * could be omitted, since we call mpool_destroy() afterwards
45 mpool_free(mpool, buffer);
47 /* Destroy memory pool and free all resources */
48 mpool_destroy(mpool);
50 return EXIT_SUCCESS;