Using vmalloc (libvmalloc.a) on Mac and CentOS- cannot include vmalloc header
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开发者_StackOverflow 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.
Nemo's comment should have been given as an answer:
The
vmalloc
in that question is a Linux kernel function. Unless you are hacking the kernel or writing a device driver, it is not relevant to you. Thevmalloc
at the AT&T Research site is something else entirely. Neither one does what you think it does. Just usemalloc
.
It's obvious that you don't know what "virtually contiguous range" means or you'd realize that malloc
must give you that; otherwise it would not work at all.
Premature optimization is the root of all evil. Especially when you don't have any idea what the optimization you're trying to make means.
When compiling, specify -Ipath for gcc to know where 'vmalloc.h' is.
Or, specify the environment variable C_INCLUDE_PATH (for C) or CPLUS_INCLUDE_PATH (for C++) before calling gcc.
GCC will search header files follow this way:
- -Ipath
- Environment variables: C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH
Default paths (if you didn't specify prefix when installing gcc), probably like:
/usr/include
/usr/local/include
/usr/lib/gcc-lib/i386-linux/2.95.2/include
/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3
/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include
精彩评论