Browse Source
[t:4050] #4050 Merge tokudb.4050 to merge, done with command svn merge -r36213:HEAD tokudb.4050 tokudb
[t:4050] #4050 Merge tokudb.4050 to merge, done with command svn merge -r36213:HEAD tokudb.4050 tokudb
git-svn-id: file:///svn/toku/tokudb@36808 c7de825b-a66e-492c-adef-691d508d4ae1pull/73/head
committed by
Yoni Fogel
16 changed files with 1040 additions and 389 deletions
-
1newbrt/Makefile
-
18newbrt/brt-internal.h
-
211newbrt/brt-serialize.c
-
15newbrt/brt-test-helpers.c
-
276newbrt/brt.c
-
1newbrt/leafentry.h
-
128newbrt/mempool.c
-
86newbrt/mempool.h
-
307newbrt/tests/brt-serialize-test.c
-
36newbrt/tests/orthopush-flush.c
-
54newbrt/tests/test-leafentry-nested.c
-
179newbrt/tests/test3884.c
-
9newbrt/ule-internal.h
-
45newbrt/ule.c
-
6newbrt/ule.h
-
57src/tests/simple.c
@ -0,0 +1,128 @@ |
|||
/* -*- mode: C; c-basic-offset: 4 -*- */ |
|||
#ident "$Id: mempool.c 19902 2010-05-06 20:41:32Z bkuszmaul $" |
|||
#ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved." |
|||
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." |
|||
|
|||
#include "includes.h" |
|||
|
|||
/* Contract: |
|||
* Caller allocates mempool struct as convenient for caller, but memory used for data storage |
|||
* must be dynamically allocated via toku_malloc(). |
|||
* Caller dynamically allocates memory for mempool and initializes mempool by calling toku_mempool_init(). |
|||
* Once a buffer is assigned to a mempool (via toku_mempool_init()), the mempool owns it and |
|||
* is responsible for destroying it when the mempool is destroyed. |
|||
* Caller destroys mempool by calling toku_mempool_destroy(). |
|||
* |
|||
* Note, toku_mempool_init() does not allocate the memory because sometimes the caller will already have |
|||
* the memory allocated and will assign the pre-allocated memory to the mempool. |
|||
*/ |
|||
|
|||
/* This is a constructor to be used when the memory for the mempool struct has been |
|||
* allocated by the caller, but no memory has yet been allocatd for the data. |
|||
*/ |
|||
void toku_mempool_zero(struct mempool *mp) { |
|||
// printf("mempool_zero %p\n", mp); |
|||
memset(mp, 0, sizeof(*mp)); |
|||
} |
|||
|
|||
/* Copy constructor. Any time a new mempool is needed, allocate 1/4 more space |
|||
* than is currently needed. |
|||
*/ |
|||
void toku_mempool_copy_construct(struct mempool *mp, const void * const data_source, const size_t data_size) { |
|||
// printf("mempool_copy %p %p %lu\n", mp, data_source, data_size); |
|||
if (data_size) { |
|||
invariant(data_source); |
|||
toku_mempool_construct(mp, data_size); |
|||
memcpy(mp->base, data_source, data_size); |
|||
mp->free_offset = data_size; // address of first available memory for new data |
|||
} |
|||
else { |
|||
toku_mempool_zero(mp); |
|||
// fprintf(stderr, "Empty mempool created (copy constructor)\n"); |
|||
} |
|||
} |
|||
|
|||
// TODO 4050 this is dirty, try to replace all uses of this |
|||
void toku_mempool_init(struct mempool *mp, void *base, size_t size) { |
|||
// printf("mempool_init %p %p %lu\n", mp, base, size); |
|||
invariant(base != 0); |
|||
invariant(size < (1U<<31)); // used to be assert(size >= 0), but changed to size_t so now let's make sure it's not more than 2GB... |
|||
mp->base = base; |
|||
mp->size = size; |
|||
mp->free_offset = 0; // address of first available memory |
|||
mp->frag_size = 0; // byte count of wasted space (formerly used, no longer used or available) |
|||
} |
|||
|
|||
/* allocate memory and construct mempool |
|||
*/ |
|||
void toku_mempool_construct(struct mempool *mp, size_t data_size) { |
|||
if (data_size) { |
|||
size_t mpsize = data_size + (data_size/4); // allow 1/4 room for expansion (would be wasted if read-only) |
|||
mp->base = toku_xmalloc(mpsize); // allocate buffer for mempool |
|||
mp->size = mpsize; |
|||
mp->free_offset = 0; // address of first available memory for new data |
|||
mp->frag_size = 0; // all allocated space is now in use |
|||
} |
|||
else { |
|||
toku_mempool_zero(mp); |
|||
// fprintf(stderr, "Empty mempool created (base constructor)\n"); |
|||
} |
|||
} |
|||
|
|||
|
|||
void toku_mempool_destroy(struct mempool *mp) { |
|||
// printf("mempool_destroy %p %p %lu %lu\n", mp, mp->base, mp->size, mp->frag_size); |
|||
if (mp->base) |
|||
toku_free(mp->base); |
|||
toku_mempool_zero(mp); |
|||
} |
|||
|
|||
void *toku_mempool_get_base(struct mempool *mp) { |
|||
return mp->base; |
|||
} |
|||
|
|||
size_t toku_mempool_get_size(struct mempool *mp) { |
|||
return mp->size; |
|||
} |
|||
|
|||
size_t toku_mempool_get_frag_size(struct mempool *mp) { |
|||
return mp->frag_size; |
|||
} |
|||
|
|||
size_t toku_mempool_get_used_space(struct mempool *mp) { |
|||
return mp->free_offset - mp->frag_size; |
|||
} |
|||
|
|||
size_t toku_mempool_get_free_space(struct mempool *mp) { |
|||
return mp->size - mp->free_offset; |
|||
} |
|||
|
|||
void *toku_mempool_malloc(struct mempool *mp, size_t size, int alignment) { |
|||
invariant(size < (1U<<31)); |
|||
invariant(mp->size < (1U<<31)); |
|||
invariant(mp->free_offset < (1U<<31)); |
|||
assert(mp->free_offset <= mp->size); |
|||
void *vp; |
|||
size_t offset = (mp->free_offset + (alignment-1)) & ~(alignment-1); |
|||
//printf("mempool_malloc size=%ld base=%p free_offset=%ld mp->size=%ld offset=%ld\n", size, mp->base, mp->free_offset, mp->size, offset); |
|||
if (offset + size > mp->size) { |
|||
vp = 0; |
|||
} else { |
|||
vp = (char *)mp->base + offset; |
|||
mp->free_offset = offset + size; |
|||
} |
|||
assert(mp->free_offset <= mp->size); |
|||
assert(((long)vp & (alignment-1)) == 0); |
|||
assert(vp == 0 || toku_mempool_inrange(mp, vp, size)); |
|||
//printf("mempool returning %p\n", vp); |
|||
return vp; |
|||
} |
|||
|
|||
// if vp is null then we are freeing something, but not specifying what. The data won't be freed until compression is done. |
|||
void toku_mempool_mfree(struct mempool *mp, void *vp, size_t size) { |
|||
if (vp) assert(toku_mempool_inrange(mp, vp, size)); |
|||
mp->frag_size += size; |
|||
assert(mp->frag_size <= mp->size); |
|||
} |
|||
|
|||
|
|||
@ -0,0 +1,86 @@ |
|||
#ifndef _TOKU_MEMPOOL_H |
|||
#define _TOKU_MEMPOOL_H |
|||
#ident "$Id: mempool.h 19902 2010-05-06 20:41:32Z bkuszmaul $" |
|||
#ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved." |
|||
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." |
|||
|
|||
/* a memory pool is a contiguous region of memory that supports single |
|||
allocations from the pool. these allocated regions are never recycled. |
|||
when the memory pool no longer has free space, the allocated chunks |
|||
must be relocated by the application to a new memory pool. */ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#if defined(__cplusplus) || defined(__cilkplusplus) |
|||
extern "C" { |
|||
#endif |
|||
|
|||
struct mempool; |
|||
|
|||
// TODO 4050 Hide mempool struct internals from callers |
|||
|
|||
struct mempool { |
|||
void *base; /* the base address of the memory */ |
|||
size_t free_offset; /* the offset of the memory pool free space */ |
|||
size_t size; /* the size of the memory */ |
|||
size_t frag_size; /* the size of the fragmented memory */ |
|||
}; |
|||
|
|||
/* This is a constructor to be used when the memory for the mempool struct has been |
|||
* allocated by the caller, but no memory has yet been allocatd for the data. |
|||
*/ |
|||
void toku_mempool_zero(struct mempool *mp); |
|||
|
|||
/* Copy constructor. Fill in empty mempool struct with new values, allocating |
|||
* a new buffer and filling the buffer with data from from data_source. |
|||
* Any time a new mempool is needed, allocate 1/4 more space |
|||
* than is currently needed. |
|||
*/ |
|||
void toku_mempool_copy_construct(struct mempool *mp, const void * const data_source, const size_t data_size); |
|||
|
|||
/* initialize the memory pool with the base address and size of a |
|||
contiguous chunk of memory */ |
|||
void toku_mempool_init(struct mempool *mp, void *base, size_t size); |
|||
|
|||
/* allocate memory and construct mempool |
|||
*/ |
|||
void toku_mempool_construct(struct mempool *mp, size_t data_size); |
|||
|
|||
/* destroy the memory pool */ |
|||
void toku_mempool_destroy(struct mempool *mp); |
|||
|
|||
/* get the base address of the memory pool */ |
|||
void *toku_mempool_get_base(struct mempool *mp); |
|||
|
|||
/* get the size of the memory pool */ |
|||
size_t toku_mempool_get_size(struct mempool *mp); |
|||
|
|||
/* get the amount of fragmented (wasted) space in the memory pool */ |
|||
size_t toku_mempool_get_frag_size(struct mempool *mp); |
|||
|
|||
/* get the amount of space that is holding useful data */ |
|||
size_t toku_mempool_get_used_space(struct mempool *mp); |
|||
|
|||
/* get the amount of space that is available for new data */ |
|||
size_t toku_mempool_get_free_space(struct mempool *mp); |
|||
|
|||
|
|||
/* allocate a chunk of memory from the memory pool suitably aligned */ |
|||
void *toku_mempool_malloc(struct mempool *mp, size_t size, int alignment); |
|||
|
|||
/* free a previously allocated chunk of memory. the free only updates |
|||
a count of the amount of free space in the memory pool. the memory |
|||
pool does not keep track of the locations of the free chunks */ |
|||
void toku_mempool_mfree(struct mempool *mp, void *vp, size_t size); |
|||
|
|||
/* verify that a memory range is contained within a mempool */ |
|||
static inline int toku_mempool_inrange(struct mempool *mp, void *vp, size_t size) { |
|||
return (mp->base <= vp) && ((char *)vp + size <= (char *)mp->base + mp->size); |
|||
} |
|||
|
|||
#if defined(__cplusplus) || defined(__cilkplusplus) |
|||
}; |
|||
#endif |
|||
|
|||
|
|||
#endif |
|||
@ -0,0 +1,57 @@ |
|||
/* -*- mode: C; c-basic-offset: 4 -*- */ |
|||
|
|||
#ident "Copyright (c) 2009 Tokutek Inc. All rights reserved." |
|||
#ident "$Id: env_startup.c 20778 2010-05-28 20:38:42Z yfogel $" |
|||
|
|||
/* Purpose of this test is to verify that a failed assert will |
|||
* cause a panic, which should be visible via engine status. |
|||
* This is a manual test, should not be checked in to repository. |
|||
* The panic must be manually induced in the debugger. |
|||
*/ |
|||
|
|||
|
|||
#include "test.h" |
|||
#include <db.h> |
|||
|
|||
static DB_ENV *env; |
|||
|
|||
#define FLAGS_NOLOG DB_INIT_LOCK|DB_INIT_MPOOL|DB_CREATE|DB_PRIVATE |
|||
#define FLAGS_LOG FLAGS_NOLOG|DB_INIT_TXN|DB_INIT_LOG |
|||
|
|||
static int mode = S_IRWXU+S_IRWXG+S_IRWXO; |
|||
|
|||
static void test_shutdown(void); |
|||
|
|||
static void |
|||
test_shutdown(void) { |
|||
int r; |
|||
r=env->close(env, 0); CKERR(r); |
|||
env = NULL; |
|||
} |
|||
|
|||
static void |
|||
setup (u_int32_t flags) { |
|||
int r; |
|||
if (env) |
|||
test_shutdown(); |
|||
r = system("rm -rf " ENVDIR); |
|||
CKERR(r); |
|||
r=toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO); |
|||
CKERR(r); |
|||
r=db_env_create(&env, 0); |
|||
CKERR(r); |
|||
env->set_errfile(env, stderr); |
|||
r=env->open(env, ENVDIR, flags, mode); |
|||
CKERR(r); |
|||
} |
|||
|
|||
|
|||
int |
|||
test_main (int argc, char * const argv[]) { |
|||
parse_args(argc, argv); |
|||
setup(FLAGS_LOG); |
|||
env->txn_checkpoint(env, 0, 0, 0); |
|||
print_engine_status(env); |
|||
test_shutdown(); |
|||
return 0; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue