LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get Page size (https://www.linuxquestions.org/questions/programming-9/get-page-size-149798/)

eshwar_ind 02-23-2004 10:29 PM

get Page size
 
Hi!
How can we get the page size in linux. Can i change that in in aprogram?
In Kernel module programming is there any way to get the memory pieces just like 10 bytes, 2 bytes? ( i think kmalloc allocates entire page if the requested memory is less than the page size So its a waste of remaining memory)
Please reply me.
bye,
Eshwar.

jinksys 02-24-2004 03:01 AM

To get page size:

Code:


#include <stdio.h>
#include <unistd.h>

int main()
{
printf("%i,        getpagesize());
return 0;
}

Simply put, you cant change the page size. And due to the way the kernel allocates memory and fragmentation within the system's physical memory, memory allocation with kmalloc isn't precise as with malloc. You'll often get a pointer to a larger chunk of memory than requested, sometimes up to double the amount.

infamous41md 02-24-2004 01:45 PM

to add one thing to the above, kmalloc keeps around a pool of various sized memory objects. whether or not there is one as small as 10 bytes, i doubt, but you should recieve the next largest chunk if that pool has a free chunk. if you are going to be allocating a large quantity of constant size memory chunks, you may want to look into having kmalloc create a special pool of objects for you of that size using kmem_cache_create() and kmem_cache_alloc() from [kernel/slab/mm.c].

jinksys 02-24-2004 07:17 PM

I seriously doubt youll get a buffer of 10bytes using kmalloc, since the minimum memory that kmalloc handles is anywhere from 32 to 64 bytes, depending on page size and kernel.

eshwar_ind 02-24-2004 11:49 PM

Quote:

Originally posted by jinksys
I seriously doubt youll get a buffer of 10bytes using kmalloc, since the minimum memory that kmalloc handles is anywhere from 32 to 64 bytes, depending on page size and kernel.

eshwar_ind 02-24-2004 11:50 PM

Hi guys thank you very much.

dhirendracool1 09-27-2014 11:20 PM

HI,

When I am trying to getpagefile(), it is always returning me 4096. Can you please help.

I am reallocating the memory from 4096 to 2048 through realloc(), but everytime it is giving me 4096.

Thanks,

mina86 09-28-2014 05:39 AM

Quote:

Originally Posted by jinksys (Post 779271)
I seriously doubt youll get a buffer of 10bytes using kmalloc, since the minimum memory that kmalloc handles is anywhere from 32 to 64 bytes, depending on page size and kernel.

Nope, the smallest is 8 bytes. Next is 16, so one ends up wasting “just” 6 bytes when allocating 10. See kmalloc_index function and /proc/slabinfo file on your system.

PS. There is a ksize function which can be used to figure out actual size of allocated memory.

jinksys 09-29-2014 09:13 AM

Quote:

Originally Posted by dhirendracool1 (Post 5245624)
HI,

When I am trying to getpagefile(), it is always returning me 4096. Can you please help.

I am reallocating the memory from 4096 to 2048 through realloc(), but everytime it is giving me 4096.

Thanks,

Did you mean getpagesize() ?
Also, keep in mind everything I said in my posts is over ten years old, so things may have changed.

mina86 09-29-2014 11:46 AM

Quote:

Originally Posted by dhirendracool1 (Post 5245624)
When I am trying to getpagefile(), it is always returning me 4096. Can you please help.

I am reallocating the memory from 4096 to 2048 through realloc(), but everytime it is giving me 4096.

On a given system getpagesize will always return the same value – the page size for that architecture. What do you think page size is? What are you trying to accomplish?


All times are GMT -5. The time now is 03:54 PM.