Skip to content

Instantly share code, notes, and snippets.

@sepisoad
Created February 21, 2017 13:33
Show Gist options
  • Save sepisoad/a80efd257ea8a07187d7032bfdd8987d to your computer and use it in GitHub Desktop.
Save sepisoad/a80efd257ea8a07187d7032bfdd8987d to your computer and use it in GitHub Desktop.
nice c codes
//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