silc_srealloc now allocates new block if reallocation fails.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 2 Jan 2008 20:37:55 +0000 (20:37 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 2 Jan 2008 20:37:55 +0000 (20:37 +0000)
lib/silcutil/silcmemory.c
lib/silcutil/silcmemory.h

index ec8eb1e39be41c7d7f214e0ba7ff21969d50334d..b1a657b2f335593c91bba82875d9e2ea1d8fb8f6 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1999 - 2007 Pekka Riikonen
+  Copyright (C) 1999 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -145,8 +145,20 @@ void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size)
 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
                    void *ptr, SilcUInt32 size)
 {
 void *silc_srealloc(SilcStack stack, SilcUInt32 old_size,
                    void *ptr, SilcUInt32 size)
 {
-  return stack ? silc_stack_realloc(stack, old_size, ptr, size) :
-    silc_realloc(ptr, size);
+  void *new_ptr;
+
+  if (!stack)
+    return silc_realloc(ptr, size);
+
+  new_ptr = silc_stack_realloc(stack, old_size, ptr, size);
+  if (!new_ptr) {
+    new_ptr = silc_smalloc(stack, size);
+    if (!new_ptr)
+      return NULL;
+    memcpy(new_ptr, ptr, old_size);
+  }
+
+  return new_ptr;
 }
 
 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
 }
 
 void *silc_smemdup(SilcStack stack, const void *ptr, SilcUInt32 size)
index ddcf8bc15b7f858202fcc0630b10a90af1c3ebd7..632af872021736455b4ecea20aa5148af3046978 100644 (file)
@@ -203,17 +203,13 @@ void *silc_scalloc(SilcStack stack, SilcUInt32 items, SilcUInt32 size);
  *    size of `size' bytes.  The contents of `ptr' will not be changed.
  *    If `ptr' is NULL the call is equivalent to silc_smalloc.  If `size'
  *    is zero (0) error will occur.  Returns NULL on error and the old
  *    size of `size' bytes.  The contents of `ptr' will not be changed.
  *    If `ptr' is NULL the call is equivalent to silc_smalloc.  If `size'
  *    is zero (0) error will occur.  Returns NULL on error and the old
- *    pointer remain intact.
+ *    pointer remain intact.  This may return different pointer from `ptr'
  *
  * NOTES
  *
  *
  * NOTES
  *
- *    This function reallocates successfully only if the previous allocation
- *    to `stack' was `ptr'.  If there was another memory allocation between
- *    allocating `ptr' and this call, this routine will return NULL.  The
- *    NULL is also returned if the `size' does not fit to current stack
- *    and allocating new block would require slow copying of the data.  It
- *    is left to the caller to decide whether to allocate new pointer and
- *    copy the old data in case this function returns NULL.
+ *    If the reallocation from `stack' fails, this function will allocate
+ *    new block of size of `size' bytes from `stack' and copy the data from
+ *    `ptr' to the new memory block.
  *
  *    If `stack' is NULL this function calls silc_realloc.
  *
  *
  *    If `stack' is NULL this function calls silc_realloc.
  *