473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

realloc, copy and VM


When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?). But, is it really
necessary to _copy_ bytes ? When in a virtual memory environment (off-topic
here, be I still think the question is worth), you don't access directly
physical memory, but allocated pages, so would it be possible on some
implementations, to just change page allocation ? Some kind of immobile
trip. It could permit optimizations even when not knowing beeforehand the
exact amount of memory needed.

Nov 15 '05 #1
8 1914
>When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?). But, is it really
necessary to _copy_ bytes ? When in a virtual memory environment (off-topic
here, be I still think the question is worth), you don't access directly
physical memory, but allocated pages, so would it be possible on some
implementations, to just change page allocation ?
*PAGE* allocation. In a typical virtual memory setup, you can re-arrange
PAGES by re-mapping. You don't get to re-map individual bytes that way.
So if the new memory and the old memory aren't lined up right, you may
be doing copying anyway. And it would seem that the chances of having
them lined up right might be so low that it's not worth checking for.

This presumes that you're not in the situation where your pages are
so small, or you've got so much memory, that malloc() aligns EVERYTHING
to a page boundary, wasting a lot of virtual address space, and possibly
physical memory in the process. Then you could do what you suggest.
Some kind of immobile
trip. It could permit optimizations even when not knowing beeforehand the
exact amount of memory needed.


Gordon L. Burditt
Nov 15 '05 #2

On 25/06/2005 00:29, Gordon Burditt wrote:
When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?). But, is it really
necessary to _copy_ bytes ?

When in a virtual memory environment (off-topic
here, be I still think the question is worth), you don't access directly
physical memory, but allocated pages, so would it be possible on some
implementations, to just change page allocation ?


*PAGE* allocation. In a typical virtual memory setup, you can re-arrange
PAGES by re-mapping. You don't get to re-map individual bytes that way.
So if the new memory and the old memory aren't lined up right, you may
be doing copying anyway. And it would seem that the chances of having
them lined up right might be so low that it's not worth checking for.

This presumes that you're not in the situation where your pages are
so small, or you've got so much memory, that malloc() aligns EVERYTHING
to a page boundary, wasting a lot of virtual address space, and possibly
physical memory in the process. Then you could do what you suggest.


It would be possible to align on page when allocating large blocks. I'm not
sure if MacOSX does that (pages are 4K). And if the block is not well
aligned, it's still cheaper to copy parts from 1 page (the first), than
copying all the others. There is a little hole after that (less than 1 page)
but, for large blocks, it's much faster.
Some kind of immobile
trip. It could permit optimizations even when not knowing beeforehand the
exact amount of memory needed.


Gordon L. Burditt


Nov 15 '05 #3
In article <BE******************************@laposte.net>,
Jean-Claude Arbaut <je****************@laposte.net> wrote:

When you realloc [...] would it be possible on some
implementations, to just change page allocation ? Some kind of immobile
trip. It could permit optimizations even when not knowing beeforehand the
exact amount of memory needed.


That's a nifty idea!

Actually, I think to some degree it was an old multics trick.
If you had a few precious items that you knew were going to
to grow and shrink drastically, you could put them into
their own individual segment(s) and resize the segments
as needed. You'd be limited to the segment size (often
128KB back then) or at least must have had sufficient sequential
segments set aside. But in any case, you could in effect
resize the object w/o having to move it, and 128KB was a
lot of memory back then.

A secondary point is that 64 bit machines are becoming
more commonplace...some operating systems will let you
skip the brk/sbrk stuff and just put something at
byte 72036854775808 or wherever: just some implementation-
defined place beyond the heap and below the stack.
It wouldn't be too much trouble to make malloc/realloc
put large items, far apart, in 64-bit space.
--
7842++
Nov 15 '05 #4
In article <LO0ve.338$HV1.187@fed1read07>,
Anonymous 7843 <an******@example.com> wrote:
In article <BE******************************@laposte.net>,
Jean-Claude Arbaut <je****************@laposte.net> wrote:

When you realloc [...] would it be possible on some
implementations, to just change page allocation ?


It wouldn't be too much trouble to make malloc/realloc
put large items, far apart, in 64-bit space.


Now I remember...I experimented with something like this
using mmap of /dev/zero at a carefully chosen location.

It was on one big old 64-bit honker of a box with...
wait for it...4G of RAM installed.
--
7842++
Nov 15 '05 #5
Jean-Claude Arbaut <je****************@laposte.net> writes:
When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?).

[...]

Others have addressed your main question. There are cases other than
not having enough free space where it could make sense to copy the
block. For example, if the object is between two large chunks of free
space, the implementation might use a realloc() call as an opportunity
to move the object somewhere else and coalesce the free chunks into a
single larger chunk.

For that matter, the C99 standard actually says that realloc()
allocates a new chunk of memory, copies the old to the new, and
deallocates the old. Leaving the object where it is and skipping the
copy is merely an allowed optimization. (The C90 standard meant to
say the same thing, but the wording was poor.)

As a user of realloc(), you should always assume that the old address
may be invalid after a realloc() call (unless the realloc() fails, in
which case it leaves the object alone).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6

On 25/06/2005 23:37, Keith Thompson wrote:
Jean-Claude Arbaut <je****************@laposte.net> writes:
When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?).

[...]

Others have addressed your main question. There are cases other than
not having enough free space where it could make sense to copy the
block. For example, if the object is between two large chunks of free
space, the implementation might use a realloc() call as an opportunity
to move the object somewhere else and coalesce the free chunks into a
single larger chunk.

For that matter, the C99 standard actually says that realloc()
allocates a new chunk of memory, copies the old to the new, and
deallocates the old. Leaving the object where it is and skipping the
copy is merely an allowed optimization. (The C90 standard meant to
say the same thing, but the wording was poor.)

As a user of realloc(), you should always assume that the old address
may be invalid after a realloc() call (unless the realloc() fails, in
which case it leaves the object alone).


Thanks. I didn't know, but I would never have used old address without
checking. Crazy, but not too much ;-)

Nov 15 '05 #7

In article <BE******************************@laposte.net>, Jean-Claude Arbaut <je****************@laposte.net> writes:

When you realloc for more size, it may be necessary to allocate a new block,
copy memory and deallocate the old, for example if there is not enough free
space after the original block (maybe the only example ?).
There are other possible cases. Not all allocators simply grab the
next available suitable free chunk. Some group objects into size
classes, for example, and your realloc may change which class the
object fits in.

The allocator scheme is not specified by the standard. It can be
*anything*, as long as the required semantics are preserved.
But, is it really
necessary to _copy_ bytes ? When in a virtual memory environment (off-topic
here, be I still think the question is worth), you don't access directly
physical memory, but allocated pages, so would it be possible on some
implementations, to just change page allocation ?
That *is* extending the original block.

Take the typical case: the implementation uses a linear virtual
address space for all allocated objects, and the contents of C object
pointers are addresses as used by the virtual memory manager. If an
object occupies the page at address n*pagesize, and you attempt to
extend it with realloc, then:

- There may be no page mapped at address (n+1)*pagesize, and the
implementation could request that the VMM map a new page at that
address. Aside from performing its internal housekeeping, the
realloc is done; no copying needs to take place.

- There may be a page mapped at address (n+1)*pagesize. If it's part
of some other object, then obviously the implementation cannot steal
that address for the object being resized, because pointers into the
other object will refer to that portion of the virtual address space.
The realloc'd object will have to be moved.

- Some other case may apply (eg the implementation is unable to
secure additional memory).
Some kind of immobile
trip. It could permit optimizations even when not knowing beeforehand the
exact amount of memory needed.


The malloc implementations I'm familiar with are already capable of
doing this. (Actually, for many of them it happens automatically
in many cases, via lazy allocation and copy-on-write.)

In short: yes, it's a good idea, for implementations where it's
appropriate; but it's one they typically already use.

On another off-topic note: in a large (eg 64-bit) virtual address
space, it's usually possible to space objects sparsely, so that they
nearly always can be extended this way. That's one of the advantages
of a large address space.

--
Michael Wojcik mi************@microfocus.com

Sure we're tossing out fluff, but tell me, where does anyone deal in words
with substance? -- Haruki Murakami (trans Alfred Birnbaum)
Nov 15 '05 #8
Michael Wojcik wrote:
.... snip ...
Take the typical case: the implementation uses a linear virtual
address space for all allocated objects, and the contents of C object
pointers are addresses as used by the virtual memory manager. If an
object occupies the page at address n*pagesize, and you attempt to
extend it with realloc, then:

- There may be no page mapped at address (n+1)*pagesize, and the
implementation could request that the VMM map a new page at that
address. Aside from performing its internal housekeeping, the
realloc is done; no copying needs to take place.

- There may be a page mapped at address (n+1)*pagesize. If it's part
of some other object, then obviously the implementation cannot steal
that address for the object being resized, because pointers into the
other object will refer to that portion of the virtual address space.
The realloc'd object will have to be moved.


There are a lot of possibilities. My nmalloc for DJGPP, available
on my site, mallocs into a linear virtual memory map and trys to
handle all the various possibilities of adjacent free space. The
objective is to avoid memory copies, which it can do when expanding
into free space above. It also checks for free space below, which
may avoid having to assign a new larger junk (and possibly
failing). At any rate it is a simple model, yet has many possible
cases. Available at:

<http://cbfalconer.home.att.download/nmalloc.zip>

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - GWB 2004-2-8
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
"This notion that the United States is getting ready to attack
Iran is simply ridiculous. And having said that, all options
are on the table." - George W. Bush, Brussels, 2005-02-22
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
5
by: James S. Singleton | last post by:
Thanks to everybody who provided an answer to my previous question on this. I am trying to grasp the nitty-gritty details of this, and I am having difficulties understanding some of the issues...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
8
by: barcaroller | last post by:
Does C++ have an equivalent to C's realloc() that can be safely used with C++'s new and delete. If not, what is the proper way to resize memory blocks in C++ (other than using malloc/realloc/free)?
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.