|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
- * Copyright 2011 Institute of Communication and Computer Systems (ICCS)
|
|
|
+ * Copyright Institute of Communication and Computer Systems (ICCS)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -15,11 +15,14 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-#include "freelist/freelist.h"
|
|
|
+/**
|
|
|
+ * @file freelist_malloc.c
|
|
|
+ * @author Ioannis Koutras
|
|
|
+ * @date September 2012
|
|
|
+ * @brief malloc() implementation for freelist-organised raw blocks
|
|
|
+ */
|
|
|
|
|
|
-#ifdef HAVE_LOCKS
|
|
|
-#include <pthread.h>
|
|
|
-#endif /* HAVE_LOCKS */
|
|
|
+#include "freelist/freelist.h"
|
|
|
|
|
|
#include "freelist/block_header_funcs.h"
|
|
|
#include "freelist/linked_lists/linked_lists.h"
|
|
@@ -29,18 +32,29 @@
|
|
|
#include "freelist/split.h"
|
|
|
#endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
|
|
|
|
|
|
+#include "trace.h"
|
|
|
+
|
|
|
+#ifdef WITH_ALLOCATOR_STATS
|
|
|
+#include "dmmlib/dmmlib.h"
|
|
|
+#endif /* WITH_ALLOCATOR_STATS */
|
|
|
+
|
|
|
#if defined (BEST_FIT)
|
|
|
-#define search_on_free(size) best_fit_on_freelist(rb_header, size)
|
|
|
+#define search_on_free(size) best_fit_on_freelist(raw_block, size)
|
|
|
#elif defined (GOOD_FIT)
|
|
|
-#define search_on_free(size) good_fit_on_freelist(rb_header, size)
|
|
|
+#define search_on_free(size) good_fit_on_freelist(raw_block, size)
|
|
|
#elif defined (EXACT_FIT)
|
|
|
-#define search_on_free(size) exact_fit_on_freelist(rb_header, size)
|
|
|
+#define search_on_free(size) exact_fit_on_freelist(raw_block, size)
|
|
|
#elif defined (FIRST_FIT)
|
|
|
-#define search_on_free(size) first_fit_on_freelist(rb_header, size)
|
|
|
+#define search_on_free(size) first_fit_on_freelist(raw_block, size)
|
|
|
#endif /* BEST_FIT */
|
|
|
|
|
|
size_t req_padding(size_t size);
|
|
|
|
|
|
+/** Tries to align the input to 32, 64, 128, 256 or to a multiple of 4 if it is
|
|
|
+ * larger.
|
|
|
+ *
|
|
|
+ * @param size The input number.
|
|
|
+ */
|
|
|
size_t req_padding(size_t size) {
|
|
|
size_t align;
|
|
|
|
|
@@ -62,23 +76,16 @@ size_t req_padding(size_t size) {
|
|
|
}
|
|
|
|
|
|
/** Tries to allocate memory from a specific free-list organized raw block.
|
|
|
- * @param raw_block The pointer of the raw block.
|
|
|
+ * @param raw_block The pointer of the freelist-organised raw block.
|
|
|
* @param size The requested size.
|
|
|
* @retval The address of the returned memory space.
|
|
|
*/
|
|
|
-void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
|
|
|
- freelist_rb_t *rb_header;
|
|
|
+void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
|
|
|
block_header_t *ptr;
|
|
|
size_t allocation_size, previous_size, previous_size_availability;
|
|
|
|
|
|
- rb_header = (freelist_rb_t *)((char *)raw_block +
|
|
|
- sizeof(raw_block_header_t));
|
|
|
ptr = NULL;
|
|
|
|
|
|
-#ifdef HAVE_LOCKS
|
|
|
- pthread_mutex_lock(&raw_block->mutex);
|
|
|
-#endif /* HAVE_LOCKS */
|
|
|
-
|
|
|
ptr = search_on_free(size);
|
|
|
|
|
|
if(ptr != NULL) {
|
|
@@ -88,59 +95,56 @@ void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
|
|
|
|
|
|
/* Try to split */
|
|
|
#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
|
|
|
- split(rb_header, ptr, size);
|
|
|
+ split(raw_block, ptr, size);
|
|
|
#endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
|
|
|
|
|
|
- mark_used(rb_header, ptr);
|
|
|
+ mark_used(raw_block, ptr);
|
|
|
} else {
|
|
|
|
|
|
allocation_size = req_padding(size) + HEADER_SIZE;
|
|
|
|
|
|
- if(allocation_size < (char *) raw_block + raw_block->size -
|
|
|
- (char *) rb_header->border_ptr -
|
|
|
- get_size(rb_header->border_ptr)) {
|
|
|
- if(rb_header->border_ptr == (block_header_t *)((char *)rb_header +
|
|
|
- sizeof(freelist_rb_t))) {
|
|
|
+ if(allocation_size <= raw_block->remaining_size) {
|
|
|
+ if(raw_block->border_ptr == NULL) {
|
|
|
previous_size_availability = 1; // Occupied and of 0 size
|
|
|
- ptr = (block_header_t *)((char *)rb_header +
|
|
|
+ ptr = (block_header_t *)((char *)raw_block +
|
|
|
sizeof(freelist_rb_t));
|
|
|
} else {
|
|
|
- previous_size = get_size(rb_header->border_ptr);
|
|
|
+ previous_size = get_size(raw_block->border_ptr);
|
|
|
previous_size_availability =
|
|
|
- get_size_availability(rb_header->border_ptr);
|
|
|
- ptr = (block_header_t *)((char *)rb_header->border_ptr +
|
|
|
+ get_size_availability(raw_block->border_ptr);
|
|
|
+ ptr = (block_header_t *)((char *)raw_block->border_ptr +
|
|
|
previous_size);
|
|
|
}
|
|
|
|
|
|
- rb_header->border_ptr = ptr;
|
|
|
+ // Update raw block metadata
|
|
|
+ raw_block->remaining_size -= allocation_size;
|
|
|
+ raw_block->border_ptr = ptr;
|
|
|
|
|
|
// Update block metadata
|
|
|
- set_size_and_used(rb_header, ptr, req_padding(size));
|
|
|
+ set_size_and_used(raw_block, ptr, req_padding(size));
|
|
|
set_previous_size_availability(ptr, previous_size_availability);
|
|
|
#ifdef REQUEST_SIZE_INFO
|
|
|
- set_requested_size(ptr, req_padding(size));
|
|
|
+ set_requested_size(ptr, size);
|
|
|
#endif /* REQUEST_SIZE_INFO */
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#ifdef WITH_RAWBLOCK_STATS
|
|
|
if(ptr != NULL) {
|
|
|
-#if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
|
|
|
- raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
|
|
|
-#endif /* !((SPLITTING_FIXED) || (SPLITTING_VARIABLE)) */
|
|
|
- raw_block->dmm_stats.live_objects++;
|
|
|
- raw_block->dmm_stats.num_malloc++;
|
|
|
+
|
|
|
+#ifdef WITH_ALLOCATOR_STATS
|
|
|
+ systemallocator.dmm_stats.total_mem_allocated +=
|
|
|
+ get_size(ptr) + HEADER_SIZE;
|
|
|
+ systemallocator.dmm_stats.live_objects++;
|
|
|
+ systemallocator.dmm_stats.num_malloc++;
|
|
|
+ TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
|
|
|
+ systemallocator.dmm_stats.total_mem_allocated);
|
|
|
#ifdef REQUEST_SIZE_INFO
|
|
|
- raw_block->dmm_stats.total_mem_requested += size;
|
|
|
+ systemallocator.dmm_stats.total_mem_requested += size;
|
|
|
+ TRACE_1("dmmlib - global requested memory: %zu bytes\n",
|
|
|
+ systemallocator.dmm_stats.total_mem_requested);
|
|
|
#endif /* REQUEST_SIZE_INFO */
|
|
|
- }
|
|
|
-#endif /* WITH_RAWBLOCK_STATS */
|
|
|
-
|
|
|
-#ifdef HAVE_LOCKS
|
|
|
- pthread_mutex_unlock(&raw_block->mutex);
|
|
|
-#endif /* HAVE_LOCKS */
|
|
|
+#endif /* WITH_ALLOCATOR_STATS */
|
|
|
|
|
|
- if(ptr != NULL) {
|
|
|
return (void *)((char *)ptr + HEADER_SIZE);
|
|
|
} else {
|
|
|
return NULL;
|