Skip to content

Commit

Permalink
Keep active span when fully freed (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansson committed May 28, 2019
1 parent f7b1039 commit ea79cf6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
4 changes: 2 additions & 2 deletions build/msvs/rpmalloc.sln
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
# Visual Studio 16
VisualStudioVersion = 16.0.28803.202
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "rpmalloc", "rpmalloc.vcxproj", "{65DC4291-954E-4B91-8889-4F3ADCC9D2D5}"
EndProject
Expand Down
10 changes: 5 additions & 5 deletions build/msvs/rpmalloc.vcxproj
Expand Up @@ -28,32 +28,32 @@
<ProjectGuid>{65DC4291-954E-4B91-8889-4F3ADCC9D2D5}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>rpmalloc</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions build/msvs/test.vcxproj
Expand Up @@ -36,32 +36,32 @@
<ProjectGuid>{C31980DD-1241-4EF8-A351-69DAF982A7B9}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>test</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion build/ninja/toolchain.py
Expand Up @@ -152,7 +152,7 @@ def initialize_configs(self, configs):
self.initialize_default_configs()

def initialize_default_configs(self):
self.configs = ['debug', 'release', 'profile', 'deploy']
self.configs = ['debug', 'release']#, 'profile', 'deploy']

def initialize_toolchain(self):
if self.android != None:
Expand Down
39 changes: 30 additions & 9 deletions rpmalloc/rpmalloc.c
Expand Up @@ -1074,15 +1074,18 @@ _memory_deallocate_to_heap(heap_t* heap, span_t* span, void* p) {

//Check if the span will become completely free
if (block_data->free_count == ((count_t)size_class->block_count - 1)) {
//If it was active, reset counter. Otherwise, if not active, remove from
//partial free list if we had a previous free block (guard for classes with only 1 block)
if (is_active)
block_data->free_count = 0;
else if (block_data->free_count > 0)
_memory_span_list_doublelink_remove(&heap->size_cache[class_idx], span);

//Add to heap span cache
_memory_heap_cache_insert(heap, span);
if (is_active) {
//If it was active, reset free block list
++block_data->free_count;
block_data->first_autolink = 0;
block_data->free_list = 0;
} else {
//If not active, remove from partial free list if we had a previous free
//block (guard for classes with only 1 block) and add to heap cache
if (block_data->free_count > 0)
_memory_span_list_doublelink_remove(&heap->size_cache[class_idx], span);
_memory_heap_cache_insert(heap, span);
}
return;
}

Expand Down Expand Up @@ -1546,6 +1549,15 @@ rpmalloc_finalize(void) {
_memory_unmap_span(span);
}

for (size_t iclass = 0; iclass < SIZE_CLASS_COUNT; ++iclass) {
span_t* span = heap->active_span[iclass];
if (span && (heap->active_block[iclass].free_count == _memory_size_class[iclass].block_count)) {
heap->active_span[iclass] = 0;
heap->active_block[iclass].free_count = 0;
_memory_heap_cache_insert(heap, span);
}
}

//Free span caches (other thread might have deferred after the thread using this heap finalized)
#if ENABLE_THREAD_CACHE
for (size_t iclass = 0; iclass < LARGE_CLASS_COUNT; ++iclass) {
Expand Down Expand Up @@ -1606,6 +1618,15 @@ rpmalloc_thread_finalize(void) {

_memory_deallocate_deferred(heap);

for (size_t iclass = 0; iclass < SIZE_CLASS_COUNT; ++iclass) {
span_t* span = heap->active_span[iclass];
if (span && (heap->active_block[iclass].free_count == _memory_size_class[iclass].block_count)) {
heap->active_span[iclass] = 0;
heap->active_block[iclass].free_count = 0;
_memory_heap_cache_insert(heap, span);
}
}

//Release thread cache spans back to global cache
#if ENABLE_THREAD_CACHE
for (size_t iclass = 0; iclass < LARGE_CLASS_COUNT; ++iclass) {
Expand Down

0 comments on commit ea79cf6

Please sign in to comment.