I want to allocate virtually contiguous range memory so that I can use the locality property (spatial locality) when accessing data, with consideration for better performance. I found out at the following page that I need to use vmalloc for better memory locality access (please correct me if I am wrong and have to use kmalloc instead).
What is the difference between vmalloc and kmalloc?
I downloaded the vmalloc package from http://www2.research.att.com/~gsf/cgi-bin/download.cgi?action=list&name=vmalloc
I followed the installation procedure for building the libmalloc.a static library from the source files, and then I copied the generated libvmalloc.a library to /usr/local/lib and /usr/lib directory on my mac.
In my C program, I tried to include the vmalloc.h header file through various approaches as follow:
#include <vmalloc.h>
.
#include <linux/vmalloc.h>
.
#include "vmalloc.h"
but none of them worked. I always got the vmalloc.h: No such file or directory error message.
I used -L/usr/local/lib -lvmalloc flags when compiling my C++ program.
I also got the same error when trying the same thing on my desktop computer (under CentOS operating system).
Here is my makefile:
SHELL = /bin/sh
PROJECT = hw2
TARGET = mkhashtbl
CFLAGS = -O3
LFLAGS = -O3 -L/usr/local/lib -lvmalloc
TFILE = $(PROJECT).tgz
ZFILE = $(PROJECT).zip
PACKFILES = Makefile hashtbl.h hashtbl.c mkhashtbl.c timer.c
all: $(TARGET)
run: all
- ./$(TARGET)
I also tried to modify my Linker flags as follows:
LFLAGS = -O3 -L/usr/local/lib -lvmalloc
and I still got the same error. What could be wrong in this case? Is there anything wrong in the way I linked the library, or does vmalloc only work for some versions of Linux? If it is the latter case, I am sure that I should still be able to at least include the header file.
EDIT
My real problem is actually the following:
hashtbl_cache * hashTable_cache; /* Hash table cache */
int i;
hashTable_cache = (hashtbl_cache*)malloc(tbl_size* sizeof(hashtbl_cache));
for( i = 0 ; i < tbl_size; i++ )
{
hashTable_cache[i]. ht_elements = (hashelt_cache*)malloc( hashTable->data_total_size[i] * sizeof(hashelt_cache) ) ;
}
I want to make sure that all the ht_elements in every cache are created in order. I read from a forum that vmalloc is good for creating cache aware application, as data are created in virtual memory. Is there any other approach to make sure that the allocation of all elements of my cache array are created in contiguous order, hence will enable me to do a fast lookup? One more thing, the size of every elements in every cache is not the same, so I guess using calloc is not a solution, but I may be wrong.