Last active
August 29, 2015 14:04
-
-
Save splinterofchaos/449426d26b1f912c8ac6 to your computer and use it in GitHub Desktop.
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 "alloc.h" | |
void *xcalloc(size_t count, size_t size) | |
{ | |
void *ret = calloc(count, size); | |
if (!ret && (!count || !size)) | |
ret = calloc(1, 1); | |
if (!ret) { | |
//try_to_free_memory(); | |
ret = calloc(count, size); // warning here | |
if (!ret && (!count || !size)) | |
ret = calloc(1, 1); | |
if (!ret) { | |
printf("Error: Out of memory.\n"); | |
} | |
} | |
return ret; | |
} | |
void *xmalloc(size_t size) | |
{ | |
void *ret = malloc(size); | |
if (!ret && !size) | |
ret = malloc(1); | |
if (!ret) { | |
//try_to_free_memory(); | |
ret = malloc(size); // no warning | |
if (!ret && !size) | |
ret = malloc( 1); | |
if (!ret) { | |
printf("Error: Out of memory.\n"); | |
} | |
} | |
return ret; | |
} |
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 <string.h> | |
#include <memory.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void *xcalloc(size_t, size_t); | |
void *xmalloc(size_t); |
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
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") | |
set(CMAKE_EXE_LINKER_FLAGS "${SMAKE_EXE_LINKER_FLAGS} --coverage") | |
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") | |
add_library(Alloc alloc.c) | |
add_executable(Main, main.c) | |
target_link_libraries(Main, Alloc) |
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 "alloc.h" | |
int main() { | |
void* p = xcalloc(0,0); // warning expected | |
void* q = xmalloc(0); // warning expected | |
printf("int @ %lu = %i\n", (size_t)p, *(int*)p); | |
printf("int @ %lu = %i\n", (size_t)q, *(int*)q); | |
free(p); | |
free(q); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testes with: