Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

realloc()

3 views
Skip to first unread message

Alexander Clauss

unread,
Aug 5, 1998, 3:00:00 AM8/5/98
to
Weiß jemand, was denn genau passiert, wenn ein realloc()-Aufruf
zum Vergrößern eines Speicherbereichs scheitert (also NULL liefert)?

Ist der ursprüngliche Block dann freigegeben, oder bleibt der erhalten
und steht weiterhin zur Verfügung?

Beispiel:
b = malloc(10);
if (b) {
a = realloc(b, 1234567890);
if (a == NULL) {
/* Ist hier "b" immer noch gülig? */
/* oder ist "b" freigegeben? */
}
}

Die CW-Doku schweigt sich über dieses Detail leider aus.

Ich hoffe mal, das der alte Speicherbereich nicht freigegeben wird, würde
es aber doch ganz gerne genau wissen.


Viele Grüße
Alexander

Markus Fritze

unread,
Aug 6, 1998, 3:00:00 AM8/6/98
to
Alexander Clauss <Alexande...@of2.maus.de> wrote:

> Ist der ursprüngliche Block dann freigegeben, oder bleibt der erhalten
> und steht weiterhin zur Verfügung?

pool_alloc.c wird von alloc.c aufgerufen:

/*
* __pool_realloc
*
* The size of the given block is changed to the new given size. If
the new
* size is less than the old size, the excess bytes are returned to
the
* free-list (unless there are not enough excess to meet minimum
block size
* requirements) and the original pointer is returned.
*
* If the new size is greater than the old size and if the memory
following
* the block is free and there is enough of it, then the block is
enlarged
* using the free bytes. Otherwise, a new block of the given size
is
* allocated and the contents of the old block are copied into it
before the
* old block is freed. A pointer to either the enlarged old block
or the
* newly allocated block is returned, unless more bytes were not
available at
* all, in which case a null pointer is returned.
*
* If 'ptr' is null, then we return 'malloc(size)'.
*
* If 'size' is zero and 'ptr' is not null, we 'free(ptr)' and
return a null
* pointer.
*/

void * __pool_realloc(mem_pool_obj * pool_obj, void * ptr, mem_size
size)
{
list_header * free_list = &pool_obj->free_list;
block_header * block;
mem_size block_size, i;
char * new_block;
long * p;
long * q;

if (!ptr)
return(__pool_alloc(pool_obj, size));

if (!size)
{
__pool_free(pool_obj, ptr);
return(0);
}

block = (block_header *) ((char *) ptr - sizeof(tag_word));

assert(block_ok(free_list, block, block_used | !block_in_list));

size = size + sizeof(tag_word);
size = (size + alignment) & ~alignment;

if (size < block_overhead)
size = block_overhead;

block_size = block->tag & this_size;

if (size == block_size)
return(ptr);

if (size < block_size)
{
split_block(free_list, block, size);
return(ptr);
}

block_size = grow_block(free_list, block, size);

if (block_size >= size)
return(ptr);

new_block = (char *) __pool_alloc(pool_obj, size -
sizeof(tag_word));

if (!new_block)
return(0);

#if !__POWERPC__

for (i = block_size / sizeof(long), p = (long *) ptr, q = (long
*) new_block; --i;)
*q++ = *p++;

#else

for (i = block_size / sizeof(long), p = (long *) ptr - 1, q =
(long *) new_block - 1; --i;)
*++q = *++p;

#endif

__pool_free(pool_obj, ptr);

return(new_block);
}

--
Markus Fritze
Emagic Soft- und Hardware GmbH
Mac OS Developer, New Technology Integrator

Alexander Clauss

unread,
Aug 8, 1998, 3:00:00 AM8/8/98
to
Markus Fritze <mfr...@mail.emagic.de> wrote:

> pool_alloc.c wird von alloc.c aufgerufen:

Danke. Der alte Block bleibt demnach erhalten.

--
Alexander

0 new messages