Add support OSX's malloc_size

This commit is contained in:
Soonho Kong 2013-09-20 17:45:50 -07:00
parent 8dc31dae4d
commit 9226c46358
6 changed files with 60 additions and 3 deletions

View file

@ -94,9 +94,14 @@ if(NOT "${TCMALLOC_FOUND}" AND "${TRACK_MEMORY_USAGE}" MATCHES "ON")
if("${MUS_FOUND}" MATCHES "TRUE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOC_USABLE_SIZE")
else()
find_package(MSize)
if("${MSIZE_FOUND}" MATCHES "TRUE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MSIZE")
find_package(MallocSize)
if("${MALLOCSIZE_FOUND}" MATCHES "TRUE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOCSIZE")
else()
find_package(MSize)
if("${MSIZE_FOUND}" MATCHES "TRUE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MSIZE")
endif()
endif()
endif()
endif()

View file

@ -6,6 +6,7 @@ Author: Leonardo de Moura
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

View file

@ -0,0 +1,26 @@
/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc/malloc.h>
#define MAX_SZ 1024
int main() {
for (unsigned i = 1; i < MAX_SZ; i++) {
void * p = malloc(i);
size_t r = malloc_size(p);
if (r < i || ((i > 128) && (r > 2 * i))) {
fprintf(stderr, "Unexpected malloc_size behavior: i = %d, r = %d\n", i, int(r));
return 1;
}
free(p);
}
return 0;
}

View file

@ -6,6 +6,7 @@ Author: Leonardo de Moura
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

View file

@ -0,0 +1,15 @@
find_path(MALLOC_DIR NAMES malloc/malloc.h )
try_run(MALLOCSIZE_CHECK MALLOCSIZE_CHECK_BUILD
${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
${LEAN_SOURCE_DIR}/cmake/Modules/CheckMallocSize.cc
CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${MALLOC_DIR}
RUN_OUTPUT_VARIABLE MALLOCSIZE_TRY_OUT)
if("${MALLOCSIZE_CHECK_BUILD}" MATCHES "TRUE" AND "${MALLOCSIZE_CHECK}" MATCHES "0")
message(STATUS "Found malloc_size " "${MALLOCSIZE_UNTESTED}")
set(MALLOCSIZE_FOUND TRUE)
else()
message(STATUS "Usable malloc_size was not detected")
set(MALLOCSIZE_FOUND FALSE)
endif()

View file

@ -60,7 +60,16 @@ inline void free_core(void * ptr) { free(ptr); }
// inline void * realloc_core(void * ptr, size_t sz) { return tc_realloc(ptr, sz); }
// inline void free_core(void * ptr) { tc_free(ptr); }
// }
#elif defined(HAS_MALLOCSIZE)
#include <malloc/malloc.h>
namespace lean {
inline size_t malloc_size(void * ptr) { return ::malloc_size(ptr); }
inline void * malloc_core(size_t sz) { return ::malloc(sz); }
inline void * realloc_core(void * ptr, size_t sz) { return realloc(ptr, sz); }
inline void free_core(void * ptr) { free(ptr); }
}
#elif defined(HAS_MSIZE)
#include <malloc.h>
namespace lean {
inline size_t malloc_size(void * ptr) { return _msize(ptr); }
inline void * malloc_core(size_t sz) { return ::malloc(sz); }