|
@@ -244,12 +244,13 @@ static INLINE chunk_t *
|
|
|
chunk_grow(chunk_t *chunk, size_t sz)
|
|
|
{
|
|
|
off_t offset;
|
|
|
+ size_t memlen_orig = chunk->memlen;
|
|
|
tor_assert(sz > chunk->memlen);
|
|
|
offset = chunk->data - chunk->mem;
|
|
|
chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz));
|
|
|
chunk->memlen = sz;
|
|
|
chunk->data = chunk->mem + offset;
|
|
|
- total_bytes_allocated_in_chunks += (sz - chunk->memlen);
|
|
|
+ total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(sz) - CHUNK_ALLOC_SIZE(memlen_orig);
|
|
|
return chunk;
|
|
|
}
|
|
|
|
|
@@ -274,12 +275,14 @@ preferred_chunk_size(size_t target)
|
|
|
}
|
|
|
|
|
|
/** Remove from the freelists most chunks that have not been used since the
|
|
|
- * last call to buf_shrink_freelists(). */
|
|
|
-void
|
|
|
+ * last call to buf_shrink_freelists(). Return the amount of memory
|
|
|
+ * freed. */
|
|
|
+size_t
|
|
|
buf_shrink_freelists(int free_all)
|
|
|
{
|
|
|
#ifdef ENABLE_BUF_FREELISTS
|
|
|
int i;
|
|
|
+ size_t total_freed = 0;
|
|
|
disable_control_logging();
|
|
|
for (i = 0; freelists[i].alloc_size; ++i) {
|
|
|
int slack = freelists[i].slack;
|
|
@@ -313,6 +316,7 @@ buf_shrink_freelists(int free_all)
|
|
|
chunk_t *next = chunk->next;
|
|
|
tor_assert(total_bytes_allocated_in_chunks >= CHUNK_ALLOC_SIZE(chunk->memlen));
|
|
|
total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
|
|
|
+ total_freed += CHUNK_ALLOC_SIZE(chunk->memlen);
|
|
|
tor_free(chunk);
|
|
|
chunk = next;
|
|
|
--n_to_free;
|
|
@@ -330,18 +334,21 @@ buf_shrink_freelists(int free_all)
|
|
|
}
|
|
|
// tor_assert(!n_to_free);
|
|
|
freelists[i].cur_length = new_length;
|
|
|
+ tor_assert(orig_n_to_skip == new_length);
|
|
|
log_info(LD_MM, "Cleaned freelist for %d-byte chunks: original "
|
|
|
- "length %d, kept %d, dropped %d.",
|
|
|
+ "length %d, kept %d, dropped %d. New length is %d",
|
|
|
(int)freelists[i].alloc_size, orig_length,
|
|
|
- orig_n_to_skip, orig_n_to_free);
|
|
|
+ orig_n_to_skip, orig_n_to_free, new_length);
|
|
|
}
|
|
|
freelists[i].lowest_length = freelists[i].cur_length;
|
|
|
assert_freelist_ok(&freelists[i]);
|
|
|
}
|
|
|
done:
|
|
|
enable_control_logging();
|
|
|
+ return total_freed;
|
|
|
#else
|
|
|
(void) free_all;
|
|
|
+ return 0;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
@@ -579,6 +586,7 @@ static chunk_t *
|
|
|
chunk_copy(const chunk_t *in_chunk)
|
|
|
{
|
|
|
chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen));
|
|
|
+ total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(in_chunk->memlen);
|
|
|
newch->next = NULL;
|
|
|
if (in_chunk->data) {
|
|
|
off_t offset = in_chunk->data - in_chunk->mem;
|