Add support for _msize

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-09-20 14:12:11 -07:00
parent 875fda80de
commit 3971265bcb
5 changed files with 59 additions and 1 deletions

View file

@ -91,7 +91,14 @@ endif()
# Check malloc_usable_size
if(NOT "${TCMALLOC_FOUND}" AND "${TRACK_MEMORY_USAGE}" MATCHES "ON")
find_package(MallocUsableSize)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I ${MALLOC_DIR} -D HAS_MALLOC_USABLE_SIZE")
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")
endif()
endif()
endif()
include_directories(${LEAN_SOURCE_DIR})

View file

@ -0,0 +1,25 @@
/*
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 <string.h>
#include <malloc.h>
#define MAX_SZ 1024
int main() {
for (unsigned i = 1; i < MAX_SZ; i++) {
void * p = malloc(i);
size_t r = _msize(p);
if (r < i || r > 2*(i + 8)) {
fprintf(stderr, "Unexpected _msize behavior");
return 0;
}
free(p);
}
return 1;
}

View file

@ -0,0 +1,17 @@
find_path(MALLOC_DIR NAMES malloc.h )
try_run(MSIZE_CHECK MSIZE_CHECK_BUILD
${LEAN_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
${LEAN_SOURCE_DIR}/cmake/Modules/CheckMSize.cc
CMAKE_FLAGS -DINCLUDE_DIRECTORIES=${MALLOC_DIR}
RUN_OUTPUT_VARIABLE MSIZE_TRY_OUT)
if("${MSIZE_CHECK_BUILD}" MATCHES "TRUE" AND "${MSIZE_CHECK}" MATCHES "0")
message(STATUS "Found malloc_usable_size")
set(MSIZE_FOUND TRUE)
else()
message(STATUS "Usable malloc_usable_size was not detected")
set(MSIZE_FOUND FALSE)
endif()

View file

@ -8,8 +8,10 @@ try_run(MUS_CHECK MUS_CHECK_BUILD
if("${MUS_CHECK_BUILD}" MATCHES "TRUE" AND "${MUS_CHECK}" MATCHES "0")
message(STATUS "Found malloc_usable_size")
set(MUS_FOUND TRUE)
else()
message(STATUS "Usable malloc_usable_size was not detected")
set(MUS_FOUND FALSE)
endif()

View file

@ -60,6 +60,13 @@ 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_MSIZE)
namespace lean {
inline size_t malloc_size(void * ptr) { return _msize(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); }
}
#else
namespace lean {
void * save_alloc_size(void * ptr, size_t sz) {