Blame lib/malloc/scratch_buffer_grow.c

Packit Service a2489d
/* Variable-sized buffer with on-stack default allocation.
Packit Service a2489d
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit Service a2489d
   This file is part of the GNU C Library.
Packit Service a2489d
Packit Service a2489d
   The GNU C Library is free software; you can redistribute it and/or
Packit Service a2489d
   modify it under the terms of the GNU General Public
Packit Service a2489d
   License as published by the Free Software Foundation; either
Packit Service a2489d
   version 3 of the License, or (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service a2489d
   General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public
Packit Service a2489d
   License along with the GNU C Library; if not, see
Packit Service a2489d
   <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#ifndef _LIBC
Packit Service a2489d
# include <libc-config.h>
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#include <scratch_buffer.h>
Packit Service a2489d
#include <errno.h>
Packit Service a2489d
Packit Service a2489d
bool
Packit Service a2489d
__libc_scratch_buffer_grow (struct scratch_buffer *buffer)
Packit Service a2489d
{
Packit Service a2489d
  void *new_ptr;
Packit Service a2489d
  size_t new_length = buffer->length * 2;
Packit Service a2489d
Packit Service a2489d
  /* Discard old buffer.  */
Packit Service a2489d
  scratch_buffer_free (buffer);
Packit Service a2489d
Packit Service a2489d
  /* Check for overflow.  */
Packit Service a2489d
  if (__glibc_likely (new_length >= buffer->length))
Packit Service a2489d
    new_ptr = malloc (new_length);
Packit Service a2489d
  else
Packit Service a2489d
    {
Packit Service a2489d
      __set_errno (ENOMEM);
Packit Service a2489d
      new_ptr = NULL;
Packit Service a2489d
    }
Packit Service a2489d
Packit Service a2489d
  if (__glibc_unlikely (new_ptr == NULL))
Packit Service a2489d
    {
Packit Service a2489d
      /* Buffer must remain valid to free.  */
Packit Service a2489d
      scratch_buffer_init (buffer);
Packit Service a2489d
      return false;
Packit Service a2489d
    }
Packit Service a2489d
Packit Service a2489d
  /* Install new heap-based buffer.  */
Packit Service a2489d
  buffer->data = new_ptr;
Packit Service a2489d
  buffer->length = new_length;
Packit Service a2489d
  return true;
Packit Service a2489d
}
Packit Service a2489d
libc_hidden_def (__libc_scratch_buffer_grow)