UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » allocate memory from a specific location

#1 2007-05-29 07:05 PM

Kapil
Member
Registered: 2007-05-28
Posts: 7

Re: allocate memory from a specific location

Hi,
I want to know how can I ensure that the memory I allocate for my data structures is allocated from a chunk, which I have previously allocated
Memory chunk instead of the heap.

e.g heap memory  :address is 10 -540
Allocate a junk : address rec : 240-500.
Available mem: 10-239.

Now i want to ensure that the memory for my structure is taken from address 240-500 and not from 10-239.

I hope I have been able to clearly post my problem.

-Kapil

Offline

#2 2007-05-29 11:26 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: allocate memory from a specific location

You shouldn't want that, because if you do, then you designed your program wrong. Most likely, anyway.

That said, if you've your own memory allocator and you're using mmap, then you could give the prefered memory address to mmap as an option.

Offline

#3 2007-05-31 06:53 PM

Kapil
Member
Registered: 2007-05-28
Posts: 7

Re: allocate memory from a specific location

The reason I was asking this question was because I have allocated some shared memory and I would like to create my data structures in that shared memory so that the data structure can be accessed by two processes.

Hope I have cleared my intent. Is there any other alternative to do the above?

Offline

#4 2007-05-31 07:58 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: allocate memory from a specific location

Well, if you already have the shared memory allocated, then you don't really need
to do anything special: just set a pointer of the appropriate type to point to the
memory and access it as a pointer to your struct...  Eg:

struct mystruct *sp = (struct mystruct *)shared_mem;
struct mystruct2 *sp2 = (struct mystruct2 *)(shared_mem + sizeof (struct mystruct));
/* and so on... */

Offline

#5 2007-05-31 08:43 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: allocate memory from a specific location

With shared memory you need to do your own memory handling/allocation, can't use malloc to manage it for you. If you're using it for fixed size chunks that's very easy. If you need variable length chunks it becomes more complex.

Offline

#6 2007-07-11 09:17 PM

Kapil
Member
Registered: 2007-05-28
Posts: 7

Re: allocate memory from a specific location

Hi Rob,
Any suggestions as to where i can get some material to read on shared memory?

Kapil

Offline

#7 2007-07-11 10:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: allocate memory from a specific location

I thought you said you already had shared memory allocated?  In which case, I
would've thought you'd already know about how to do it...

But, how about "man shm_open", "man mmap", or if you want to use old-style SYSV
shared memory, "man shmget", "man shmctl", "man shmop", and "man svipc"...

Offline

  • Index
  • » C
  • » allocate memory from a specific location

Board footer

Powered by FluxBB