allocation limit

crux-00006: $SRC/utils/ar_ref.c

#include "utils/ar_i.h"
#include "utils/chk.h"
#include "utils/panic.h"


void *
ar_ref  (void ** ar, int * arn, int sz, int at)
{
  if (at < 0) panic ("negative array index");
  if (at >= *arn)
    {
      int new_arn = ar_round_up (at);
      int new_len = new_arn * sz;
      int old_len = *arn * sz;

      if (sz < 0) panic ("negatively sized array elements are not supported");
      if (at < 0) panic ("negative array indexes are not supported");
      if (new_len < old_len) panic ("allocation size overflow");
      *ar = chk_realloc (*ar, old_len, new_len);
      while (old_len < new_len) ((char *)*ar)[old_len++] = 0;
      *arn = new_arn;
    }
  return (void *)((char *)*ar + at * sz);
}


/*

    This file is part of Crux.

    Crux is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Crux is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Crux.  If not, see <http://www.gnu.org/licenses/>.

*/