29 lines
647 B
C
29 lines
647 B
C
|
|
#define DEBUG
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include "../include/mem.h"
|
||
|
|
|
||
|
|
int main(void){
|
||
|
|
size_t nbytes = MEM_GB(1024);
|
||
|
|
void *buf;
|
||
|
|
TRY {
|
||
|
|
buf = ALLOC(0,nbytes); /* try 1GB */
|
||
|
|
if (!buf)
|
||
|
|
RAISE(OOM);
|
||
|
|
free(buf);
|
||
|
|
}
|
||
|
|
EXCEPT(OOM) {
|
||
|
|
/* handle memory failure gracefully */
|
||
|
|
fprintf(stderr, "Caught: %s\n", OOM.reason);
|
||
|
|
return EXIT_SUCCESS; /* requested behavior */
|
||
|
|
}
|
||
|
|
FINALLY {
|
||
|
|
/* cleanup if needed, runs always */
|
||
|
|
if (buf) free(buf);
|
||
|
|
}
|
||
|
|
END_TRY;
|
||
|
|
|
||
|
|
return EXIT_FAILURE; /* shouldn't reach here for this example */
|
||
|
|
}
|