Created
February 21, 2017 13:33
-
-
Save sepisoad/a80efd257ea8a07187d7032bfdd8987d to your computer and use it in GitHub Desktop.
nice c codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//took from: https://github.com/reagent/buffer | |
#define jump_to_error_if(A) if (A) { goto error; } | |
#define jump_to_error_unless(A) if (!(A)) { goto error; } | |
Buffer * | |
buffer_alloc(int initial_size) | |
{ | |
Buffer *buf = malloc(sizeof(Buffer)); | |
char *tmp = calloc(1, initial_size * sizeof(char)); | |
jump_to_error_if(buf == NULL || tmp == NULL); | |
buf->contents = tmp; | |
buf->bytes_used = 0; | |
buf->total_size = initial_size; | |
return buf; | |
error: | |
if (buf) { buffer_free(buf); } | |
if (tmp) { free(tmp); } | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment