Last active
December 11, 2018 05:30
-
-
Save tripulse/502d67c7b19878daa41c13f7283fcdc1 to your computer and use it in GitHub Desktop.
Little buffer I/O libray
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
/* Buffer structure */ | |
typedef struct | |
{ | |
void* memory; | |
uint64_t size; | |
uint64_t nmemb; | |
} | |
buffer_t; | |
int buffer_allocate(buffer_t** buffer, uint64_t size, uint64_t nmemb) | |
{ | |
/* Double allocation protection */ | |
if((*buffer) != NULL) return EPERM; | |
(*buffer) = (buffer_t*)malloc(sizeof(buffer_t)); | |
(*buffer)->size = size; | |
(*buffer)->nmemb = nmemb; | |
(*buffer)->memory = malloc(size * nmemb); | |
/* OOM detection */ | |
if((*buffer)->memory == NULL) | |
return ENOMEM; | |
return 0; | |
} | |
int buffer_deallocate(buffer_t** buffer) | |
{ | |
/* 'undefined' behaviour protection */ | |
if((*buffer) == NULL) return EPERM; | |
if((*buffer)->memory == NULL) return EPERM; | |
free((*buffer)->memory); | |
free((*buffer)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment