-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
big refactoring on memory allocation
- Loading branch information
Showing
22 changed files
with
141 additions
and
59 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,50 @@ | ||
#include "alloc.h" | ||
|
||
extern inline void *moviedb_alloc(size_t bytes, struct error *restrict error); | ||
void *moviedb_alloc( | ||
size_t elem_size, | ||
size_t elements, | ||
struct error *restrict error) | ||
{ | ||
size_t size = elem_size * elements; | ||
bool fail = true; | ||
void *mem = NULL; | ||
|
||
extern inline void *moviedb_realloc( | ||
if (elem_size == 0 || size / elem_size == elements) { | ||
mem = malloc(size); | ||
fail = mem == NULL && size != 0; | ||
} | ||
|
||
if (fail) { | ||
error_set_code(error, error_alloc); | ||
error->data.alloc.elem_size = elem_size; | ||
error->data.alloc.elements = elements; | ||
} | ||
|
||
return mem; | ||
} | ||
|
||
void *moviedb_realloc( | ||
void *mem, | ||
size_t bytes, | ||
struct error *restrict error); | ||
size_t elem_size, | ||
size_t elements, | ||
struct error *restrict error) | ||
{ | ||
size_t size = elem_size * elements; | ||
bool fail = true; | ||
void *new_mem = NULL; | ||
|
||
if (elem_size == 0 || size / elem_size == elements) { | ||
new_mem = realloc(mem, size); | ||
fail = new_mem == NULL && size != 0; | ||
} | ||
|
||
if (fail) { | ||
error_set_code(error, error_alloc); | ||
error->data.alloc.elem_size = elem_size; | ||
error->data.alloc.elements = elements; | ||
} | ||
|
||
return new_mem; | ||
} | ||
|
||
extern inline void moviedb_free(void *mem); |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.