|
@@ -34,6 +34,7 @@
|
|
|
|
|
|
#include "locks.h"
|
|
|
#include "default_rb.h"
|
|
|
+#include "slab.h"
|
|
|
|
|
|
#include "dmmlib_trace.h"
|
|
|
#include "statistics.h"
|
|
@@ -56,37 +57,21 @@ void * malloc(size_t size) {
|
|
|
sys_alloc_size = SYS_ALLOC_SIZE;
|
|
|
#endif /* WITH_KNOBS */
|
|
|
|
|
|
- /* Check if the size is appropriate for the use of a freelist-organized or
|
|
|
- * bitmap-organized raw block. */
|
|
|
+ ptr = NULL;
|
|
|
|
|
|
-#ifdef BITMAP_RB_ONLY
|
|
|
- /* FIXME currently the raw block size and resolution are fixed */
|
|
|
- size_t bm_vector_size = BMAP_EL_SIZE * (sys_alloc_size + BMAP_EL_SIZE -
|
|
|
- sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
|
|
|
- (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
|
|
|
+ /* Check for slab allocation */
|
|
|
+ if(size <= 512) {
|
|
|
+ size = (size + 15) & ~(size_t) 15;
|
|
|
+ size_t index = (size >> 4) - 1;
|
|
|
|
|
|
- if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
|
|
|
- sizeof(bitmap_rb_t) - bm_vector_size) {
|
|
|
-#endif /* BITMAP_RB_ONLY */
|
|
|
+ slab_rb_t *encapsulated_slab_rb;
|
|
|
|
|
|
-#ifdef FL_RB_ONLY
|
|
|
- if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
|
|
|
- sizeof(freelist_rb_t)) {
|
|
|
-#endif /* FL_RB_ONLY */
|
|
|
-
|
|
|
- ptr = NULL;
|
|
|
-
|
|
|
- /* Try to find a raw block available for allocation */
|
|
|
- SLIST_FOREACH(raw_block, &tls_allocator->rb_head, pointers) {
|
|
|
-#ifdef TRYLOCK_ON_MALLOC
|
|
|
- if(DEALY_TRYLOCK(raw_block->lock) == 0) {
|
|
|
-#else /* TRYLOCK_ON_MALLOC */
|
|
|
- DEALY_LOCK(raw_block->lock);
|
|
|
-#endif /* TRYLOCK_ON_MALLOC */
|
|
|
- encapsulated_rb = (DEFAULT_RB_T *)
|
|
|
+ /* Try to find a slab raw block available for allocation */
|
|
|
+ SLIST_FOREACH(raw_block, &tls_allocator->slab_rb_head[index],
|
|
|
+ pointers) {
|
|
|
+ encapsulated_slab_rb = (slab_rb_t *)
|
|
|
((uintptr_t) raw_block + sizeof(raw_block_header_t));
|
|
|
- ptr = dmmlib_malloc(encapsulated_rb, size);
|
|
|
- DEALY_UNLOCK(raw_block->lock);
|
|
|
+ ptr = slab_malloc(encapsulated_slab_rb, size);
|
|
|
|
|
|
if(ptr != NULL) {
|
|
|
/* Check that a valid pointer has been returned */
|
|
@@ -95,36 +80,96 @@ void * malloc(size_t size) {
|
|
|
raw_block->size + sizeof(raw_block_header_t)));
|
|
|
break;
|
|
|
}
|
|
|
-#ifdef TRYLOCK_ON_MALLOC
|
|
|
- }
|
|
|
-#endif /* TRYLOCK_ON_MALLOC */
|
|
|
}
|
|
|
|
|
|
if(ptr == NULL) {
|
|
|
- /* No raw block was found, try to create a new one and allocate a
|
|
|
- * memory block from there. */
|
|
|
-
|
|
|
- new_raw_block = create_raw_block(sys_alloc_size, DEFAULT_RB_TYPE);
|
|
|
+ /* No slab raw block was found, try to create a new one and allocate
|
|
|
+ * a memory block from there. */
|
|
|
+ new_raw_block = create_raw_block(sys_alloc_size, SLAB);
|
|
|
if(new_raw_block != NULL) {
|
|
|
- SLIST_INSERT_HEAD(&tls_allocator->rb_head, new_raw_block,
|
|
|
- pointers);
|
|
|
-
|
|
|
- encapsulated_rb = (DEFAULT_RB_T *)
|
|
|
+ SLIST_INSERT_HEAD(&tls_allocator->slab_rb_head[index],
|
|
|
+ new_raw_block, pointers);
|
|
|
+ encapsulated_slab_rb = (slab_rb_t *)
|
|
|
((uintptr_t) new_raw_block + sizeof(raw_block_header_t));
|
|
|
- ptr = dmmlib_malloc(encapsulated_rb, size);
|
|
|
+ encapsulated_slab_rb->handling_size = size;
|
|
|
+ ptr = slab_malloc(encapsulated_slab_rb, size);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- /* The memory request size is too large to serve it inside a
|
|
|
- * freelist-organized or bitmap-organized raw block, so a big raw block
|
|
|
- * shall be used. */
|
|
|
+ /* Check if the size is appropriate for the use of a freelist-organized
|
|
|
+ * or bitmap-organized raw block. */
|
|
|
|
|
|
- ptr = create_raw_block(size + sizeof(raw_block_header_t), BIGBLOCK);
|
|
|
+#ifdef BITMAP_RB_ONLY
|
|
|
+ /* FIXME currently the raw block size and resolution are fixed */
|
|
|
+ size_t bm_vector_size = BMAP_EL_SIZE * (sys_alloc_size + BMAP_EL_SIZE -
|
|
|
+ sizeof(raw_block_header_t) - sizeof(bitmap_rb_t)) /
|
|
|
+ (BMAP_EL_SIZE + BMAP_EL_SIZE_BITS * BITMAP_RESOLUTION);
|
|
|
+
|
|
|
+ if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
|
|
|
+ sizeof(bitmap_rb_t) - bm_vector_size) {
|
|
|
+#endif /* BITMAP_RB_ONLY */
|
|
|
|
|
|
- if(ptr != NULL) {
|
|
|
- ptr = (void *)((uintptr_t) ptr + sizeof(raw_block_header_t));
|
|
|
+#ifdef FL_RB_ONLY
|
|
|
+ if(2 * size < sys_alloc_size - sizeof(raw_block_header_t) -
|
|
|
+ sizeof(freelist_rb_t)) {
|
|
|
+#endif /* FL_RB_ONLY */
|
|
|
+
|
|
|
+ /* Try to find a raw block available for allocation */
|
|
|
+ SLIST_FOREACH(raw_block, &tls_allocator->rb_head, pointers) {
|
|
|
+#ifdef TRYLOCK_ON_MALLOC
|
|
|
+ if(DEALY_TRYLOCK(raw_block->lock) == 0) {
|
|
|
+#else /* TRYLOCK_ON_MALLOC */
|
|
|
+ DEALY_LOCK(raw_block->lock);
|
|
|
+#endif /* TRYLOCK_ON_MALLOC */
|
|
|
+ encapsulated_rb = (DEFAULT_RB_T *)
|
|
|
+ ((uintptr_t) raw_block +
|
|
|
+ sizeof(raw_block_header_t));
|
|
|
+ ptr = dmmlib_malloc(encapsulated_rb, size);
|
|
|
+ DEALY_UNLOCK(raw_block->lock);
|
|
|
+
|
|
|
+ if(ptr != NULL) {
|
|
|
+ /* Check that a valid pointer has been returned */
|
|
|
+ assert(((uintptr_t) raw_block < (uintptr_t) ptr) &&
|
|
|
+ ((uintptr_t) ptr < (uintptr_t) raw_block +
|
|
|
+ raw_block->size +
|
|
|
+ sizeof(raw_block_header_t)));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+#ifdef TRYLOCK_ON_MALLOC
|
|
|
+ }
|
|
|
+#endif /* TRYLOCK_ON_MALLOC */
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ptr == NULL) {
|
|
|
+ /* No raw block was found, try to create a new one and
|
|
|
+ * allocate a memory block from there. */
|
|
|
+
|
|
|
+ new_raw_block = create_raw_block(sys_alloc_size,
|
|
|
+ DEFAULT_RB_TYPE);
|
|
|
+ if(new_raw_block != NULL) {
|
|
|
+ SLIST_INSERT_HEAD(&tls_allocator->rb_head,
|
|
|
+ new_raw_block, pointers);
|
|
|
+
|
|
|
+ encapsulated_rb = (DEFAULT_RB_T *)
|
|
|
+ ((uintptr_t) new_raw_block +
|
|
|
+ sizeof(raw_block_header_t));
|
|
|
+ ptr = dmmlib_malloc(encapsulated_rb, size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /* The memory request size is too large to serve it inside a
|
|
|
+ * freelist-organized or bitmap-organized raw block, so a big
|
|
|
+ * raw block shall be used. */
|
|
|
+
|
|
|
+ ptr = create_raw_block(size + sizeof(raw_block_header_t),
|
|
|
+ BIGBLOCK);
|
|
|
+
|
|
|
+ if(ptr != NULL) {
|
|
|
+ ptr = (void *)((uintptr_t) ptr +
|
|
|
+ sizeof(raw_block_header_t));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
MEM_TRACE("dmmlib - m %p %zu\n", ptr, size);
|
|
|
|