2007-11-29 14:18:54 +00:00
|
|
|
/* -*- mode: C; c-basic-offset: 4 -*- */
|
2008-01-24 15:10:32 +00:00
|
|
|
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
|
2007-11-29 14:18:54 +00:00
|
|
|
|
2007-10-10 19:33:31 +00:00
|
|
|
#include "mempool.h"
|
2008-02-08 03:17:38 +00:00
|
|
|
#include "toku_assert.h"
|
|
|
|
#include <stdio.h>
|
2007-10-10 19:33:31 +00:00
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_init(struct mempool *mp, void *base, int size) {
|
2007-10-10 19:33:31 +00:00
|
|
|
// printf("mempool_init %p %p %d\n", mp, base, size);
|
2007-10-16 14:18:02 +00:00
|
|
|
assert(base != 0 && size >= 0);
|
2007-10-10 19:33:31 +00:00
|
|
|
mp->base = base;
|
|
|
|
mp->size = size;
|
|
|
|
mp->free_offset = 0;
|
|
|
|
mp->frag_size = 0;
|
2007-10-12 23:50:05 +00:00
|
|
|
mp->compress_func = 0;
|
|
|
|
mp->compress_arg = 0;
|
2007-10-10 19:33:31 +00:00
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_fini(struct mempool *mp __attribute__((unused))) {
|
2007-10-10 19:33:31 +00:00
|
|
|
// printf("mempool_fini %p %p %d %d\n", mp, mp->base, mp->size, mp->frag_size);
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_set_compress_func(struct mempool *mp, mempool_compress_func compress_func, void *compress_arg) {
|
2007-10-12 23:50:05 +00:00
|
|
|
mp->compress_func = compress_func;
|
|
|
|
mp->compress_arg = compress_arg;
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_call_compress_func(struct mempool *mp) {
|
2007-10-12 23:50:05 +00:00
|
|
|
mp->compress_func(mp, mp->compress_arg);
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void *toku_mempool_get_base(struct mempool *mp) {
|
2007-10-12 23:50:05 +00:00
|
|
|
return mp->base;
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
int toku_mempool_get_size(struct mempool *mp) {
|
2007-10-12 23:50:05 +00:00
|
|
|
return mp->size;
|
2007-10-10 19:33:31 +00:00
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
int toku_mempool_get_frag_size(struct mempool *mp) {
|
2007-10-10 19:33:31 +00:00
|
|
|
return mp->frag_size;
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void *toku_mempool_malloc(struct mempool *mp, int size, int alignment) {
|
2007-10-16 14:18:02 +00:00
|
|
|
assert(mp->free_offset <= mp->size);
|
2007-10-10 19:33:31 +00:00
|
|
|
void *vp;
|
|
|
|
int offset = (mp->free_offset + (alignment-1)) & ~(alignment-1);
|
|
|
|
if (offset + size > mp->size) {
|
|
|
|
vp = 0;
|
|
|
|
} else {
|
|
|
|
vp = mp->base + offset;
|
|
|
|
mp->free_offset = offset + size;
|
|
|
|
}
|
2007-10-16 14:18:02 +00:00
|
|
|
assert(mp->free_offset <= mp->size);
|
2007-10-10 19:33:31 +00:00
|
|
|
assert(((long)vp & (alignment-1)) == 0);
|
2007-10-16 14:18:02 +00:00
|
|
|
assert(vp == 0 || (mp->base <= vp && vp + size <= mp->base + mp->size));
|
2007-10-10 19:33:31 +00:00
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:51:16 +00:00
|
|
|
void toku_mempool_mfree(struct mempool *mp, void *vp, int size) {
|
2007-10-16 14:18:02 +00:00
|
|
|
assert(size >= 0 && mp->base <= vp && vp + size <= mp->base + mp->size);
|
2007-10-10 19:33:31 +00:00
|
|
|
mp->frag_size += size;
|
|
|
|
assert(mp->frag_size <= mp->size);
|
|
|
|
}
|