Created
August 12, 2016 08:29
-
-
Save findstr/0f30ec34b9d88358369e50e9dd9a0d85 to your computer and use it in GitHub Desktop.
memleak check
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
local f = io.open("a.txt", "r") | |
local malloc = {} | |
for line in f:lines() do | |
local typ, ptr, src = string.match(line, "([^:]+):([^:]+):(.*)") | |
if typ == "malloc" then | |
malloc[ptr] = src | |
end | |
if typ == "free" then | |
malloc[ptr] = nil | |
end | |
end | |
for k, v in pairs(malloc) do | |
if v then | |
print("memleak at", k, v) | |
end | |
end |
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 "silly_malloc.h" | |
#if defined(__linux__) | |
#include <malloc.h> | |
#elif defined(__macosx__) | |
#include <malloc/malloc.h> | |
#endif | |
static size_t allocsize = 0; | |
static size_t | |
xalloc_usable_size(void *ptr) | |
{ | |
#if defined(__linux__) | |
return malloc_usable_size(ptr); | |
#elif defined(__macosx__) | |
return malloc_size(ptr); | |
#else | |
return 0; | |
#endif | |
} | |
void * | |
silly_malloc_dbg(size_t sz, const char *file, int linenr) | |
{ | |
void *ptr = malloc(sz); | |
int real = xalloc_usable_size(ptr); | |
__sync_fetch_and_add(&allocsize, real); | |
printf("malloc:%p:%s:%d\n", ptr, file, linenr); | |
return ptr; | |
} | |
void * | |
silly_realloc_dbg(void *ptr, size_t sz, const char *file, int linenr) | |
{ | |
size_t realo = xalloc_usable_size(ptr); | |
printf("free:%p:%s:%d\n", ptr, file, linenr); | |
ptr = realloc(ptr, sz); | |
printf("malloc:%p:%s:%d\n", ptr, file, linenr); | |
size_t realn = xalloc_usable_size(ptr); | |
if (realo > realn) //shrink | |
__sync_fetch_and_sub(&allocsize, realo - realn); | |
else | |
__sync_fetch_and_add(&allocsize, realn - realo); | |
return ptr; | |
} | |
void | |
silly_free_dbg(void *ptr, const char *file, int linenr) | |
{ | |
printf("free:%p:%s:%d\n", ptr, file, linenr); | |
size_t real = xalloc_usable_size(ptr); | |
__sync_fetch_and_sub(&allocsize, real); | |
free(ptr); | |
} | |
size_t | |
silly_memstatus() | |
{ | |
return allocsize; | |
} |
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
#ifndef _SILLY_MALLOC_H | |
#define _SILLY_MALLOC_H | |
#include <stdlib.h> | |
#define silly_malloc(sz)\ | |
silly_malloc_dbg(sz, __FILE__, __LINE__) | |
#define silly_free(ptr)\ | |
silly_free_dbg(ptr, __FILE__, __LINE__) | |
#define silly_realloc(ptr, sz)\ | |
silly_realloc_dbg(ptr, sz, __FILE__, __LINE__) | |
void *silly_malloc_dbg(size_t sz, const char *file, int linenr); | |
void silly_free_dbg(void *ptr, const char *file, int linenr); | |
void *silly_realloc_dbg(void *ptr, size_t sz, const char *file, int linenr); | |
size_t silly_memstatus(); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment