Skip to content

Commit

Permalink
Merge PR #2415 'Use jemalloc instead of libc allocator'
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Apr 13, 2015
2 parents 0248c75 + 2d104f1 commit a9ee85b
Show file tree
Hide file tree
Showing 79 changed files with 1,519 additions and 1,420 deletions.
23 changes: 23 additions & 0 deletions CMakeLists.txt
Expand Up @@ -202,6 +202,29 @@ option(LIBVTERM_USE_STATIC "Use static libvterm" ON)
find_package(LibVterm REQUIRED)
include_directories(SYSTEM ${LIBVTERM_INCLUDE_DIRS})

option(SANITIZE "Enable Clang sanitizers for nvim binary" OFF)
if(SANITIZE AND NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
message(WARNING "SANITIZE is only supported for Clang ... disabling")
set(SANITIZE OFF)
endif()

if(SANITIZE)
option(USE_JEMALLOC "Use jemalloc" OFF)
else()
option(USE_JEMALLOC "Use jemalloc" ON)
endif()

if(USE_JEMALLOC)
option(JEMALLOC_USE_STATIC "Use static jemalloc" ON)
find_package(JeMalloc)
if(JEMALLOC_FOUND)
message(STATUS "Using jemalloc instead of libc allocator")
include_directories(SYSTEM ${JEMALLOC_INCLUDE_DIRS})
else()
set(USE_JEMALLOC OFF)
endif()
endif()

find_package(LibIntl)
if(LibIntl_FOUND)
include_directories(SYSTEM ${LibIntl_INCLUDE_DIRS})
Expand Down
34 changes: 34 additions & 0 deletions clint.py
Expand Up @@ -1274,6 +1274,39 @@ def CheckPosixThreading(filename, clean_lines, linenum, error):
' see os_localtime_r for an example.')


memory_functions = (
('malloc(', 'xmalloc('),
('calloc(', 'xcalloc('),
('realloc(', 'xrealloc('),
('strdup(', 'xstrdup('),
('free(', 'xfree('),
)
memory_ignore_pattern = re.compile(r'src/nvim/memory.c$')


def CheckMemoryFunctions(filename, clean_lines, linenum, error):
"""Checks for calls to invalid functions.
Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.
"""
if memory_ignore_pattern.search(filename):
return
line = clean_lines.elided[linenum]
for function, suggested_function in memory_functions:
ix = line.find(function)
# Comparisons made explicit for clarity -- pylint:
# disable=g-explicit-bool-comparison
if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and
line[ix - 1] not in ('_', '.', '>'))):
error(filename, linenum, 'runtime/memory_fn', 2,
'Use ' + suggested_function +
'...) instead of ' + function + '...).')


# Matches invalid increment: *count++, which moves pointer instead of
# incrementing a value.
_RE_PATTERN_INVALID_INCREMENT = re.compile(
Expand Down Expand Up @@ -2925,6 +2958,7 @@ def ProcessLine(filename, file_extension, clean_lines, line,
CheckForNonStandardConstructs(filename, clean_lines, line,
nesting_state, error)
CheckPosixThreading(filename, clean_lines, line, error)
CheckMemoryFunctions(filename, clean_lines, line, error)
for check_fn in extra_check_functions:
check_fn(filename, clean_lines, line, error)

Expand Down
48 changes: 48 additions & 0 deletions cmake/FindJeMalloc.cmake
@@ -0,0 +1,48 @@
# - Try to find jemalloc
# Once done this will define
# JEMALLOC_FOUND - System has jemalloc
# JEMALLOC_INCLUDE_DIRS - The jemalloc include directories
# JEMALLOC_LIBRARIES - The libraries needed to use jemalloc

find_package(PkgConfig)
if(NOT JEMALLOC_USE_BUNDLED)
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_JEMALLOC QUIET jemalloc)
endif()
else()
set(PC_JEMALLOC_INCLUDEDIR)
set(PC_JEMALLOC_INCLUDE_DIRS)
set(PC_JEMALLOC_LIBDIR)
set(PC_JEMALLOC_LIBRARY_DIRS)
set(LIMIT_SEARCH NO_DEFAULT_PATH)
endif()

set(JEMALLOC_DEFINITIONS ${PC_JEMALLOC_CFLAGS_OTHER})

find_path(JEMALLOC_INCLUDE_DIR jemalloc/jemalloc.h
PATHS ${PC_JEMALLOC_INCLUDEDIR} ${PC_JEMALLOC_INCLUDE_DIRS}
${LIMIT_SEARCH})

# If we're asked to use static linkage, add libjemalloc.a as a preferred library name.
if(JEMALLOC_USE_STATIC)
list(APPEND JEMALLOC_NAMES
"${CMAKE_STATIC_LIBRARY_PREFIX}jemalloc${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()

list(APPEND JEMALLOC_NAMES jemalloc)

find_library(JEMALLOC_LIBRARY NAMES ${JEMALLOC_NAMES}
HINTS ${PC_JEMALLOC_LIBDIR} ${PC_JEMALLOC_LIBRARY_DIRS}
${LIMIT_SEARCH})

set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY})
set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(JeMalloc DEFAULT_MSG
JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR)

mark_as_advanced(JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY)
1 change: 1 addition & 0 deletions config/config.h.in
Expand Up @@ -65,5 +65,6 @@
#define FEAT_BROWSE
#define FEAT_CSCOPE
#define FEAT_MOUSE
#cmakedefine USE_JEMALLOC

#endif // AUTO_CONFIG_H
16 changes: 9 additions & 7 deletions src/nvim/CMakeLists.txt
@@ -1,11 +1,5 @@
include(CheckLibraryExists)

option(SANITIZE "Enable Clang sanitizers for nvim binary" OFF)
if(SANITIZE AND NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
message(WARNING "SANITIZE is only supported for Clang ... disabling")
set(SANITIZE OFF)
endif()

set(GENERATED_DIR ${PROJECT_BINARY_DIR}/src/nvim/auto)
set(DISPATCH_GENERATOR ${PROJECT_SOURCE_DIR}/scripts/msgpack-gen.lua)
file(GLOB API_HEADERS api/*.h)
Expand Down Expand Up @@ -176,9 +170,16 @@ list(APPEND NVIM_LINK_LIBRARIES
${CMAKE_THREAD_LIBS_INIT}
)

set(NVIM_EXEC_LINK_LIBRARIES ${NVIM_LINK_LIBRARIES})

if(USE_JEMALLOC)
# dont use jemalloc in the unit test library
list(APPEND NVIM_EXEC_LINK_LIBRARIES ${JEMALLOC_LIBRARIES})
endif()

add_executable(nvim ${NEOVIM_GENERATED_SOURCES} ${NEOVIM_SOURCES}
${NEOVIM_HEADERS})
target_link_libraries(nvim ${NVIM_LINK_LIBRARIES})
target_link_libraries(nvim ${NVIM_EXEC_LINK_LIBRARIES})
install_helper(TARGETS nvim)

if(SANITIZE)
Expand All @@ -199,5 +200,6 @@ set_property(TARGET libnvim APPEND_STRING PROPERTY COMPILE_FLAGS " -DMAKE_LIB ")
add_library(nvim-test MODULE EXCLUDE_FROM_ALL ${NEOVIM_GENERATED_SOURCES}
${NEOVIM_SOURCES} ${NEOVIM_HEADERS})
target_link_libraries(nvim-test ${NVIM_LINK_LIBRARIES})
set_target_properties(nvim-test PROPERTIES COMPILE_FLAGS -DUNIT_TESTING)

add_subdirectory(po)
12 changes: 6 additions & 6 deletions src/nvim/api/buffer.c
Expand Up @@ -58,7 +58,7 @@ String buffer_get_line(Buffer buffer, Integer index, Error *err)
rv = slice.items[0].data.string;
}

free(slice.items);
xfree(slice.items);

return rv;
}
Expand Down Expand Up @@ -144,10 +144,10 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer,
end:
if (err->set) {
for (size_t i = 0; i < rv.size; i++) {
free(rv.items[i].data.string.data);
xfree(rv.items[i].data.string.data);
}

free(rv.items);
xfree(rv.items);
rv.items = NULL;
}

Expand Down Expand Up @@ -280,7 +280,7 @@ void buffer_set_line_slice(Buffer buffer,
}

// Same as with replacing, but we also need to free lines
free(lines[i]);
xfree(lines[i]);
lines[i] = NULL;
extra++;
}
Expand All @@ -301,10 +301,10 @@ void buffer_set_line_slice(Buffer buffer,

end:
for (size_t i = 0; i < new_len; i++) {
free(lines[i]);
xfree(lines[i]);
}

free(lines);
xfree(lines);
restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
try_end(err);
}
Expand Down
8 changes: 4 additions & 4 deletions src/nvim/api/private/helpers.c
Expand Up @@ -61,7 +61,7 @@ bool try_end(Error *err)
free_global_msglist();

if (should_free) {
free(msg);
xfree(msg);
}
} else if (did_throw) {
api_set_error(err, Exception, "%s", current_exception->value);
Expand Down Expand Up @@ -489,7 +489,7 @@ void api_free_string(String value)
return;
}

free(value.data);
xfree(value.data);
}

void api_free_object(Object value)
Expand Down Expand Up @@ -527,7 +527,7 @@ void api_free_array(Array value)
api_free_object(value.items[i]);
}

free(value.items);
xfree(value.items);
}

void api_free_dictionary(Dictionary value)
Expand All @@ -537,7 +537,7 @@ void api_free_dictionary(Dictionary value)
api_free_object(value.items[i].value);
}

free(value.items);
xfree(value.items);
}

Dictionary api_metadata(void)
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/api/vim.c
Expand Up @@ -85,7 +85,7 @@ void vim_feedkeys(String keys, String mode, Boolean escape_csi)
insert ? 0 : typebuf.tb_len, !typed, false);

if (escape_csi) {
free(keys_esc);
xfree(keys_esc);
}

if (vgetc_busy)
Expand Down

0 comments on commit a9ee85b

Please sign in to comment.