+2009-09-02 Petr Machata <pmachata@redhat.com>
+
+ * libebl/eblstrtab.c (morememory): Allocate memory in multiples of
+ pagesize.
+
2009-08-06 Petr Machata <pmachata@redhat.com>
* libebl/eblstrtab.c (ebl_strtabfinalize): Only call copystrings
};
-/* Cache for the pagesize. We correct this value a bit so that `malloc'
- is not allocating more than a page. */
+/* Cache for the pagesize. */
static size_t ps;
+/* We correct this value a bit so that `malloc' is not allocating more
+ than a page. */
+#define MALLOC_OVERHEAD (2 * sizeof (void *))
struct Ebl_Strtab *
{
if (ps == 0)
{
- ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void *);
- assert (sizeof (struct memoryblock) < ps);
+ ps = sysconf (_SC_PAGESIZE);
+ assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
}
struct Ebl_Strtab *ret
static int
morememory (struct Ebl_Strtab *st, size_t len)
{
- if (len < ps)
- len = ps;
+ size_t overhead = offsetof (struct memoryblock, memory);
+ len += overhead + MALLOC_OVERHEAD;
+
+ /* Allocate nearest multiple of pagesize >= len. */
+ len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
struct memoryblock *newmem = (struct memoryblock *) malloc (len);
if (newmem == NULL)
newmem->next = st->memory;
st->memory = newmem;
st->backp = newmem->memory;
- st->left = len - offsetof (struct memoryblock, memory);
+ st->left = len - overhead;
return 0;
}