/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8   -*-â
âvi: set et ft=c ts=8 tw=8 fenc=utf-8                                       :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2016 Google Inc.                                                   â
â                                                                              â
â Licensed under the Apache License, Version 2.0 (the "License");              â
â you may not use this file except in compliance with the License.             â
â You may obtain a copy of the License at                                      â
â                                                                              â
â     http://www.apache.org/licenses/LICENSE-2.0                               â
â                                                                              â
â Unless required by applicable law or agreed to in writing, software          â
â distributed under the License is distributed on an "AS IS" BASIS,            â
â WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     â
â See the License for the specific language governing permissions and          â
â limitations under the License.                                               â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "third_party/nsync/array.internal.h"
// clang-format off

void a_ensure_ (void *v, int delta, int sz) {
	typedef A_TYPE (void *) a_void_ptr;
	a_void_ptr *a = (a_void_ptr *) v;
	int omax = a->h_.max_;
	if (omax < 0) {
		omax = -omax;
	}
	if (a->h_.len_ + delta > omax) {
		int nmax = a->h_.len_ + delta;
		void *na;
		if (nmax < omax * 2) {
			nmax = omax * 2;
		}
		if (a->h_.max_ <= 0) {
			na = malloc (nmax * sz);
			memcpy (na, a->a_, omax*sz);
		} else {
			na = realloc (a->a_, nmax*sz);
		}
		memset (omax *sz + (char *)na, 0, (nmax - omax) * sz);
		a->a_ = (void **) na;
		a->h_.max_ = nmax;
	}
}
