31 lines
		
	
	
		
			701 B
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			31 lines
		
	
	
		
			701 B
		
	
	
	
		
			C
		
	
	
	
	
	
|  | #define DEBUG
 | ||
|  | #include <stdlib.h>
 | ||
|  | #include <stdint.h>
 | ||
|  | #include <stdio.h>
 | ||
|  | #include <assert.h>
 | ||
|  | #include "../include/arena.h"
 | ||
|  | 
 | ||
|  | #include "../include/mem.h"
 | ||
|  | int main(void){ | ||
|  |     size_t nbytes = MEM_GB(1024); | ||
|  |     Arena a; | ||
|  |     TRY { | ||
|  |         a = ARENA(0,nbytes); /* try 1GB */ | ||
|  |         if (!a.beg) | ||
|  |             RAISE(OOM); | ||
|  |         free(a.beg); | ||
|  |     } | ||
|  |     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 (a.beg) free(a.beg); | ||
|  |     } | ||
|  |     END_TRY; | ||
|  | 
 | ||
|  |     return EXIT_FAILURE; /* shouldn't reach here for this example */ | ||
|  |   } |