summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2014-06-30 12:05:25 -0700
committerVicent Marti <tanoku@gmail.com>2014-08-05 02:06:52 +0200
commit38ddf2277ff136f0e07240cbc6352fd80c5ca884 (patch)
tree0e53d1d8ed3ebe553f0790717283fe4fc72da239
parent63ee946d63d9427f0ef9fbae2e1e8869f7ec8479 (diff)
downloadlibgit2-38ddf2277ff136f0e07240cbc6352fd80c5ca884.tar.gz
Round up pool alloc sizes for alignment
To make sure that items returned from pool allocations are aligned on nice boundaries, this rounds up all pool allocation sizes to a multiple of 8. This adds a small amount of overhead to each item. The rounding up could be made optional with an extra parameter to the pool initialization that turned on rounding only for pools where item alignment actually matters, but I think for the extra code and complexity that would be involved, that it makes sense just to burn a little bit of extra memory and enable this all the time.
-rw-r--r--src/pool.c2
-rw-r--r--tests/core/pool.c10
2 files changed, 6 insertions, 6 deletions
diff --git a/src/pool.c b/src/pool.c
index 146f118b..a516ff9e 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -146,7 +146,7 @@ GIT_INLINE(void) pool_remove_page(
void *git_pool_malloc(git_pool *pool, uint32_t items)
{
git_pool_page *scan = pool->open, *prev;
- uint32_t size = items * pool->item_size;
+ uint32_t size = ((items * pool->item_size) + 7) & ~7;
void *ptr = NULL;
pool->has_string_alloc = 0;
diff --git a/tests/core/pool.c b/tests/core/pool.c
index 351d0c20..a7ec8801 100644
--- a/tests/core/pool.c
+++ b/tests/core/pool.c
@@ -38,19 +38,19 @@ void test_core_pool__1(void)
cl_assert(git_pool_malloc(&p, i) != NULL);
/* with fixed page size, allocation must end up with these values */
- cl_assert(git_pool__open_pages(&p) == 1);
- cl_assert(git_pool__full_pages(&p) == 505);
+ cl_assert_equal_i(1, git_pool__open_pages(&p));
+ cl_assert_equal_i(507, git_pool__full_pages(&p));
git_pool_clear(&p);
- cl_git_pass(git_pool_init(&p, 1, 4100));
+ cl_git_pass(git_pool_init(&p, 1, 4120));
for (i = 2010; i > 0; i--)
cl_assert(git_pool_malloc(&p, i) != NULL);
/* with fixed page size, allocation must end up with these values */
- cl_assert(git_pool__open_pages(&p) == 1);
- cl_assert(git_pool__full_pages(&p) == 492);
+ cl_assert_equal_i(1, git_pool__open_pages(&p));
+ cl_assert_equal_i(492, git_pool__full_pages(&p));
git_pool_clear(&p);
}