Commit fd291dad authored by Rich Prohaska's avatar Rich Prohaska

tighten up the mempool assertions

git-svn-id: file:///svn/tokudb@469 c7de825b-a66e-492c-adef-691d508d4ae1
parent 01658181
......@@ -4,7 +4,7 @@
void mempool_init(struct mempool *mp, void *base, int size) {
// printf("mempool_init %p %p %d\n", mp, base, size);
assert(base != 0 && size > 0);
assert(base != 0 && size >= 0);
mp->base = base;
mp->size = size;
mp->free_offset = 0;
......@@ -39,7 +39,7 @@ int mempool_get_frag_size(struct mempool *mp) {
}
void *mempool_malloc(struct mempool *mp, int size, int alignment) {
assert(mp->free_offset < mp->size);
assert(mp->free_offset <= mp->size);
void *vp;
int offset = (mp->free_offset + (alignment-1)) & ~(alignment-1);
if (offset + size > mp->size) {
......@@ -48,14 +48,14 @@ void *mempool_malloc(struct mempool *mp, int size, int alignment) {
vp = mp->base + offset;
mp->free_offset = offset + size;
}
assert(mp->free_offset < mp->size);
assert(mp->free_offset <= mp->size);
assert(((long)vp & (alignment-1)) == 0);
assert(vp == 0 || (mp->base <= vp && vp + size < mp->base + mp->size));
assert(vp == 0 || (mp->base <= vp && vp + size <= mp->base + mp->size));
return vp;
}
void mempool_mfree(struct mempool *mp, void *vp, int size) {
assert(size > 0 && mp->base <= vp && vp + size < mp->base + mp->size);
assert(size >= 0 && mp->base <= vp && vp + size <= mp->base + mp->size);
mp->frag_size += size;
assert(mp->frag_size <= mp->size);
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "memory.h"
#include "mempool.h"
void test_mempool_limits(int size) {
void *base = malloc(size);
struct mempool mempool;
mempool_init(&mempool, base, size);
int i;
for (i=0;; i++) {
void *vp = mempool_malloc(&mempool, 1, 1);
if (vp == 0)
break;
}
assert(i == size);
mempool_fini(&mempool);
free(base);
}
void test_mempool_malloc_mfree(int size) {
void *base = malloc(size);
struct mempool mempool;
mempool_init(&mempool, base, size);
void *vp[size];
int i;
for (i=0;; i++) {
vp[i] = mempool_malloc(&mempool, 1, 1);
if (vp[i] == 0)
break;
}
assert(i == size);
for (i=0; i<size; i++)
mempool_mfree(&mempool, vp[i], 1);
assert(mempool_get_frag_size(&mempool) == size);
mempool_fini(&mempool);
free(base);
}
int main() {
test_mempool_limits(0);
test_mempool_limits(256);
test_mempool_malloc_mfree(0);
test_mempool_malloc_mfree(256);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment