-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle small memory allocations ( < 16 bytes) more space efficiently
This patch adjusts std_malloc() to handle small allocations (smaller than 16-bytes) more efficiently. More specifically instead of allocating whole page for each such request, adjusted std_malloc() uses memory pool adequate to requested size and thus saves almost entire page (4K) of memory. Fixes #1011 Signed-off-by: Waldemar Kozaczuk <[email protected]> Message-Id: <[email protected]>
- Loading branch information
Showing
3 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2018 Waldemar Kozaczuk | ||
* | ||
* This work is open source software, licensed under the terms of the | ||
* BSD license as described in the LICENSE file in the top-level directory. | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <cassert> | ||
#include <osv/trace-count.hh> | ||
|
||
int main() { | ||
tracepoint_counter *memory_malloc_mempool_counter = nullptr, | ||
*memory_malloc_page_counter = nullptr; | ||
|
||
for (auto & tp : tracepoint_base::tp_list) { | ||
if ("memory_malloc_mempool" == std::string(tp.name)) { | ||
memory_malloc_mempool_counter = new tracepoint_counter(tp); | ||
} | ||
if ("memory_malloc_page" == std::string(tp.name)) { | ||
memory_malloc_page_counter = new tracepoint_counter(tp); | ||
} | ||
} | ||
assert(memory_malloc_mempool_counter != nullptr); | ||
assert(memory_malloc_page_counter != nullptr); | ||
|
||
auto memory_malloc_mempool_counter_now = memory_malloc_mempool_counter->read(); | ||
auto memory_malloc_page_counter_now = memory_malloc_page_counter->read(); | ||
|
||
const int allocation_count = 1024; | ||
for( int i = 0; i < allocation_count; i++) { | ||
void *addr = malloc(7); | ||
assert(addr); | ||
free(addr); | ||
|
||
addr = malloc(17); | ||
assert(addr); | ||
free(addr); | ||
} | ||
|
||
// Verify all allocations above were handled by malloc_pool | ||
assert(memory_malloc_mempool_counter->read() - memory_malloc_mempool_counter_now >= 2 * allocation_count); | ||
// Verify that NO allocations were handled by alloc_page | ||
assert(memory_malloc_page_counter->read() - memory_malloc_page_counter_now == 0); | ||
} |