2019-11-05 13:50:43 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2019-11-05 13:50:43 -08:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2019-11-05 13:50:43 -08:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/astack.h>
|
|
|
|
#include <isc/atomic.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
struct isc_astack {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
size_t size;
|
|
|
|
size_t pos;
|
2019-11-05 13:50:43 -08:00
|
|
|
isc_mutex_t lock;
|
2020-02-13 14:44:37 -08:00
|
|
|
uintptr_t nodes[];
|
2019-11-05 13:50:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_astack_t *
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_astack_new(isc_mem_t *mctx, size_t size) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_astack_t *stack = isc_mem_get(
|
|
|
|
mctx, sizeof(isc_astack_t) + size * sizeof(uintptr_t));
|
2019-11-05 13:50:43 -08:00
|
|
|
|
2019-11-19 14:10:07 +08:00
|
|
|
*stack = (isc_astack_t){
|
|
|
|
.size = size,
|
|
|
|
};
|
2019-11-05 13:50:43 -08:00
|
|
|
isc_mem_attach(mctx, &stack->mctx);
|
|
|
|
memset(stack->nodes, 0, size * sizeof(uintptr_t));
|
|
|
|
isc_mutex_init(&stack->lock);
|
|
|
|
return (stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_astack_trypush(isc_astack_t *stack, void *obj) {
|
2020-03-30 13:49:55 -07:00
|
|
|
if (!isc_mutex_trylock(&stack->lock)) {
|
2019-11-05 13:50:43 -08:00
|
|
|
if (stack->pos >= stack->size) {
|
2019-11-19 14:10:07 +08:00
|
|
|
UNLOCK(&stack->lock);
|
2019-11-05 13:50:43 -08:00
|
|
|
return (false);
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
stack->nodes[stack->pos++] = (uintptr_t)obj;
|
2019-11-19 14:10:07 +08:00
|
|
|
UNLOCK(&stack->lock);
|
2019-11-05 13:50:43 -08:00
|
|
|
return (true);
|
|
|
|
} else {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_astack_pop(isc_astack_t *stack) {
|
2019-11-19 14:10:07 +08:00
|
|
|
LOCK(&stack->lock);
|
2019-11-05 13:50:43 -08:00
|
|
|
uintptr_t rv;
|
|
|
|
if (stack->pos == 0) {
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
rv = stack->nodes[--stack->pos];
|
|
|
|
}
|
2019-11-19 14:10:07 +08:00
|
|
|
UNLOCK(&stack->lock);
|
2020-02-12 13:59:18 +01:00
|
|
|
return ((void *)rv);
|
2019-11-05 13:50:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_astack_destroy(isc_astack_t *stack) {
|
2019-11-19 14:10:07 +08:00
|
|
|
LOCK(&stack->lock);
|
2019-11-05 13:50:43 -08:00
|
|
|
REQUIRE(stack->pos == 0);
|
2019-11-19 14:10:07 +08:00
|
|
|
UNLOCK(&stack->lock);
|
|
|
|
|
|
|
|
isc_mutex_destroy(&stack->lock);
|
2019-11-05 13:50:43 -08:00
|
|
|
|
|
|
|
isc_mem_putanddetach(&stack->mctx, stack,
|
|
|
|
sizeof(struct isc_astack) +
|
2020-02-12 13:59:18 +01:00
|
|
|
stack->size * sizeof(uintptr_t));
|
2019-11-05 13:50:43 -08:00
|
|
|
}
|